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;
70 struct xc2028_ctrl ctrl;
72 v4l2_std_id firm_type; /* video stds supported
73 by current firmware */
74 fe_bandwidth_t bandwidth; /* Firmware bandwidth:
76 int need_load_generic; /* The generic firmware
79 int max_len; /* Max firmware chunk */
82 struct i2c_client *i2c_client;
87 #define i2c_send(priv, buf, size) ({ \
89 _rc = tuner_i2c_xfer_send(&priv->i2c_props, buf, size); \
91 tuner_info("i2c output error: rc = %d (should be %d)\n",\
96 #define i2c_rcv(priv, buf, size) ({ \
98 _rc = tuner_i2c_xfer_recv(&priv->i2c_props, buf, size); \
100 tuner_err("i2c input error: rc = %d (should be %d)\n", \
105 #define i2c_send_recv(priv, obuf, osize, ibuf, isize) ({ \
107 _rc = tuner_i2c_xfer_send_recv(&priv->i2c_props, obuf, osize, \
110 tuner_err("i2c input error: rc = %d (should be %d)\n", \
115 #define send_seq(priv, data...) ({ \
116 static u8 _val[] = data; \
118 if (sizeof(_val) != \
119 (_rc = tuner_i2c_xfer_send(&priv->i2c_props, \
120 _val, sizeof(_val)))) { \
121 tuner_err("Error on line %d: %d\n", __LINE__, _rc); \
127 static unsigned int xc2028_get_reg(struct xc2028_data *priv, u16 reg, u16 *val)
129 unsigned char buf[2];
130 unsigned char ibuf[2];
132 tuner_dbg("%s %04x called\n", __FUNCTION__, reg);
135 buf[1] = (unsigned char) reg;
137 if (i2c_send_recv(priv, buf, 2, ibuf, 2) != 2)
140 *val = (ibuf[1]) | (ibuf[0] << 8);
144 void dump_firm_type(unsigned int type)
190 if (type & TOYOTA388)
191 printk("TOYOTA388 ");
192 if (type & TOYOTA794)
193 printk("TOYOTA794 ");
196 if (type & ZARLINK456)
197 printk("ZARLINK456 ");
208 static v4l2_std_id parse_audio_std_option(void)
210 if (strcasecmp(audio_std, "A2") == 0)
212 if (strcasecmp(audio_std, "A2/A") == 0)
213 return V4L2_STD_A2_A;
214 if (strcasecmp(audio_std, "A2/B") == 0)
215 return V4L2_STD_A2_B;
216 if (strcasecmp(audio_std, "NICAM") == 0)
217 return V4L2_STD_NICAM;
218 if (strcasecmp(audio_std, "NICAM/A") == 0)
219 return V4L2_STD_NICAM_A;
220 if (strcasecmp(audio_std, "NICAM/B") == 0)
221 return V4L2_STD_NICAM_B;
226 static void free_firmware(struct xc2028_data *priv)
233 for (i = 0; i < priv->firm_size; i++)
234 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_dbg("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 + 2) {
271 tuner_err("Error: firmware file %s has invalid size!\n",
276 memcpy(name, p, sizeof(name) - 1);
277 name[sizeof(name) - 1] = 0;
278 p += sizeof(name) - 1;
280 priv->firm_version = le16_to_cpu(*(__u16 *) p);
283 n_array = le16_to_cpu(*(__u16 *) p);
286 tuner_info("Loading %d firmware images from %s, type: %s, ver %d.%d\n",
287 n_array, priv->ctrl.fname, name,
288 priv->firm_version >> 8, priv->firm_version & 0xff);
290 priv->firm = kzalloc(sizeof(*priv->firm) * n_array, GFP_KERNEL);
291 if (priv->firm == NULL) {
292 tuner_err("Not enough memory to load firmware file.\n");
296 priv->firm_size = n_array;
305 tuner_err("More firmware images in file than "
310 /* Checks if there's enough bytes to read */
311 if (p + sizeof(type) + sizeof(id) + sizeof(size) > endp) {
312 tuner_err("Firmware header is incomplete!\n");
316 type = le32_to_cpu(*(__u32 *) p);
319 id = le64_to_cpu(*(v4l2_std_id *) p);
322 size = le32_to_cpu(*(__u32 *) p);
325 if ((!size) || (size + p > endp)) {
326 tuner_err("Firmware type ");
327 dump_firm_type(type);
328 printk("(%x), id %llx is corrupted "
329 "(size=%d, expected %d)\n",
330 type, (unsigned long long)id,
331 (unsigned)(endp - p), size);
335 priv->firm[n].ptr = kzalloc(size, GFP_KERNEL);
336 if (priv->firm[n].ptr == NULL) {
337 tuner_err("Not enough memory to load firmware file.\n");
341 tuner_dbg("Reading firmware type ");
343 dump_firm_type(type);
344 printk("(%x), id %llx, size=%d.\n",
345 type, (unsigned long long)id, size);
348 memcpy(priv->firm[n].ptr, p, size);
349 priv->firm[n].type = type;
350 priv->firm[n].id = id;
351 priv->firm[n].size = size;
356 if (n + 1 != priv->firm_size) {
357 tuner_err("Firmware file is incomplete!\n");
365 tuner_err("Error: firmware file is corrupted!\n");
368 tuner_info("Releasing partially loaded firmware file.\n");
372 release_firmware(fw);
374 tuner_dbg("Firmware files loaded.\n");
379 static int seek_firmware(struct dvb_frontend *fe, unsigned int type,
382 struct xc2028_data *priv = fe->tuner_priv;
385 tuner_dbg("%s called\n", __FUNCTION__);
388 tuner_err("Error! firmware not loaded\n");
392 if (((type & ~SCODE) == 0) && (*id == 0))
395 /* Seek for exact match */
396 for (i = 0; i < priv->firm_size; i++) {
397 if ((type == priv->firm[i].type) && (*id == priv->firm[i].id))
401 /* Seek for generic video standard match */
402 for (i = 0; i < priv->firm_size; i++) {
403 if ((type == priv->firm[i].type) && (*id & priv->firm[i].id))
407 /*FIXME: Would make sense to seek for type "hint" match ? */
413 *id = priv->firm[i].id;
416 tuner_dbg("%s firmware for type=", (i < 0)? "Can't find": "Found");
418 dump_firm_type(type);
419 printk("(%x), id %016llx.\n", type, (unsigned long long)*id);
424 static int load_firmware(struct dvb_frontend *fe, unsigned int type,
427 struct xc2028_data *priv = fe->tuner_priv;
429 unsigned char *p, *endp, buf[priv->max_len];
431 tuner_dbg("%s called\n", __FUNCTION__);
433 pos = seek_firmware(fe, type, id);
437 tuner_info("Loading firmware for type=");
438 dump_firm_type(type);
439 printk("(%x), id %016llx.\n", type, (unsigned long long)*id);
441 p = priv->firm[pos].ptr;
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",
482 tuner_info("Invalid RESET code %d\n",
490 /* Checks for a sleep command */
492 msleep(size & 0x7fff);
496 if ((size + p > endp)) {
497 tuner_err("missing bytes: need %d, have %d\n",
498 size, (int)(endp - p));
506 /* Sends message chunks */
508 int len = (size < priv->max_len - 1) ?
509 size : priv->max_len - 1;
511 memcpy(buf + 1, p, len);
513 rc = i2c_send(priv, buf, len + 1);
515 tuner_err("%d returned from send\n", rc);
526 static int load_scode(struct dvb_frontend *fe, unsigned int type,
527 v4l2_std_id *id, int scode)
529 struct xc2028_data *priv = fe->tuner_priv;
533 tuner_dbg("%s called\n", __FUNCTION__);
535 pos = seek_firmware(fe, type, id);
539 p = priv->firm[pos].ptr;
541 if ((priv->firm[pos].size != 12 * 16) || (scode >= 16))
544 if (priv->firm_version < 0x0202)
545 rc = send_seq(priv, {0x20, 0x00, 0x00, 0x00});
547 rc = send_seq(priv, {0xa0, 0x00, 0x00, 0x00});
551 rc = i2c_send(priv, p + 12 * scode, 12);
555 rc = send_seq(priv, {0x00, 0x8c});
562 static int check_firmware(struct dvb_frontend *fe, enum tuner_mode new_mode,
563 v4l2_std_id std, fe_bandwidth_t bandwidth)
565 struct xc2028_data *priv = fe->tuner_priv;
567 u16 version, hwmodel;
568 v4l2_std_id std0 = 0;
569 unsigned int type0 = 0, type = 0;
570 int change_digital_bandwidth;
572 tuner_dbg("%s called\n", __FUNCTION__);
575 if (!priv->ctrl.fname) {
576 tuner_info("xc2028/3028 firmware name not set!\n");
580 rc = load_all_firmwares(fe);
585 tuner_dbg("I am in mode %u and I should switch to mode %i\n",
586 priv->mode, new_mode);
588 /* first of all, determine whether we have switched the mode */
589 if (new_mode != priv->mode) {
590 priv->mode = new_mode;
591 priv->need_load_generic = 1;
594 change_digital_bandwidth = (priv->mode == T_DIGITAL_TV
595 && bandwidth != priv->bandwidth) ? 1 : 0;
596 tuner_dbg("old bandwidth %u, new bandwidth %u\n", priv->bandwidth,
599 if (priv->need_load_generic) {
600 /* Reset is needed before loading firmware */
601 rc = priv->tuner_callback(priv->video_dev,
602 XC2028_TUNER_RESET, 0);
608 if (priv->ctrl.type == XC2028_FIRM_MTS)
611 if (priv->bandwidth == 8)
614 /* FIXME: How to load FM and FM|INPUT1 firmwares? */
616 rc = load_firmware(fe, type0, &std0);
618 tuner_err("Error %d while loading generic firmware\n",
623 priv->need_load_generic = 0;
625 if (priv->mode == T_DIGITAL_TV)
626 change_digital_bandwidth = 1;
629 tuner_dbg("I should change bandwidth %u\n", change_digital_bandwidth);
631 if (change_digital_bandwidth) {
633 /*FIXME: Should allow selecting between D2620 and D2633 */
636 /* FIXME: When should select a DTV78 firmware?
639 case BANDWIDTH_8_MHZ:
642 case BANDWIDTH_7_MHZ:
645 case BANDWIDTH_6_MHZ:
646 /* FIXME: Should allow select also ATSC */
651 tuner_err("error: bandwidth not supported.\n");
653 priv->bandwidth = bandwidth;
656 if (!change_digital_bandwidth && priv->mode == T_DIGITAL_TV)
659 /* Load INIT1, if needed */
660 tuner_dbg("Load init1 firmware, if exists\n");
661 type0 = BASE | INIT1;
662 if (priv->ctrl.type == XC2028_FIRM_MTS)
665 /* FIXME: Should handle errors - if INIT1 found */
666 rc = load_firmware(fe, type0, &std0);
668 /* FIXME: Should add support for FM radio
671 if (priv->ctrl.type == XC2028_FIRM_MTS)
674 if (priv->firm_type & std) {
675 tuner_dbg("Std-specific firmware already loaded.\n");
679 /* Add audio hack to std mask */
680 std |= parse_audio_std_option();
682 rc = load_firmware(fe, type, &std);
686 /* Load SCODE firmware, if exists */
687 tuner_dbg("Trying to load scode 0\n");
690 rc = load_scode(fe, type, &std, 0);
692 xc2028_get_reg(priv, 0x0004, &version);
693 xc2028_get_reg(priv, 0x0008, &hwmodel);
695 tuner_info("Device is Xceive %d version %d.%d, "
696 "firmware version %d.%d\n",
697 hwmodel, (version & 0xf000) >> 12, (version & 0xf00) >> 8,
698 (version & 0xf0) >> 4, version & 0xf);
700 priv->firm_type = std;
705 static int xc2028_signal(struct dvb_frontend *fe, u16 *strength)
707 struct xc2028_data *priv = fe->tuner_priv;
708 u16 frq_lock, signal = 0;
711 tuner_dbg("%s called\n", __FUNCTION__);
713 mutex_lock(&priv->lock);
715 /* Sync Lock Indicator */
716 rc = xc2028_get_reg(priv, 0x0002, &frq_lock);
717 if (rc < 0 || frq_lock == 0)
720 /* Frequency is locked. Return signal quality */
722 /* Get SNR of the video signal */
723 rc = xc2028_get_reg(priv, 0x0040, &signal);
728 mutex_unlock(&priv->lock);
737 static int generic_set_tv_freq(struct dvb_frontend *fe, u32 freq /* in Hz */ ,
738 enum tuner_mode new_mode,
739 v4l2_std_id std, fe_bandwidth_t bandwidth)
741 struct xc2028_data *priv = fe->tuner_priv;
743 unsigned char buf[5];
746 tuner_dbg("%s called\n", __FUNCTION__);
748 mutex_lock(&priv->lock);
750 /* HACK: It seems that specific firmware need to be reloaded
751 when freq is changed */
756 rc = priv->tuner_callback(priv->video_dev, XC2028_TUNER_RESET, 0);
761 tuner_dbg("should set frequency %d kHz)\n", freq / 1000);
763 if (check_firmware(fe, new_mode, std, bandwidth) < 0)
766 if (new_mode == T_DIGITAL_TV)
769 div = (freq - offset + DIV / 2) / DIV;
771 /* CMD= Set frequency */
773 if (priv->firm_version < 0x0202)
774 rc = send_seq(priv, {0x00, 0x02, 0x00, 0x00});
776 rc = send_seq(priv, {0x80, 0x02, 0x00, 0x00});
780 rc = priv->tuner_callback(priv->video_dev, XC2028_RESET_CLK, 1);
786 buf[0] = 0xff & (div >> 24);
787 buf[1] = 0xff & (div >> 16);
788 buf[2] = 0xff & (div >> 8);
789 buf[3] = 0xff & (div);
792 rc = i2c_send(priv, buf, sizeof(buf));
797 priv->frequency = freq;
799 tuner_dbg("divider= %02x %02x %02x %02x (freq=%d.%02d)\n",
800 buf[1], buf[2], buf[3], buf[4],
801 freq / 1000000, (freq % 1000000) / 10000);
806 mutex_unlock(&priv->lock);
811 static int xc2028_set_tv_freq(struct dvb_frontend *fe,
812 struct analog_parameters *p)
814 struct xc2028_data *priv = fe->tuner_priv;
816 tuner_dbg("%s called\n", __FUNCTION__);
818 return generic_set_tv_freq(fe, 62500l * p->frequency, T_ANALOG_TV,
819 p->std, BANDWIDTH_8_MHZ /* NOT USED */);
822 static int xc2028_set_params(struct dvb_frontend *fe,
823 struct dvb_frontend_parameters *p)
825 struct xc2028_data *priv = fe->tuner_priv;
827 tuner_dbg("%s called\n", __FUNCTION__);
829 /* FIXME: Only OFDM implemented */
830 if (fe->ops.info.type != FE_OFDM) {
831 tuner_err("DTV type not implemented.\n");
835 return generic_set_tv_freq(fe, p->frequency, T_DIGITAL_TV,
837 p->u.ofdm.bandwidth);
841 static int xc2028_dvb_release(struct dvb_frontend *fe)
843 struct xc2028_data *priv = fe->tuner_priv;
845 tuner_dbg("%s called\n", __FUNCTION__);
847 mutex_lock(&xc2028_list_mutex);
852 list_del(&priv->xc2028_list);
854 kfree(priv->ctrl.fname);
858 fe->tuner_priv = NULL;
861 mutex_unlock(&xc2028_list_mutex);
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 mutex_lock(&priv->lock);
886 priv->ctrl.type = p->type;
889 kfree(priv->ctrl.fname);
891 priv->ctrl.fname = kmalloc(strlen(p->fname) + 1, GFP_KERNEL);
892 if (priv->ctrl.fname == NULL) {
893 mutex_unlock(&priv->lock);
898 strcpy(priv->ctrl.fname, p->fname);
902 priv->max_len = p->max_len;
904 mutex_unlock(&priv->lock);
909 static const struct dvb_tuner_ops xc2028_dvb_tuner_ops = {
911 .name = "Xceive XC3028",
912 .frequency_min = 42000000,
913 .frequency_max = 864000000,
914 .frequency_step = 50000,
917 .set_config = xc2028_set_config,
918 .set_analog_params = xc2028_set_tv_freq,
919 .release = xc2028_dvb_release,
920 .get_frequency = xc2028_get_frequency,
921 .get_rf_strength = xc2028_signal,
922 .set_params = xc2028_set_params,
926 void *xc2028_attach(struct dvb_frontend *fe, struct xc2028_config *cfg)
928 struct xc2028_data *priv;
932 printk(KERN_DEBUG PREFIX ": Xcv2028/3028 init called!\n");
934 if (NULL == cfg->video_dev)
938 printk(KERN_ERR PREFIX ": No frontend!\n");
942 video_dev = cfg->video_dev;
944 mutex_lock(&xc2028_list_mutex);
946 list_for_each_entry(priv, &xc2028_list, xc2028_list) {
947 if (priv->video_dev == cfg->video_dev) {
954 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
956 mutex_unlock(&xc2028_list_mutex);
960 priv->bandwidth = BANDWIDTH_6_MHZ;
961 priv->need_load_generic = 1;
962 priv->mode = T_UNINITIALIZED;
963 priv->i2c_props.addr = cfg->i2c_addr;
964 priv->i2c_props.adap = cfg->i2c_adap;
965 priv->video_dev = video_dev;
966 priv->tuner_callback = cfg->callback;
969 mutex_init(&priv->lock);
971 list_add_tail(&priv->xc2028_list, &xc2028_list);
974 fe->tuner_priv = priv;
977 memcpy(&fe->ops.tuner_ops, &xc2028_dvb_tuner_ops,
978 sizeof(xc2028_dvb_tuner_ops));
980 tuner_info("type set to %s\n", "XCeive xc2028/xc3028 tuner");
982 mutex_unlock(&xc2028_list_mutex);
987 EXPORT_SYMBOL(xc2028_attach);
989 MODULE_DESCRIPTION("Xceive xc2028/xc3028 tuner driver");
990 MODULE_AUTHOR("Michel Ludwig <michel.ludwig@gmail.com>");
991 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
992 MODULE_LICENSE("GPL");