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 <asm/unaligned.h>
19 #include "tuner-i2c.h"
20 #include "tuner-xc2028.h"
21 #include "tuner-xc2028-types.h"
23 #include <linux/dvb/frontend.h>
24 #include "dvb_frontend.h"
28 module_param(debug
, int, 0644);
29 MODULE_PARM_DESC(debug
, "enable verbose debug messages");
31 static int no_poweroff
;
32 module_param(no_poweroff
, int, 0644);
33 MODULE_PARM_DESC(no_poweroff
, "0 (default) powers device off when not used.\n"
34 "1 keep device energized and with tuner ready all the times.\n"
35 " Faster, but consumes more power and keeps the device hotter\n");
37 static char audio_std
[8];
38 module_param_string(audio_std
, audio_std
, sizeof(audio_std
), 0);
39 MODULE_PARM_DESC(audio_std
,
40 "Audio standard. XC3028 audio decoder explicitly "
41 "needs to know what audio\n"
42 "standard is needed for some video standards with audio A2 or NICAM.\n"
43 "The valid values are:\n"
51 static char firmware_name
[30];
52 module_param_string(firmware_name
, firmware_name
, sizeof(firmware_name
), 0);
53 MODULE_PARM_DESC(firmware_name
, "Firmware file name. Allows overriding the "
54 "default firmware name\n");
56 static LIST_HEAD(hybrid_tuner_instance_list
);
57 static DEFINE_MUTEX(xc2028_list_mutex
);
59 /* struct for storing firmware table */
60 struct firmware_description
{
68 struct firmware_properties
{
73 unsigned int scode_table
;
78 struct list_head hybrid_tuner_instance_list
;
79 struct tuner_i2c_props i2c_props
;
82 struct firmware_description
*firm
;
89 struct xc2028_ctrl ctrl
;
91 struct firmware_properties cur_fw
;
96 #define i2c_send(priv, buf, size) ({ \
98 _rc = tuner_i2c_xfer_send(&priv->i2c_props, buf, size); \
100 tuner_info("i2c output error: rc = %d (should be %d)\n",\
105 #define i2c_rcv(priv, buf, size) ({ \
107 _rc = tuner_i2c_xfer_recv(&priv->i2c_props, buf, size); \
109 tuner_err("i2c input error: rc = %d (should be %d)\n", \
114 #define i2c_send_recv(priv, obuf, osize, ibuf, isize) ({ \
116 _rc = tuner_i2c_xfer_send_recv(&priv->i2c_props, obuf, osize, \
119 tuner_err("i2c input error: rc = %d (should be %d)\n", \
124 #define send_seq(priv, data...) ({ \
125 static u8 _val[] = data; \
127 if (sizeof(_val) != \
128 (_rc = tuner_i2c_xfer_send(&priv->i2c_props, \
129 _val, sizeof(_val)))) { \
130 tuner_err("Error on line %d: %d\n", __LINE__, _rc); \
136 static int xc2028_get_reg(struct xc2028_data
*priv
, u16 reg
, u16
*val
)
138 unsigned char buf
[2];
139 unsigned char ibuf
[2];
141 tuner_dbg("%s %04x called\n", __func__
, reg
);
144 buf
[1] = (unsigned char) reg
;
146 if (i2c_send_recv(priv
, buf
, 2, ibuf
, 2) != 2)
149 *val
= (ibuf
[1]) | (ibuf
[0] << 8);
153 #define dump_firm_type(t) dump_firm_type_and_int_freq(t, 0)
154 static void dump_firm_type_and_int_freq(unsigned int type
, u16 int_freq
)
200 if (type
& TOYOTA388
)
201 printk("TOYOTA388 ");
202 if (type
& TOYOTA794
)
203 printk("TOYOTA794 ");
206 if (type
& ZARLINK456
)
207 printk("ZARLINK456 ");
217 printk("HAS_IF_%d ", int_freq
);
220 static v4l2_std_id
parse_audio_std_option(void)
222 if (strcasecmp(audio_std
, "A2") == 0)
224 if (strcasecmp(audio_std
, "A2/A") == 0)
225 return V4L2_STD_A2_A
;
226 if (strcasecmp(audio_std
, "A2/B") == 0)
227 return V4L2_STD_A2_B
;
228 if (strcasecmp(audio_std
, "NICAM") == 0)
229 return V4L2_STD_NICAM
;
230 if (strcasecmp(audio_std
, "NICAM/A") == 0)
231 return V4L2_STD_NICAM_A
;
232 if (strcasecmp(audio_std
, "NICAM/B") == 0)
233 return V4L2_STD_NICAM_B
;
238 static void free_firmware(struct xc2028_data
*priv
)
241 tuner_dbg("%s called\n", __func__
);
246 for (i
= 0; i
< priv
->firm_size
; i
++)
247 kfree(priv
->firm
[i
].ptr
);
254 memset(&priv
->cur_fw
, 0, sizeof(priv
->cur_fw
));
257 static int load_all_firmwares(struct dvb_frontend
*fe
)
259 struct xc2028_data
*priv
= fe
->tuner_priv
;
260 const struct firmware
*fw
= NULL
;
261 const unsigned char *p
, *endp
;
267 tuner_dbg("%s called\n", __func__
);
269 if (!firmware_name
[0])
270 fname
= priv
->ctrl
.fname
;
272 fname
= firmware_name
;
274 tuner_dbg("Reading firmware %s\n", fname
);
275 rc
= request_firmware(&fw
, fname
, priv
->i2c_props
.adap
->dev
.parent
);
278 tuner_err("Error: firmware %s not found.\n",
281 tuner_err("Error %d while requesting firmware %s \n",
289 if (fw
->size
< sizeof(name
) - 1 + 2 + 2) {
290 tuner_err("Error: firmware file %s has invalid size!\n",
295 memcpy(name
, p
, sizeof(name
) - 1);
296 name
[sizeof(name
) - 1] = 0;
297 p
+= sizeof(name
) - 1;
299 priv
->firm_version
= get_unaligned_le16(p
);
302 n_array
= get_unaligned_le16(p
);
305 tuner_info("Loading %d firmware images from %s, type: %s, ver %d.%d\n",
306 n_array
, fname
, name
,
307 priv
->firm_version
>> 8, priv
->firm_version
& 0xff);
309 priv
->firm
= kzalloc(sizeof(*priv
->firm
) * n_array
, GFP_KERNEL
);
310 if (priv
->firm
== NULL
) {
311 tuner_err("Not enough memory to load firmware file.\n");
315 priv
->firm_size
= n_array
;
325 tuner_err("More firmware images in file than "
330 /* Checks if there's enough bytes to read */
331 if (endp
- p
< sizeof(type
) + sizeof(id
) + sizeof(size
))
334 type
= get_unaligned_le32(p
);
337 id
= get_unaligned_le64(p
);
341 int_freq
= get_unaligned_le16(p
);
342 p
+= sizeof(int_freq
);
343 if (endp
- p
< sizeof(size
))
347 size
= get_unaligned_le32(p
);
350 if (!size
|| size
> endp
- p
) {
351 tuner_err("Firmware type ");
352 dump_firm_type(type
);
353 printk("(%x), id %llx is corrupted "
354 "(size=%d, expected %d)\n",
355 type
, (unsigned long long)id
,
356 (unsigned)(endp
- p
), size
);
360 priv
->firm
[n
].ptr
= kzalloc(size
, GFP_KERNEL
);
361 if (priv
->firm
[n
].ptr
== NULL
) {
362 tuner_err("Not enough memory to load firmware file.\n");
366 tuner_dbg("Reading firmware type ");
368 dump_firm_type_and_int_freq(type
, int_freq
);
369 printk("(%x), id %llx, size=%d.\n",
370 type
, (unsigned long long)id
, size
);
373 memcpy(priv
->firm
[n
].ptr
, p
, size
);
374 priv
->firm
[n
].type
= type
;
375 priv
->firm
[n
].id
= id
;
376 priv
->firm
[n
].size
= size
;
377 priv
->firm
[n
].int_freq
= int_freq
;
382 if (n
+ 1 != priv
->firm_size
) {
383 tuner_err("Firmware file is incomplete!\n");
390 tuner_err("Firmware header is incomplete!\n");
393 tuner_err("Error: firmware file is corrupted!\n");
396 tuner_info("Releasing partially loaded firmware file.\n");
400 release_firmware(fw
);
402 tuner_dbg("Firmware files loaded.\n");
407 static int seek_firmware(struct dvb_frontend
*fe
, unsigned int type
,
410 struct xc2028_data
*priv
= fe
->tuner_priv
;
411 int i
, best_i
= -1, best_nr_matches
= 0;
412 unsigned int type_mask
= 0;
414 tuner_dbg("%s called, want type=", __func__
);
416 dump_firm_type(type
);
417 printk("(%x), id %016llx.\n", type
, (unsigned long long)*id
);
421 tuner_err("Error! firmware not loaded\n");
425 if (((type
& ~SCODE
) == 0) && (*id
== 0))
429 type_mask
= BASE_TYPES
;
430 else if (type
& SCODE
) {
432 type_mask
= SCODE_TYPES
& ~HAS_IF
;
433 } else if (type
& DTV_TYPES
)
434 type_mask
= DTV_TYPES
;
435 else if (type
& STD_SPECIFIC_TYPES
)
436 type_mask
= STD_SPECIFIC_TYPES
;
443 /* Seek for exact match */
444 for (i
= 0; i
< priv
->firm_size
; i
++) {
445 if ((type
== (priv
->firm
[i
].type
& type_mask
)) &&
446 (*id
== priv
->firm
[i
].id
))
450 /* Seek for generic video standard match */
451 for (i
= 0; i
< priv
->firm_size
; i
++) {
452 v4l2_std_id match_mask
;
455 if (type
!= (priv
->firm
[i
].type
& type_mask
))
458 match_mask
= *id
& priv
->firm
[i
].id
;
462 if ((*id
& match_mask
) == *id
)
463 goto found
; /* Supports all the requested standards */
465 nr_matches
= hweight64(match_mask
);
466 if (nr_matches
> best_nr_matches
) {
467 best_nr_matches
= nr_matches
;
472 if (best_nr_matches
> 0) {
473 tuner_dbg("Selecting best matching firmware (%d bits) for "
474 "type=", best_nr_matches
);
475 dump_firm_type(type
);
476 printk("(%x), id %016llx:\n", type
, (unsigned long long)*id
);
481 /*FIXME: Would make sense to seek for type "hint" match ? */
487 *id
= priv
->firm
[i
].id
;
490 tuner_dbg("%s firmware for type=", (i
< 0) ? "Can't find" : "Found");
492 dump_firm_type(type
);
493 printk("(%x), id %016llx.\n", type
, (unsigned long long)*id
);
498 static inline int do_tuner_callback(struct dvb_frontend
*fe
, int cmd
, int arg
)
500 struct xc2028_data
*priv
= fe
->tuner_priv
;
502 /* analog side (tuner-core) uses i2c_adap->algo_data.
503 * digital side is not guaranteed to have algo_data defined.
505 * digital side will always have fe->dvb defined.
506 * analog side (tuner-core) doesn't (yet) define fe->dvb.
509 return (!fe
->callback
) ? -EINVAL
:
510 fe
->callback(((fe
->dvb
) && (fe
->dvb
->priv
)) ?
511 fe
->dvb
->priv
: priv
->i2c_props
.adap
->algo_data
,
512 DVB_FRONTEND_COMPONENT_TUNER
, cmd
, arg
);
515 static int load_firmware(struct dvb_frontend
*fe
, unsigned int type
,
518 struct xc2028_data
*priv
= fe
->tuner_priv
;
520 unsigned char *p
, *endp
, buf
[priv
->ctrl
.max_len
];
522 tuner_dbg("%s called\n", __func__
);
524 pos
= seek_firmware(fe
, type
, id
);
528 tuner_info("Loading firmware for type=");
529 dump_firm_type(priv
->firm
[pos
].type
);
530 printk("(%x), id %016llx.\n", priv
->firm
[pos
].type
,
531 (unsigned long long)*id
);
533 p
= priv
->firm
[pos
].ptr
;
534 endp
= p
+ priv
->firm
[pos
].size
;
539 /* Checks if there's enough bytes to read */
540 if (p
+ sizeof(size
) > endp
) {
541 tuner_err("Firmware chunk size is wrong\n");
545 size
= le16_to_cpu(*(__u16
*) p
);
552 /* Special callback command received */
553 rc
= do_tuner_callback(fe
, XC2028_TUNER_RESET
, 0);
555 tuner_err("Error at RESET code %d\n",
561 if (size
>= 0xff00) {
564 rc
= do_tuner_callback(fe
, XC2028_RESET_CLK
, 0);
566 tuner_err("Error at RESET code %d\n",
572 tuner_info("Invalid RESET code %d\n",
580 /* Checks for a sleep command */
582 msleep(size
& 0x7fff);
586 if ((size
+ p
> endp
)) {
587 tuner_err("missing bytes: need %d, have %d\n",
588 size
, (int)(endp
- p
));
596 /* Sends message chunks */
598 int len
= (size
< priv
->ctrl
.max_len
- 1) ?
599 size
: priv
->ctrl
.max_len
- 1;
601 memcpy(buf
+ 1, p
, len
);
603 rc
= i2c_send(priv
, buf
, len
+ 1);
605 tuner_err("%d returned from send\n", rc
);
616 static int load_scode(struct dvb_frontend
*fe
, unsigned int type
,
617 v4l2_std_id
*id
, __u16 int_freq
, int scode
)
619 struct xc2028_data
*priv
= fe
->tuner_priv
;
623 tuner_dbg("%s called\n", __func__
);
626 pos
= seek_firmware(fe
, type
, id
);
630 for (pos
= 0; pos
< priv
->firm_size
; pos
++) {
631 if ((priv
->firm
[pos
].int_freq
== int_freq
) &&
632 (priv
->firm
[pos
].type
& HAS_IF
))
635 if (pos
== priv
->firm_size
)
639 p
= priv
->firm
[pos
].ptr
;
641 if (priv
->firm
[pos
].type
& HAS_IF
) {
642 if (priv
->firm
[pos
].size
!= 12 * 16 || scode
>= 16)
646 /* 16 SCODE entries per file; each SCODE entry is 12 bytes and
647 * has a 2-byte size header in the firmware format. */
648 if (priv
->firm
[pos
].size
!= 14 * 16 || scode
>= 16 ||
649 le16_to_cpu(*(__u16
*)(p
+ 14 * scode
)) != 12)
654 tuner_info("Loading SCODE for type=");
655 dump_firm_type_and_int_freq(priv
->firm
[pos
].type
,
656 priv
->firm
[pos
].int_freq
);
657 printk("(%x), id %016llx.\n", priv
->firm
[pos
].type
,
658 (unsigned long long)*id
);
660 if (priv
->firm_version
< 0x0202)
661 rc
= send_seq(priv
, {0x20, 0x00, 0x00, 0x00});
663 rc
= send_seq(priv
, {0xa0, 0x00, 0x00, 0x00});
667 rc
= i2c_send(priv
, p
, 12);
671 rc
= send_seq(priv
, {0x00, 0x8c});
678 static int check_firmware(struct dvb_frontend
*fe
, unsigned int type
,
679 v4l2_std_id std
, __u16 int_freq
)
681 struct xc2028_data
*priv
= fe
->tuner_priv
;
682 struct firmware_properties new_fw
;
683 int rc
= 0, is_retry
= 0;
684 u16 version
, hwmodel
;
687 tuner_dbg("%s called\n", __func__
);
690 if (!priv
->ctrl
.fname
) {
691 tuner_info("xc2028/3028 firmware name not set!\n");
695 rc
= load_all_firmwares(fe
);
700 if (priv
->ctrl
.mts
&& !(type
& FM
))
706 new_fw
.std_req
= std
;
707 new_fw
.scode_table
= SCODE
| priv
->ctrl
.scode_table
;
709 new_fw
.int_freq
= int_freq
;
711 tuner_dbg("checking firmware, user requested type=");
713 dump_firm_type(new_fw
.type
);
714 printk("(%x), id %016llx, ", new_fw
.type
,
715 (unsigned long long)new_fw
.std_req
);
717 printk("scode_tbl ");
718 dump_firm_type(priv
->ctrl
.scode_table
);
719 printk("(%x), ", priv
->ctrl
.scode_table
);
721 printk("int_freq %d, ", new_fw
.int_freq
);
722 printk("scode_nr %d\n", new_fw
.scode_nr
);
725 /* No need to reload base firmware if it matches */
726 if (((BASE
| new_fw
.type
) & BASE_TYPES
) ==
727 (priv
->cur_fw
.type
& BASE_TYPES
)) {
728 tuner_dbg("BASE firmware not changed.\n");
732 /* Updating BASE - forget about all currently loaded firmware */
733 memset(&priv
->cur_fw
, 0, sizeof(priv
->cur_fw
));
735 /* Reset is needed before loading firmware */
736 rc
= do_tuner_callback(fe
, XC2028_TUNER_RESET
, 0);
740 /* BASE firmwares are all std0 */
742 rc
= load_firmware(fe
, BASE
| new_fw
.type
, &std0
);
744 tuner_err("Error %d while loading base firmware\n",
749 /* Load INIT1, if needed */
750 tuner_dbg("Load init1 firmware, if exists\n");
752 rc
= load_firmware(fe
, BASE
| INIT1
| new_fw
.type
, &std0
);
754 rc
= load_firmware(fe
, (BASE
| INIT1
| new_fw
.type
) & ~F8MHZ
,
756 if (rc
< 0 && rc
!= -ENOENT
) {
757 tuner_err("Error %d while loading init1 firmware\n",
764 * No need to reload standard specific firmware if base firmware
765 * was not reloaded and requested video standards have not changed.
767 if (priv
->cur_fw
.type
== (BASE
| new_fw
.type
) &&
768 priv
->cur_fw
.std_req
== std
) {
769 tuner_dbg("Std-specific firmware already loaded.\n");
770 goto skip_std_specific
;
773 /* Reloading std-specific firmware forces a SCODE update */
774 priv
->cur_fw
.scode_table
= 0;
776 rc
= load_firmware(fe
, new_fw
.type
, &new_fw
.id
);
778 rc
= load_firmware(fe
, new_fw
.type
& ~F8MHZ
, &new_fw
.id
);
784 if (priv
->cur_fw
.scode_table
== new_fw
.scode_table
&&
785 priv
->cur_fw
.scode_nr
== new_fw
.scode_nr
) {
786 tuner_dbg("SCODE firmware already loaded.\n");
790 if (new_fw
.type
& FM
)
793 /* Load SCODE firmware, if exists */
794 tuner_dbg("Trying to load scode %d\n", new_fw
.scode_nr
);
796 rc
= load_scode(fe
, new_fw
.type
| new_fw
.scode_table
, &new_fw
.id
,
797 new_fw
.int_freq
, new_fw
.scode_nr
);
800 if (xc2028_get_reg(priv
, 0x0004, &version
) < 0 ||
801 xc2028_get_reg(priv
, 0x0008, &hwmodel
) < 0) {
802 tuner_err("Unable to read tuner registers.\n");
806 tuner_dbg("Device is Xceive %d version %d.%d, "
807 "firmware version %d.%d\n",
808 hwmodel
, (version
& 0xf000) >> 12, (version
& 0xf00) >> 8,
809 (version
& 0xf0) >> 4, version
& 0xf);
811 /* Check firmware version against what we downloaded. */
812 if (priv
->firm_version
!= ((version
& 0xf0) << 4 | (version
& 0x0f))) {
813 tuner_err("Incorrect readback of firmware version.\n");
817 /* Check that the tuner hardware model remains consistent over time. */
818 if (priv
->hwmodel
== 0 && (hwmodel
== 2028 || hwmodel
== 3028)) {
819 priv
->hwmodel
= hwmodel
;
820 priv
->hwvers
= version
& 0xff00;
821 } else if (priv
->hwmodel
== 0 || priv
->hwmodel
!= hwmodel
||
822 priv
->hwvers
!= (version
& 0xff00)) {
823 tuner_err("Read invalid device hardware information - tuner "
828 memcpy(&priv
->cur_fw
, &new_fw
, sizeof(priv
->cur_fw
));
831 * By setting BASE in cur_fw.type only after successfully loading all
833 * 1. Identify that BASE firmware with type=0 has been loaded;
834 * 2. Tell whether BASE firmware was just changed the next time through.
836 priv
->cur_fw
.type
|= BASE
;
841 memset(&priv
->cur_fw
, 0, sizeof(priv
->cur_fw
));
845 tuner_dbg("Retrying firmware load\n");
854 static int xc2028_signal(struct dvb_frontend
*fe
, u16
*strength
)
856 struct xc2028_data
*priv
= fe
->tuner_priv
;
857 u16 frq_lock
, signal
= 0;
860 tuner_dbg("%s called\n", __func__
);
862 mutex_lock(&priv
->lock
);
864 /* Sync Lock Indicator */
865 rc
= xc2028_get_reg(priv
, 0x0002, &frq_lock
);
869 /* Frequency is locked */
873 /* Get SNR of the video signal */
874 rc
= xc2028_get_reg(priv
, 0x0040, &signal
);
878 /* Use both frq_lock and signal to generate the result */
879 signal
= signal
|| ((signal
& 0x07) << 12);
882 mutex_unlock(&priv
->lock
);
886 tuner_dbg("signal strength is %d\n", signal
);
893 static int generic_set_freq(struct dvb_frontend
*fe
, u32 freq
/* in HZ */,
894 enum tuner_mode new_mode
,
899 struct xc2028_data
*priv
= fe
->tuner_priv
;
901 unsigned char buf
[4];
904 tuner_dbg("%s called\n", __func__
);
906 mutex_lock(&priv
->lock
);
908 tuner_dbg("should set frequency %d kHz\n", freq
/ 1000);
910 if (check_firmware(fe
, type
, std
, int_freq
) < 0)
913 /* On some cases xc2028 can disable video output, if
914 * very weak signals are received. By sending a soft
915 * reset, this is re-enabled. So, it is better to always
916 * send a soft reset before changing channels, to be sure
917 * that xc2028 will be in a safe state.
918 * Maybe this might also be needed for DTV.
920 if (new_mode
== T_ANALOG_TV
) {
921 rc
= send_seq(priv
, {0x00, 0x00});
923 /* Analog modes require offset = 0 */
926 * Digital modes require an offset to adjust to the
927 * proper frequency. The offset depends on what
928 * firmware version is used.
932 * Adjust to the center frequency. This is calculated by the
933 * formula: offset = 1.25MHz - BW/2
934 * For DTV 7/8, the firmware uses BW = 8000, so it needs a
935 * further adjustment to get the frequency center on VHF
937 if (priv
->cur_fw
.type
& DTV6
)
939 else if (priv
->cur_fw
.type
& DTV7
)
941 else /* DTV8 or DTV78 */
943 if ((priv
->cur_fw
.type
& DTV78
) && freq
< 470000000)
947 * xc3028 additional "magic"
948 * Depending on the firmware version, it needs some adjustments
949 * to properly centralize the frequency. This seems to be
950 * needed to compensate the SCODE table adjustments made by
956 * The proper adjustment would be to do it at s-code table.
957 * However, this didn't work, as reported by
958 * Robert Lowery <rglowery@exemail.com.au>
961 if (priv
->cur_fw
.type
& DTV7
)
966 * Still need tests for XC3028L (firmware 3.2 or upper)
967 * So, for now, let's just comment the per-firmware
968 * version of this change. Reports with xc3028l working
969 * with and without the lines bellow are welcome
972 if (priv
->firm_version
< 0x0302) {
973 if (priv
->cur_fw
.type
& DTV7
)
976 if (priv
->cur_fw
.type
& DTV7
)
978 else if (type
!= ATSC
) /* DVB @6MHz, DTV 8 and DTV 7/8 */
984 div
= (freq
- offset
+ DIV
/ 2) / DIV
;
986 /* CMD= Set frequency */
987 if (priv
->firm_version
< 0x0202)
988 rc
= send_seq(priv
, {0x00, 0x02, 0x00, 0x00});
990 rc
= send_seq(priv
, {0x80, 0x02, 0x00, 0x00});
994 /* Return code shouldn't be checked.
995 The reset CLK is needed only with tm6000.
996 Driver should work fine even if this fails.
998 do_tuner_callback(fe
, XC2028_RESET_CLK
, 1);
1002 buf
[0] = 0xff & (div
>> 24);
1003 buf
[1] = 0xff & (div
>> 16);
1004 buf
[2] = 0xff & (div
>> 8);
1005 buf
[3] = 0xff & (div
);
1007 rc
= i2c_send(priv
, buf
, sizeof(buf
));
1012 priv
->frequency
= freq
;
1014 tuner_dbg("divisor= %02x %02x %02x %02x (freq=%d.%03d)\n",
1015 buf
[0], buf
[1], buf
[2], buf
[3],
1016 freq
/ 1000000, (freq
% 1000000) / 1000);
1021 mutex_unlock(&priv
->lock
);
1026 static int xc2028_set_analog_freq(struct dvb_frontend
*fe
,
1027 struct analog_parameters
*p
)
1029 struct xc2028_data
*priv
= fe
->tuner_priv
;
1030 unsigned int type
=0;
1032 tuner_dbg("%s called\n", __func__
);
1034 if (p
->mode
== V4L2_TUNER_RADIO
) {
1036 if (priv
->ctrl
.input1
)
1038 return generic_set_freq(fe
, (625l * p
->frequency
) / 10,
1039 T_RADIO
, type
, 0, 0);
1042 /* if std is not defined, choose one */
1044 p
->std
= V4L2_STD_MN
;
1046 /* PAL/M, PAL/N, PAL/Nc and NTSC variants should use 6MHz firmware */
1047 if (!(p
->std
& V4L2_STD_MN
))
1050 /* Add audio hack to std mask */
1051 p
->std
|= parse_audio_std_option();
1053 return generic_set_freq(fe
, 62500l * p
->frequency
,
1054 T_ANALOG_TV
, type
, p
->std
, 0);
1057 static int xc2028_set_params(struct dvb_frontend
*fe
,
1058 struct dvb_frontend_parameters
*p
)
1060 struct xc2028_data
*priv
= fe
->tuner_priv
;
1061 unsigned int type
=0;
1062 fe_bandwidth_t bw
= BANDWIDTH_8_MHZ
;
1065 tuner_dbg("%s called\n", __func__
);
1067 switch(fe
->ops
.info
.type
) {
1069 bw
= p
->u
.ofdm
.bandwidth
;
1071 * The only countries with 6MHz seem to be Taiwan/Uruguay.
1072 * Both seem to require QAM firmware for OFDM decoding
1073 * Tested in Taiwan by Terry Wu <terrywu2009@gmail.com>
1075 if (bw
== BANDWIDTH_6_MHZ
)
1079 bw
= BANDWIDTH_6_MHZ
;
1080 /* The only ATSC firmware (at least on v2.7) is D2633 */
1081 type
|= ATSC
| D2633
;
1083 /* DVB-S and pure QAM (FE_QAM) are not supported */
1089 case BANDWIDTH_8_MHZ
:
1090 if (p
->frequency
< 470000000)
1091 priv
->ctrl
.vhfbw7
= 0;
1093 priv
->ctrl
.uhfbw8
= 1;
1094 type
|= (priv
->ctrl
.vhfbw7
&& priv
->ctrl
.uhfbw8
) ? DTV78
: DTV8
;
1097 case BANDWIDTH_7_MHZ
:
1098 if (p
->frequency
< 470000000)
1099 priv
->ctrl
.vhfbw7
= 1;
1101 priv
->ctrl
.uhfbw8
= 0;
1102 type
|= (priv
->ctrl
.vhfbw7
&& priv
->ctrl
.uhfbw8
) ? DTV78
: DTV7
;
1105 case BANDWIDTH_6_MHZ
:
1107 priv
->ctrl
.vhfbw7
= 0;
1108 priv
->ctrl
.uhfbw8
= 0;
1111 tuner_err("error: bandwidth not supported.\n");
1115 Selects between D2633 or D2620 firmware.
1116 It doesn't make sense for ATSC, since it should be D2633 on all cases
1118 if (fe
->ops
.info
.type
!= FE_ATSC
) {
1119 switch (priv
->ctrl
.type
) {
1128 /* Zarlink seems to need D2633 */
1129 if (priv
->ctrl
.demod
== XC3028_FE_ZARLINK456
)
1136 /* All S-code tables need a 200kHz shift */
1137 if (priv
->ctrl
.demod
) {
1138 demod
= priv
->ctrl
.demod
;
1141 * Newer firmwares require a 200 kHz offset only for ATSC
1143 if (type
== ATSC
|| priv
->firm_version
< 0x0302)
1146 * The DTV7 S-code table needs a 700 kHz shift.
1148 * DTV7 is only used in Australia. Germany or Italy may also
1149 * use this firmware after initialization, but a tune to a UHF
1150 * channel should then cause DTV78 to be used.
1152 * Unfortunately, on real-field tests, the s-code offset
1153 * didn't work as expected, as reported by
1154 * Robert Lowery <rglowery@exemail.com.au>
1158 return generic_set_freq(fe
, p
->frequency
,
1159 T_DIGITAL_TV
, type
, 0, demod
);
1162 static int xc2028_sleep(struct dvb_frontend
*fe
)
1164 struct xc2028_data
*priv
= fe
->tuner_priv
;
1167 /* Avoid firmware reload on slow devices or if PM disabled */
1168 if (no_poweroff
|| priv
->ctrl
.disable_power_mgmt
)
1171 tuner_dbg("Putting xc2028/3028 into poweroff mode.\n");
1173 tuner_dbg("Printing sleep stack trace:\n");
1177 mutex_lock(&priv
->lock
);
1179 if (priv
->firm_version
< 0x0202)
1180 rc
= send_seq(priv
, {0x00, 0x08, 0x00, 0x00});
1182 rc
= send_seq(priv
, {0x80, 0x08, 0x00, 0x00});
1184 priv
->cur_fw
.type
= 0; /* need firmware reload */
1186 mutex_unlock(&priv
->lock
);
1191 static int xc2028_dvb_release(struct dvb_frontend
*fe
)
1193 struct xc2028_data
*priv
= fe
->tuner_priv
;
1195 tuner_dbg("%s called\n", __func__
);
1197 mutex_lock(&xc2028_list_mutex
);
1199 /* only perform final cleanup if this is the last instance */
1200 if (hybrid_tuner_report_instance_count(priv
) == 1) {
1201 kfree(priv
->ctrl
.fname
);
1202 free_firmware(priv
);
1206 hybrid_tuner_release_state(priv
);
1208 mutex_unlock(&xc2028_list_mutex
);
1210 fe
->tuner_priv
= NULL
;
1215 static int xc2028_get_frequency(struct dvb_frontend
*fe
, u32
*frequency
)
1217 struct xc2028_data
*priv
= fe
->tuner_priv
;
1219 tuner_dbg("%s called\n", __func__
);
1221 *frequency
= priv
->frequency
;
1226 static int xc2028_set_config(struct dvb_frontend
*fe
, void *priv_cfg
)
1228 struct xc2028_data
*priv
= fe
->tuner_priv
;
1229 struct xc2028_ctrl
*p
= priv_cfg
;
1232 tuner_dbg("%s called\n", __func__
);
1234 mutex_lock(&priv
->lock
);
1236 memcpy(&priv
->ctrl
, p
, sizeof(priv
->ctrl
));
1237 if (priv
->ctrl
.max_len
< 9)
1238 priv
->ctrl
.max_len
= 13;
1241 if (priv
->ctrl
.fname
&& strcmp(p
->fname
, priv
->ctrl
.fname
)) {
1242 kfree(priv
->ctrl
.fname
);
1243 free_firmware(priv
);
1246 priv
->ctrl
.fname
= kstrdup(p
->fname
, GFP_KERNEL
);
1247 if (priv
->ctrl
.fname
== NULL
)
1251 mutex_unlock(&priv
->lock
);
1256 static const struct dvb_tuner_ops xc2028_dvb_tuner_ops
= {
1258 .name
= "Xceive XC3028",
1259 .frequency_min
= 42000000,
1260 .frequency_max
= 864000000,
1261 .frequency_step
= 50000,
1264 .set_config
= xc2028_set_config
,
1265 .set_analog_params
= xc2028_set_analog_freq
,
1266 .release
= xc2028_dvb_release
,
1267 .get_frequency
= xc2028_get_frequency
,
1268 .get_rf_strength
= xc2028_signal
,
1269 .set_params
= xc2028_set_params
,
1270 .sleep
= xc2028_sleep
,
1273 struct dvb_frontend
*xc2028_attach(struct dvb_frontend
*fe
,
1274 struct xc2028_config
*cfg
)
1276 struct xc2028_data
*priv
;
1280 printk(KERN_DEBUG
"xc2028: Xcv2028/3028 init called!\n");
1286 printk(KERN_ERR
"xc2028: No frontend!\n");
1290 mutex_lock(&xc2028_list_mutex
);
1292 instance
= hybrid_tuner_request_state(struct xc2028_data
, priv
,
1293 hybrid_tuner_instance_list
,
1294 cfg
->i2c_adap
, cfg
->i2c_addr
,
1298 /* memory allocation failure */
1302 /* new tuner instance */
1303 priv
->ctrl
.max_len
= 13;
1305 mutex_init(&priv
->lock
);
1307 fe
->tuner_priv
= priv
;
1310 /* existing tuner instance */
1311 fe
->tuner_priv
= priv
;
1315 memcpy(&fe
->ops
.tuner_ops
, &xc2028_dvb_tuner_ops
,
1316 sizeof(xc2028_dvb_tuner_ops
));
1318 tuner_info("type set to %s\n", "XCeive xc2028/xc3028 tuner");
1321 xc2028_set_config(fe
, cfg
->ctrl
);
1323 mutex_unlock(&xc2028_list_mutex
);
1327 mutex_unlock(&xc2028_list_mutex
);
1329 xc2028_dvb_release(fe
);
1333 EXPORT_SYMBOL(xc2028_attach
);
1335 MODULE_DESCRIPTION("Xceive xc2028/xc3028 tuner driver");
1336 MODULE_AUTHOR("Michel Ludwig <michel.ludwig@gmail.com>");
1337 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
1338 MODULE_LICENSE("GPL");