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"
26 #define PREFIX "xc2028"
29 module_param(debug
, int, 0644);
30 MODULE_PARM_DESC(debug
, "enable verbose debug messages");
32 static char audio_std
[8];
33 module_param_string(audio_std
, audio_std
, sizeof(audio_std
), 0);
34 MODULE_PARM_DESC(audio_std
,
35 "Audio standard. XC3028 audio decoder explicitly "
36 "needs to know what audio\n"
37 "standard is needed for some video standards with audio A2 or NICAM.\n"
38 "The valid values are:\n"
46 static LIST_HEAD(xc2028_list
);
47 static DEFINE_MUTEX(xc2028_list_mutex
);
49 /* struct for storing firmware table */
50 struct firmware_description
{
58 struct list_head xc2028_list
;
59 struct tuner_i2c_props i2c_props
;
60 int (*tuner_callback
) (void *dev
,
61 int command
, int arg
);
66 struct firmware_description
*firm
;
71 struct xc2028_ctrl ctrl
;
73 v4l2_std_id firm_type
; /* video stds supported
74 by current firmware */
75 fe_bandwidth_t bandwidth
; /* Firmware bandwidth:
77 int need_load_generic
; /* The generic firmware
80 int max_len
; /* Max firmware chunk */
83 struct i2c_client
*i2c_client
;
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 void dump_firm_type(unsigned int type
)
191 if (type
& TOYOTA388
)
192 printk("TOYOTA388 ");
193 if (type
& TOYOTA794
)
194 printk("TOYOTA794 ");
197 if (type
& ZARLINK456
)
198 printk("ZARLINK456 ");
209 static v4l2_std_id
parse_audio_std_option(void)
211 if (strcasecmp(audio_std
, "A2") == 0)
213 if (strcasecmp(audio_std
, "A2/A") == 0)
214 return V4L2_STD_A2_A
;
215 if (strcasecmp(audio_std
, "A2/B") == 0)
216 return V4L2_STD_A2_B
;
217 if (strcasecmp(audio_std
, "NICAM") == 0)
218 return V4L2_STD_NICAM
;
219 if (strcasecmp(audio_std
, "NICAM/A") == 0)
220 return V4L2_STD_NICAM_A
;
221 if (strcasecmp(audio_std
, "NICAM/B") == 0)
222 return V4L2_STD_NICAM_B
;
227 static void free_firmware(struct xc2028_data
*priv
)
234 for (i
= 0; i
< priv
->firm_size
; i
++)
235 kfree(priv
->firm
[i
].ptr
);
240 priv
->need_load_generic
= 1;
243 static int load_all_firmwares(struct dvb_frontend
*fe
)
245 struct xc2028_data
*priv
= fe
->tuner_priv
;
246 const struct firmware
*fw
= NULL
;
247 unsigned char *p
, *endp
;
252 tuner_dbg("%s called\n", __FUNCTION__
);
254 tuner_info("Reading firmware %s\n", priv
->ctrl
.fname
);
255 rc
= request_firmware(&fw
, priv
->ctrl
.fname
,
256 &priv
->i2c_props
.adap
->dev
);
259 tuner_err("Error: firmware %s not found.\n",
262 tuner_err("Error %d while requesting firmware %s \n",
263 rc
, priv
->ctrl
.fname
);
270 if (fw
->size
< sizeof(name
) - 1 + 2) {
271 tuner_err("Error: firmware size is zero!\n");
276 memcpy(name
, p
, sizeof(name
) - 1);
277 name
[sizeof(name
) - 1] = 0;
278 p
+= sizeof(name
) - 1;
280 priv
->version
= le16_to_cpu(*(__u16
*) p
);
283 tuner_info("Firmware: %s, ver %d.%d\n", name
,
284 priv
->version
>> 8, priv
->version
& 0xff);
289 n_array
= le16_to_cpu(*(__u16
*) p
);
292 tuner_info("There are %d firmwares at %s\n",
293 n_array
, priv
->ctrl
.fname
);
295 priv
->firm
= kzalloc(sizeof(*priv
->firm
) * n_array
, GFP_KERNEL
);
298 tuner_err("Not enough memory for reading firmware.\n");
303 priv
->firm_size
= n_array
;
311 tuner_err("Too much firmwares at the file\n");
315 /* Checks if there's enough bytes to read */
316 if (p
+ sizeof(type
) + sizeof(id
) + sizeof(size
) > endp
) {
317 tuner_err("Firmware header is incomplete!\n");
321 type
= le32_to_cpu(*(__u32
*) p
);
324 id
= le64_to_cpu(*(v4l2_std_id
*) p
);
327 size
= le32_to_cpu(*(__u32
*) p
);
330 if ((!size
) || (size
+ p
> endp
)) {
331 tuner_err("Firmware type ");
332 dump_firm_type(type
);
333 printk("(%x), id %llx is corrupted "
334 "(size=%d, expected %d)\n",
335 type
, (unsigned long long)id
,
336 (unsigned)(endp
- p
), size
);
340 priv
->firm
[n
].ptr
= kzalloc(size
, GFP_KERNEL
);
341 if (!priv
->firm
[n
].ptr
) {
342 tuner_err("Not enough memory.\n");
346 tuner_info("Reading firmware type ");
347 dump_firm_type(type
);
348 printk("(%x), id %llx, size=%d.\n",
349 type
, (unsigned long long)id
, size
);
351 memcpy(priv
->firm
[n
].ptr
, p
, size
);
352 priv
->firm
[n
].type
= type
;
353 priv
->firm
[n
].id
= id
;
354 priv
->firm
[n
].size
= size
;
359 if (n
+ 1 != priv
->firm_size
) {
360 tuner_err("Firmware file is incomplete!\n");
368 tuner_err("Error: firmware file is corrupted!\n");
371 tuner_info("Releasing loaded firmware file.\n");
376 release_firmware(fw
);
377 tuner_dbg("Firmware files loaded.\n");
382 static int seek_firmware(struct dvb_frontend
*fe
, unsigned int type
,
385 struct xc2028_data
*priv
= fe
->tuner_priv
;
388 tuner_dbg("%s called\n", __FUNCTION__
);
391 tuner_err("Error! firmware not loaded\n");
395 if (((type
& ~SCODE
) == 0) && (*id
== 0))
398 /* Seek for exact match */
399 for (i
= 0; i
< priv
->firm_size
; i
++) {
400 if ((type
== priv
->firm
[i
].type
) && (*id
== priv
->firm
[i
].id
))
404 /* Seek for generic video standard match */
405 for (i
= 0; i
< priv
->firm_size
; i
++) {
406 if ((type
== priv
->firm
[i
].type
) && (*id
& priv
->firm
[i
].id
))
410 /*FIXME: Would make sense to seek for type "hint" match ? */
416 *id
= priv
->firm
[i
].id
;
419 tuner_dbg("%s firmware for type=", (i
< 0)? "Can't find": "Found");
421 dump_firm_type(type
);
422 printk("(%x), id %016llx.\n", type
, (unsigned long long)*id
);
427 static int load_firmware(struct dvb_frontend
*fe
, unsigned int type
,
430 struct xc2028_data
*priv
= fe
->tuner_priv
;
432 unsigned char *p
, *endp
, buf
[priv
->max_len
];
434 tuner_dbg("%s called\n", __FUNCTION__
);
436 pos
= seek_firmware(fe
, type
, id
);
440 tuner_info("Loading firmware for type=");
441 dump_firm_type(type
);
442 printk("(%x), id %016llx.\n", type
, (unsigned long long)*id
);
444 p
= priv
->firm
[pos
].ptr
;
447 tuner_err("Firmware pointer were freed!");
450 endp
= p
+ priv
->firm
[pos
].size
;
455 /* Checks if there's enough bytes to read */
456 if (p
+ sizeof(size
) > endp
) {
457 tuner_err("Firmware chunk size is wrong\n");
461 size
= le16_to_cpu(*(__u16
*) p
);
468 /* Special callback command received */
469 rc
= priv
->tuner_callback(priv
->video_dev
,
470 XC2028_TUNER_RESET
, 0);
472 tuner_err("Error at RESET code %d\n",
478 if (size
>= 0xff00) {
481 rc
= priv
->tuner_callback(priv
->video_dev
,
482 XC2028_RESET_CLK
, 0);
484 tuner_err("Error at RESET code %d\n",
490 tuner_info("Invalid RESET code %d\n",
498 /* Checks for a sleep command */
500 msleep(size
& 0x7fff);
504 if ((size
+ p
> endp
)) {
505 tuner_err("missing bytes: need %d, have %d\n",
506 size
, (int)(endp
- p
));
514 /* Sends message chunks */
516 int len
= (size
< priv
->max_len
- 1) ?
517 size
: priv
->max_len
- 1;
519 memcpy(buf
+ 1, p
, len
);
521 rc
= i2c_send(priv
, buf
, len
+ 1);
523 tuner_err("%d returned from send\n", rc
);
534 static int load_scode(struct dvb_frontend
*fe
, unsigned int type
,
535 v4l2_std_id
*id
, int scode
)
537 struct xc2028_data
*priv
= fe
->tuner_priv
;
541 tuner_dbg("%s called\n", __FUNCTION__
);
543 pos
= seek_firmware(fe
, type
, id
);
547 p
= priv
->firm
[pos
].ptr
;
550 tuner_err("Firmware pointer were freed!");
554 if ((priv
->firm
[pos
].size
!= 12 * 16) || (scode
>= 16))
557 if (priv
->version
< 0x0202)
558 rc
= send_seq(priv
, {0x20, 0x00, 0x00, 0x00});
560 rc
= send_seq(priv
, {0xa0, 0x00, 0x00, 0x00});
564 rc
= i2c_send(priv
, p
+ 12 * scode
, 12);
568 rc
= send_seq(priv
, {0x00, 0x8c});
575 static int check_firmware(struct dvb_frontend
*fe
, enum tuner_mode new_mode
,
576 v4l2_std_id std
, fe_bandwidth_t bandwidth
)
578 struct xc2028_data
*priv
= fe
->tuner_priv
;
580 u16 version
, hwmodel
;
581 v4l2_std_id std0
= 0;
582 unsigned int type0
= 0, type
= 0;
583 int change_digital_bandwidth
;
585 tuner_dbg("%s called\n", __FUNCTION__
);
588 if (!priv
->ctrl
.fname
) {
589 tuner_info("xc2028/3028 firmware name not set!\n");
593 rc
= load_all_firmwares(fe
);
598 tuner_dbg("I am in mode %u and I should switch to mode %i\n",
599 priv
->mode
, new_mode
);
601 /* first of all, determine whether we have switched the mode */
602 if (new_mode
!= priv
->mode
) {
603 priv
->mode
= new_mode
;
604 priv
->need_load_generic
= 1;
607 change_digital_bandwidth
= (priv
->mode
== T_DIGITAL_TV
608 && bandwidth
!= priv
->bandwidth
) ? 1 : 0;
609 tuner_dbg("old bandwidth %u, new bandwidth %u\n", priv
->bandwidth
,
612 if (priv
->need_load_generic
) {
613 /* Reset is needed before loading firmware */
614 rc
= priv
->tuner_callback(priv
->video_dev
,
615 XC2028_TUNER_RESET
, 0);
621 if (priv
->ctrl
.type
== XC2028_FIRM_MTS
)
624 if (priv
->bandwidth
== 8)
627 /* FIXME: How to load FM and FM|INPUT1 firmwares? */
629 rc
= load_firmware(fe
, type0
, &std0
);
631 tuner_err("Error %d while loading generic firmware\n",
636 priv
->need_load_generic
= 0;
638 if (priv
->mode
== T_DIGITAL_TV
)
639 change_digital_bandwidth
= 1;
642 tuner_dbg("I should change bandwidth %u\n", change_digital_bandwidth
);
644 if (change_digital_bandwidth
) {
646 /*FIXME: Should allow selecting between D2620 and D2633 */
649 /* FIXME: When should select a DTV78 firmware?
652 case BANDWIDTH_8_MHZ
:
655 case BANDWIDTH_7_MHZ
:
658 case BANDWIDTH_6_MHZ
:
659 /* FIXME: Should allow select also ATSC */
664 tuner_err("error: bandwidth not supported.\n");
666 priv
->bandwidth
= bandwidth
;
669 if (!change_digital_bandwidth
&& priv
->mode
== T_DIGITAL_TV
)
672 /* Load INIT1, if needed */
673 tuner_dbg("Load init1 firmware, if exists\n");
674 type0
= BASE
| INIT1
;
675 if (priv
->ctrl
.type
== XC2028_FIRM_MTS
)
678 /* FIXME: Should handle errors - if INIT1 found */
679 rc
= load_firmware(fe
, type0
, &std0
);
681 /* FIXME: Should add support for FM radio
684 if (priv
->ctrl
.type
== XC2028_FIRM_MTS
)
687 if (priv
->firm_type
& std
) {
688 tuner_dbg("Std-specific firmware already loaded.\n");
692 /* Add audio hack to std mask */
693 std
|= parse_audio_std_option();
695 rc
= load_firmware(fe
, type
, &std
);
699 /* Load SCODE firmware, if exists */
700 tuner_dbg("Trying to load scode 0\n");
703 rc
= load_scode(fe
, type
, &std
, 0);
705 xc2028_get_reg(priv
, 0x0004, &version
);
706 xc2028_get_reg(priv
, 0x0008, &hwmodel
);
708 tuner_info("Device is Xceive %d version %d.%d, "
709 "firmware version %d.%d\n",
710 hwmodel
, (version
& 0xf000) >> 12, (version
& 0xf00) >> 8,
711 (version
& 0xf0) >> 4, version
& 0xf);
713 priv
->firm_type
= std
;
718 static int xc2028_signal(struct dvb_frontend
*fe
, u16
*strength
)
720 struct xc2028_data
*priv
= fe
->tuner_priv
;
721 u16 frq_lock
, signal
= 0;
724 tuner_dbg("%s called\n", __FUNCTION__
);
726 mutex_lock(&priv
->lock
);
728 /* Sync Lock Indicator */
729 rc
= xc2028_get_reg(priv
, 0x0002, &frq_lock
);
730 if (rc
< 0 || frq_lock
== 0)
733 /* Frequency is locked. Return signal quality */
735 /* Get SNR of the video signal */
736 rc
= xc2028_get_reg(priv
, 0x0040, &signal
);
741 mutex_unlock(&priv
->lock
);
750 static int generic_set_tv_freq(struct dvb_frontend
*fe
, u32 freq
/* in Hz */ ,
751 enum tuner_mode new_mode
,
752 v4l2_std_id std
, fe_bandwidth_t bandwidth
)
754 struct xc2028_data
*priv
= fe
->tuner_priv
;
756 unsigned char buf
[5];
759 tuner_dbg("%s called\n", __FUNCTION__
);
761 mutex_lock(&priv
->lock
);
763 /* HACK: It seems that specific firmware need to be reloaded
764 when freq is changed */
769 rc
= priv
->tuner_callback(priv
->video_dev
, XC2028_TUNER_RESET
, 0);
774 tuner_dbg("should set frequency %d kHz)\n", freq
/ 1000);
776 if (check_firmware(fe
, new_mode
, std
, bandwidth
) < 0)
779 if (new_mode
== T_DIGITAL_TV
)
782 div
= (freq
- offset
+ DIV
/ 2) / DIV
;
784 /* CMD= Set frequency */
786 if (priv
->version
< 0x0202)
787 rc
= send_seq(priv
, {0x00, 0x02, 0x00, 0x00});
789 rc
= send_seq(priv
, {0x80, 0x02, 0x00, 0x00});
793 rc
= priv
->tuner_callback(priv
->video_dev
, XC2028_RESET_CLK
, 1);
799 buf
[0] = 0xff & (div
>> 24);
800 buf
[1] = 0xff & (div
>> 16);
801 buf
[2] = 0xff & (div
>> 8);
802 buf
[3] = 0xff & (div
);
805 rc
= i2c_send(priv
, buf
, sizeof(buf
));
810 priv
->frequency
= freq
;
812 tuner_dbg("divider= %02x %02x %02x %02x (freq=%d.%02d)\n",
813 buf
[1], buf
[2], buf
[3], buf
[4],
814 freq
/ 1000000, (freq
% 1000000) / 10000);
819 mutex_unlock(&priv
->lock
);
824 static int xc2028_set_tv_freq(struct dvb_frontend
*fe
,
825 struct analog_parameters
*p
)
827 struct xc2028_data
*priv
= fe
->tuner_priv
;
829 tuner_dbg("%s called\n", __FUNCTION__
);
831 return generic_set_tv_freq(fe
, 62500l * p
->frequency
, T_ANALOG_TV
,
832 p
->std
, BANDWIDTH_8_MHZ
/* NOT USED */);
835 static int xc2028_set_params(struct dvb_frontend
*fe
,
836 struct dvb_frontend_parameters
*p
)
838 struct xc2028_data
*priv
= fe
->tuner_priv
;
840 tuner_dbg("%s called\n", __FUNCTION__
);
842 /* FIXME: Only OFDM implemented */
843 if (fe
->ops
.info
.type
!= FE_OFDM
) {
844 tuner_err("DTV type not implemented.\n");
848 return generic_set_tv_freq(fe
, p
->frequency
, T_DIGITAL_TV
,
850 p
->u
.ofdm
.bandwidth
);
854 static int xc2028_dvb_release(struct dvb_frontend
*fe
)
856 struct xc2028_data
*priv
= fe
->tuner_priv
;
858 tuner_dbg("%s called\n", __FUNCTION__
);
860 mutex_lock(&xc2028_list_mutex
);
865 list_del(&priv
->xc2028_list
);
867 kfree(priv
->ctrl
.fname
);
873 mutex_unlock(&xc2028_list_mutex
);
878 static int xc2028_get_frequency(struct dvb_frontend
*fe
, u32
*frequency
)
880 struct xc2028_data
*priv
= fe
->tuner_priv
;
882 tuner_dbg("%s called\n", __FUNCTION__
);
884 *frequency
= priv
->frequency
;
889 static int xc2028_set_config(struct dvb_frontend
*fe
, void *priv_cfg
)
891 struct xc2028_data
*priv
= fe
->tuner_priv
;
892 struct xc2028_ctrl
*p
= priv_cfg
;
894 tuner_dbg("%s called\n", __FUNCTION__
);
896 priv
->ctrl
.type
= p
->type
;
899 kfree(priv
->ctrl
.fname
);
901 priv
->ctrl
.fname
= kmalloc(strlen(p
->fname
) + 1, GFP_KERNEL
);
902 if (!priv
->ctrl
.fname
)
906 strcpy(priv
->ctrl
.fname
, p
->fname
);
910 priv
->max_len
= p
->max_len
;
915 static const struct dvb_tuner_ops xc2028_dvb_tuner_ops
= {
917 .name
= "Xceive XC3028",
918 .frequency_min
= 42000000,
919 .frequency_max
= 864000000,
920 .frequency_step
= 50000,
923 .set_config
= xc2028_set_config
,
924 .set_analog_params
= xc2028_set_tv_freq
,
925 .release
= xc2028_dvb_release
,
926 .get_frequency
= xc2028_get_frequency
,
927 .get_rf_strength
= xc2028_signal
,
928 .set_params
= xc2028_set_params
,
932 void *xc2028_attach(struct dvb_frontend
*fe
, struct xc2028_config
*cfg
)
934 struct xc2028_data
*priv
;
938 printk(KERN_DEBUG PREFIX
": Xcv2028/3028 init called!\n");
940 if (NULL
== cfg
->video_dev
)
944 printk(KERN_ERR PREFIX
": No frontend!\n");
948 video_dev
= cfg
->video_dev
;
950 mutex_lock(&xc2028_list_mutex
);
952 list_for_each_entry(priv
, &xc2028_list
, xc2028_list
) {
953 if (priv
->video_dev
== cfg
->video_dev
) {
960 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
962 mutex_unlock(&xc2028_list_mutex
);
966 priv
->bandwidth
= BANDWIDTH_6_MHZ
;
967 priv
->need_load_generic
= 1;
968 priv
->mode
= T_UNINITIALIZED
;
969 priv
->i2c_props
.addr
= cfg
->i2c_addr
;
970 priv
->i2c_props
.adap
= cfg
->i2c_adap
;
971 priv
->video_dev
= video_dev
;
972 priv
->tuner_callback
= cfg
->callback
;
975 mutex_init(&priv
->lock
);
977 list_add_tail(&priv
->xc2028_list
, &xc2028_list
);
980 fe
->tuner_priv
= priv
;
983 memcpy(&fe
->ops
.tuner_ops
, &xc2028_dvb_tuner_ops
,
984 sizeof(xc2028_dvb_tuner_ops
));
986 tuner_info("type set to %s\n", "XCeive xc2028/xc3028 tuner");
988 mutex_unlock(&xc2028_list_mutex
);
993 EXPORT_SYMBOL(xc2028_attach
);
995 MODULE_DESCRIPTION("Xceive xc2028/xc3028 tuner driver");
996 MODULE_AUTHOR("Michel Ludwig <michel.ludwig@gmail.com>");
997 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
998 MODULE_LICENSE("GPL");