V4L/DVB (6639): xc2028: correct divisor length
[linux-2.6.git] / drivers / media / video / tuner-xc2028.c
blobe85992f970f78fc6d6897f04e6fc6a56f151004b
1 /* tuner-xc2028
3 * Copyright (c) 2007 Mauro Carvalho Chehab (mchehab@infradead.org)
5 * Copyright (c) 2007 Michel Ludwig (michel.ludwig@gmail.com)
6 * - frontend interface
8 * This code is placed under the terms of the GNU General Public License v2
9 */
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"
22 #include <linux/dvb/frontend.h>
23 #include "dvb_frontend.h"
26 #define PREFIX "xc2028"
28 static int debug;
29 module_param(debug, int, 0644);
30 MODULE_PARM_DESC(debug, "enable verbose debug messages");
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");
46 static LIST_HEAD(xc2028_list);
47 static DEFINE_MUTEX(xc2028_list_mutex);
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;
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;
66 struct firmware_description *firm;
67 int firm_size;
68 __u16 firm_version;
70 struct xc2028_ctrl ctrl;
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? */
79 int max_len; /* Max firmware chunk */
81 enum tuner_mode mode;
82 struct i2c_client *i2c_client;
84 struct mutex lock;
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; \
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; \
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; \
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; \
127 static unsigned int xc2028_get_reg(struct xc2028_data *priv, u16 reg, u16 *val)
129 unsigned char buf[2];
130 unsigned char ibuf[2];
132 tuner_dbg("%s %04x called\n", __FUNCTION__, reg);
134 buf[0] = reg >> 8;
135 buf[1] = (unsigned char) reg;
137 if (i2c_send_recv(priv, buf, 2, ibuf, 2) != 2)
138 return -EIO;
140 *val = (ibuf[1]) | (ibuf[0] << 8);
141 return 0;
144 void dump_firm_type(unsigned int type)
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 ");
208 static v4l2_std_id parse_audio_std_option(void)
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;
223 return 0;
226 static void free_firmware(struct xc2028_data *priv)
228 int i;
230 if (!priv->firm)
231 return;
233 for (i = 0; i < priv->firm_size; i++)
234 kfree(priv->firm[i].ptr);
236 kfree(priv->firm);
238 priv->firm = NULL;
239 priv->firm_size = 0;
240 priv->need_load_generic = 1;
243 static int load_all_firmwares(struct dvb_frontend *fe)
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];
252 tuner_dbg("%s called\n", __FUNCTION__);
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);
265 return rc;
267 p = fw->data;
268 endp = p + fw->size;
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;
276 memcpy(name, p, sizeof(name) - 1);
277 name[sizeof(name) - 1] = 0;
278 p += sizeof(name) - 1;
280 priv->firm_version = le16_to_cpu(*(__u16 *) p);
281 p += 2;
283 n_array = le16_to_cpu(*(__u16 *) p);
284 p += 2;
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);
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;
296 priv->firm_size = n_array;
298 n = -1;
299 while (p < endp) {
300 __u32 type, size;
301 v4l2_std_id id;
303 n++;
304 if (n >= n_array) {
305 tuner_err("More firmware images in file than "
306 "were expected!\n");
307 goto corrupt;
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;
316 type = le32_to_cpu(*(__u32 *) p);
317 p += sizeof(type);
319 id = le64_to_cpu(*(v4l2_std_id *) p);
320 p += sizeof(id);
322 size = le32_to_cpu(*(__u32 *) p);
323 p += sizeof(size);
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;
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;
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);
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;
353 p += size;
356 if (n + 1 != priv->firm_size) {
357 tuner_err("Firmware file is incomplete!\n");
358 goto corrupt;
361 goto done;
363 corrupt:
364 rc = -EINVAL;
365 tuner_err("Error: firmware file is corrupted!\n");
367 err:
368 tuner_info("Releasing partially loaded firmware file.\n");
369 free_firmware(priv);
371 done:
372 release_firmware(fw);
373 if (rc == 0)
374 tuner_dbg("Firmware files loaded.\n");
376 return rc;
379 static int seek_firmware(struct dvb_frontend *fe, unsigned int type,
380 v4l2_std_id *id)
382 struct xc2028_data *priv = fe->tuner_priv;
383 int i;
385 tuner_dbg("%s called\n", __FUNCTION__);
387 if (!priv->firm) {
388 tuner_err("Error! firmware not loaded\n");
389 return -EINVAL;
392 if (((type & ~SCODE) == 0) && (*id == 0))
393 *id = V4L2_STD_PAL;
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;
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;
407 /*FIXME: Would make sense to seek for type "hint" match ? */
409 i = -EINVAL;
410 goto ret;
412 found:
413 *id = priv->firm[i].id;
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);
421 return i;
424 static int load_firmware(struct dvb_frontend *fe, unsigned int type,
425 v4l2_std_id *id)
427 struct xc2028_data *priv = fe->tuner_priv;
428 int pos, rc;
429 unsigned char *p, *endp, buf[priv->max_len];
431 tuner_dbg("%s called\n", __FUNCTION__);
433 pos = seek_firmware(fe, type, id);
434 if (pos < 0)
435 return pos;
437 tuner_info("Loading firmware for type=");
438 dump_firm_type(type);
439 printk("(%x), id %016llx.\n", type, (unsigned long long)*id);
441 p = priv->firm[pos].ptr;
442 endp = p + priv->firm[pos].size;
444 while (p < endp) {
445 __u16 size;
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;
453 size = le16_to_cpu(*(__u16 *) p);
454 p += sizeof(size);
456 if (size == 0xffff)
457 return 0;
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;
468 continue;
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;
480 break;
481 default:
482 tuner_info("Invalid RESET code %d\n",
483 size & 0x7f);
484 return -EINVAL;
487 continue;
490 /* Checks for a sleep command */
491 if (size & 0x8000) {
492 msleep(size & 0x7fff);
493 continue;
496 if ((size + p > endp)) {
497 tuner_err("missing bytes: need %d, have %d\n",
498 size, (int)(endp - p));
499 return -EINVAL;
502 buf[0] = *p;
503 p++;
504 size--;
506 /* Sends message chunks */
507 while (size > 0) {
508 int len = (size < priv->max_len - 1) ?
509 size : priv->max_len - 1;
511 memcpy(buf + 1, p, len);
513 rc = i2c_send(priv, buf, len + 1);
514 if (rc < 0) {
515 tuner_err("%d returned from send\n", rc);
516 return -EINVAL;
519 p += len;
520 size -= len;
523 return 0;
526 static int load_scode(struct dvb_frontend *fe, unsigned int type,
527 v4l2_std_id *id, int scode)
529 struct xc2028_data *priv = fe->tuner_priv;
530 int pos, rc;
531 unsigned char *p;
533 tuner_dbg("%s called\n", __FUNCTION__);
535 pos = seek_firmware(fe, type, id);
536 if (pos < 0)
537 return pos;
539 p = priv->firm[pos].ptr;
541 if ((priv->firm[pos].size != 12 * 16) || (scode >= 16))
542 return -EINVAL;
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;
551 rc = i2c_send(priv, p + 12 * scode, 12);
552 if (rc < 0)
553 return -EIO;
555 rc = send_seq(priv, {0x00, 0x8c});
556 if (rc < 0)
557 return -EIO;
559 return 0;
562 static int check_firmware(struct dvb_frontend *fe, enum tuner_mode new_mode,
563 v4l2_std_id std, fe_bandwidth_t bandwidth)
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;
572 tuner_dbg("%s called\n", __FUNCTION__);
574 if (!priv->firm) {
575 if (!priv->ctrl.fname) {
576 tuner_info("xc2028/3028 firmware name not set!\n");
577 return -EINVAL;
580 rc = load_all_firmwares(fe);
581 if (rc < 0)
582 return rc;
585 tuner_dbg("I am in mode %u and I should switch to mode %i\n",
586 priv->mode, new_mode);
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;
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);
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;
606 type0 = BASE;
608 if (priv->ctrl.type == XC2028_FIRM_MTS)
609 type0 |= MTS;
611 if (priv->bandwidth == 8)
612 type0 |= F8MHZ;
614 /* FIXME: How to load FM and FM|INPUT1 firmwares? */
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;
623 priv->need_load_generic = 0;
624 priv->firm_type = 0;
625 if (priv->mode == T_DIGITAL_TV)
626 change_digital_bandwidth = 1;
629 tuner_dbg("I should change bandwidth %u\n", change_digital_bandwidth);
631 if (change_digital_bandwidth) {
633 /*FIXME: Should allow selecting between D2620 and D2633 */
634 type |= D2620;
636 /* FIXME: When should select a DTV78 firmware?
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;
650 default:
651 tuner_err("error: bandwidth not supported.\n");
653 priv->bandwidth = bandwidth;
656 if (!change_digital_bandwidth && priv->mode == T_DIGITAL_TV)
657 return 0;
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;
665 /* FIXME: Should handle errors - if INIT1 found */
666 rc = load_firmware(fe, type0, &std0);
668 /* FIXME: Should add support for FM radio
671 if (priv->ctrl.type == XC2028_FIRM_MTS)
672 type |= MTS;
674 if (priv->firm_type & std) {
675 tuner_dbg("Std-specific firmware already loaded.\n");
676 return 0;
679 /* Add audio hack to std mask */
680 std |= parse_audio_std_option();
682 rc = load_firmware(fe, type, &std);
683 if (rc < 0)
684 return rc;
686 /* Load SCODE firmware, if exists */
687 tuner_dbg("Trying to load scode 0\n");
688 type |= SCODE;
690 rc = load_scode(fe, type, &std, 0);
692 xc2028_get_reg(priv, 0x0004, &version);
693 xc2028_get_reg(priv, 0x0008, &hwmodel);
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);
700 priv->firm_type = std;
702 return 0;
705 static int xc2028_signal(struct dvb_frontend *fe, u16 *strength)
707 struct xc2028_data *priv = fe->tuner_priv;
708 u16 frq_lock, signal = 0;
709 int rc;
711 tuner_dbg("%s called\n", __FUNCTION__);
713 mutex_lock(&priv->lock);
715 /* Sync Lock Indicator */
716 rc = xc2028_get_reg(priv, 0x0002, &frq_lock);
717 if (rc < 0 || frq_lock == 0)
718 goto ret;
720 /* Frequency is locked. Return signal quality */
722 /* Get SNR of the video signal */
723 rc = xc2028_get_reg(priv, 0x0040, &signal);
724 if (rc < 0)
725 signal = -frq_lock;
727 ret:
728 mutex_unlock(&priv->lock);
730 *strength = signal;
732 return rc;
735 #define DIV 15625
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)
741 struct xc2028_data *priv = fe->tuner_priv;
742 int rc = -EINVAL;
743 unsigned char buf[4];
744 u32 div, offset = 0;
746 tuner_dbg("%s called\n", __FUNCTION__);
748 mutex_lock(&priv->lock);
750 /* HACK: It seems that specific firmware need to be reloaded
751 when freq is changed */
753 priv->firm_type = 0;
755 /* Reset GPIO 1 */
756 rc = priv->tuner_callback(priv->video_dev, XC2028_TUNER_RESET, 0);
757 if (rc < 0)
758 goto ret;
760 msleep(10);
761 tuner_dbg("should set frequency %d kHz\n", freq / 1000);
763 if (check_firmware(fe, new_mode, std, bandwidth) < 0)
764 goto ret;
766 if (new_mode == T_DIGITAL_TV)
767 offset = 2750000;
769 div = (freq - offset + DIV / 2) / DIV;
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;
779 rc = priv->tuner_callback(priv->video_dev, XC2028_RESET_CLK, 1);
780 if (rc < 0)
781 goto ret;
783 msleep(10);
785 buf[0] = 0xff & (div >> 24);
786 buf[1] = 0xff & (div >> 16);
787 buf[2] = 0xff & (div >> 8);
788 buf[3] = 0xff & (div);
790 rc = i2c_send(priv, buf, sizeof(buf));
791 if (rc < 0)
792 goto ret;
793 msleep(100);
795 priv->frequency = freq;
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);
801 rc = 0;
803 ret:
804 mutex_unlock(&priv->lock);
806 return rc;
809 static int xc2028_set_tv_freq(struct dvb_frontend *fe,
810 struct analog_parameters *p)
812 struct xc2028_data *priv = fe->tuner_priv;
814 tuner_dbg("%s called\n", __FUNCTION__);
816 return generic_set_tv_freq(fe, 62500l * p->frequency, T_ANALOG_TV,
817 p->std, BANDWIDTH_8_MHZ /* NOT USED */);
820 static int xc2028_set_params(struct dvb_frontend *fe,
821 struct dvb_frontend_parameters *p)
823 struct xc2028_data *priv = fe->tuner_priv;
825 tuner_dbg("%s called\n", __FUNCTION__);
827 /* FIXME: Only OFDM implemented */
828 if (fe->ops.info.type != FE_OFDM) {
829 tuner_err("DTV type not implemented.\n");
830 return -EINVAL;
833 return generic_set_tv_freq(fe, p->frequency, T_DIGITAL_TV,
834 0 /* NOT USED */,
835 p->u.ofdm.bandwidth);
839 static int xc2028_dvb_release(struct dvb_frontend *fe)
841 struct xc2028_data *priv = fe->tuner_priv;
843 tuner_dbg("%s called\n", __FUNCTION__);
845 mutex_lock(&xc2028_list_mutex);
847 priv->count--;
849 if (!priv->count) {
850 list_del(&priv->xc2028_list);
852 kfree(priv->ctrl.fname);
854 free_firmware(priv);
855 kfree(priv);
856 fe->tuner_priv = NULL;
859 mutex_unlock(&xc2028_list_mutex);
861 return 0;
864 static int xc2028_get_frequency(struct dvb_frontend *fe, u32 *frequency)
866 struct xc2028_data *priv = fe->tuner_priv;
868 tuner_dbg("%s called\n", __FUNCTION__);
870 *frequency = priv->frequency;
872 return 0;
875 static int xc2028_set_config(struct dvb_frontend *fe, void *priv_cfg)
877 struct xc2028_data *priv = fe->tuner_priv;
878 struct xc2028_ctrl *p = priv_cfg;
880 tuner_dbg("%s called\n", __FUNCTION__);
882 mutex_lock(&priv->lock);
884 priv->ctrl.type = p->type;
886 if (p->fname) {
887 kfree(priv->ctrl.fname);
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;
895 free_firmware(priv);
896 strcpy(priv->ctrl.fname, p->fname);
899 if (p->max_len > 0)
900 priv->max_len = p->max_len;
902 mutex_unlock(&priv->lock);
904 return 0;
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,
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,
924 void *xc2028_attach(struct dvb_frontend *fe, struct xc2028_config *cfg)
926 struct xc2028_data *priv;
927 void *video_dev;
929 if (debug)
930 printk(KERN_DEBUG PREFIX ": Xcv2028/3028 init called!\n");
932 if (NULL == cfg->video_dev)
933 return NULL;
935 if (!fe) {
936 printk(KERN_ERR PREFIX ": No frontend!\n");
937 return NULL;
940 video_dev = cfg->video_dev;
942 mutex_lock(&xc2028_list_mutex);
944 list_for_each_entry(priv, &xc2028_list, xc2028_list) {
945 if (priv->video_dev == cfg->video_dev) {
946 video_dev = NULL;
947 break;
951 if (video_dev) {
952 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
953 if (priv == NULL) {
954 mutex_unlock(&xc2028_list_mutex);
955 return NULL;
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;
967 mutex_init(&priv->lock);
969 list_add_tail(&priv->xc2028_list, &xc2028_list);
972 fe->tuner_priv = priv;
973 priv->count++;
975 memcpy(&fe->ops.tuner_ops, &xc2028_dvb_tuner_ops,
976 sizeof(xc2028_dvb_tuner_ops));
978 tuner_info("type set to %s\n", "XCeive xc2028/xc3028 tuner");
980 mutex_unlock(&xc2028_list_mutex);
982 return fe;
985 EXPORT_SYMBOL(xc2028_attach);
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");