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"
25 #define PREFIX "xc2028"
28 module_param(debug, int, 0644);
29 MODULE_PARM_DESC(debug, "enable verbose debug messages");
31 static char audio_std[8];
32 module_param_string(audio_std, audio_std, sizeof(audio_std), 0);
33 MODULE_PARM_DESC(audio_std,
34 "Audio standard. XC3028 audio decoder explicitly "
35 "needs to know what audio\n"
36 "standard is needed for some video standards with audio A2 or NICAM.\n"
37 "The valid values are:\n"
45 static LIST_HEAD(xc2028_list);
46 /* struct for storing firmware table */
47 struct firmware_description {
55 struct list_head xc2028_list;
56 struct tuner_i2c_props i2c_props;
57 int (*tuner_callback) (void *dev,
58 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(rc, priv, buf, size) do { \
87 rc = tuner_i2c_xfer_send(&priv->i2c_props, buf, size); \
89 tuner_err("i2c output error: rc = %d (should be %d)\n", \
93 #define i2c_rcv(rc, priv, buf, size) do { \
94 rc = tuner_i2c_xfer_recv(&priv->i2c_props, buf, size); \
96 tuner_err("i2c input error: rc = %d (should be %d)\n", \
100 #define send_seq(priv, data...) do { \
102 static u8 _val[] = data; \
103 if (sizeof(_val) != \
104 (rc = tuner_i2c_xfer_send(&priv->i2c_props, \
105 _val, sizeof(_val)))) { \
106 tuner_err("Error on line %d: %d\n", __LINE__, rc); \
112 static unsigned int xc2028_get_reg(struct xc2028_data *priv, u16 reg)
115 unsigned char buf[2];
117 tuner_dbg("%s called\n", __FUNCTION__);
120 buf[1] = (unsigned char) reg;
122 i2c_send(rc, priv, buf, 2);
126 i2c_rcv(rc, priv, buf, 2);
130 return (buf[1]) | (buf[0] << 8);
133 void dump_firm_type(unsigned int type)
179 if (type & TOYOTA388)
180 printk("TOYOTA388 ");
181 if (type & TOYOTA794)
182 printk("TOYOTA794 ");
185 if (type & ZARLINK456)
186 printk("ZARLINK456 ");
197 static v4l2_std_id parse_audio_std_option(void)
199 if (strcasecmp(audio_std, "A2"))
201 if (strcasecmp(audio_std, "A2/A"))
202 return V4L2_STD_A2_A;
203 if (strcasecmp(audio_std, "A2/B"))
204 return V4L2_STD_A2_B;
205 if (strcasecmp(audio_std, "NICAM"))
206 return V4L2_STD_NICAM;
207 if (strcasecmp(audio_std, "NICAM/A"))
208 return V4L2_STD_NICAM_A;
209 if (strcasecmp(audio_std, "NICAM/B"))
210 return V4L2_STD_NICAM_B;
215 static void free_firmware(struct xc2028_data *priv)
222 for (i = 0; i < priv->firm_size; i++)
223 kfree(priv->firm[i].ptr);
228 priv->need_load_generic = 1;
231 static int load_all_firmwares(struct dvb_frontend *fe)
233 struct xc2028_data *priv = fe->tuner_priv;
234 const struct firmware *fw = NULL;
235 unsigned char *p, *endp;
240 tuner_dbg("%s called\n", __FUNCTION__);
242 tuner_info("Reading firmware %s\n", priv->ctrl.fname);
243 rc = request_firmware(&fw, priv->ctrl.fname, priv->dev);
246 tuner_err("Error: firmware %s not found.\n",
249 tuner_err("Error %d while requesting firmware %s \n",
250 rc, priv->ctrl.fname);
257 if (fw->size < sizeof(name) - 1 + 2) {
258 tuner_err("Error: firmware size is zero!\n");
263 memcpy(name, p, sizeof(name) - 1);
264 name[sizeof(name) - 1] = 0;
265 p += sizeof(name) - 1;
267 priv->version = le16_to_cpu(*(__u16 *) p);
270 tuner_info("Firmware: %s, ver %d.%d\n", name,
271 priv->version >> 8, priv->version & 0xff);
276 n_array = le16_to_cpu(*(__u16 *) p);
279 tuner_info("There are %d firmwares at %s\n",
280 n_array, priv->ctrl.fname);
282 priv->firm = kzalloc(sizeof(*priv->firm) * n_array, GFP_KERNEL);
285 tuner_err("Not enough memory for reading firmware.\n");
290 priv->firm_size = n_array;
298 tuner_err("Too much firmwares at the file\n");
302 /* Checks if there's enough bytes to read */
303 if (p + sizeof(type) + sizeof(id) + sizeof(size) > endp) {
304 tuner_err("Firmware header is incomplete!\n");
308 type = le32_to_cpu(*(__u32 *) p);
311 id = le64_to_cpu(*(v4l2_std_id *) p);
314 size = le32_to_cpu(*(v4l2_std_id *) p);
317 if ((!size) || (size + p > endp)) {
318 tuner_err("Firmware type ");
319 dump_firm_type(type);
320 printk("(%x), id %lx is corrupted "
321 "(size=%ld, expected %d)\n",
322 type, (unsigned long)id, endp - p, size);
326 priv->firm[n].ptr = kzalloc(size, GFP_KERNEL);
327 if (!priv->firm[n].ptr) {
328 tuner_err("Not enough memory.\n");
332 tuner_info("Reading firmware type ");
333 dump_firm_type(type);
334 printk("(%x), id %lx, size=%d.\n",
335 type, (unsigned long)id, size);
337 memcpy(priv->firm[n].ptr, p, size);
338 priv->firm[n].type = type;
339 priv->firm[n].id = id;
340 priv->firm[n].size = size;
345 if (n + 1 != priv->firm_size) {
346 tuner_err("Firmware file is incomplete!\n");
354 tuner_err("Error: firmware file is corrupted!\n");
357 tuner_info("Releasing loaded firmware file.\n");
362 release_firmware(fw);
363 tuner_dbg("Firmware files loaded.\n");
368 static int seek_firmware(struct dvb_frontend *fe, unsigned int type,
371 struct xc2028_data *priv = fe->tuner_priv;
374 tuner_dbg("%s called\n", __FUNCTION__);
377 tuner_err("Error! firmware not loaded\n");
381 if (((type & ~SCODE) == 0) && (*id == 0))
384 /* Seek for exact match */
385 for (i = 0; i < priv->firm_size; i++) {
386 if ((type == priv->firm[i].type) && (*id == priv->firm[i].id))
390 /* Seek for generic video standard match */
391 for (i = 0; i < priv->firm_size; i++) {
392 if ((type == priv->firm[i].type) && (*id & priv->firm[i].id))
396 /*FIXME: Would make sense to seek for type "hint" match ? */
402 *id = priv->firm[i].id;
405 tuner_dbg("%s firmware for type=", (i < 0)? "Can't find": "Found");
407 dump_firm_type(type);
408 printk("(%x), id %08lx.\n", type, (unsigned long)*id);
413 static int load_firmware(struct dvb_frontend *fe, unsigned int type,
416 struct xc2028_data *priv = fe->tuner_priv;
418 unsigned char *p, *endp, buf[priv->max_len];
420 tuner_dbg("%s called\n", __FUNCTION__);
422 pos = seek_firmware(fe, type, id);
426 tuner_info("Loading firmware for type=");
427 dump_firm_type(type);
428 printk("(%x), id %08lx.\n", type, (unsigned long)*id);
430 p = priv->firm[pos].ptr;
433 tuner_err("Firmware pointer were freed!");
436 endp = p + priv->firm[pos].size;
441 /* Checks if there's enough bytes to read */
442 if (p + sizeof(size) > endp) {
443 tuner_err("Firmware chunk size is wrong\n");
447 size = le16_to_cpu(*(__u16 *) p);
454 /* Special callback command received */
455 rc = priv->tuner_callback(priv->video_dev,
456 XC2028_TUNER_RESET, 0);
458 tuner_err("Error at RESET code %d\n",
465 /* Checks for a sleep command */
467 msleep(size & 0x7fff);
471 if ((size + p > endp)) {
472 tuner_err("missing bytes: need %d, have %d\n",
473 size, (int)(endp - p));
481 /* Sends message chunks */
483 int len = (size < priv->max_len - 1) ?
484 size : priv->max_len - 1;
486 memcpy(buf + 1, p, len);
488 i2c_send(rc, priv, buf, len + 1);
490 tuner_err("%d returned from send\n", rc);
501 static int load_scode(struct dvb_frontend *fe, unsigned int type,
502 v4l2_std_id *id, int scode)
504 struct xc2028_data *priv = fe->tuner_priv;
508 tuner_dbg("%s called\n", __FUNCTION__);
510 pos = seek_firmware(fe, type, id);
514 p = priv->firm[pos].ptr;
517 tuner_err("Firmware pointer were freed!");
521 if ((priv->firm[pos].size != 12 * 16) || (scode >= 16))
524 if (priv->version < 0x0202) {
525 send_seq(priv, {0x20, 0x00, 0x00, 0x00});
527 send_seq(priv, {0xa0, 0x00, 0x00, 0x00});
530 i2c_send(rc, priv, p + 12 * scode, 12);
532 send_seq(priv, {0x00, 0x8c});
537 static int check_firmware(struct dvb_frontend *fe, enum tuner_mode new_mode,
538 v4l2_std_id std, fe_bandwidth_t bandwidth)
540 struct xc2028_data *priv = fe->tuner_priv;
541 int rc, version, hwmodel;
542 v4l2_std_id std0 = 0;
543 unsigned int type0 = 0, type = 0;
544 int change_digital_bandwidth;
546 tuner_dbg("%s called\n", __FUNCTION__);
549 if (!priv->ctrl.fname)
552 rc = load_all_firmwares(fe);
557 tuner_dbg("I am in mode %u and I should switch to mode %i\n",
558 priv->mode, new_mode);
560 /* first of all, determine whether we have switched the mode */
561 if (new_mode != priv->mode) {
562 priv->mode = new_mode;
563 priv->need_load_generic = 1;
566 change_digital_bandwidth = (priv->mode == T_DIGITAL_TV
567 && bandwidth != priv->bandwidth) ? 1 : 0;
568 tuner_dbg("old bandwidth %u, new bandwidth %u\n", priv->bandwidth,
571 if (priv->need_load_generic) {
572 /* Reset is needed before loading firmware */
573 rc = priv->tuner_callback(priv->video_dev,
574 XC2028_TUNER_RESET, 0);
580 if (priv->ctrl.type == XC2028_FIRM_MTS)
583 if (priv->bandwidth == 8)
586 /* FIXME: How to load FM and FM|INPUT1 firmwares? */
588 rc = load_firmware(fe, type0, &std0);
590 tuner_err("Error %d while loading generic firmware\n",
595 priv->need_load_generic = 0;
597 if (priv->mode == T_DIGITAL_TV)
598 change_digital_bandwidth = 1;
601 tuner_dbg("I should change bandwidth %u\n", change_digital_bandwidth);
603 if (change_digital_bandwidth) {
605 /*FIXME: Should allow selecting between D2620 and D2633 */
608 /* FIXME: When should select a DTV78 firmware?
611 case BANDWIDTH_8_MHZ:
614 case BANDWIDTH_7_MHZ:
617 case BANDWIDTH_6_MHZ:
618 /* FIXME: Should allow select also ATSC */
623 tuner_err("error: bandwidth not supported.\n");
625 priv->bandwidth = bandwidth;
628 /* Load INIT1, if needed */
629 tuner_dbg("Load init1 firmware, if exists\n");
630 type0 = BASE | INIT1;
631 if (priv->ctrl.type == XC2028_FIRM_MTS)
634 /* FIXME: Should handle errors - if INIT1 found */
635 rc = load_firmware(fe, type0, &std0);
637 /* FIXME: Should add support for FM radio
640 if (priv->ctrl.type == XC2028_FIRM_MTS)
643 if (priv->firm_type & std) {
644 tuner_dbg("Std-specific firmware already loaded.\n");
648 /* Add audio hack to std mask */
649 std |= parse_audio_std_option();
651 rc = load_firmware(fe, type, &std);
655 /* Load SCODE firmware, if exists */
656 tuner_dbg("Trying to load scode 0\n");
659 rc = load_scode(fe, type, &std, 0);
661 version = xc2028_get_reg(priv, 0x0004);
662 hwmodel = xc2028_get_reg(priv, 0x0008);
664 tuner_info("Device is Xceive %d version %d.%d, "
665 "firmware version %d.%d\n",
666 hwmodel, (version & 0xf000) >> 12, (version & 0xf00) >> 8,
667 (version & 0xf0) >> 4, version & 0xf);
669 priv->firm_type = std;
674 static int xc2028_signal(struct dvb_frontend *fe, u16 *strength)
676 struct xc2028_data *priv = fe->tuner_priv;
677 int frq_lock, signal = 0;
679 tuner_dbg("%s called\n", __FUNCTION__);
681 mutex_lock(&priv->lock);
685 /* Sync Lock Indicator */
686 frq_lock = xc2028_get_reg(priv, 0x0002);
690 /* Frequency is locked. Return signal quality */
692 /* Get SNR of the video signal */
693 signal = xc2028_get_reg(priv, 0x0040);
699 mutex_unlock(&priv->lock);
708 static int generic_set_tv_freq(struct dvb_frontend *fe, u32 freq /* in Hz */ ,
709 enum tuner_mode new_mode,
710 v4l2_std_id std, fe_bandwidth_t bandwidth)
712 struct xc2028_data *priv = fe->tuner_priv;
714 unsigned char buf[5];
717 tuner_dbg("%s called\n", __FUNCTION__);
719 mutex_lock(&priv->lock);
721 /* HACK: It seems that specific firmware need to be reloaded
722 when freq is changed */
727 rc = priv->tuner_callback(priv->video_dev, XC2028_TUNER_RESET, 0);
732 tuner_dbg("should set frequency %d kHz)\n", freq / 1000);
734 if (check_firmware(fe, new_mode, std, bandwidth) < 0)
737 if (new_mode == T_DIGITAL_TV)
740 div = (freq - offset + DIV / 2) / DIV;
742 /* CMD= Set frequency */
744 if (priv->version < 0x0202) {
745 send_seq(priv, {0x00, 0x02, 0x00, 0x00});
747 send_seq(priv, {0x80, 0x02, 0x00, 0x00});
750 rc = priv->tuner_callback(priv->video_dev, XC2028_RESET_CLK, 1);
756 buf[0] = 0xff & (div >> 24);
757 buf[1] = 0xff & (div >> 16);
758 buf[2] = 0xff & (div >> 8);
759 buf[3] = 0xff & (div);
762 i2c_send(rc, priv, buf, sizeof(buf));
767 priv->frequency = freq;
769 tuner_dbg("divider= %02x %02x %02x %02x (freq=%d.%02d)\n",
770 buf[1], buf[2], buf[3], buf[4],
771 freq / 1000000, (freq % 1000000) / 10000);
776 mutex_unlock(&priv->lock);
781 static int xc2028_set_tv_freq(struct dvb_frontend *fe,
782 struct analog_parameters *p)
784 struct xc2028_data *priv = fe->tuner_priv;
786 tuner_dbg("%s called\n", __FUNCTION__);
788 return generic_set_tv_freq(fe, 62500l * p->frequency, T_ANALOG_TV,
789 p->std, BANDWIDTH_8_MHZ /* NOT USED */);
792 static int xc2028_set_params(struct dvb_frontend *fe,
793 struct dvb_frontend_parameters *p)
795 struct xc2028_data *priv = fe->tuner_priv;
797 tuner_dbg("%s called\n", __FUNCTION__);
799 /* FIXME: Only OFDM implemented */
800 if (fe->ops.info.type != FE_OFDM) {
801 tuner_err("DTV type not implemented.\n");
805 return generic_set_tv_freq(fe, p->frequency, T_DIGITAL_TV,
807 p->u.ofdm.bandwidth);
811 static int xc2028_dvb_release(struct dvb_frontend *fe)
813 struct xc2028_data *priv = fe->tuner_priv;
815 tuner_dbg("%s called\n", __FUNCTION__);
820 list_del(&priv->xc2028_list);
822 kfree(priv->ctrl.fname);
831 static int xc2028_get_frequency(struct dvb_frontend *fe, u32 *frequency)
833 struct xc2028_data *priv = fe->tuner_priv;
835 tuner_dbg("%s called\n", __FUNCTION__);
837 *frequency = priv->frequency;
842 static int xc2028_set_config(struct dvb_frontend *fe, void *priv_cfg)
844 struct xc2028_data *priv = fe->tuner_priv;
845 struct xc2028_ctrl *p = priv_cfg;
847 tuner_dbg("%s called\n", __FUNCTION__);
849 priv->ctrl.type = p->type;
852 kfree(priv->ctrl.fname);
854 priv->ctrl.fname = kmalloc(strlen(p->fname) + 1, GFP_KERNEL);
855 if (!priv->ctrl.fname)
859 strcpy(priv->ctrl.fname, p->fname);
863 priv->max_len = p->max_len;
868 static const struct dvb_tuner_ops xc2028_dvb_tuner_ops = {
870 .name = "Xceive XC3028",
871 .frequency_min = 42000000,
872 .frequency_max = 864000000,
873 .frequency_step = 50000,
876 .set_config = xc2028_set_config,
877 .set_analog_params = xc2028_set_tv_freq,
878 .release = xc2028_dvb_release,
879 .get_frequency = xc2028_get_frequency,
880 .get_rf_strength = xc2028_signal,
881 .set_params = xc2028_set_params,
885 int xc2028_attach(struct dvb_frontend *fe, struct i2c_adapter *i2c_adap,
886 u8 i2c_addr, struct device *dev, void *video_dev,
887 int (*tuner_callback) (void *dev, int command, int arg))
889 struct xc2028_data *priv;
892 printk(KERN_DEBUG PREFIX "Xcv2028/3028 init called!\n");
897 if (NULL == video_dev)
900 if (!tuner_callback) {
901 printk(KERN_ERR PREFIX "No tuner callback!\n");
905 list_for_each_entry(priv, &xc2028_list, xc2028_list) {
906 if (priv->dev == dev)
911 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
915 fe->tuner_priv = priv;
917 priv->bandwidth = BANDWIDTH_6_MHZ;
918 priv->need_load_generic = 1;
919 priv->mode = T_UNINITIALIZED;
920 priv->i2c_props.addr = i2c_addr;
921 priv->i2c_props.adap = i2c_adap;
923 priv->video_dev = video_dev;
924 priv->tuner_callback = tuner_callback;
928 mutex_init(&priv->lock);
930 list_add_tail(&priv->xc2028_list, &xc2028_list);
934 memcpy(&fe->ops.tuner_ops, &xc2028_dvb_tuner_ops,
935 sizeof(xc2028_dvb_tuner_ops));
937 tuner_info("type set to %s\n", "XCeive xc2028/xc3028 tuner");
941 EXPORT_SYMBOL(xc2028_attach);
943 MODULE_DESCRIPTION("Xceive xc2028/xc3028 tuner driver");
944 MODULE_AUTHOR("Michel Ludwig <michel.ludwig@gmail.com>");
945 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
946 MODULE_LICENSE("GPL");