3 * Copyright (c) 2007 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 LIST_HEAD(xc2028_list);
45 static DEFINE_MUTEX(xc2028_list_mutex);
47 /* struct for storing firmware table */
48 struct firmware_description {
56 struct firmware_properties {
61 unsigned int scode_table;
66 struct list_head xc2028_list;
67 struct tuner_i2c_props i2c_props;
68 int (*tuner_callback) (void *dev,
69 int command, int arg);
74 struct firmware_description *firm;
81 struct xc2028_ctrl ctrl;
83 struct firmware_properties cur_fw;
88 #define i2c_send(priv, buf, size) ({ \
90 _rc = tuner_i2c_xfer_send(&priv->i2c_props, buf, size); \
92 tuner_info("i2c output error: rc = %d (should be %d)\n",\
97 #define i2c_rcv(priv, buf, size) ({ \
99 _rc = tuner_i2c_xfer_recv(&priv->i2c_props, buf, size); \
101 tuner_err("i2c input error: rc = %d (should be %d)\n", \
106 #define i2c_send_recv(priv, obuf, osize, ibuf, isize) ({ \
108 _rc = tuner_i2c_xfer_send_recv(&priv->i2c_props, obuf, osize, \
111 tuner_err("i2c input error: rc = %d (should be %d)\n", \
116 #define send_seq(priv, data...) ({ \
117 static u8 _val[] = data; \
119 if (sizeof(_val) != \
120 (_rc = tuner_i2c_xfer_send(&priv->i2c_props, \
121 _val, sizeof(_val)))) { \
122 tuner_err("Error on line %d: %d\n", __LINE__, _rc); \
128 static unsigned int xc2028_get_reg(struct xc2028_data *priv, u16 reg, u16 *val)
130 unsigned char buf[2];
131 unsigned char ibuf[2];
133 tuner_dbg("%s %04x called\n", __FUNCTION__, reg);
136 buf[1] = (unsigned char) reg;
138 if (i2c_send_recv(priv, buf, 2, ibuf, 2) != 2)
141 *val = (ibuf[1]) | (ibuf[0] << 8);
145 #define dump_firm_type(t) dump_firm_type_and_int_freq(t, 0)
146 static void dump_firm_type_and_int_freq(unsigned int type, u16 int_freq)
192 if (type & TOYOTA388)
193 printk("TOYOTA388 ");
194 if (type & TOYOTA794)
195 printk("TOYOTA794 ");
198 if (type & ZARLINK456)
199 printk("ZARLINK456 ");
209 printk("HAS_IF_%d ", int_freq);
212 static v4l2_std_id parse_audio_std_option(void)
214 if (strcasecmp(audio_std, "A2") == 0)
216 if (strcasecmp(audio_std, "A2/A") == 0)
217 return V4L2_STD_A2_A;
218 if (strcasecmp(audio_std, "A2/B") == 0)
219 return V4L2_STD_A2_B;
220 if (strcasecmp(audio_std, "NICAM") == 0)
221 return V4L2_STD_NICAM;
222 if (strcasecmp(audio_std, "NICAM/A") == 0)
223 return V4L2_STD_NICAM_A;
224 if (strcasecmp(audio_std, "NICAM/B") == 0)
225 return V4L2_STD_NICAM_B;
230 static void free_firmware(struct xc2028_data *priv)
237 for (i = 0; i < priv->firm_size; i++)
238 kfree(priv->firm[i].ptr);
245 memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
248 static int load_all_firmwares(struct dvb_frontend *fe)
250 struct xc2028_data *priv = fe->tuner_priv;
251 const struct firmware *fw = NULL;
252 unsigned char *p, *endp;
257 tuner_dbg("%s called\n", __FUNCTION__);
259 tuner_dbg("Reading firmware %s\n", priv->ctrl.fname);
260 rc = request_firmware(&fw, priv->ctrl.fname,
261 &priv->i2c_props.adap->dev);
264 tuner_err("Error: firmware %s not found.\n",
267 tuner_err("Error %d while requesting firmware %s \n",
268 rc, priv->ctrl.fname);
275 if (fw->size < sizeof(name) - 1 + 2 + 2) {
276 tuner_err("Error: firmware file %s has invalid size!\n",
281 memcpy(name, p, sizeof(name) - 1);
282 name[sizeof(name) - 1] = 0;
283 p += sizeof(name) - 1;
285 priv->firm_version = le16_to_cpu(*(__u16 *) p);
288 n_array = le16_to_cpu(*(__u16 *) p);
291 tuner_info("Loading %d firmware images from %s, type: %s, ver %d.%d\n",
292 n_array, priv->ctrl.fname, name,
293 priv->firm_version >> 8, priv->firm_version & 0xff);
295 priv->firm = kzalloc(sizeof(*priv->firm) * n_array, GFP_KERNEL);
296 if (priv->firm == NULL) {
297 tuner_err("Not enough memory to load firmware file.\n");
301 priv->firm_size = n_array;
311 tuner_err("More firmware images in file than "
316 /* Checks if there's enough bytes to read */
317 if (p + sizeof(type) + sizeof(id) + sizeof(size) > endp) {
318 tuner_err("Firmware header is incomplete!\n");
322 type = le32_to_cpu(*(__u32 *) p);
325 id = le64_to_cpu(*(v4l2_std_id *) p);
329 int_freq = le16_to_cpu(*(__u16 *) p);
330 p += sizeof(int_freq);
333 size = le32_to_cpu(*(__u32 *) p);
336 if ((!size) || (size + p > endp)) {
337 tuner_err("Firmware type ");
338 dump_firm_type(type);
339 printk("(%x), id %llx is corrupted "
340 "(size=%d, expected %d)\n",
341 type, (unsigned long long)id,
342 (unsigned)(endp - p), size);
346 priv->firm[n].ptr = kzalloc(size, GFP_KERNEL);
347 if (priv->firm[n].ptr == NULL) {
348 tuner_err("Not enough memory to load firmware file.\n");
352 tuner_dbg("Reading firmware type ");
354 dump_firm_type_and_int_freq(type, int_freq);
355 printk("(%x), id %llx, size=%d.\n",
356 type, (unsigned long long)id, size);
359 memcpy(priv->firm[n].ptr, p, size);
360 priv->firm[n].type = type;
361 priv->firm[n].id = id;
362 priv->firm[n].size = size;
363 priv->firm[n].int_freq = int_freq;
368 if (n + 1 != priv->firm_size) {
369 tuner_err("Firmware file is incomplete!\n");
377 tuner_err("Error: firmware file is corrupted!\n");
380 tuner_info("Releasing partially loaded firmware file.\n");
384 release_firmware(fw);
386 tuner_dbg("Firmware files loaded.\n");
391 static int seek_firmware(struct dvb_frontend *fe, unsigned int type,
394 struct xc2028_data *priv = fe->tuner_priv;
395 int i, best_i = -1, best_nr_matches = 0;
396 unsigned int ign_firm_type_mask = 0;
398 tuner_dbg("%s called, want type=", __FUNCTION__);
400 dump_firm_type(type);
401 printk("(%x), id %016llx.\n", type, (unsigned long long)*id);
405 tuner_err("Error! firmware not loaded\n");
409 if (((type & ~SCODE) == 0) && (*id == 0))
414 else if (type & SCODE) {
416 ign_firm_type_mask = HAS_IF;
417 } else if (type & DTV_TYPES)
419 else if (type & STD_SPECIFIC_TYPES)
420 type &= STD_SPECIFIC_TYPES;
422 /* Seek for exact match */
423 for (i = 0; i < priv->firm_size; i++) {
424 if ((type == (priv->firm[i].type & ~ign_firm_type_mask)) &&
425 (*id == priv->firm[i].id))
429 /* Seek for generic video standard match */
430 for (i = 0; i < priv->firm_size; i++) {
431 v4l2_std_id match_mask;
434 if (type != (priv->firm[i].type & ~ign_firm_type_mask))
437 match_mask = *id & priv->firm[i].id;
441 if ((*id & match_mask) == *id)
442 goto found; /* Supports all the requested standards */
444 nr_matches = hweight64(match_mask);
445 if (nr_matches > best_nr_matches) {
446 best_nr_matches = nr_matches;
451 if (best_nr_matches > 0) {
452 tuner_dbg("Selecting best matching firmware (%d bits) for "
453 "type=", best_nr_matches);
454 dump_firm_type(type);
455 printk("(%x), id %016llx:\n", type, (unsigned long long)*id);
460 /*FIXME: Would make sense to seek for type "hint" match ? */
466 *id = priv->firm[i].id;
469 tuner_dbg("%s firmware for type=", (i < 0) ? "Can't find" : "Found");
471 dump_firm_type(type);
472 printk("(%x), id %016llx.\n", type, (unsigned long long)*id);
477 static int load_firmware(struct dvb_frontend *fe, unsigned int type,
480 struct xc2028_data *priv = fe->tuner_priv;
482 unsigned char *p, *endp, buf[priv->ctrl.max_len];
484 tuner_dbg("%s called\n", __FUNCTION__);
486 pos = seek_firmware(fe, type, id);
490 tuner_info("Loading firmware for type=");
491 dump_firm_type(priv->firm[pos].type);
492 printk("(%x), id %016llx.\n", priv->firm[pos].type,
493 (unsigned long long)*id);
495 p = priv->firm[pos].ptr;
496 endp = p + priv->firm[pos].size;
501 /* Checks if there's enough bytes to read */
502 if (p + sizeof(size) > endp) {
503 tuner_err("Firmware chunk size is wrong\n");
507 size = le16_to_cpu(*(__u16 *) p);
514 /* Special callback command received */
515 rc = priv->tuner_callback(priv->video_dev,
516 XC2028_TUNER_RESET, 0);
518 tuner_err("Error at RESET code %d\n",
524 if (size >= 0xff00) {
527 rc = priv->tuner_callback(priv->video_dev,
528 XC2028_RESET_CLK, 0);
530 tuner_err("Error at RESET code %d\n",
536 tuner_info("Invalid RESET code %d\n",
544 /* Checks for a sleep command */
546 msleep(size & 0x7fff);
550 if ((size + p > endp)) {
551 tuner_err("missing bytes: need %d, have %d\n",
552 size, (int)(endp - p));
560 /* Sends message chunks */
562 int len = (size < priv->ctrl.max_len - 1) ?
563 size : priv->ctrl.max_len - 1;
565 memcpy(buf + 1, p, len);
567 rc = i2c_send(priv, buf, len + 1);
569 tuner_err("%d returned from send\n", rc);
580 static int load_scode(struct dvb_frontend *fe, unsigned int type,
581 v4l2_std_id *id, __u16 int_freq, int scode)
583 struct xc2028_data *priv = fe->tuner_priv;
587 tuner_dbg("%s called\n", __FUNCTION__);
590 pos = seek_firmware(fe, type, id);
594 for (pos = 0; pos < priv->firm_size; pos++) {
595 if ((priv->firm[pos].int_freq == int_freq) &&
596 (priv->firm[pos].type & HAS_IF))
599 if (pos == priv->firm_size)
603 p = priv->firm[pos].ptr;
605 if (priv->firm[pos].type & HAS_IF) {
606 if (priv->firm[pos].size != 12 * 16 || scode >= 16)
610 /* 16 SCODE entries per file; each SCODE entry is 12 bytes and
611 * has a 2-byte size header in the firmware format. */
612 if (priv->firm[pos].size != 14 * 16 || scode >= 16 ||
613 le16_to_cpu(*(__u16 *)(p + 14 * scode)) != 12)
618 tuner_info("Loading SCODE for type=");
619 dump_firm_type_and_int_freq(priv->firm[pos].type,
620 priv->firm[pos].int_freq);
621 printk("(%x), id %016llx.\n", priv->firm[pos].type,
622 (unsigned long long)*id);
624 if (priv->firm_version < 0x0202)
625 rc = send_seq(priv, {0x20, 0x00, 0x00, 0x00});
627 rc = send_seq(priv, {0xa0, 0x00, 0x00, 0x00});
631 rc = i2c_send(priv, p, 12);
635 rc = send_seq(priv, {0x00, 0x8c});
642 static int check_firmware(struct dvb_frontend *fe, unsigned int type,
643 v4l2_std_id std, __u16 int_freq)
645 struct xc2028_data *priv = fe->tuner_priv;
646 struct firmware_properties new_fw;
647 int rc = 0, is_retry = 0;
648 u16 version, hwmodel;
651 tuner_dbg("%s called\n", __FUNCTION__);
654 if (!priv->ctrl.fname) {
655 tuner_info("xc2028/3028 firmware name not set!\n");
659 rc = load_all_firmwares(fe);
664 if (priv->ctrl.mts && !(type & FM))
670 new_fw.std_req = std;
671 new_fw.scode_table = SCODE | priv->ctrl.scode_table;
673 new_fw.int_freq = int_freq;
675 tuner_dbg("checking firmware, user requested type=");
677 dump_firm_type(new_fw.type);
678 printk("(%x), id %016llx, ", new_fw.type,
679 (unsigned long long)new_fw.std_req);
681 printk("scode_tbl ");
682 dump_firm_type(priv->ctrl.scode_table);
683 printk("(%x), ", priv->ctrl.scode_table);
685 printk("int_freq %d, ", new_fw.int_freq);
686 printk("scode_nr %d\n", new_fw.scode_nr);
689 /* No need to reload base firmware if it matches */
690 if (((BASE | new_fw.type) & BASE_TYPES) ==
691 (priv->cur_fw.type & BASE_TYPES)) {
692 tuner_dbg("BASE firmware not changed.\n");
696 /* Updating BASE - forget about all currently loaded firmware */
697 memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
699 /* Reset is needed before loading firmware */
700 rc = priv->tuner_callback(priv->video_dev,
701 XC2028_TUNER_RESET, 0);
705 /* BASE firmwares are all std0 */
707 rc = load_firmware(fe, BASE | new_fw.type, &std0);
709 tuner_err("Error %d while loading base firmware\n",
714 /* Load INIT1, if needed */
715 tuner_dbg("Load init1 firmware, if exists\n");
717 rc = load_firmware(fe, BASE | INIT1 | new_fw.type, &std0);
719 rc = load_firmware(fe, (BASE | INIT1 | new_fw.type) & ~F8MHZ,
721 if (rc < 0 && rc != -ENOENT) {
722 tuner_err("Error %d while loading init1 firmware\n",
729 * No need to reload standard specific firmware if base firmware
730 * was not reloaded and requested video standards have not changed.
732 if (priv->cur_fw.type == (BASE | new_fw.type) &&
733 priv->cur_fw.std_req == std) {
734 tuner_dbg("Std-specific firmware already loaded.\n");
735 goto skip_std_specific;
738 /* Reloading std-specific firmware forces a SCODE update */
739 priv->cur_fw.scode_table = 0;
741 rc = load_firmware(fe, new_fw.type, &new_fw.id);
743 rc = load_firmware(fe, new_fw.type & ~F8MHZ, &new_fw.id);
749 if (priv->cur_fw.scode_table == new_fw.scode_table &&
750 priv->cur_fw.scode_nr == new_fw.scode_nr) {
751 tuner_dbg("SCODE firmware already loaded.\n");
755 if (new_fw.type & FM)
758 /* Load SCODE firmware, if exists */
759 tuner_dbg("Trying to load scode %d\n", new_fw.scode_nr);
761 rc = load_scode(fe, new_fw.type | new_fw.scode_table, &new_fw.id,
762 new_fw.int_freq, new_fw.scode_nr);
765 if (xc2028_get_reg(priv, 0x0004, &version) < 0 ||
766 xc2028_get_reg(priv, 0x0008, &hwmodel) < 0) {
767 tuner_err("Unable to read tuner registers.\n");
771 tuner_info("Device is Xceive %d version %d.%d, "
772 "firmware version %d.%d\n",
773 hwmodel, (version & 0xf000) >> 12, (version & 0xf00) >> 8,
774 (version & 0xf0) >> 4, version & 0xf);
776 /* Check firmware version against what we downloaded. */
777 if (priv->firm_version != ((version & 0xf0) << 4 | (version & 0x0f))) {
778 tuner_err("Incorrect readback of firmware version.\n");
782 /* Check that the tuner hardware model remains consistent over time. */
783 if (priv->hwmodel == 0 && (hwmodel == 2028 || hwmodel == 3028)) {
784 priv->hwmodel = hwmodel;
785 priv->hwvers = version & 0xff00;
786 } else if (priv->hwmodel == 0 || priv->hwmodel != hwmodel ||
787 priv->hwvers != (version & 0xff00)) {
788 tuner_err("Read invalid device hardware information - tuner "
793 memcpy(&priv->cur_fw, &new_fw, sizeof(priv->cur_fw));
796 * By setting BASE in cur_fw.type only after successfully loading all
798 * 1. Identify that BASE firmware with type=0 has been loaded;
799 * 2. Tell whether BASE firmware was just changed the next time through.
801 priv->cur_fw.type |= BASE;
806 memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
810 tuner_dbg("Retrying firmware load\n");
819 static int xc2028_signal(struct dvb_frontend *fe, u16 *strength)
821 struct xc2028_data *priv = fe->tuner_priv;
822 u16 frq_lock, signal = 0;
825 tuner_dbg("%s called\n", __FUNCTION__);
827 mutex_lock(&priv->lock);
829 /* Sync Lock Indicator */
830 rc = xc2028_get_reg(priv, 0x0002, &frq_lock);
831 if (rc < 0 || frq_lock == 0)
834 /* Frequency is locked. Return signal quality */
836 /* Get SNR of the video signal */
837 rc = xc2028_get_reg(priv, 0x0040, &signal);
842 mutex_unlock(&priv->lock);
851 static int generic_set_freq(struct dvb_frontend *fe, u32 freq /* in HZ */,
852 enum tuner_mode new_mode,
857 struct xc2028_data *priv = fe->tuner_priv;
859 unsigned char buf[4];
862 tuner_dbg("%s called\n", __FUNCTION__);
864 mutex_lock(&priv->lock);
866 tuner_dbg("should set frequency %d kHz\n", freq / 1000);
868 if (check_firmware(fe, type, std, int_freq) < 0)
871 /* On some cases xc2028 can disable video output, if
872 * very weak signals are received. By sending a soft
873 * reset, this is re-enabled. So, it is better to always
874 * send a soft reset before changing channels, to be sure
875 * that xc2028 will be in a safe state.
876 * Maybe this might also be needed for DTV.
878 if (new_mode == T_ANALOG_TV) {
879 rc = send_seq(priv, {0x00, 0x00});
880 } else if (priv->cur_fw.type & ATSC) {
885 * We must adjust the offset by 500kHz in two cases in order
886 * to correctly center the IF output:
887 * 1) When the ZARLINK456 or DIBCOM52 tables were explicitly
888 * selected and a 7MHz channel is tuned;
889 * 2) When tuning a VHF channel with DTV78 firmware.
891 if (((priv->cur_fw.type & DTV7) &&
892 (priv->cur_fw.scode_table & (ZARLINK456 | DIBCOM52))) ||
893 ((priv->cur_fw.type & DTV78) && freq < 470000000))
897 div = (freq - offset + DIV / 2) / DIV;
899 /* CMD= Set frequency */
900 if (priv->firm_version < 0x0202)
901 rc = send_seq(priv, {0x00, 0x02, 0x00, 0x00});
903 rc = send_seq(priv, {0x80, 0x02, 0x00, 0x00});
907 /* Return code shouldn't be checked.
908 The reset CLK is needed only with tm6000.
909 Driver should work fine even if this fails.
911 priv->tuner_callback(priv->video_dev, XC2028_RESET_CLK, 1);
915 buf[0] = 0xff & (div >> 24);
916 buf[1] = 0xff & (div >> 16);
917 buf[2] = 0xff & (div >> 8);
918 buf[3] = 0xff & (div);
920 rc = i2c_send(priv, buf, sizeof(buf));
925 priv->frequency = freq;
927 tuner_dbg("divisor= %02x %02x %02x %02x (freq=%d.%03d)\n",
928 buf[0], buf[1], buf[2], buf[3],
929 freq / 1000000, (freq % 1000000) / 1000);
934 mutex_unlock(&priv->lock);
939 static int xc2028_set_analog_freq(struct dvb_frontend *fe,
940 struct analog_parameters *p)
942 struct xc2028_data *priv = fe->tuner_priv;
945 tuner_dbg("%s called\n", __FUNCTION__);
947 if (p->mode == V4L2_TUNER_RADIO) {
949 if (priv->ctrl.input1)
951 return generic_set_freq(fe, (625l * p->frequency) / 10,
952 T_ANALOG_TV, type, 0, 0);
955 /* if std is not defined, choose one */
957 p->std = V4L2_STD_MN;
959 /* PAL/M, PAL/N, PAL/Nc and NTSC variants should use 6MHz firmware */
960 if (!(p->std & V4L2_STD_MN))
963 /* Add audio hack to std mask */
964 p->std |= parse_audio_std_option();
966 return generic_set_freq(fe, 62500l * p->frequency,
967 T_ANALOG_TV, type, p->std, 0);
970 static int xc2028_set_params(struct dvb_frontend *fe,
971 struct dvb_frontend_parameters *p)
973 struct xc2028_data *priv = fe->tuner_priv;
975 fe_bandwidth_t bw = BANDWIDTH_8_MHZ;
978 tuner_dbg("%s called\n", __FUNCTION__);
980 if (priv->ctrl.d2633)
985 switch(fe->ops.info.type) {
987 bw = p->u.ofdm.bandwidth;
990 tuner_info("WARN: There are some reports that "
991 "QAM 6 MHz doesn't work.\n"
992 "If this works for you, please report by "
993 "e-mail to: v4l-dvb-maintainer@linuxtv.org\n");
994 bw = BANDWIDTH_6_MHZ;
998 bw = BANDWIDTH_6_MHZ;
999 /* The only ATSC firmware (at least on v2.7) is D2633,
1000 so overrides ctrl->d2633 */
1001 type |= ATSC| D2633;
1004 /* DVB-S is not supported */
1010 case BANDWIDTH_8_MHZ:
1011 if (p->frequency < 470000000)
1012 priv->ctrl.vhfbw7 = 0;
1014 priv->ctrl.uhfbw8 = 1;
1015 type |= (priv->ctrl.vhfbw7 && priv->ctrl.uhfbw8) ? DTV78 : DTV8;
1018 case BANDWIDTH_7_MHZ:
1019 if (p->frequency < 470000000)
1020 priv->ctrl.vhfbw7 = 1;
1022 priv->ctrl.uhfbw8 = 0;
1023 type |= (priv->ctrl.vhfbw7 && priv->ctrl.uhfbw8) ? DTV78 : DTV7;
1026 case BANDWIDTH_6_MHZ:
1028 priv->ctrl.vhfbw7 = 0;
1029 priv->ctrl.uhfbw8 = 0;
1032 tuner_err("error: bandwidth not supported.\n");
1035 /* All S-code tables need a 200kHz shift */
1036 if (priv->ctrl.demod)
1037 demod = priv->ctrl.demod + 200;
1039 return generic_set_freq(fe, p->frequency,
1040 T_DIGITAL_TV, type, 0, demod);
1043 static int xc2028_sleep(struct dvb_frontend *fe)
1045 struct xc2028_data *priv = fe->tuner_priv;
1048 tuner_dbg("%s called\n", __FUNCTION__);
1050 mutex_lock(&priv->lock);
1052 if (priv->firm_version < 0x0202)
1053 rc = send_seq(priv, {0x00, 0x08, 0x00, 0x00});
1055 rc = send_seq(priv, {0x80, 0x08, 0x00, 0x00});
1057 priv->cur_fw.type = 0; /* need firmware reload */
1059 mutex_unlock(&priv->lock);
1065 static int xc2028_dvb_release(struct dvb_frontend *fe)
1067 struct xc2028_data *priv = fe->tuner_priv;
1069 tuner_dbg("%s called\n", __FUNCTION__);
1071 mutex_lock(&xc2028_list_mutex);
1076 list_del(&priv->xc2028_list);
1078 kfree(priv->ctrl.fname);
1080 free_firmware(priv);
1082 fe->tuner_priv = NULL;
1085 mutex_unlock(&xc2028_list_mutex);
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", __FUNCTION__);
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", __FUNCTION__);
1109 mutex_lock(&priv->lock);
1111 kfree(priv->ctrl.fname);
1112 free_firmware(priv);
1114 memcpy(&priv->ctrl, p, sizeof(priv->ctrl));
1115 priv->ctrl.fname = NULL;
1118 priv->ctrl.fname = kstrdup(p->fname, GFP_KERNEL);
1119 if (priv->ctrl.fname == NULL)
1123 if (priv->ctrl.max_len < 9)
1124 priv->ctrl.max_len = 13;
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,
1145 .sleep = xc2028_sleep,
1149 struct dvb_frontend *xc2028_attach(struct dvb_frontend *fe,
1150 struct xc2028_config *cfg)
1152 struct xc2028_data *priv;
1156 printk(KERN_DEBUG "xc2028: Xcv2028/3028 init called!\n");
1162 printk(KERN_ERR "xc2028: No frontend!\n");
1166 video_dev = cfg->i2c_adap->algo_data;
1169 printk(KERN_DEBUG "xc2028: video_dev =%p\n", video_dev);
1171 mutex_lock(&xc2028_list_mutex);
1173 list_for_each_entry(priv, &xc2028_list, xc2028_list) {
1174 if (&priv->i2c_props.adap->dev == &cfg->i2c_adap->dev) {
1177 printk(KERN_DEBUG "xc2028: reusing device\n");
1184 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1186 mutex_unlock(&xc2028_list_mutex);
1190 priv->i2c_props.addr = cfg->i2c_addr;
1191 priv->i2c_props.adap = cfg->i2c_adap;
1192 priv->i2c_props.name = "xc2028";
1194 priv->video_dev = video_dev;
1195 priv->tuner_callback = cfg->callback;
1196 priv->ctrl.max_len = 13;
1198 mutex_init(&priv->lock);
1200 list_add_tail(&priv->xc2028_list, &xc2028_list);
1203 fe->tuner_priv = priv;
1207 printk(KERN_DEBUG "xc2028: usage count is %i\n", priv->count);
1209 memcpy(&fe->ops.tuner_ops, &xc2028_dvb_tuner_ops,
1210 sizeof(xc2028_dvb_tuner_ops));
1212 tuner_info("type set to %s\n", "XCeive xc2028/xc3028 tuner");
1215 xc2028_set_config(fe, cfg->ctrl);
1217 mutex_unlock(&xc2028_list_mutex);
1222 EXPORT_SYMBOL(xc2028_attach);
1224 MODULE_DESCRIPTION("Xceive xc2028/xc3028 tuner driver");
1225 MODULE_AUTHOR("Michel Ludwig <michel.ludwig@gmail.com>");
1226 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
1227 MODULE_LICENSE("GPL");