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 /* struct for storing firmware table */
48 struct firmware_description
{
56 struct list_head xc2028_list
;
57 struct tuner_i2c_props i2c_props
;
58 int (*tuner_callback
) (void *dev
,
59 int command
, int arg
);
64 struct firmware_description
*firm
;
69 struct xc2028_ctrl ctrl
;
71 v4l2_std_id firm_type
; /* video stds supported
72 by current firmware */
73 fe_bandwidth_t bandwidth
; /* Firmware bandwidth:
75 int need_load_generic
; /* The generic firmware
78 int max_len
; /* Max firmware chunk */
81 struct i2c_client
*i2c_client
;
86 #define i2c_send(priv, buf, size) ({ \
88 _rc = tuner_i2c_xfer_send(&priv->i2c_props, buf, size); \
90 tuner_info("i2c output error: rc = %d (should be %d)\n",\
95 #define i2c_rcv(priv, buf, size) ({ \
97 _rc = tuner_i2c_xfer_recv(&priv->i2c_props, buf, size); \
99 tuner_err("i2c input error: rc = %d (should be %d)\n", \
104 #define send_seq(priv, data...) ({ \
105 static u8 _val[] = data; \
107 if (sizeof(_val) != \
108 (_rc = tuner_i2c_xfer_send(&priv->i2c_props, \
109 _val, sizeof(_val)))) { \
110 tuner_err("Error on line %d: %d\n", __LINE__, _rc); \
116 static unsigned int xc2028_get_reg(struct xc2028_data
*priv
, u16 reg
)
119 unsigned char buf
[2];
121 tuner_dbg("%s called\n", __FUNCTION__
);
124 buf
[1] = (unsigned char) reg
;
126 rc
= i2c_send(priv
, buf
, 2);
130 rc
= i2c_rcv(priv
, buf
, 2);
134 return (buf
[1]) | (buf
[0] << 8);
137 void dump_firm_type(unsigned int type
)
183 if (type
& TOYOTA388
)
184 printk("TOYOTA388 ");
185 if (type
& TOYOTA794
)
186 printk("TOYOTA794 ");
189 if (type
& ZARLINK456
)
190 printk("ZARLINK456 ");
201 static v4l2_std_id
parse_audio_std_option(void)
203 if (strcasecmp(audio_std
, "A2"))
205 if (strcasecmp(audio_std
, "A2/A"))
206 return V4L2_STD_A2_A
;
207 if (strcasecmp(audio_std
, "A2/B"))
208 return V4L2_STD_A2_B
;
209 if (strcasecmp(audio_std
, "NICAM"))
210 return V4L2_STD_NICAM
;
211 if (strcasecmp(audio_std
, "NICAM/A"))
212 return V4L2_STD_NICAM_A
;
213 if (strcasecmp(audio_std
, "NICAM/B"))
214 return V4L2_STD_NICAM_B
;
219 static void free_firmware(struct xc2028_data
*priv
)
226 for (i
= 0; i
< priv
->firm_size
; i
++)
227 kfree(priv
->firm
[i
].ptr
);
232 priv
->need_load_generic
= 1;
235 static int load_all_firmwares(struct dvb_frontend
*fe
)
237 struct xc2028_data
*priv
= fe
->tuner_priv
;
238 const struct firmware
*fw
= NULL
;
239 unsigned char *p
, *endp
;
244 tuner_dbg("%s called\n", __FUNCTION__
);
246 tuner_info("Reading firmware %s\n", priv
->ctrl
.fname
);
247 rc
= request_firmware(&fw
, priv
->ctrl
.fname
,
248 &priv
->i2c_props
.adap
->dev
);
251 tuner_err("Error: firmware %s not found.\n",
254 tuner_err("Error %d while requesting firmware %s \n",
255 rc
, priv
->ctrl
.fname
);
262 if (fw
->size
< sizeof(name
) - 1 + 2) {
263 tuner_err("Error: firmware size is zero!\n");
268 memcpy(name
, p
, sizeof(name
) - 1);
269 name
[sizeof(name
) - 1] = 0;
270 p
+= sizeof(name
) - 1;
272 priv
->version
= le16_to_cpu(*(__u16
*) p
);
275 tuner_info("Firmware: %s, ver %d.%d\n", name
,
276 priv
->version
>> 8, priv
->version
& 0xff);
281 n_array
= le16_to_cpu(*(__u16
*) p
);
284 tuner_info("There are %d firmwares at %s\n",
285 n_array
, priv
->ctrl
.fname
);
287 priv
->firm
= kzalloc(sizeof(*priv
->firm
) * n_array
, GFP_KERNEL
);
290 tuner_err("Not enough memory for reading firmware.\n");
295 priv
->firm_size
= n_array
;
303 tuner_err("Too much firmwares at the file\n");
307 /* Checks if there's enough bytes to read */
308 if (p
+ sizeof(type
) + sizeof(id
) + sizeof(size
) > endp
) {
309 tuner_err("Firmware header is incomplete!\n");
313 type
= le32_to_cpu(*(__u32
*) p
);
316 id
= le64_to_cpu(*(v4l2_std_id
*) p
);
319 size
= le32_to_cpu(*(__u32
*) p
);
322 if ((!size
) || (size
+ p
> endp
)) {
323 tuner_err("Firmware type ");
324 dump_firm_type(type
);
325 printk("(%x), id %llx is corrupted "
326 "(size=%d, expected %d)\n",
328 (unsigned)(endp
- p
), size
);
332 priv
->firm
[n
].ptr
= kzalloc(size
, GFP_KERNEL
);
333 if (!priv
->firm
[n
].ptr
) {
334 tuner_err("Not enough memory.\n");
338 tuner_info("Reading firmware type ");
339 dump_firm_type(type
);
340 printk("(%x), id %lx, size=%d.\n",
341 type
, (unsigned long)id
, size
);
343 memcpy(priv
->firm
[n
].ptr
, p
, size
);
344 priv
->firm
[n
].type
= type
;
345 priv
->firm
[n
].id
= id
;
346 priv
->firm
[n
].size
= size
;
351 if (n
+ 1 != priv
->firm_size
) {
352 tuner_err("Firmware file is incomplete!\n");
360 tuner_err("Error: firmware file is corrupted!\n");
363 tuner_info("Releasing loaded firmware file.\n");
368 release_firmware(fw
);
369 tuner_dbg("Firmware files loaded.\n");
374 static int seek_firmware(struct dvb_frontend
*fe
, unsigned int type
,
377 struct xc2028_data
*priv
= fe
->tuner_priv
;
380 tuner_dbg("%s called\n", __FUNCTION__
);
383 tuner_err("Error! firmware not loaded\n");
387 if (((type
& ~SCODE
) == 0) && (*id
== 0))
390 /* Seek for exact match */
391 for (i
= 0; i
< priv
->firm_size
; i
++) {
392 if ((type
== priv
->firm
[i
].type
) && (*id
== priv
->firm
[i
].id
))
396 /* Seek for generic video standard match */
397 for (i
= 0; i
< priv
->firm_size
; i
++) {
398 if ((type
== priv
->firm
[i
].type
) && (*id
& priv
->firm
[i
].id
))
402 /*FIXME: Would make sense to seek for type "hint" match ? */
408 *id
= priv
->firm
[i
].id
;
411 tuner_dbg("%s firmware for type=", (i
< 0)? "Can't find": "Found");
413 dump_firm_type(type
);
414 printk("(%x), id %08lx.\n", type
, (unsigned long)*id
);
419 static int load_firmware(struct dvb_frontend
*fe
, unsigned int type
,
422 struct xc2028_data
*priv
= fe
->tuner_priv
;
424 unsigned char *p
, *endp
, buf
[priv
->max_len
];
426 tuner_dbg("%s called\n", __FUNCTION__
);
428 pos
= seek_firmware(fe
, type
, id
);
432 tuner_info("Loading firmware for type=");
433 dump_firm_type(type
);
434 printk("(%x), id %08lx.\n", type
, (unsigned long)*id
);
436 p
= priv
->firm
[pos
].ptr
;
439 tuner_err("Firmware pointer were freed!");
442 endp
= p
+ priv
->firm
[pos
].size
;
447 /* Checks if there's enough bytes to read */
448 if (p
+ sizeof(size
) > endp
) {
449 tuner_err("Firmware chunk size is wrong\n");
453 size
= le16_to_cpu(*(__u16
*) p
);
460 /* Special callback command received */
461 rc
= priv
->tuner_callback(priv
->video_dev
,
462 XC2028_TUNER_RESET
, 0);
464 tuner_err("Error at RESET code %d\n",
470 if (size
>= 0xff00) {
473 rc
= priv
->tuner_callback(priv
->video_dev
,
474 XC2028_RESET_CLK
, 0);
476 tuner_err("Error at RESET code %d\n",
481 tuner_info("Invalid RESET code %d\n",
489 /* Checks for a sleep command */
491 msleep(size
& 0x7fff);
495 if ((size
+ p
> endp
)) {
496 tuner_err("missing bytes: need %d, have %d\n",
497 size
, (int)(endp
- p
));
505 /* Sends message chunks */
507 int len
= (size
< priv
->max_len
- 1) ?
508 size
: priv
->max_len
- 1;
510 memcpy(buf
+ 1, p
, len
);
512 rc
= i2c_send(priv
, buf
, len
+ 1);
514 tuner_err("%d returned from send\n", rc
);
525 static int load_scode(struct dvb_frontend
*fe
, unsigned int type
,
526 v4l2_std_id
*id
, int scode
)
528 struct xc2028_data
*priv
= fe
->tuner_priv
;
532 tuner_dbg("%s called\n", __FUNCTION__
);
534 pos
= seek_firmware(fe
, type
, id
);
538 p
= priv
->firm
[pos
].ptr
;
541 tuner_err("Firmware pointer were freed!");
545 if ((priv
->firm
[pos
].size
!= 12 * 16) || (scode
>= 16))
548 if (priv
->version
< 0x0202)
549 rc
= send_seq(priv
, {0x20, 0x00, 0x00, 0x00});
551 rc
= send_seq(priv
, {0xa0, 0x00, 0x00, 0x00});
555 rc
= i2c_send(priv
, p
+ 12 * scode
, 12);
559 rc
= send_seq(priv
, {0x00, 0x8c});
566 static int check_firmware(struct dvb_frontend
*fe
, enum tuner_mode new_mode
,
567 v4l2_std_id std
, fe_bandwidth_t bandwidth
)
569 struct xc2028_data
*priv
= fe
->tuner_priv
;
570 int rc
, version
, hwmodel
;
571 v4l2_std_id std0
= 0;
572 unsigned int type0
= 0, type
= 0;
573 int change_digital_bandwidth
;
575 tuner_dbg("%s called\n", __FUNCTION__
);
578 if (!priv
->ctrl
.fname
) {
579 tuner_info("xc2028/3028 firmware name not set!\n");
583 rc
= load_all_firmwares(fe
);
588 tuner_dbg("I am in mode %u and I should switch to mode %i\n",
589 priv
->mode
, new_mode
);
591 /* first of all, determine whether we have switched the mode */
592 if (new_mode
!= priv
->mode
) {
593 priv
->mode
= new_mode
;
594 priv
->need_load_generic
= 1;
597 change_digital_bandwidth
= (priv
->mode
== T_DIGITAL_TV
598 && bandwidth
!= priv
->bandwidth
) ? 1 : 0;
599 tuner_dbg("old bandwidth %u, new bandwidth %u\n", priv
->bandwidth
,
602 if (priv
->need_load_generic
) {
603 /* Reset is needed before loading firmware */
604 rc
= priv
->tuner_callback(priv
->video_dev
,
605 XC2028_TUNER_RESET
, 0);
611 if (priv
->ctrl
.type
== XC2028_FIRM_MTS
)
614 if (priv
->bandwidth
== 8)
617 /* FIXME: How to load FM and FM|INPUT1 firmwares? */
619 rc
= load_firmware(fe
, type0
, &std0
);
621 tuner_err("Error %d while loading generic firmware\n",
626 priv
->need_load_generic
= 0;
628 if (priv
->mode
== T_DIGITAL_TV
)
629 change_digital_bandwidth
= 1;
632 tuner_dbg("I should change bandwidth %u\n", change_digital_bandwidth
);
634 if (change_digital_bandwidth
) {
636 /*FIXME: Should allow selecting between D2620 and D2633 */
639 /* FIXME: When should select a DTV78 firmware?
642 case BANDWIDTH_8_MHZ
:
645 case BANDWIDTH_7_MHZ
:
648 case BANDWIDTH_6_MHZ
:
649 /* FIXME: Should allow select also ATSC */
654 tuner_err("error: bandwidth not supported.\n");
656 priv
->bandwidth
= bandwidth
;
659 if (!change_digital_bandwidth
&& priv
->mode
== T_DIGITAL_TV
)
662 /* Load INIT1, if needed */
663 tuner_dbg("Load init1 firmware, if exists\n");
664 type0
= BASE
| INIT1
;
665 if (priv
->ctrl
.type
== XC2028_FIRM_MTS
)
668 /* FIXME: Should handle errors - if INIT1 found */
669 rc
= load_firmware(fe
, type0
, &std0
);
671 /* FIXME: Should add support for FM radio
674 if (priv
->ctrl
.type
== XC2028_FIRM_MTS
)
677 if (priv
->firm_type
& std
) {
678 tuner_dbg("Std-specific firmware already loaded.\n");
682 /* Add audio hack to std mask */
683 std
|= parse_audio_std_option();
685 rc
= load_firmware(fe
, type
, &std
);
689 /* Load SCODE firmware, if exists */
690 tuner_dbg("Trying to load scode 0\n");
693 rc
= load_scode(fe
, type
, &std
, 0);
695 version
= xc2028_get_reg(priv
, 0x0004);
696 hwmodel
= xc2028_get_reg(priv
, 0x0008);
698 tuner_info("Device is Xceive %d version %d.%d, "
699 "firmware version %d.%d\n",
700 hwmodel
, (version
& 0xf000) >> 12, (version
& 0xf00) >> 8,
701 (version
& 0xf0) >> 4, version
& 0xf);
703 priv
->firm_type
= std
;
708 static int xc2028_signal(struct dvb_frontend
*fe
, u16
*strength
)
710 struct xc2028_data
*priv
= fe
->tuner_priv
;
711 int frq_lock
, signal
= 0;
713 tuner_dbg("%s called\n", __FUNCTION__
);
715 mutex_lock(&priv
->lock
);
719 /* Sync Lock Indicator */
720 frq_lock
= xc2028_get_reg(priv
, 0x0002);
724 /* Frequency is locked. Return signal quality */
726 /* Get SNR of the video signal */
727 signal
= xc2028_get_reg(priv
, 0x0040);
733 mutex_unlock(&priv
->lock
);
742 static int generic_set_tv_freq(struct dvb_frontend
*fe
, u32 freq
/* in Hz */ ,
743 enum tuner_mode new_mode
,
744 v4l2_std_id std
, fe_bandwidth_t bandwidth
)
746 struct xc2028_data
*priv
= fe
->tuner_priv
;
748 unsigned char buf
[5];
751 tuner_dbg("%s called\n", __FUNCTION__
);
753 mutex_lock(&priv
->lock
);
755 /* HACK: It seems that specific firmware need to be reloaded
756 when freq is changed */
761 rc
= priv
->tuner_callback(priv
->video_dev
, XC2028_TUNER_RESET
, 0);
766 tuner_dbg("should set frequency %d kHz)\n", freq
/ 1000);
768 if (check_firmware(fe
, new_mode
, std
, bandwidth
) < 0)
771 if (new_mode
== T_DIGITAL_TV
)
774 div
= (freq
- offset
+ DIV
/ 2) / DIV
;
776 /* CMD= Set frequency */
778 if (priv
->version
< 0x0202)
779 rc
= send_seq(priv
, {0x00, 0x02, 0x00, 0x00});
781 rc
= send_seq(priv
, {0x80, 0x02, 0x00, 0x00});
785 rc
= priv
->tuner_callback(priv
->video_dev
, XC2028_RESET_CLK
, 1);
791 buf
[0] = 0xff & (div
>> 24);
792 buf
[1] = 0xff & (div
>> 16);
793 buf
[2] = 0xff & (div
>> 8);
794 buf
[3] = 0xff & (div
);
797 rc
= i2c_send(priv
, buf
, sizeof(buf
));
802 priv
->frequency
= freq
;
804 tuner_dbg("divider= %02x %02x %02x %02x (freq=%d.%02d)\n",
805 buf
[1], buf
[2], buf
[3], buf
[4],
806 freq
/ 1000000, (freq
% 1000000) / 10000);
811 mutex_unlock(&priv
->lock
);
816 static int xc2028_set_tv_freq(struct dvb_frontend
*fe
,
817 struct analog_parameters
*p
)
819 struct xc2028_data
*priv
= fe
->tuner_priv
;
821 tuner_dbg("%s called\n", __FUNCTION__
);
823 return generic_set_tv_freq(fe
, 62500l * p
->frequency
, T_ANALOG_TV
,
824 p
->std
, BANDWIDTH_8_MHZ
/* NOT USED */);
827 static int xc2028_set_params(struct dvb_frontend
*fe
,
828 struct dvb_frontend_parameters
*p
)
830 struct xc2028_data
*priv
= fe
->tuner_priv
;
832 tuner_dbg("%s called\n", __FUNCTION__
);
834 /* FIXME: Only OFDM implemented */
835 if (fe
->ops
.info
.type
!= FE_OFDM
) {
836 tuner_err("DTV type not implemented.\n");
840 return generic_set_tv_freq(fe
, p
->frequency
, T_DIGITAL_TV
,
842 p
->u
.ofdm
.bandwidth
);
846 static int xc2028_dvb_release(struct dvb_frontend
*fe
)
848 struct xc2028_data
*priv
= fe
->tuner_priv
;
850 tuner_dbg("%s called\n", __FUNCTION__
);
855 list_del(&priv
->xc2028_list
);
857 kfree(priv
->ctrl
.fname
);
866 static int xc2028_get_frequency(struct dvb_frontend
*fe
, u32
*frequency
)
868 struct xc2028_data
*priv
= fe
->tuner_priv
;
870 tuner_dbg("%s called\n", __FUNCTION__
);
872 *frequency
= priv
->frequency
;
877 static int xc2028_set_config(struct dvb_frontend
*fe
, void *priv_cfg
)
879 struct xc2028_data
*priv
= fe
->tuner_priv
;
880 struct xc2028_ctrl
*p
= priv_cfg
;
882 tuner_dbg("%s called\n", __FUNCTION__
);
884 priv
->ctrl
.type
= p
->type
;
887 kfree(priv
->ctrl
.fname
);
889 priv
->ctrl
.fname
= kmalloc(strlen(p
->fname
) + 1, GFP_KERNEL
);
890 if (!priv
->ctrl
.fname
)
894 strcpy(priv
->ctrl
.fname
, p
->fname
);
898 priv
->max_len
= p
->max_len
;
903 static const struct dvb_tuner_ops xc2028_dvb_tuner_ops
= {
905 .name
= "Xceive XC3028",
906 .frequency_min
= 42000000,
907 .frequency_max
= 864000000,
908 .frequency_step
= 50000,
911 .set_config
= xc2028_set_config
,
912 .set_analog_params
= xc2028_set_tv_freq
,
913 .release
= xc2028_dvb_release
,
914 .get_frequency
= xc2028_get_frequency
,
915 .get_rf_strength
= xc2028_signal
,
916 .set_params
= xc2028_set_params
,
920 void *xc2028_attach(struct dvb_frontend
*fe
, struct xc2028_config
*cfg
)
922 struct xc2028_data
*priv
;
926 printk(KERN_DEBUG PREFIX
"Xcv2028/3028 init called!\n");
928 if (NULL
== cfg
->video_dev
)
932 printk(KERN_ERR PREFIX
"No frontend!\n");
936 video_dev
= cfg
->video_dev
;
938 list_for_each_entry(priv
, &xc2028_list
, xc2028_list
) {
939 if (priv
->video_dev
== cfg
->video_dev
) {
946 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
950 priv
->bandwidth
= BANDWIDTH_6_MHZ
;
951 priv
->need_load_generic
= 1;
952 priv
->mode
= T_UNINITIALIZED
;
953 priv
->i2c_props
.addr
= cfg
->i2c_addr
;
954 priv
->i2c_props
.adap
= cfg
->i2c_adap
;
955 priv
->video_dev
= video_dev
;
956 priv
->tuner_callback
= cfg
->callback
;
959 mutex_init(&priv
->lock
);
961 list_add_tail(&priv
->xc2028_list
, &xc2028_list
);
964 fe
->tuner_priv
= priv
;
967 memcpy(&fe
->ops
.tuner_ops
, &xc2028_dvb_tuner_ops
,
968 sizeof(xc2028_dvb_tuner_ops
));
970 tuner_info("type set to %s\n", "XCeive xc2028/xc3028 tuner");
975 EXPORT_SYMBOL(xc2028_attach
);
977 MODULE_DESCRIPTION("Xceive xc2028/xc3028 tuner driver");
978 MODULE_AUTHOR("Michel Ludwig <michel.ludwig@gmail.com>");
979 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
980 MODULE_LICENSE("GPL");