V4L/DVB (6631): xc2028: eliminate i2c macro side-effects
[linux-2.6/verdex.git] / drivers / media / video / tuner-xc2028.c
blob6e23fad0e56052f9ef784f1fd5a46f5b15ff00a4
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 /* 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;
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;
64 struct firmware_description *firm;
65 int firm_size;
67 __u16 version;
69 struct xc2028_ctrl ctrl;
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? */
78 int max_len; /* Max firmware chunk */
80 enum tuner_mode mode;
81 struct i2c_client *i2c_client;
83 struct mutex lock;
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; \
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; \
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; \
116 static unsigned int xc2028_get_reg(struct xc2028_data *priv, u16 reg)
118 int rc;
119 unsigned char buf[2];
121 tuner_dbg("%s called\n", __FUNCTION__);
123 buf[0] = reg>>8;
124 buf[1] = (unsigned char) reg;
126 rc = i2c_send(priv, buf, 2);
127 if (rc < 0)
128 return rc;
130 rc = i2c_rcv(priv, buf, 2);
131 if (rc < 0)
132 return rc;
134 return (buf[1]) | (buf[0] << 8);
137 void dump_firm_type(unsigned int type)
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 ");
201 static v4l2_std_id parse_audio_std_option(void)
203 if (strcasecmp(audio_std, "A2"))
204 return V4L2_STD_A2;
205 if (strcasecmp(audio_std, "A2/A"))
206 return V4L2_STD_A2_A;
207 if (strcasecmp(audio_std, "A2/B"))
208 return V4L2_STD_A2_B;
209 if (strcasecmp(audio_std, "NICAM"))
210 return V4L2_STD_NICAM;
211 if (strcasecmp(audio_std, "NICAM/A"))
212 return V4L2_STD_NICAM_A;
213 if (strcasecmp(audio_std, "NICAM/B"))
214 return V4L2_STD_NICAM_B;
216 return 0;
219 static void free_firmware(struct xc2028_data *priv)
221 int i;
223 if (!priv->firm)
224 return;
226 for (i = 0; i < priv->firm_size; i++)
227 kfree(priv->firm[i].ptr);
229 kfree(priv->firm);
231 priv->firm = NULL;
232 priv->need_load_generic = 1;
235 static int load_all_firmwares(struct dvb_frontend *fe)
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];
244 tuner_dbg("%s called\n", __FUNCTION__);
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);
257 return rc;
259 p = fw->data;
260 endp = p + fw->size;
262 if (fw->size < sizeof(name) - 1 + 2) {
263 tuner_err("Error: firmware size is zero!\n");
264 rc = -EINVAL;
265 goto done;
268 memcpy(name, p, sizeof(name) - 1);
269 name[sizeof(name) - 1] = 0;
270 p += sizeof(name) - 1;
272 priv->version = le16_to_cpu(*(__u16 *) p);
273 p += 2;
275 tuner_info("Firmware: %s, ver %d.%d\n", name,
276 priv->version >> 8, priv->version & 0xff);
278 if (p + 2 > endp)
279 goto corrupt;
281 n_array = le16_to_cpu(*(__u16 *) p);
282 p += 2;
284 tuner_info("There are %d firmwares at %s\n",
285 n_array, priv->ctrl.fname);
287 priv->firm = kzalloc(sizeof(*priv->firm) * n_array, GFP_KERNEL);
289 if (!fw) {
290 tuner_err("Not enough memory for reading firmware.\n");
291 rc = -ENOMEM;
292 goto done;
295 priv->firm_size = n_array;
296 n = -1;
297 while (p < endp) {
298 __u32 type, size;
299 v4l2_std_id id;
301 n++;
302 if (n >= n_array) {
303 tuner_err("Too much firmwares at the file\n");
304 goto corrupt;
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;
313 type = le32_to_cpu(*(__u32 *) p);
314 p += sizeof(type);
316 id = le64_to_cpu(*(v4l2_std_id *) p);
317 p += sizeof(id);
319 size = le32_to_cpu(*(__u32 *) p);
320 p += sizeof(size);
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;
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;
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);
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;
348 p += size;
351 if (n + 1 != priv->firm_size) {
352 tuner_err("Firmware file is incomplete!\n");
353 goto corrupt;
356 goto done;
358 corrupt:
359 rc = -EINVAL;
360 tuner_err("Error: firmware file is corrupted!\n");
362 err:
363 tuner_info("Releasing loaded firmware file.\n");
365 free_firmware(priv);
367 done:
368 release_firmware(fw);
369 tuner_dbg("Firmware files loaded.\n");
371 return rc;
374 static int seek_firmware(struct dvb_frontend *fe, unsigned int type,
375 v4l2_std_id *id)
377 struct xc2028_data *priv = fe->tuner_priv;
378 int i;
380 tuner_dbg("%s called\n", __FUNCTION__);
382 if (!priv->firm) {
383 tuner_err("Error! firmware not loaded\n");
384 return -EINVAL;
387 if (((type & ~SCODE) == 0) && (*id == 0))
388 *id = V4L2_STD_PAL;
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;
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;
402 /*FIXME: Would make sense to seek for type "hint" match ? */
404 i = -EINVAL;
405 goto ret;
407 found:
408 *id = priv->firm[i].id;
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);
416 return i;
419 static int load_firmware(struct dvb_frontend *fe, unsigned int type,
420 v4l2_std_id *id)
422 struct xc2028_data *priv = fe->tuner_priv;
423 int pos, rc;
424 unsigned char *p, *endp, buf[priv->max_len];
426 tuner_dbg("%s called\n", __FUNCTION__);
428 pos = seek_firmware(fe, type, id);
429 if (pos < 0)
430 return pos;
432 tuner_info("Loading firmware for type=");
433 dump_firm_type(type);
434 printk("(%x), id %08lx.\n", type, (unsigned long)*id);
436 p = priv->firm[pos].ptr;
438 if (!p) {
439 tuner_err("Firmware pointer were freed!");
440 return -EINVAL;
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 default:
481 tuner_info("Invalid RESET code %d\n",
482 size & 0x7f);
483 return -EINVAL;
486 continue;
489 /* Checks for a sleep command */
490 if (size & 0x8000) {
491 msleep(size & 0x7fff);
492 continue;
495 if ((size + p > endp)) {
496 tuner_err("missing bytes: need %d, have %d\n",
497 size, (int)(endp - p));
498 return -EINVAL;
501 buf[0] = *p;
502 p++;
503 size--;
505 /* Sends message chunks */
506 while (size > 0) {
507 int len = (size < priv->max_len - 1) ?
508 size : priv->max_len - 1;
510 memcpy(buf + 1, p, len);
512 rc = i2c_send(priv, buf, len + 1);
513 if (rc < 0) {
514 tuner_err("%d returned from send\n", rc);
515 return -EINVAL;
518 p += len;
519 size -= len;
522 return 0;
525 static int load_scode(struct dvb_frontend *fe, unsigned int type,
526 v4l2_std_id *id, int scode)
528 struct xc2028_data *priv = fe->tuner_priv;
529 int pos, rc;
530 unsigned char *p;
532 tuner_dbg("%s called\n", __FUNCTION__);
534 pos = seek_firmware(fe, type, id);
535 if (pos < 0)
536 return pos;
538 p = priv->firm[pos].ptr;
540 if (!p) {
541 tuner_err("Firmware pointer were freed!");
542 return -EINVAL;
545 if ((priv->firm[pos].size != 12 * 16) || (scode >= 16))
546 return -EINVAL;
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;
555 rc = i2c_send(priv, p + 12 * scode, 12);
556 if (rc < 0)
557 return -EIO;
559 rc = send_seq(priv, {0x00, 0x8c});
560 if (rc < 0)
561 return -EIO;
563 return 0;
566 static int check_firmware(struct dvb_frontend *fe, enum tuner_mode new_mode,
567 v4l2_std_id std, fe_bandwidth_t bandwidth)
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;
575 tuner_dbg("%s called\n", __FUNCTION__);
577 if (!priv->firm) {
578 if (!priv->ctrl.fname) {
579 tuner_info("xc2028/3028 firmware name not set!\n");
580 return -EINVAL;
583 rc = load_all_firmwares(fe);
584 if (rc < 0)
585 return rc;
588 tuner_dbg("I am in mode %u and I should switch to mode %i\n",
589 priv->mode, new_mode);
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;
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);
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;
609 type0 = BASE;
611 if (priv->ctrl.type == XC2028_FIRM_MTS)
612 type0 |= MTS;
614 if (priv->bandwidth == 8)
615 type0 |= F8MHZ;
617 /* FIXME: How to load FM and FM|INPUT1 firmwares? */
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;
626 priv->need_load_generic = 0;
627 priv->firm_type = 0;
628 if (priv->mode == T_DIGITAL_TV)
629 change_digital_bandwidth = 1;
632 tuner_dbg("I should change bandwidth %u\n", change_digital_bandwidth);
634 if (change_digital_bandwidth) {
636 /*FIXME: Should allow selecting between D2620 and D2633 */
637 type |= D2620;
639 /* FIXME: When should select a DTV78 firmware?
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;
653 default:
654 tuner_err("error: bandwidth not supported.\n");
656 priv->bandwidth = bandwidth;
659 if (!change_digital_bandwidth && priv->mode == T_DIGITAL_TV)
660 return 0;
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;
668 /* FIXME: Should handle errors - if INIT1 found */
669 rc = load_firmware(fe, type0, &std0);
671 /* FIXME: Should add support for FM radio
674 if (priv->ctrl.type == XC2028_FIRM_MTS)
675 type |= MTS;
677 if (priv->firm_type & std) {
678 tuner_dbg("Std-specific firmware already loaded.\n");
679 return 0;
682 /* Add audio hack to std mask */
683 std |= parse_audio_std_option();
685 rc = load_firmware(fe, type, &std);
686 if (rc < 0)
687 return rc;
689 /* Load SCODE firmware, if exists */
690 tuner_dbg("Trying to load scode 0\n");
691 type |= SCODE;
693 rc = load_scode(fe, type, &std, 0);
695 version = xc2028_get_reg(priv, 0x0004);
696 hwmodel = xc2028_get_reg(priv, 0x0008);
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);
703 priv->firm_type = std;
705 return 0;
708 static int xc2028_signal(struct dvb_frontend *fe, u16 *strength)
710 struct xc2028_data *priv = fe->tuner_priv;
711 int frq_lock, signal = 0;
713 tuner_dbg("%s called\n", __FUNCTION__);
715 mutex_lock(&priv->lock);
717 *strength = 0;
719 /* Sync Lock Indicator */
720 frq_lock = xc2028_get_reg(priv, 0x0002);
721 if (frq_lock <= 0)
722 goto ret;
724 /* Frequency is locked. Return signal quality */
726 /* Get SNR of the video signal */
727 signal = xc2028_get_reg(priv, 0x0040);
729 if (signal <= 0)
730 signal = frq_lock;
732 ret:
733 mutex_unlock(&priv->lock);
735 *strength = signal;
737 return 0;
740 #define DIV 15625
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)
746 struct xc2028_data *priv = fe->tuner_priv;
747 int rc = -EINVAL;
748 unsigned char buf[5];
749 u32 div, offset = 0;
751 tuner_dbg("%s called\n", __FUNCTION__);
753 mutex_lock(&priv->lock);
755 /* HACK: It seems that specific firmware need to be reloaded
756 when freq is changed */
758 priv->firm_type = 0;
760 /* Reset GPIO 1 */
761 rc = priv->tuner_callback(priv->video_dev, XC2028_TUNER_RESET, 0);
762 if (rc < 0)
763 goto ret;
765 msleep(10);
766 tuner_dbg("should set frequency %d kHz)\n", freq / 1000);
768 if (check_firmware(fe, new_mode, std, bandwidth) < 0)
769 goto ret;
771 if (new_mode == T_DIGITAL_TV)
772 offset = 2750000;
774 div = (freq - offset + DIV / 2) / DIV;
776 /* CMD= Set frequency */
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;
785 rc = priv->tuner_callback(priv->video_dev, XC2028_RESET_CLK, 1);
786 if (rc < 0)
787 goto ret;
789 msleep(10);
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;
797 rc = i2c_send(priv, buf, sizeof(buf));
798 if (rc < 0)
799 goto ret;
800 msleep(100);
802 priv->frequency = freq;
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);
808 rc = 0;
810 ret:
811 mutex_unlock(&priv->lock);
813 return rc;
816 static int xc2028_set_tv_freq(struct dvb_frontend *fe,
817 struct analog_parameters *p)
819 struct xc2028_data *priv = fe->tuner_priv;
821 tuner_dbg("%s called\n", __FUNCTION__);
823 return generic_set_tv_freq(fe, 62500l * p->frequency, T_ANALOG_TV,
824 p->std, BANDWIDTH_8_MHZ /* NOT USED */);
827 static int xc2028_set_params(struct dvb_frontend *fe,
828 struct dvb_frontend_parameters *p)
830 struct xc2028_data *priv = fe->tuner_priv;
832 tuner_dbg("%s called\n", __FUNCTION__);
834 /* FIXME: Only OFDM implemented */
835 if (fe->ops.info.type != FE_OFDM) {
836 tuner_err("DTV type not implemented.\n");
837 return -EINVAL;
840 return generic_set_tv_freq(fe, p->frequency, T_DIGITAL_TV,
841 0 /* NOT USED */,
842 p->u.ofdm.bandwidth);
846 static int xc2028_dvb_release(struct dvb_frontend *fe)
848 struct xc2028_data *priv = fe->tuner_priv;
850 tuner_dbg("%s called\n", __FUNCTION__);
852 priv->count--;
854 if (!priv->count) {
855 list_del(&priv->xc2028_list);
857 kfree(priv->ctrl.fname);
859 free_firmware(priv);
860 kfree(priv);
863 return 0;
866 static int xc2028_get_frequency(struct dvb_frontend *fe, u32 *frequency)
868 struct xc2028_data *priv = fe->tuner_priv;
870 tuner_dbg("%s called\n", __FUNCTION__);
872 *frequency = priv->frequency;
874 return 0;
877 static int xc2028_set_config(struct dvb_frontend *fe, void *priv_cfg)
879 struct xc2028_data *priv = fe->tuner_priv;
880 struct xc2028_ctrl *p = priv_cfg;
882 tuner_dbg("%s called\n", __FUNCTION__);
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)
891 return -ENOMEM;
893 free_firmware(priv);
894 strcpy(priv->ctrl.fname, p->fname);
897 if (p->max_len > 0)
898 priv->max_len = p->max_len;
900 return 0;
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,
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,
920 void *xc2028_attach(struct dvb_frontend *fe, struct xc2028_config *cfg)
922 struct xc2028_data *priv;
923 void *video_dev;
925 if (debug)
926 printk(KERN_DEBUG PREFIX "Xcv2028/3028 init called!\n");
928 if (NULL == cfg->video_dev)
929 return NULL;
931 if (!fe) {
932 printk(KERN_ERR PREFIX "No frontend!\n");
933 return NULL;
936 video_dev = cfg->video_dev;
938 list_for_each_entry(priv, &xc2028_list, xc2028_list) {
939 if (priv->video_dev == cfg->video_dev) {
940 video_dev = NULL;
941 break;
945 if (video_dev) {
946 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
947 if (priv == NULL)
948 return NULL;
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;
959 mutex_init(&priv->lock);
961 list_add_tail(&priv->xc2028_list, &xc2028_list);
964 fe->tuner_priv = priv;
965 priv->count++;
967 memcpy(&fe->ops.tuner_ops, &xc2028_dvb_tuner_ops,
968 sizeof(xc2028_dvb_tuner_ops));
970 tuner_info("type set to %s\n", "XCeive xc2028/xc3028 tuner");
972 return fe;
975 EXPORT_SYMBOL(xc2028_attach);
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");