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 <linux/slab.h>
19 #include <asm/unaligned.h>
20 #include "tuner-i2c.h"
21 #include "tuner-xc2028.h"
22 #include "tuner-xc2028-types.h"
24 #include <linux/dvb/frontend.h>
25 #include "dvb_frontend.h"
27 /* Registers (Write-only) */
28 #define XREG_INIT 0x00
29 #define XREG_RF_FREQ 0x02
30 #define XREG_POWER_DOWN 0x08
32 /* Registers (Read-only) */
33 #define XREG_FREQ_ERROR 0x01
34 #define XREG_LOCK 0x02
35 #define XREG_VERSION 0x04
36 #define XREG_PRODUCT_ID 0x08
37 #define XREG_HSYNC_FREQ 0x10
38 #define XREG_FRAME_LINES 0x20
41 #define XREG_ADC_ENV 0x0100
44 module_param(debug
, int, 0644);
45 MODULE_PARM_DESC(debug
, "enable verbose debug messages");
47 static int no_poweroff
;
48 module_param(no_poweroff
, int, 0644);
49 MODULE_PARM_DESC(no_poweroff
, "0 (default) powers device off when not used.\n"
50 "1 keep device energized and with tuner ready all the times.\n"
51 " Faster, but consumes more power and keeps the device hotter\n");
53 static char audio_std
[8];
54 module_param_string(audio_std
, audio_std
, sizeof(audio_std
), 0);
55 MODULE_PARM_DESC(audio_std
,
56 "Audio standard. XC3028 audio decoder explicitly "
57 "needs to know what audio\n"
58 "standard is needed for some video standards with audio A2 or NICAM.\n"
59 "The valid values are:\n"
67 static char firmware_name
[30];
68 module_param_string(firmware_name
, firmware_name
, sizeof(firmware_name
), 0);
69 MODULE_PARM_DESC(firmware_name
, "Firmware file name. Allows overriding the "
70 "default firmware name\n");
72 static LIST_HEAD(hybrid_tuner_instance_list
);
73 static DEFINE_MUTEX(xc2028_list_mutex
);
75 /* struct for storing firmware table */
76 struct firmware_description
{
84 struct firmware_properties
{
89 unsigned int scode_table
;
94 struct list_head hybrid_tuner_instance_list
;
95 struct tuner_i2c_props i2c_props
;
98 struct firmware_description
*firm
;
105 struct xc2028_ctrl ctrl
;
107 struct firmware_properties cur_fw
;
112 #define i2c_send(priv, buf, size) ({ \
114 _rc = tuner_i2c_xfer_send(&priv->i2c_props, buf, size); \
116 tuner_info("i2c output error: rc = %d (should be %d)\n",\
118 if (priv->ctrl.msleep) \
119 msleep(priv->ctrl.msleep); \
123 #define i2c_rcv(priv, buf, size) ({ \
125 _rc = tuner_i2c_xfer_recv(&priv->i2c_props, buf, size); \
127 tuner_err("i2c input error: rc = %d (should be %d)\n", \
132 #define i2c_send_recv(priv, obuf, osize, ibuf, isize) ({ \
134 _rc = tuner_i2c_xfer_send_recv(&priv->i2c_props, obuf, osize, \
137 tuner_err("i2c input error: rc = %d (should be %d)\n", \
139 if (priv->ctrl.msleep) \
140 msleep(priv->ctrl.msleep); \
144 #define send_seq(priv, data...) ({ \
145 static u8 _val[] = data; \
147 if (sizeof(_val) != \
148 (_rc = tuner_i2c_xfer_send(&priv->i2c_props, \
149 _val, sizeof(_val)))) { \
150 tuner_err("Error on line %d: %d\n", __LINE__, _rc); \
151 } else if (priv->ctrl.msleep) \
152 msleep(priv->ctrl.msleep); \
156 static int xc2028_get_reg(struct xc2028_data
*priv
, u16 reg
, u16
*val
)
158 unsigned char buf
[2];
159 unsigned char ibuf
[2];
161 tuner_dbg("%s %04x called\n", __func__
, reg
);
164 buf
[1] = (unsigned char) reg
;
166 if (i2c_send_recv(priv
, buf
, 2, ibuf
, 2) != 2)
169 *val
= (ibuf
[1]) | (ibuf
[0] << 8);
173 #define dump_firm_type(t) dump_firm_type_and_int_freq(t, 0)
174 static void dump_firm_type_and_int_freq(unsigned int type
, u16 int_freq
)
220 if (type
& TOYOTA388
)
221 printk("TOYOTA388 ");
222 if (type
& TOYOTA794
)
223 printk("TOYOTA794 ");
226 if (type
& ZARLINK456
)
227 printk("ZARLINK456 ");
237 printk("HAS_IF_%d ", int_freq
);
240 static v4l2_std_id
parse_audio_std_option(void)
242 if (strcasecmp(audio_std
, "A2") == 0)
244 if (strcasecmp(audio_std
, "A2/A") == 0)
245 return V4L2_STD_A2_A
;
246 if (strcasecmp(audio_std
, "A2/B") == 0)
247 return V4L2_STD_A2_B
;
248 if (strcasecmp(audio_std
, "NICAM") == 0)
249 return V4L2_STD_NICAM
;
250 if (strcasecmp(audio_std
, "NICAM/A") == 0)
251 return V4L2_STD_NICAM_A
;
252 if (strcasecmp(audio_std
, "NICAM/B") == 0)
253 return V4L2_STD_NICAM_B
;
258 static void free_firmware(struct xc2028_data
*priv
)
261 tuner_dbg("%s called\n", __func__
);
266 for (i
= 0; i
< priv
->firm_size
; i
++)
267 kfree(priv
->firm
[i
].ptr
);
274 memset(&priv
->cur_fw
, 0, sizeof(priv
->cur_fw
));
277 static int load_all_firmwares(struct dvb_frontend
*fe
)
279 struct xc2028_data
*priv
= fe
->tuner_priv
;
280 const struct firmware
*fw
= NULL
;
281 const unsigned char *p
, *endp
;
287 tuner_dbg("%s called\n", __func__
);
289 if (!firmware_name
[0])
290 fname
= priv
->ctrl
.fname
;
292 fname
= firmware_name
;
294 tuner_dbg("Reading firmware %s\n", fname
);
295 rc
= request_firmware(&fw
, fname
, priv
->i2c_props
.adap
->dev
.parent
);
298 tuner_err("Error: firmware %s not found.\n",
301 tuner_err("Error %d while requesting firmware %s \n",
309 if (fw
->size
< sizeof(name
) - 1 + 2 + 2) {
310 tuner_err("Error: firmware file %s has invalid size!\n",
315 memcpy(name
, p
, sizeof(name
) - 1);
316 name
[sizeof(name
) - 1] = 0;
317 p
+= sizeof(name
) - 1;
319 priv
->firm_version
= get_unaligned_le16(p
);
322 n_array
= get_unaligned_le16(p
);
325 tuner_info("Loading %d firmware images from %s, type: %s, ver %d.%d\n",
326 n_array
, fname
, name
,
327 priv
->firm_version
>> 8, priv
->firm_version
& 0xff);
329 priv
->firm
= kcalloc(n_array
, sizeof(*priv
->firm
), GFP_KERNEL
);
330 if (priv
->firm
== NULL
) {
331 tuner_err("Not enough memory to load firmware file.\n");
335 priv
->firm_size
= n_array
;
345 tuner_err("More firmware images in file than "
350 /* Checks if there's enough bytes to read */
351 if (endp
- p
< sizeof(type
) + sizeof(id
) + sizeof(size
))
354 type
= get_unaligned_le32(p
);
357 id
= get_unaligned_le64(p
);
361 int_freq
= get_unaligned_le16(p
);
362 p
+= sizeof(int_freq
);
363 if (endp
- p
< sizeof(size
))
367 size
= get_unaligned_le32(p
);
370 if (!size
|| size
> endp
- p
) {
371 tuner_err("Firmware type ");
372 dump_firm_type(type
);
373 printk("(%x), id %llx is corrupted "
374 "(size=%d, expected %d)\n",
375 type
, (unsigned long long)id
,
376 (unsigned)(endp
- p
), size
);
380 priv
->firm
[n
].ptr
= kzalloc(size
, GFP_KERNEL
);
381 if (priv
->firm
[n
].ptr
== NULL
) {
382 tuner_err("Not enough memory to load firmware file.\n");
386 tuner_dbg("Reading firmware type ");
388 dump_firm_type_and_int_freq(type
, int_freq
);
389 printk("(%x), id %llx, size=%d.\n",
390 type
, (unsigned long long)id
, size
);
393 memcpy(priv
->firm
[n
].ptr
, p
, size
);
394 priv
->firm
[n
].type
= type
;
395 priv
->firm
[n
].id
= id
;
396 priv
->firm
[n
].size
= size
;
397 priv
->firm
[n
].int_freq
= int_freq
;
402 if (n
+ 1 != priv
->firm_size
) {
403 tuner_err("Firmware file is incomplete!\n");
410 tuner_err("Firmware header is incomplete!\n");
413 tuner_err("Error: firmware file is corrupted!\n");
416 tuner_info("Releasing partially loaded firmware file.\n");
420 release_firmware(fw
);
422 tuner_dbg("Firmware files loaded.\n");
427 static int seek_firmware(struct dvb_frontend
*fe
, unsigned int type
,
430 struct xc2028_data
*priv
= fe
->tuner_priv
;
431 int i
, best_i
= -1, best_nr_matches
= 0;
432 unsigned int type_mask
= 0;
434 tuner_dbg("%s called, want type=", __func__
);
436 dump_firm_type(type
);
437 printk("(%x), id %016llx.\n", type
, (unsigned long long)*id
);
441 tuner_err("Error! firmware not loaded\n");
445 if (((type
& ~SCODE
) == 0) && (*id
== 0))
449 type_mask
= BASE_TYPES
;
450 else if (type
& SCODE
) {
452 type_mask
= SCODE_TYPES
& ~HAS_IF
;
453 } else if (type
& DTV_TYPES
)
454 type_mask
= DTV_TYPES
;
455 else if (type
& STD_SPECIFIC_TYPES
)
456 type_mask
= STD_SPECIFIC_TYPES
;
463 /* Seek for exact match */
464 for (i
= 0; i
< priv
->firm_size
; i
++) {
465 if ((type
== (priv
->firm
[i
].type
& type_mask
)) &&
466 (*id
== priv
->firm
[i
].id
))
470 /* Seek for generic video standard match */
471 for (i
= 0; i
< priv
->firm_size
; i
++) {
472 v4l2_std_id match_mask
;
475 if (type
!= (priv
->firm
[i
].type
& type_mask
))
478 match_mask
= *id
& priv
->firm
[i
].id
;
482 if ((*id
& match_mask
) == *id
)
483 goto found
; /* Supports all the requested standards */
485 nr_matches
= hweight64(match_mask
);
486 if (nr_matches
> best_nr_matches
) {
487 best_nr_matches
= nr_matches
;
492 if (best_nr_matches
> 0) {
493 tuner_dbg("Selecting best matching firmware (%d bits) for "
494 "type=", best_nr_matches
);
495 dump_firm_type(type
);
496 printk("(%x), id %016llx:\n", type
, (unsigned long long)*id
);
501 /*FIXME: Would make sense to seek for type "hint" match ? */
507 *id
= priv
->firm
[i
].id
;
510 tuner_dbg("%s firmware for type=", (i
< 0) ? "Can't find" : "Found");
512 dump_firm_type(type
);
513 printk("(%x), id %016llx.\n", type
, (unsigned long long)*id
);
518 static inline int do_tuner_callback(struct dvb_frontend
*fe
, int cmd
, int arg
)
520 struct xc2028_data
*priv
= fe
->tuner_priv
;
522 /* analog side (tuner-core) uses i2c_adap->algo_data.
523 * digital side is not guaranteed to have algo_data defined.
525 * digital side will always have fe->dvb defined.
526 * analog side (tuner-core) doesn't (yet) define fe->dvb.
529 return (!fe
->callback
) ? -EINVAL
:
530 fe
->callback(((fe
->dvb
) && (fe
->dvb
->priv
)) ?
531 fe
->dvb
->priv
: priv
->i2c_props
.adap
->algo_data
,
532 DVB_FRONTEND_COMPONENT_TUNER
, cmd
, arg
);
535 static int load_firmware(struct dvb_frontend
*fe
, unsigned int type
,
538 struct xc2028_data
*priv
= fe
->tuner_priv
;
540 unsigned char *p
, *endp
, buf
[priv
->ctrl
.max_len
];
542 tuner_dbg("%s called\n", __func__
);
544 pos
= seek_firmware(fe
, type
, id
);
548 tuner_info("Loading firmware for type=");
549 dump_firm_type(priv
->firm
[pos
].type
);
550 printk("(%x), id %016llx.\n", priv
->firm
[pos
].type
,
551 (unsigned long long)*id
);
553 p
= priv
->firm
[pos
].ptr
;
554 endp
= p
+ priv
->firm
[pos
].size
;
559 /* Checks if there's enough bytes to read */
560 if (p
+ sizeof(size
) > endp
) {
561 tuner_err("Firmware chunk size is wrong\n");
565 size
= le16_to_cpu(*(__u16
*) p
);
572 /* Special callback command received */
573 rc
= do_tuner_callback(fe
, XC2028_TUNER_RESET
, 0);
575 tuner_err("Error at RESET code %d\n",
581 if (size
>= 0xff00) {
584 rc
= do_tuner_callback(fe
, XC2028_RESET_CLK
, 0);
586 tuner_err("Error at RESET code %d\n",
592 tuner_info("Invalid RESET code %d\n",
600 /* Checks for a sleep command */
602 msleep(size
& 0x7fff);
606 if ((size
+ p
> endp
)) {
607 tuner_err("missing bytes: need %d, have %d\n",
608 size
, (int)(endp
- p
));
616 /* Sends message chunks */
618 int len
= (size
< priv
->ctrl
.max_len
- 1) ?
619 size
: priv
->ctrl
.max_len
- 1;
621 memcpy(buf
+ 1, p
, len
);
623 rc
= i2c_send(priv
, buf
, len
+ 1);
625 tuner_err("%d returned from send\n", rc
);
633 /* silently fail if the frontend doesn't support I2C flush */
634 rc
= do_tuner_callback(fe
, XC2028_I2C_FLUSH
, 0);
635 if ((rc
< 0) && (rc
!= -EINVAL
)) {
636 tuner_err("error executing flush: %d\n", rc
);
643 static int load_scode(struct dvb_frontend
*fe
, unsigned int type
,
644 v4l2_std_id
*id
, __u16 int_freq
, int scode
)
646 struct xc2028_data
*priv
= fe
->tuner_priv
;
650 tuner_dbg("%s called\n", __func__
);
653 pos
= seek_firmware(fe
, type
, id
);
657 for (pos
= 0; pos
< priv
->firm_size
; pos
++) {
658 if ((priv
->firm
[pos
].int_freq
== int_freq
) &&
659 (priv
->firm
[pos
].type
& HAS_IF
))
662 if (pos
== priv
->firm_size
)
666 p
= priv
->firm
[pos
].ptr
;
668 if (priv
->firm
[pos
].type
& HAS_IF
) {
669 if (priv
->firm
[pos
].size
!= 12 * 16 || scode
>= 16)
673 /* 16 SCODE entries per file; each SCODE entry is 12 bytes and
674 * has a 2-byte size header in the firmware format. */
675 if (priv
->firm
[pos
].size
!= 14 * 16 || scode
>= 16 ||
676 le16_to_cpu(*(__u16
*)(p
+ 14 * scode
)) != 12)
681 tuner_info("Loading SCODE for type=");
682 dump_firm_type_and_int_freq(priv
->firm
[pos
].type
,
683 priv
->firm
[pos
].int_freq
);
684 printk("(%x), id %016llx.\n", priv
->firm
[pos
].type
,
685 (unsigned long long)*id
);
687 if (priv
->firm_version
< 0x0202)
688 rc
= send_seq(priv
, {0x20, 0x00, 0x00, 0x00});
690 rc
= send_seq(priv
, {0xa0, 0x00, 0x00, 0x00});
694 rc
= i2c_send(priv
, p
, 12);
698 rc
= send_seq(priv
, {0x00, 0x8c});
705 static int check_firmware(struct dvb_frontend
*fe
, unsigned int type
,
706 v4l2_std_id std
, __u16 int_freq
)
708 struct xc2028_data
*priv
= fe
->tuner_priv
;
709 struct firmware_properties new_fw
;
710 int rc
= 0, retry_count
= 0;
711 u16 version
, hwmodel
;
714 tuner_dbg("%s called\n", __func__
);
717 if (!priv
->ctrl
.fname
) {
718 tuner_info("xc2028/3028 firmware name not set!\n");
722 rc
= load_all_firmwares(fe
);
727 if (priv
->ctrl
.mts
&& !(type
& FM
))
733 new_fw
.std_req
= std
;
734 new_fw
.scode_table
= SCODE
| priv
->ctrl
.scode_table
;
736 new_fw
.int_freq
= int_freq
;
738 tuner_dbg("checking firmware, user requested type=");
740 dump_firm_type(new_fw
.type
);
741 printk("(%x), id %016llx, ", new_fw
.type
,
742 (unsigned long long)new_fw
.std_req
);
744 printk("scode_tbl ");
745 dump_firm_type(priv
->ctrl
.scode_table
);
746 printk("(%x), ", priv
->ctrl
.scode_table
);
748 printk("int_freq %d, ", new_fw
.int_freq
);
749 printk("scode_nr %d\n", new_fw
.scode_nr
);
752 /* No need to reload base firmware if it matches */
753 if (((BASE
| new_fw
.type
) & BASE_TYPES
) ==
754 (priv
->cur_fw
.type
& BASE_TYPES
)) {
755 tuner_dbg("BASE firmware not changed.\n");
759 /* Updating BASE - forget about all currently loaded firmware */
760 memset(&priv
->cur_fw
, 0, sizeof(priv
->cur_fw
));
762 /* Reset is needed before loading firmware */
763 rc
= do_tuner_callback(fe
, XC2028_TUNER_RESET
, 0);
767 /* BASE firmwares are all std0 */
769 rc
= load_firmware(fe
, BASE
| new_fw
.type
, &std0
);
771 tuner_err("Error %d while loading base firmware\n",
776 /* Load INIT1, if needed */
777 tuner_dbg("Load init1 firmware, if exists\n");
779 rc
= load_firmware(fe
, BASE
| INIT1
| new_fw
.type
, &std0
);
781 rc
= load_firmware(fe
, (BASE
| INIT1
| new_fw
.type
) & ~F8MHZ
,
783 if (rc
< 0 && rc
!= -ENOENT
) {
784 tuner_err("Error %d while loading init1 firmware\n",
791 * No need to reload standard specific firmware if base firmware
792 * was not reloaded and requested video standards have not changed.
794 if (priv
->cur_fw
.type
== (BASE
| new_fw
.type
) &&
795 priv
->cur_fw
.std_req
== std
) {
796 tuner_dbg("Std-specific firmware already loaded.\n");
797 goto skip_std_specific
;
800 /* Reloading std-specific firmware forces a SCODE update */
801 priv
->cur_fw
.scode_table
= 0;
803 rc
= load_firmware(fe
, new_fw
.type
, &new_fw
.id
);
805 rc
= load_firmware(fe
, new_fw
.type
& ~F8MHZ
, &new_fw
.id
);
811 if (priv
->cur_fw
.scode_table
== new_fw
.scode_table
&&
812 priv
->cur_fw
.scode_nr
== new_fw
.scode_nr
) {
813 tuner_dbg("SCODE firmware already loaded.\n");
817 if (new_fw
.type
& FM
)
820 /* Load SCODE firmware, if exists */
821 tuner_dbg("Trying to load scode %d\n", new_fw
.scode_nr
);
823 rc
= load_scode(fe
, new_fw
.type
| new_fw
.scode_table
, &new_fw
.id
,
824 new_fw
.int_freq
, new_fw
.scode_nr
);
827 if (xc2028_get_reg(priv
, 0x0004, &version
) < 0 ||
828 xc2028_get_reg(priv
, 0x0008, &hwmodel
) < 0) {
829 tuner_err("Unable to read tuner registers.\n");
833 tuner_dbg("Device is Xceive %d version %d.%d, "
834 "firmware version %d.%d\n",
835 hwmodel
, (version
& 0xf000) >> 12, (version
& 0xf00) >> 8,
836 (version
& 0xf0) >> 4, version
& 0xf);
839 if (priv
->ctrl
.read_not_reliable
)
840 goto read_not_reliable
;
842 /* Check firmware version against what we downloaded. */
843 if (priv
->firm_version
!= ((version
& 0xf0) << 4 | (version
& 0x0f))) {
844 if (!priv
->ctrl
.read_not_reliable
) {
845 tuner_err("Incorrect readback of firmware version.\n");
848 tuner_err("Returned an incorrect version. However, "
849 "read is not reliable enough. Ignoring it.\n");
854 /* Check that the tuner hardware model remains consistent over time. */
855 if (priv
->hwmodel
== 0 && (hwmodel
== 2028 || hwmodel
== 3028)) {
856 priv
->hwmodel
= hwmodel
;
857 priv
->hwvers
= version
& 0xff00;
858 } else if (priv
->hwmodel
== 0 || priv
->hwmodel
!= hwmodel
||
859 priv
->hwvers
!= (version
& 0xff00)) {
860 tuner_err("Read invalid device hardware information - tuner "
866 memcpy(&priv
->cur_fw
, &new_fw
, sizeof(priv
->cur_fw
));
869 * By setting BASE in cur_fw.type only after successfully loading all
871 * 1. Identify that BASE firmware with type=0 has been loaded;
872 * 2. Tell whether BASE firmware was just changed the next time through.
874 priv
->cur_fw
.type
|= BASE
;
879 memset(&priv
->cur_fw
, 0, sizeof(priv
->cur_fw
));
880 if (retry_count
< 8) {
883 tuner_dbg("Retrying firmware load\n");
892 static int xc2028_signal(struct dvb_frontend
*fe
, u16
*strength
)
894 struct xc2028_data
*priv
= fe
->tuner_priv
;
895 u16 frq_lock
, signal
= 0;
898 tuner_dbg("%s called\n", __func__
);
900 mutex_lock(&priv
->lock
);
902 /* Sync Lock Indicator */
903 rc
= xc2028_get_reg(priv
, XREG_LOCK
, &frq_lock
);
907 /* Frequency is locked */
911 /* Get SNR of the video signal */
912 rc
= xc2028_get_reg(priv
, XREG_SNR
, &signal
);
916 /* Use both frq_lock and signal to generate the result */
917 signal
= signal
|| ((signal
& 0x07) << 12);
920 mutex_unlock(&priv
->lock
);
924 tuner_dbg("signal strength is %d\n", signal
);
931 static int generic_set_freq(struct dvb_frontend
*fe
, u32 freq
/* in HZ */,
932 enum v4l2_tuner_type new_type
,
937 struct xc2028_data
*priv
= fe
->tuner_priv
;
939 unsigned char buf
[4];
942 tuner_dbg("%s called\n", __func__
);
944 mutex_lock(&priv
->lock
);
946 tuner_dbg("should set frequency %d kHz\n", freq
/ 1000);
948 if (check_firmware(fe
, type
, std
, int_freq
) < 0)
951 /* On some cases xc2028 can disable video output, if
952 * very weak signals are received. By sending a soft
953 * reset, this is re-enabled. So, it is better to always
954 * send a soft reset before changing channels, to be sure
955 * that xc2028 will be in a safe state.
956 * Maybe this might also be needed for DTV.
959 case V4L2_TUNER_ANALOG_TV
:
960 rc
= send_seq(priv
, {0x00, 0x00});
962 /* Analog mode requires offset = 0 */
964 case V4L2_TUNER_RADIO
:
965 /* Radio mode requires offset = 0 */
967 case V4L2_TUNER_DIGITAL_TV
:
969 * Digital modes require an offset to adjust to the
970 * proper frequency. The offset depends on what
971 * firmware version is used.
975 * Adjust to the center frequency. This is calculated by the
976 * formula: offset = 1.25MHz - BW/2
977 * For DTV 7/8, the firmware uses BW = 8000, so it needs a
978 * further adjustment to get the frequency center on VHF
982 * The firmware DTV78 used to work fine in UHF band (8 MHz
983 * bandwidth) but not at all in VHF band (7 MHz bandwidth).
984 * The real problem was connected to the formula used to
985 * calculate the center frequency offset in VHF band.
986 * In fact, removing the 500KHz adjustment fixed the problem.
987 * This is coherent to what was implemented for the DTV7
989 * In the end, now the center frequency is the same for all 3
990 * firmwares (DTV7, DTV8, DTV78) and doesn't depend on channel
994 if (priv
->cur_fw
.type
& DTV6
)
996 else /* DTV7 or DTV8 or DTV78 */
1000 * xc3028 additional "magic"
1001 * Depending on the firmware version, it needs some adjustments
1002 * to properly centralize the frequency. This seems to be
1003 * needed to compensate the SCODE table adjustments made by
1008 * The proper adjustment would be to do it at s-code table.
1009 * However, this didn't work, as reported by
1010 * Robert Lowery <rglowery@exemail.com.au>
1015 * Still need tests for XC3028L (firmware 3.2 or upper)
1016 * So, for now, let's just comment the per-firmware
1017 * version of this change. Reports with xc3028l working
1018 * with and without the lines bellow are welcome
1021 if (priv
->firm_version
< 0x0302) {
1022 if (priv
->cur_fw
.type
& DTV7
)
1025 if (priv
->cur_fw
.type
& DTV7
)
1027 else if (type
!= ATSC
) /* DVB @6MHz, DTV 8 and DTV 7/8 */
1033 div
= (freq
- offset
+ DIV
/ 2) / DIV
;
1035 /* CMD= Set frequency */
1036 if (priv
->firm_version
< 0x0202)
1037 rc
= send_seq(priv
, {0x00, XREG_RF_FREQ
, 0x00, 0x00});
1039 rc
= send_seq(priv
, {0x80, XREG_RF_FREQ
, 0x00, 0x00});
1043 /* Return code shouldn't be checked.
1044 The reset CLK is needed only with tm6000.
1045 Driver should work fine even if this fails.
1047 if (priv
->ctrl
.msleep
)
1048 msleep(priv
->ctrl
.msleep
);
1049 do_tuner_callback(fe
, XC2028_RESET_CLK
, 1);
1053 buf
[0] = 0xff & (div
>> 24);
1054 buf
[1] = 0xff & (div
>> 16);
1055 buf
[2] = 0xff & (div
>> 8);
1056 buf
[3] = 0xff & (div
);
1058 rc
= i2c_send(priv
, buf
, sizeof(buf
));
1063 priv
->frequency
= freq
;
1065 tuner_dbg("divisor= %02x %02x %02x %02x (freq=%d.%03d)\n",
1066 buf
[0], buf
[1], buf
[2], buf
[3],
1067 freq
/ 1000000, (freq
% 1000000) / 1000);
1072 mutex_unlock(&priv
->lock
);
1077 static int xc2028_set_analog_freq(struct dvb_frontend
*fe
,
1078 struct analog_parameters
*p
)
1080 struct xc2028_data
*priv
= fe
->tuner_priv
;
1081 unsigned int type
=0;
1083 tuner_dbg("%s called\n", __func__
);
1085 if (p
->mode
== V4L2_TUNER_RADIO
) {
1087 if (priv
->ctrl
.input1
)
1089 return generic_set_freq(fe
, (625l * p
->frequency
) / 10,
1090 V4L2_TUNER_RADIO
, type
, 0, 0);
1093 /* if std is not defined, choose one */
1095 p
->std
= V4L2_STD_MN
;
1097 /* PAL/M, PAL/N, PAL/Nc and NTSC variants should use 6MHz firmware */
1098 if (!(p
->std
& V4L2_STD_MN
))
1101 /* Add audio hack to std mask */
1102 p
->std
|= parse_audio_std_option();
1104 return generic_set_freq(fe
, 62500l * p
->frequency
,
1105 V4L2_TUNER_ANALOG_TV
, type
, p
->std
, 0);
1108 static int xc2028_set_params(struct dvb_frontend
*fe
)
1110 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
1111 u32 delsys
= c
->delivery_system
;
1112 u32 bw
= c
->bandwidth_hz
;
1113 struct xc2028_data
*priv
= fe
->tuner_priv
;
1114 unsigned int type
=0;
1117 tuner_dbg("%s called\n", __func__
);
1123 * The only countries with 6MHz seem to be Taiwan/Uruguay.
1124 * Both seem to require QAM firmware for OFDM decoding
1125 * Tested in Taiwan by Terry Wu <terrywu2009@gmail.com>
1130 switch (priv
->ctrl
.type
) {
1139 /* Zarlink seems to need D2633 */
1140 if (priv
->ctrl
.demod
== XC3028_FE_ZARLINK456
)
1147 /* The only ATSC firmware (at least on v2.7) is D2633 */
1148 type
|= ATSC
| D2633
;
1150 /* DVB-S and pure QAM (FE_QAM) are not supported */
1155 if (bw
<= 6000000) {
1157 priv
->ctrl
.vhfbw7
= 0;
1158 priv
->ctrl
.uhfbw8
= 0;
1159 } else if (bw
<= 7000000) {
1160 if (c
->frequency
< 470000000)
1161 priv
->ctrl
.vhfbw7
= 1;
1163 priv
->ctrl
.uhfbw8
= 0;
1164 type
|= (priv
->ctrl
.vhfbw7
&& priv
->ctrl
.uhfbw8
) ? DTV78
: DTV7
;
1167 if (c
->frequency
< 470000000)
1168 priv
->ctrl
.vhfbw7
= 0;
1170 priv
->ctrl
.uhfbw8
= 1;
1171 type
|= (priv
->ctrl
.vhfbw7
&& priv
->ctrl
.uhfbw8
) ? DTV78
: DTV8
;
1175 /* All S-code tables need a 200kHz shift */
1176 if (priv
->ctrl
.demod
) {
1177 demod
= priv
->ctrl
.demod
;
1180 * Newer firmwares require a 200 kHz offset only for ATSC
1182 if (type
== ATSC
|| priv
->firm_version
< 0x0302)
1185 * The DTV7 S-code table needs a 700 kHz shift.
1187 * DTV7 is only used in Australia. Germany or Italy may also
1188 * use this firmware after initialization, but a tune to a UHF
1189 * channel should then cause DTV78 to be used.
1191 * Unfortunately, on real-field tests, the s-code offset
1192 * didn't work as expected, as reported by
1193 * Robert Lowery <rglowery@exemail.com.au>
1197 return generic_set_freq(fe
, c
->frequency
,
1198 V4L2_TUNER_DIGITAL_TV
, type
, 0, demod
);
1201 static int xc2028_sleep(struct dvb_frontend
*fe
)
1203 struct xc2028_data
*priv
= fe
->tuner_priv
;
1206 /* Avoid firmware reload on slow devices or if PM disabled */
1207 if (no_poweroff
|| priv
->ctrl
.disable_power_mgmt
)
1210 tuner_dbg("Putting xc2028/3028 into poweroff mode.\n");
1212 tuner_dbg("Printing sleep stack trace:\n");
1216 mutex_lock(&priv
->lock
);
1218 if (priv
->firm_version
< 0x0202)
1219 rc
= send_seq(priv
, {0x00, XREG_POWER_DOWN
, 0x00, 0x00});
1221 rc
= send_seq(priv
, {0x80, XREG_POWER_DOWN
, 0x00, 0x00});
1223 priv
->cur_fw
.type
= 0; /* need firmware reload */
1225 mutex_unlock(&priv
->lock
);
1230 static int xc2028_dvb_release(struct dvb_frontend
*fe
)
1232 struct xc2028_data
*priv
= fe
->tuner_priv
;
1234 tuner_dbg("%s called\n", __func__
);
1236 mutex_lock(&xc2028_list_mutex
);
1238 /* only perform final cleanup if this is the last instance */
1239 if (hybrid_tuner_report_instance_count(priv
) == 1) {
1240 kfree(priv
->ctrl
.fname
);
1241 free_firmware(priv
);
1245 hybrid_tuner_release_state(priv
);
1247 mutex_unlock(&xc2028_list_mutex
);
1249 fe
->tuner_priv
= NULL
;
1254 static int xc2028_get_frequency(struct dvb_frontend
*fe
, u32
*frequency
)
1256 struct xc2028_data
*priv
= fe
->tuner_priv
;
1258 tuner_dbg("%s called\n", __func__
);
1260 *frequency
= priv
->frequency
;
1265 static int xc2028_set_config(struct dvb_frontend
*fe
, void *priv_cfg
)
1267 struct xc2028_data
*priv
= fe
->tuner_priv
;
1268 struct xc2028_ctrl
*p
= priv_cfg
;
1271 tuner_dbg("%s called\n", __func__
);
1273 mutex_lock(&priv
->lock
);
1275 memcpy(&priv
->ctrl
, p
, sizeof(priv
->ctrl
));
1276 if (priv
->ctrl
.max_len
< 9)
1277 priv
->ctrl
.max_len
= 13;
1280 if (priv
->ctrl
.fname
&& strcmp(p
->fname
, priv
->ctrl
.fname
)) {
1281 kfree(priv
->ctrl
.fname
);
1282 free_firmware(priv
);
1285 priv
->ctrl
.fname
= kstrdup(p
->fname
, GFP_KERNEL
);
1286 if (priv
->ctrl
.fname
== NULL
)
1290 mutex_unlock(&priv
->lock
);
1295 static const struct dvb_tuner_ops xc2028_dvb_tuner_ops
= {
1297 .name
= "Xceive XC3028",
1298 .frequency_min
= 42000000,
1299 .frequency_max
= 864000000,
1300 .frequency_step
= 50000,
1303 .set_config
= xc2028_set_config
,
1304 .set_analog_params
= xc2028_set_analog_freq
,
1305 .release
= xc2028_dvb_release
,
1306 .get_frequency
= xc2028_get_frequency
,
1307 .get_rf_strength
= xc2028_signal
,
1308 .set_params
= xc2028_set_params
,
1309 .sleep
= xc2028_sleep
,
1312 struct dvb_frontend
*xc2028_attach(struct dvb_frontend
*fe
,
1313 struct xc2028_config
*cfg
)
1315 struct xc2028_data
*priv
;
1319 printk(KERN_DEBUG
"xc2028: Xcv2028/3028 init called!\n");
1325 printk(KERN_ERR
"xc2028: No frontend!\n");
1329 mutex_lock(&xc2028_list_mutex
);
1331 instance
= hybrid_tuner_request_state(struct xc2028_data
, priv
,
1332 hybrid_tuner_instance_list
,
1333 cfg
->i2c_adap
, cfg
->i2c_addr
,
1337 /* memory allocation failure */
1341 /* new tuner instance */
1342 priv
->ctrl
.max_len
= 13;
1344 mutex_init(&priv
->lock
);
1346 fe
->tuner_priv
= priv
;
1349 /* existing tuner instance */
1350 fe
->tuner_priv
= priv
;
1354 memcpy(&fe
->ops
.tuner_ops
, &xc2028_dvb_tuner_ops
,
1355 sizeof(xc2028_dvb_tuner_ops
));
1357 tuner_info("type set to %s\n", "XCeive xc2028/xc3028 tuner");
1360 xc2028_set_config(fe
, cfg
->ctrl
);
1362 mutex_unlock(&xc2028_list_mutex
);
1366 mutex_unlock(&xc2028_list_mutex
);
1368 xc2028_dvb_release(fe
);
1372 EXPORT_SYMBOL(xc2028_attach
);
1374 MODULE_DESCRIPTION("Xceive xc2028/xc3028 tuner driver");
1375 MODULE_AUTHOR("Michel Ludwig <michel.ludwig@gmail.com>");
1376 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
1377 MODULE_LICENSE("GPL");