3 * Copyright (c) 2007-2008 Mauro Carvalho Chehab (mchehab@infradead.org)
5 * Copyright (c) 2007 Michel Ludwig (michel.ludwig@gmail.com)
8 * This code is placed under the terms of the GNU General Public License v2
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"
27 module_param(debug, int, 0644);
28 MODULE_PARM_DESC(debug, "enable verbose debug messages");
30 static char audio_std[8];
31 module_param_string(audio_std, audio_std, sizeof(audio_std), 0);
32 MODULE_PARM_DESC(audio_std,
33 "Audio standard. XC3028 audio decoder explicitly "
34 "needs to know what audio\n"
35 "standard is needed for some video standards with audio A2 or NICAM.\n"
36 "The valid values are:\n"
44 static char firmware_name[FIRMWARE_NAME_MAX];
45 module_param_string(firmware_name, firmware_name, sizeof(firmware_name), 0);
46 MODULE_PARM_DESC(firmware_name, "Firmware file name. Allows overriding the "
47 "default firmware name\n");
49 static LIST_HEAD(hybrid_tuner_instance_list);
50 static DEFINE_MUTEX(xc2028_list_mutex);
52 /* struct for storing firmware table */
53 struct firmware_description {
61 struct firmware_properties {
66 unsigned int scode_table;
71 struct list_head hybrid_tuner_instance_list;
72 struct tuner_i2c_props i2c_props;
73 int (*tuner_callback) (void *dev,
74 int command, int arg);
78 struct firmware_description *firm;
85 struct xc2028_ctrl ctrl;
87 struct firmware_properties cur_fw;
92 #define i2c_send(priv, buf, size) ({ \
94 _rc = tuner_i2c_xfer_send(&priv->i2c_props, buf, size); \
96 tuner_info("i2c output error: rc = %d (should be %d)\n",\
101 #define i2c_rcv(priv, buf, size) ({ \
103 _rc = tuner_i2c_xfer_recv(&priv->i2c_props, buf, size); \
105 tuner_err("i2c input error: rc = %d (should be %d)\n", \
110 #define i2c_send_recv(priv, obuf, osize, ibuf, isize) ({ \
112 _rc = tuner_i2c_xfer_send_recv(&priv->i2c_props, obuf, osize, \
115 tuner_err("i2c input error: rc = %d (should be %d)\n", \
120 #define send_seq(priv, data...) ({ \
121 static u8 _val[] = data; \
123 if (sizeof(_val) != \
124 (_rc = tuner_i2c_xfer_send(&priv->i2c_props, \
125 _val, sizeof(_val)))) { \
126 tuner_err("Error on line %d: %d\n", __LINE__, _rc); \
132 static int xc2028_get_reg(struct xc2028_data *priv, u16 reg, u16 *val)
134 unsigned char buf[2];
135 unsigned char ibuf[2];
137 tuner_dbg("%s %04x called\n", __func__, reg);
140 buf[1] = (unsigned char) reg;
142 if (i2c_send_recv(priv, buf, 2, ibuf, 2) != 2)
145 *val = (ibuf[1]) | (ibuf[0] << 8);
149 #define dump_firm_type(t) dump_firm_type_and_int_freq(t, 0)
150 static void dump_firm_type_and_int_freq(unsigned int type, u16 int_freq)
196 if (type & TOYOTA388)
197 printk("TOYOTA388 ");
198 if (type & TOYOTA794)
199 printk("TOYOTA794 ");
202 if (type & ZARLINK456)
203 printk("ZARLINK456 ");
213 printk("HAS_IF_%d ", int_freq);
216 static v4l2_std_id parse_audio_std_option(void)
218 if (strcasecmp(audio_std, "A2") == 0)
220 if (strcasecmp(audio_std, "A2/A") == 0)
221 return V4L2_STD_A2_A;
222 if (strcasecmp(audio_std, "A2/B") == 0)
223 return V4L2_STD_A2_B;
224 if (strcasecmp(audio_std, "NICAM") == 0)
225 return V4L2_STD_NICAM;
226 if (strcasecmp(audio_std, "NICAM/A") == 0)
227 return V4L2_STD_NICAM_A;
228 if (strcasecmp(audio_std, "NICAM/B") == 0)
229 return V4L2_STD_NICAM_B;
234 static void free_firmware(struct xc2028_data *priv)
237 tuner_dbg("%s called\n", __func__);
242 for (i = 0; i < priv->firm_size; i++)
243 kfree(priv->firm[i].ptr);
250 memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
253 static int load_all_firmwares(struct dvb_frontend *fe)
255 struct xc2028_data *priv = fe->tuner_priv;
256 const struct firmware *fw = NULL;
257 const unsigned char *p, *endp;
263 tuner_dbg("%s called\n", __func__);
265 if (!firmware_name[0])
266 fname = priv->ctrl.fname;
268 fname = firmware_name;
270 tuner_dbg("Reading firmware %s\n", fname);
271 rc = request_firmware(&fw, fname, &priv->i2c_props.adap->dev);
274 tuner_err("Error: firmware %s not found.\n",
277 tuner_err("Error %d while requesting firmware %s \n",
285 if (fw->size < sizeof(name) - 1 + 2 + 2) {
286 tuner_err("Error: firmware file %s has invalid size!\n",
291 memcpy(name, p, sizeof(name) - 1);
292 name[sizeof(name) - 1] = 0;
293 p += sizeof(name) - 1;
295 priv->firm_version = le16_to_cpu(*(__u16 *) p);
298 n_array = le16_to_cpu(*(__u16 *) p);
301 tuner_info("Loading %d firmware images from %s, type: %s, ver %d.%d\n",
302 n_array, fname, name,
303 priv->firm_version >> 8, priv->firm_version & 0xff);
305 priv->firm = kzalloc(sizeof(*priv->firm) * n_array, GFP_KERNEL);
306 if (priv->firm == NULL) {
307 tuner_err("Not enough memory to load firmware file.\n");
311 priv->firm_size = n_array;
321 tuner_err("More firmware images in file than "
326 /* Checks if there's enough bytes to read */
327 if (p + sizeof(type) + sizeof(id) + sizeof(size) > endp) {
328 tuner_err("Firmware header is incomplete!\n");
332 type = le32_to_cpu(*(__u32 *) p);
335 id = le64_to_cpu(*(v4l2_std_id *) p);
339 int_freq = le16_to_cpu(*(__u16 *) p);
340 p += sizeof(int_freq);
343 size = le32_to_cpu(*(__u32 *) p);
346 if ((!size) || (size + p > endp)) {
347 tuner_err("Firmware type ");
348 dump_firm_type(type);
349 printk("(%x), id %llx is corrupted "
350 "(size=%d, expected %d)\n",
351 type, (unsigned long long)id,
352 (unsigned)(endp - p), size);
356 priv->firm[n].ptr = kzalloc(size, GFP_KERNEL);
357 if (priv->firm[n].ptr == NULL) {
358 tuner_err("Not enough memory to load firmware file.\n");
362 tuner_dbg("Reading firmware type ");
364 dump_firm_type_and_int_freq(type, int_freq);
365 printk("(%x), id %llx, size=%d.\n",
366 type, (unsigned long long)id, size);
369 memcpy(priv->firm[n].ptr, p, size);
370 priv->firm[n].type = type;
371 priv->firm[n].id = id;
372 priv->firm[n].size = size;
373 priv->firm[n].int_freq = int_freq;
378 if (n + 1 != priv->firm_size) {
379 tuner_err("Firmware file is incomplete!\n");
387 tuner_err("Error: firmware file is corrupted!\n");
390 tuner_info("Releasing partially loaded firmware file.\n");
394 release_firmware(fw);
396 tuner_dbg("Firmware files loaded.\n");
401 static int seek_firmware(struct dvb_frontend *fe, unsigned int type,
404 struct xc2028_data *priv = fe->tuner_priv;
405 int i, best_i = -1, best_nr_matches = 0;
406 unsigned int type_mask = 0;
408 tuner_dbg("%s called, want type=", __func__);
410 dump_firm_type(type);
411 printk("(%x), id %016llx.\n", type, (unsigned long long)*id);
415 tuner_err("Error! firmware not loaded\n");
419 if (((type & ~SCODE) == 0) && (*id == 0))
423 type_mask = BASE_TYPES;
424 else if (type & SCODE) {
426 type_mask = SCODE_TYPES & ~HAS_IF;
427 } else if (type & DTV_TYPES)
428 type_mask = DTV_TYPES;
429 else if (type & STD_SPECIFIC_TYPES)
430 type_mask = STD_SPECIFIC_TYPES;
437 /* Seek for exact match */
438 for (i = 0; i < priv->firm_size; i++) {
439 if ((type == (priv->firm[i].type & type_mask)) &&
440 (*id == priv->firm[i].id))
444 /* Seek for generic video standard match */
445 for (i = 0; i < priv->firm_size; i++) {
446 v4l2_std_id match_mask;
449 if (type != (priv->firm[i].type & type_mask))
452 match_mask = *id & priv->firm[i].id;
456 if ((*id & match_mask) == *id)
457 goto found; /* Supports all the requested standards */
459 nr_matches = hweight64(match_mask);
460 if (nr_matches > best_nr_matches) {
461 best_nr_matches = nr_matches;
466 if (best_nr_matches > 0) {
467 tuner_dbg("Selecting best matching firmware (%d bits) for "
468 "type=", best_nr_matches);
469 dump_firm_type(type);
470 printk("(%x), id %016llx:\n", type, (unsigned long long)*id);
475 /*FIXME: Would make sense to seek for type "hint" match ? */
481 *id = priv->firm[i].id;
484 tuner_dbg("%s firmware for type=", (i < 0) ? "Can't find" : "Found");
486 dump_firm_type(type);
487 printk("(%x), id %016llx.\n", type, (unsigned long long)*id);
492 static int load_firmware(struct dvb_frontend *fe, unsigned int type,
495 struct xc2028_data *priv = fe->tuner_priv;
497 unsigned char *p, *endp, buf[priv->ctrl.max_len];
499 tuner_dbg("%s called\n", __func__);
501 pos = seek_firmware(fe, type, id);
505 tuner_info("Loading firmware for type=");
506 dump_firm_type(priv->firm[pos].type);
507 printk("(%x), id %016llx.\n", priv->firm[pos].type,
508 (unsigned long long)*id);
510 p = priv->firm[pos].ptr;
511 endp = p + priv->firm[pos].size;
516 /* Checks if there's enough bytes to read */
517 if (p + sizeof(size) > endp) {
518 tuner_err("Firmware chunk size is wrong\n");
522 size = le16_to_cpu(*(__u16 *) p);
529 /* Special callback command received */
530 rc = priv->tuner_callback(priv->video_dev,
531 XC2028_TUNER_RESET, 0);
533 tuner_err("Error at RESET code %d\n",
539 if (size >= 0xff00) {
542 rc = priv->tuner_callback(priv->video_dev,
543 XC2028_RESET_CLK, 0);
545 tuner_err("Error at RESET code %d\n",
551 tuner_info("Invalid RESET code %d\n",
559 /* Checks for a sleep command */
561 msleep(size & 0x7fff);
565 if ((size + p > endp)) {
566 tuner_err("missing bytes: need %d, have %d\n",
567 size, (int)(endp - p));
575 /* Sends message chunks */
577 int len = (size < priv->ctrl.max_len - 1) ?
578 size : priv->ctrl.max_len - 1;
580 memcpy(buf + 1, p, len);
582 rc = i2c_send(priv, buf, len + 1);
584 tuner_err("%d returned from send\n", rc);
595 static int load_scode(struct dvb_frontend *fe, unsigned int type,
596 v4l2_std_id *id, __u16 int_freq, int scode)
598 struct xc2028_data *priv = fe->tuner_priv;
602 tuner_dbg("%s called\n", __func__);
605 pos = seek_firmware(fe, type, id);
609 for (pos = 0; pos < priv->firm_size; pos++) {
610 if ((priv->firm[pos].int_freq == int_freq) &&
611 (priv->firm[pos].type & HAS_IF))
614 if (pos == priv->firm_size)
618 p = priv->firm[pos].ptr;
620 if (priv->firm[pos].type & HAS_IF) {
621 if (priv->firm[pos].size != 12 * 16 || scode >= 16)
625 /* 16 SCODE entries per file; each SCODE entry is 12 bytes and
626 * has a 2-byte size header in the firmware format. */
627 if (priv->firm[pos].size != 14 * 16 || scode >= 16 ||
628 le16_to_cpu(*(__u16 *)(p + 14 * scode)) != 12)
633 tuner_info("Loading SCODE for type=");
634 dump_firm_type_and_int_freq(priv->firm[pos].type,
635 priv->firm[pos].int_freq);
636 printk("(%x), id %016llx.\n", priv->firm[pos].type,
637 (unsigned long long)*id);
639 if (priv->firm_version < 0x0202)
640 rc = send_seq(priv, {0x20, 0x00, 0x00, 0x00});
642 rc = send_seq(priv, {0xa0, 0x00, 0x00, 0x00});
646 rc = i2c_send(priv, p, 12);
650 rc = send_seq(priv, {0x00, 0x8c});
657 static int check_firmware(struct dvb_frontend *fe, unsigned int type,
658 v4l2_std_id std, __u16 int_freq)
660 struct xc2028_data *priv = fe->tuner_priv;
661 struct firmware_properties new_fw;
662 int rc = 0, is_retry = 0;
663 u16 version, hwmodel;
666 tuner_dbg("%s called\n", __func__);
669 if (!priv->ctrl.fname) {
670 tuner_info("xc2028/3028 firmware name not set!\n");
674 rc = load_all_firmwares(fe);
679 if (priv->ctrl.mts && !(type & FM))
685 new_fw.std_req = std;
686 new_fw.scode_table = SCODE | priv->ctrl.scode_table;
688 new_fw.int_freq = int_freq;
690 tuner_dbg("checking firmware, user requested type=");
692 dump_firm_type(new_fw.type);
693 printk("(%x), id %016llx, ", new_fw.type,
694 (unsigned long long)new_fw.std_req);
696 printk("scode_tbl ");
697 dump_firm_type(priv->ctrl.scode_table);
698 printk("(%x), ", priv->ctrl.scode_table);
700 printk("int_freq %d, ", new_fw.int_freq);
701 printk("scode_nr %d\n", new_fw.scode_nr);
704 /* No need to reload base firmware if it matches */
705 if (((BASE | new_fw.type) & BASE_TYPES) ==
706 (priv->cur_fw.type & BASE_TYPES)) {
707 tuner_dbg("BASE firmware not changed.\n");
711 /* Updating BASE - forget about all currently loaded firmware */
712 memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
714 /* Reset is needed before loading firmware */
715 rc = priv->tuner_callback(priv->video_dev,
716 XC2028_TUNER_RESET, 0);
720 /* BASE firmwares are all std0 */
722 rc = load_firmware(fe, BASE | new_fw.type, &std0);
724 tuner_err("Error %d while loading base firmware\n",
729 /* Load INIT1, if needed */
730 tuner_dbg("Load init1 firmware, if exists\n");
732 rc = load_firmware(fe, BASE | INIT1 | new_fw.type, &std0);
734 rc = load_firmware(fe, (BASE | INIT1 | new_fw.type) & ~F8MHZ,
736 if (rc < 0 && rc != -ENOENT) {
737 tuner_err("Error %d while loading init1 firmware\n",
744 * No need to reload standard specific firmware if base firmware
745 * was not reloaded and requested video standards have not changed.
747 if (priv->cur_fw.type == (BASE | new_fw.type) &&
748 priv->cur_fw.std_req == std) {
749 tuner_dbg("Std-specific firmware already loaded.\n");
750 goto skip_std_specific;
753 /* Reloading std-specific firmware forces a SCODE update */
754 priv->cur_fw.scode_table = 0;
756 rc = load_firmware(fe, new_fw.type, &new_fw.id);
758 rc = load_firmware(fe, new_fw.type & ~F8MHZ, &new_fw.id);
764 if (priv->cur_fw.scode_table == new_fw.scode_table &&
765 priv->cur_fw.scode_nr == new_fw.scode_nr) {
766 tuner_dbg("SCODE firmware already loaded.\n");
770 if (new_fw.type & FM)
773 /* Load SCODE firmware, if exists */
774 tuner_dbg("Trying to load scode %d\n", new_fw.scode_nr);
776 rc = load_scode(fe, new_fw.type | new_fw.scode_table, &new_fw.id,
777 new_fw.int_freq, new_fw.scode_nr);
780 if (xc2028_get_reg(priv, 0x0004, &version) < 0 ||
781 xc2028_get_reg(priv, 0x0008, &hwmodel) < 0) {
782 tuner_err("Unable to read tuner registers.\n");
786 tuner_dbg("Device is Xceive %d version %d.%d, "
787 "firmware version %d.%d\n",
788 hwmodel, (version & 0xf000) >> 12, (version & 0xf00) >> 8,
789 (version & 0xf0) >> 4, version & 0xf);
791 /* Check firmware version against what we downloaded. */
792 if (priv->firm_version != ((version & 0xf0) << 4 | (version & 0x0f))) {
793 tuner_err("Incorrect readback of firmware version.\n");
797 /* Check that the tuner hardware model remains consistent over time. */
798 if (priv->hwmodel == 0 && (hwmodel == 2028 || hwmodel == 3028)) {
799 priv->hwmodel = hwmodel;
800 priv->hwvers = version & 0xff00;
801 } else if (priv->hwmodel == 0 || priv->hwmodel != hwmodel ||
802 priv->hwvers != (version & 0xff00)) {
803 tuner_err("Read invalid device hardware information - tuner "
808 memcpy(&priv->cur_fw, &new_fw, sizeof(priv->cur_fw));
811 * By setting BASE in cur_fw.type only after successfully loading all
813 * 1. Identify that BASE firmware with type=0 has been loaded;
814 * 2. Tell whether BASE firmware was just changed the next time through.
816 priv->cur_fw.type |= BASE;
821 memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
825 tuner_dbg("Retrying firmware load\n");
834 static int xc2028_signal(struct dvb_frontend *fe, u16 *strength)
836 struct xc2028_data *priv = fe->tuner_priv;
837 u16 frq_lock, signal = 0;
840 tuner_dbg("%s called\n", __func__);
842 mutex_lock(&priv->lock);
844 /* Sync Lock Indicator */
845 rc = xc2028_get_reg(priv, 0x0002, &frq_lock);
849 /* Frequency is locked */
853 /* Get SNR of the video signal */
854 rc = xc2028_get_reg(priv, 0x0040, &signal);
858 /* Use both frq_lock and signal to generate the result */
859 signal = signal || ((signal & 0x07) << 12);
862 mutex_unlock(&priv->lock);
866 tuner_dbg("signal strength is %d\n", signal);
873 static int generic_set_freq(struct dvb_frontend *fe, u32 freq /* in HZ */,
874 enum tuner_mode new_mode,
879 struct xc2028_data *priv = fe->tuner_priv;
881 unsigned char buf[4];
884 tuner_dbg("%s called\n", __func__);
886 mutex_lock(&priv->lock);
888 tuner_dbg("should set frequency %d kHz\n", freq / 1000);
890 if (check_firmware(fe, type, std, int_freq) < 0)
893 /* On some cases xc2028 can disable video output, if
894 * very weak signals are received. By sending a soft
895 * reset, this is re-enabled. So, it is better to always
896 * send a soft reset before changing channels, to be sure
897 * that xc2028 will be in a safe state.
898 * Maybe this might also be needed for DTV.
900 if (new_mode == T_ANALOG_TV) {
901 rc = send_seq(priv, {0x00, 0x00});
902 } else if (priv->cur_fw.type & ATSC) {
907 * We must adjust the offset by 500kHz in two cases in order
908 * to correctly center the IF output:
909 * 1) When the ZARLINK456 or DIBCOM52 tables were explicitly
910 * selected and a 7MHz channel is tuned;
911 * 2) When tuning a VHF channel with DTV78 firmware.
913 if (((priv->cur_fw.type & DTV7) &&
914 (priv->cur_fw.scode_table & (ZARLINK456 | DIBCOM52))) ||
915 ((priv->cur_fw.type & DTV78) && freq < 470000000))
919 div = (freq - offset + DIV / 2) / DIV;
921 /* CMD= Set frequency */
922 if (priv->firm_version < 0x0202)
923 rc = send_seq(priv, {0x00, 0x02, 0x00, 0x00});
925 rc = send_seq(priv, {0x80, 0x02, 0x00, 0x00});
929 /* Return code shouldn't be checked.
930 The reset CLK is needed only with tm6000.
931 Driver should work fine even if this fails.
933 priv->tuner_callback(priv->video_dev, XC2028_RESET_CLK, 1);
937 buf[0] = 0xff & (div >> 24);
938 buf[1] = 0xff & (div >> 16);
939 buf[2] = 0xff & (div >> 8);
940 buf[3] = 0xff & (div);
942 rc = i2c_send(priv, buf, sizeof(buf));
947 priv->frequency = freq;
949 tuner_dbg("divisor= %02x %02x %02x %02x (freq=%d.%03d)\n",
950 buf[0], buf[1], buf[2], buf[3],
951 freq / 1000000, (freq % 1000000) / 1000);
956 mutex_unlock(&priv->lock);
961 static int xc2028_set_analog_freq(struct dvb_frontend *fe,
962 struct analog_parameters *p)
964 struct xc2028_data *priv = fe->tuner_priv;
967 tuner_dbg("%s called\n", __func__);
969 if (p->mode == V4L2_TUNER_RADIO) {
971 if (priv->ctrl.input1)
973 return generic_set_freq(fe, (625l * p->frequency) / 10,
974 T_ANALOG_TV, type, 0, 0);
977 /* if std is not defined, choose one */
979 p->std = V4L2_STD_MN;
981 /* PAL/M, PAL/N, PAL/Nc and NTSC variants should use 6MHz firmware */
982 if (!(p->std & V4L2_STD_MN))
985 /* Add audio hack to std mask */
986 p->std |= parse_audio_std_option();
988 return generic_set_freq(fe, 62500l * p->frequency,
989 T_ANALOG_TV, type, p->std, 0);
992 static int xc2028_set_params(struct dvb_frontend *fe,
993 struct dvb_frontend_parameters *p)
995 struct xc2028_data *priv = fe->tuner_priv;
997 fe_bandwidth_t bw = BANDWIDTH_8_MHZ;
1000 tuner_dbg("%s called\n", __func__);
1002 if (priv->ctrl.d2633)
1007 switch(fe->ops.info.type) {
1009 bw = p->u.ofdm.bandwidth;
1012 tuner_info("WARN: There are some reports that "
1013 "QAM 6 MHz doesn't work.\n"
1014 "If this works for you, please report by "
1015 "e-mail to: v4l-dvb-maintainer@linuxtv.org\n");
1016 bw = BANDWIDTH_6_MHZ;
1020 bw = BANDWIDTH_6_MHZ;
1021 /* The only ATSC firmware (at least on v2.7) is D2633,
1022 so overrides ctrl->d2633 */
1023 type |= ATSC| D2633;
1026 /* DVB-S is not supported */
1032 case BANDWIDTH_8_MHZ:
1033 if (p->frequency < 470000000)
1034 priv->ctrl.vhfbw7 = 0;
1036 priv->ctrl.uhfbw8 = 1;
1037 type |= (priv->ctrl.vhfbw7 && priv->ctrl.uhfbw8) ? DTV78 : DTV8;
1040 case BANDWIDTH_7_MHZ:
1041 if (p->frequency < 470000000)
1042 priv->ctrl.vhfbw7 = 1;
1044 priv->ctrl.uhfbw8 = 0;
1045 type |= (priv->ctrl.vhfbw7 && priv->ctrl.uhfbw8) ? DTV78 : DTV7;
1048 case BANDWIDTH_6_MHZ:
1050 priv->ctrl.vhfbw7 = 0;
1051 priv->ctrl.uhfbw8 = 0;
1054 tuner_err("error: bandwidth not supported.\n");
1057 /* All S-code tables need a 200kHz shift */
1058 if (priv->ctrl.demod)
1059 demod = priv->ctrl.demod + 200;
1061 return generic_set_freq(fe, p->frequency,
1062 T_DIGITAL_TV, type, 0, demod);
1066 static int xc2028_dvb_release(struct dvb_frontend *fe)
1068 struct xc2028_data *priv = fe->tuner_priv;
1070 tuner_dbg("%s called\n", __func__);
1072 mutex_lock(&xc2028_list_mutex);
1074 /* only perform final cleanup if this is the last instance */
1075 if (hybrid_tuner_report_instance_count(priv) == 1) {
1076 kfree(priv->ctrl.fname);
1077 free_firmware(priv);
1081 hybrid_tuner_release_state(priv);
1083 mutex_unlock(&xc2028_list_mutex);
1085 fe->tuner_priv = NULL;
1090 static int xc2028_get_frequency(struct dvb_frontend *fe, u32 *frequency)
1092 struct xc2028_data *priv = fe->tuner_priv;
1094 tuner_dbg("%s called\n", __func__);
1096 *frequency = priv->frequency;
1101 static int xc2028_set_config(struct dvb_frontend *fe, void *priv_cfg)
1103 struct xc2028_data *priv = fe->tuner_priv;
1104 struct xc2028_ctrl *p = priv_cfg;
1107 tuner_dbg("%s called\n", __func__);
1109 mutex_lock(&priv->lock);
1111 memcpy(&priv->ctrl, p, sizeof(priv->ctrl));
1112 if (priv->ctrl.max_len < 9)
1113 priv->ctrl.max_len = 13;
1116 if (priv->ctrl.fname && strcmp(p->fname, priv->ctrl.fname)) {
1117 kfree(priv->ctrl.fname);
1118 free_firmware(priv);
1121 priv->ctrl.fname = kstrdup(p->fname, GFP_KERNEL);
1122 if (priv->ctrl.fname == NULL)
1126 mutex_unlock(&priv->lock);
1131 static const struct dvb_tuner_ops xc2028_dvb_tuner_ops = {
1133 .name = "Xceive XC3028",
1134 .frequency_min = 42000000,
1135 .frequency_max = 864000000,
1136 .frequency_step = 50000,
1139 .set_config = xc2028_set_config,
1140 .set_analog_params = xc2028_set_analog_freq,
1141 .release = xc2028_dvb_release,
1142 .get_frequency = xc2028_get_frequency,
1143 .get_rf_strength = xc2028_signal,
1144 .set_params = xc2028_set_params,
1147 struct dvb_frontend *xc2028_attach(struct dvb_frontend *fe,
1148 struct xc2028_config *cfg)
1150 struct xc2028_data *priv;
1154 printk(KERN_DEBUG "xc2028: Xcv2028/3028 init called!\n");
1160 printk(KERN_ERR "xc2028: No frontend!\n");
1164 mutex_lock(&xc2028_list_mutex);
1166 instance = hybrid_tuner_request_state(struct xc2028_data, priv,
1167 hybrid_tuner_instance_list,
1168 cfg->i2c_adap, cfg->i2c_addr,
1172 /* memory allocation failure */
1176 /* new tuner instance */
1177 priv->tuner_callback = cfg->callback;
1178 priv->ctrl.max_len = 13;
1180 mutex_init(&priv->lock);
1182 /* analog side (tuner-core) uses i2c_adap->algo_data.
1183 * digital side is not guaranteed to have algo_data defined.
1185 * digital side will always have fe->dvb defined.
1186 * analog side (tuner-core) doesn't (yet) define fe->dvb.
1188 priv->video_dev = ((fe->dvb) && (fe->dvb->priv)) ?
1189 fe->dvb->priv : cfg->i2c_adap->algo_data;
1191 fe->tuner_priv = priv;
1194 /* existing tuner instance */
1195 fe->tuner_priv = priv;
1199 memcpy(&fe->ops.tuner_ops, &xc2028_dvb_tuner_ops,
1200 sizeof(xc2028_dvb_tuner_ops));
1202 tuner_info("type set to %s\n", "XCeive xc2028/xc3028 tuner");
1205 xc2028_set_config(fe, cfg->ctrl);
1207 mutex_unlock(&xc2028_list_mutex);
1211 mutex_unlock(&xc2028_list_mutex);
1213 xc2028_dvb_release(fe);
1217 EXPORT_SYMBOL(xc2028_attach);
1219 MODULE_DESCRIPTION("Xceive xc2028/xc3028 tuner driver");
1220 MODULE_AUTHOR("Michel Ludwig <michel.ludwig@gmail.com>");
1221 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
1222 MODULE_LICENSE("GPL");