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 i2c_send_recv(priv, obuf, osize, ibuf, isize) ({ \
106 _rc = tuner_i2c_xfer_send_recv(&priv->i2c_props, obuf, osize, \
109 tuner_err("i2c input error: rc = %d (should be %d)\n", \
114 #define send_seq(priv, data...) ({ \
115 static u8 _val[] = data; \
117 if (sizeof(_val) != \
118 (_rc = tuner_i2c_xfer_send(&priv->i2c_props, \
119 _val, sizeof(_val)))) { \
120 tuner_err("Error on line %d: %d\n", __LINE__, _rc); \
126 static unsigned int xc2028_get_reg(struct xc2028_data *priv, u16 reg, u16 *val)
128 unsigned char buf[2];
129 unsigned char ibuf[2];
131 tuner_dbg("%s %04x called\n", __FUNCTION__, reg);
134 buf[1] = (unsigned char) reg;
136 if (i2c_send_recv(priv, buf, 2, ibuf, 2) != 2)
139 *val = (ibuf[1]) | (ibuf[0] << 8);
143 void dump_firm_type(unsigned int type)
189 if (type & TOYOTA388)
190 printk("TOYOTA388 ");
191 if (type & TOYOTA794)
192 printk("TOYOTA794 ");
195 if (type & ZARLINK456)
196 printk("ZARLINK456 ");
207 static v4l2_std_id parse_audio_std_option(void)
209 if (strcasecmp(audio_std, "A2") == 0)
211 if (strcasecmp(audio_std, "A2/A") == 0)
212 return V4L2_STD_A2_A;
213 if (strcasecmp(audio_std, "A2/B") == 0)
214 return V4L2_STD_A2_B;
215 if (strcasecmp(audio_std, "NICAM") == 0)
216 return V4L2_STD_NICAM;
217 if (strcasecmp(audio_std, "NICAM/A") == 0)
218 return V4L2_STD_NICAM_A;
219 if (strcasecmp(audio_std, "NICAM/B") == 0)
220 return V4L2_STD_NICAM_B;
225 static void free_firmware(struct xc2028_data *priv)
232 for (i = 0; i < priv->firm_size; i++)
233 kfree(priv->firm[i].ptr);
238 priv->need_load_generic = 1;
241 static int load_all_firmwares(struct dvb_frontend *fe)
243 struct xc2028_data *priv = fe->tuner_priv;
244 const struct firmware *fw = NULL;
245 unsigned char *p, *endp;
250 tuner_dbg("%s called\n", __FUNCTION__);
252 tuner_info("Reading firmware %s\n", priv->ctrl.fname);
253 rc = request_firmware(&fw, priv->ctrl.fname,
254 &priv->i2c_props.adap->dev);
257 tuner_err("Error: firmware %s not found.\n",
260 tuner_err("Error %d while requesting firmware %s \n",
261 rc, priv->ctrl.fname);
268 if (fw->size < sizeof(name) - 1 + 2) {
269 tuner_err("Error: firmware size is zero!\n");
274 memcpy(name, p, sizeof(name) - 1);
275 name[sizeof(name) - 1] = 0;
276 p += sizeof(name) - 1;
278 priv->version = le16_to_cpu(*(__u16 *) p);
281 tuner_info("Firmware: %s, ver %d.%d\n", name,
282 priv->version >> 8, priv->version & 0xff);
287 n_array = le16_to_cpu(*(__u16 *) p);
290 tuner_info("There are %d firmwares at %s\n",
291 n_array, priv->ctrl.fname);
293 priv->firm = kzalloc(sizeof(*priv->firm) * n_array, GFP_KERNEL);
296 tuner_err("Not enough memory for reading firmware.\n");
301 priv->firm_size = n_array;
309 tuner_err("Too much firmwares at the file\n");
313 /* Checks if there's enough bytes to read */
314 if (p + sizeof(type) + sizeof(id) + sizeof(size) > endp) {
315 tuner_err("Firmware header is incomplete!\n");
319 type = le32_to_cpu(*(__u32 *) p);
322 id = le64_to_cpu(*(v4l2_std_id *) p);
325 size = le32_to_cpu(*(__u32 *) p);
328 if ((!size) || (size + p > endp)) {
329 tuner_err("Firmware type ");
330 dump_firm_type(type);
331 printk("(%x), id %llx is corrupted "
332 "(size=%d, expected %d)\n",
334 (unsigned)(endp - p), size);
338 priv->firm[n].ptr = kzalloc(size, GFP_KERNEL);
339 if (!priv->firm[n].ptr) {
340 tuner_err("Not enough memory.\n");
344 tuner_info("Reading firmware type ");
345 dump_firm_type(type);
346 printk("(%x), id %lx, size=%d.\n",
347 type, (unsigned long)id, size);
349 memcpy(priv->firm[n].ptr, p, size);
350 priv->firm[n].type = type;
351 priv->firm[n].id = id;
352 priv->firm[n].size = size;
357 if (n + 1 != priv->firm_size) {
358 tuner_err("Firmware file is incomplete!\n");
366 tuner_err("Error: firmware file is corrupted!\n");
369 tuner_info("Releasing loaded firmware file.\n");
374 release_firmware(fw);
375 tuner_dbg("Firmware files loaded.\n");
380 static int seek_firmware(struct dvb_frontend *fe, unsigned int type,
383 struct xc2028_data *priv = fe->tuner_priv;
386 tuner_dbg("%s called\n", __FUNCTION__);
389 tuner_err("Error! firmware not loaded\n");
393 if (((type & ~SCODE) == 0) && (*id == 0))
396 /* Seek for exact match */
397 for (i = 0; i < priv->firm_size; i++) {
398 if ((type == priv->firm[i].type) && (*id == priv->firm[i].id))
402 /* Seek for generic video standard match */
403 for (i = 0; i < priv->firm_size; i++) {
404 if ((type == priv->firm[i].type) && (*id & priv->firm[i].id))
408 /*FIXME: Would make sense to seek for type "hint" match ? */
414 *id = priv->firm[i].id;
417 tuner_dbg("%s firmware for type=", (i < 0)? "Can't find": "Found");
419 dump_firm_type(type);
420 printk("(%x), id %08lx.\n", type, (unsigned long)*id);
425 static int load_firmware(struct dvb_frontend *fe, unsigned int type,
428 struct xc2028_data *priv = fe->tuner_priv;
430 unsigned char *p, *endp, buf[priv->max_len];
432 tuner_dbg("%s called\n", __FUNCTION__);
434 pos = seek_firmware(fe, type, id);
438 tuner_info("Loading firmware for type=");
439 dump_firm_type(type);
440 printk("(%x), id %08lx.\n", type, (unsigned long)*id);
442 p = priv->firm[pos].ptr;
445 tuner_err("Firmware pointer were freed!");
448 endp = p + priv->firm[pos].size;
453 /* Checks if there's enough bytes to read */
454 if (p + sizeof(size) > endp) {
455 tuner_err("Firmware chunk size is wrong\n");
459 size = le16_to_cpu(*(__u16 *) p);
466 /* Special callback command received */
467 rc = priv->tuner_callback(priv->video_dev,
468 XC2028_TUNER_RESET, 0);
470 tuner_err("Error at RESET code %d\n",
476 if (size >= 0xff00) {
479 rc = priv->tuner_callback(priv->video_dev,
480 XC2028_RESET_CLK, 0);
482 tuner_err("Error at RESET code %d\n",
487 tuner_info("Invalid RESET code %d\n",
495 /* Checks for a sleep command */
497 msleep(size & 0x7fff);
501 if ((size + p > endp)) {
502 tuner_err("missing bytes: need %d, have %d\n",
503 size, (int)(endp - p));
511 /* Sends message chunks */
513 int len = (size < priv->max_len - 1) ?
514 size : priv->max_len - 1;
516 memcpy(buf + 1, p, len);
518 rc = i2c_send(priv, buf, len + 1);
520 tuner_err("%d returned from send\n", rc);
531 static int load_scode(struct dvb_frontend *fe, unsigned int type,
532 v4l2_std_id *id, int scode)
534 struct xc2028_data *priv = fe->tuner_priv;
538 tuner_dbg("%s called\n", __FUNCTION__);
540 pos = seek_firmware(fe, type, id);
544 p = priv->firm[pos].ptr;
547 tuner_err("Firmware pointer were freed!");
551 if ((priv->firm[pos].size != 12 * 16) || (scode >= 16))
554 if (priv->version < 0x0202)
555 rc = send_seq(priv, {0x20, 0x00, 0x00, 0x00});
557 rc = send_seq(priv, {0xa0, 0x00, 0x00, 0x00});
561 rc = i2c_send(priv, p + 12 * scode, 12);
565 rc = send_seq(priv, {0x00, 0x8c});
572 static int check_firmware(struct dvb_frontend *fe, enum tuner_mode new_mode,
573 v4l2_std_id std, fe_bandwidth_t bandwidth)
575 struct xc2028_data *priv = fe->tuner_priv;
577 u16 version, hwmodel;
578 v4l2_std_id std0 = 0;
579 unsigned int type0 = 0, type = 0;
580 int change_digital_bandwidth;
582 tuner_dbg("%s called\n", __FUNCTION__);
585 if (!priv->ctrl.fname) {
586 tuner_info("xc2028/3028 firmware name not set!\n");
590 rc = load_all_firmwares(fe);
595 tuner_dbg("I am in mode %u and I should switch to mode %i\n",
596 priv->mode, new_mode);
598 /* first of all, determine whether we have switched the mode */
599 if (new_mode != priv->mode) {
600 priv->mode = new_mode;
601 priv->need_load_generic = 1;
604 change_digital_bandwidth = (priv->mode == T_DIGITAL_TV
605 && bandwidth != priv->bandwidth) ? 1 : 0;
606 tuner_dbg("old bandwidth %u, new bandwidth %u\n", priv->bandwidth,
609 if (priv->need_load_generic) {
610 /* Reset is needed before loading firmware */
611 rc = priv->tuner_callback(priv->video_dev,
612 XC2028_TUNER_RESET, 0);
618 if (priv->ctrl.type == XC2028_FIRM_MTS)
621 if (priv->bandwidth == 8)
624 /* FIXME: How to load FM and FM|INPUT1 firmwares? */
626 rc = load_firmware(fe, type0, &std0);
628 tuner_err("Error %d while loading generic firmware\n",
633 priv->need_load_generic = 0;
635 if (priv->mode == T_DIGITAL_TV)
636 change_digital_bandwidth = 1;
639 tuner_dbg("I should change bandwidth %u\n", change_digital_bandwidth);
641 if (change_digital_bandwidth) {
643 /*FIXME: Should allow selecting between D2620 and D2633 */
646 /* FIXME: When should select a DTV78 firmware?
649 case BANDWIDTH_8_MHZ:
652 case BANDWIDTH_7_MHZ:
655 case BANDWIDTH_6_MHZ:
656 /* FIXME: Should allow select also ATSC */
661 tuner_err("error: bandwidth not supported.\n");
663 priv->bandwidth = bandwidth;
666 if (!change_digital_bandwidth && priv->mode == T_DIGITAL_TV)
669 /* Load INIT1, if needed */
670 tuner_dbg("Load init1 firmware, if exists\n");
671 type0 = BASE | INIT1;
672 if (priv->ctrl.type == XC2028_FIRM_MTS)
675 /* FIXME: Should handle errors - if INIT1 found */
676 rc = load_firmware(fe, type0, &std0);
678 /* FIXME: Should add support for FM radio
681 if (priv->ctrl.type == XC2028_FIRM_MTS)
684 if (priv->firm_type & std) {
685 tuner_dbg("Std-specific firmware already loaded.\n");
689 /* Add audio hack to std mask */
690 std |= parse_audio_std_option();
692 rc = load_firmware(fe, type, &std);
696 /* Load SCODE firmware, if exists */
697 tuner_dbg("Trying to load scode 0\n");
700 rc = load_scode(fe, type, &std, 0);
702 xc2028_get_reg(priv, 0x0004, &version);
703 xc2028_get_reg(priv, 0x0008, &hwmodel);
705 tuner_info("Device is Xceive %d version %d.%d, "
706 "firmware version %d.%d\n",
707 hwmodel, (version & 0xf000) >> 12, (version & 0xf00) >> 8,
708 (version & 0xf0) >> 4, version & 0xf);
710 priv->firm_type = std;
715 static int xc2028_signal(struct dvb_frontend *fe, u16 *strength)
717 struct xc2028_data *priv = fe->tuner_priv;
718 u16 frq_lock, signal = 0;
721 tuner_dbg("%s called\n", __FUNCTION__);
723 mutex_lock(&priv->lock);
725 /* Sync Lock Indicator */
726 rc = xc2028_get_reg(priv, 0x0002, &frq_lock);
727 if (rc < 0 || frq_lock == 0)
730 /* Frequency is locked. Return signal quality */
732 /* Get SNR of the video signal */
733 rc = xc2028_get_reg(priv, 0x0040, &signal);
738 mutex_unlock(&priv->lock);
747 static int generic_set_tv_freq(struct dvb_frontend *fe, u32 freq /* in Hz */ ,
748 enum tuner_mode new_mode,
749 v4l2_std_id std, fe_bandwidth_t bandwidth)
751 struct xc2028_data *priv = fe->tuner_priv;
753 unsigned char buf[5];
756 tuner_dbg("%s called\n", __FUNCTION__);
758 mutex_lock(&priv->lock);
760 /* HACK: It seems that specific firmware need to be reloaded
761 when freq is changed */
766 rc = priv->tuner_callback(priv->video_dev, XC2028_TUNER_RESET, 0);
771 tuner_dbg("should set frequency %d kHz)\n", freq / 1000);
773 if (check_firmware(fe, new_mode, std, bandwidth) < 0)
776 if (new_mode == T_DIGITAL_TV)
779 div = (freq - offset + DIV / 2) / DIV;
781 /* CMD= Set frequency */
783 if (priv->version < 0x0202)
784 rc = send_seq(priv, {0x00, 0x02, 0x00, 0x00});
786 rc = send_seq(priv, {0x80, 0x02, 0x00, 0x00});
790 rc = priv->tuner_callback(priv->video_dev, XC2028_RESET_CLK, 1);
796 buf[0] = 0xff & (div >> 24);
797 buf[1] = 0xff & (div >> 16);
798 buf[2] = 0xff & (div >> 8);
799 buf[3] = 0xff & (div);
802 rc = i2c_send(priv, buf, sizeof(buf));
807 priv->frequency = freq;
809 tuner_dbg("divider= %02x %02x %02x %02x (freq=%d.%02d)\n",
810 buf[1], buf[2], buf[3], buf[4],
811 freq / 1000000, (freq % 1000000) / 10000);
816 mutex_unlock(&priv->lock);
821 static int xc2028_set_tv_freq(struct dvb_frontend *fe,
822 struct analog_parameters *p)
824 struct xc2028_data *priv = fe->tuner_priv;
826 tuner_dbg("%s called\n", __FUNCTION__);
828 return generic_set_tv_freq(fe, 62500l * p->frequency, T_ANALOG_TV,
829 p->std, BANDWIDTH_8_MHZ /* NOT USED */);
832 static int xc2028_set_params(struct dvb_frontend *fe,
833 struct dvb_frontend_parameters *p)
835 struct xc2028_data *priv = fe->tuner_priv;
837 tuner_dbg("%s called\n", __FUNCTION__);
839 /* FIXME: Only OFDM implemented */
840 if (fe->ops.info.type != FE_OFDM) {
841 tuner_err("DTV type not implemented.\n");
845 return generic_set_tv_freq(fe, p->frequency, T_DIGITAL_TV,
847 p->u.ofdm.bandwidth);
851 static int xc2028_dvb_release(struct dvb_frontend *fe)
853 struct xc2028_data *priv = fe->tuner_priv;
855 tuner_dbg("%s called\n", __FUNCTION__);
860 list_del(&priv->xc2028_list);
862 kfree(priv->ctrl.fname);
871 static int xc2028_get_frequency(struct dvb_frontend *fe, u32 *frequency)
873 struct xc2028_data *priv = fe->tuner_priv;
875 tuner_dbg("%s called\n", __FUNCTION__);
877 *frequency = priv->frequency;
882 static int xc2028_set_config(struct dvb_frontend *fe, void *priv_cfg)
884 struct xc2028_data *priv = fe->tuner_priv;
885 struct xc2028_ctrl *p = priv_cfg;
887 tuner_dbg("%s called\n", __FUNCTION__);
889 priv->ctrl.type = p->type;
892 kfree(priv->ctrl.fname);
894 priv->ctrl.fname = kmalloc(strlen(p->fname) + 1, GFP_KERNEL);
895 if (!priv->ctrl.fname)
899 strcpy(priv->ctrl.fname, p->fname);
903 priv->max_len = p->max_len;
908 static const struct dvb_tuner_ops xc2028_dvb_tuner_ops = {
910 .name = "Xceive XC3028",
911 .frequency_min = 42000000,
912 .frequency_max = 864000000,
913 .frequency_step = 50000,
916 .set_config = xc2028_set_config,
917 .set_analog_params = xc2028_set_tv_freq,
918 .release = xc2028_dvb_release,
919 .get_frequency = xc2028_get_frequency,
920 .get_rf_strength = xc2028_signal,
921 .set_params = xc2028_set_params,
925 void *xc2028_attach(struct dvb_frontend *fe, struct xc2028_config *cfg)
927 struct xc2028_data *priv;
931 printk(KERN_DEBUG PREFIX ": Xcv2028/3028 init called!\n");
933 if (NULL == cfg->video_dev)
937 printk(KERN_ERR PREFIX ": No frontend!\n");
941 video_dev = cfg->video_dev;
943 list_for_each_entry(priv, &xc2028_list, xc2028_list) {
944 if (priv->video_dev == cfg->video_dev) {
951 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
955 priv->bandwidth = BANDWIDTH_6_MHZ;
956 priv->need_load_generic = 1;
957 priv->mode = T_UNINITIALIZED;
958 priv->i2c_props.addr = cfg->i2c_addr;
959 priv->i2c_props.adap = cfg->i2c_adap;
960 priv->video_dev = video_dev;
961 priv->tuner_callback = cfg->callback;
964 mutex_init(&priv->lock);
966 list_add_tail(&priv->xc2028_list, &xc2028_list);
969 fe->tuner_priv = priv;
972 memcpy(&fe->ops.tuner_ops, &xc2028_dvb_tuner_ops,
973 sizeof(xc2028_dvb_tuner_ops));
975 tuner_info("type set to %s\n", "XCeive xc2028/xc3028 tuner");
980 EXPORT_SYMBOL(xc2028_attach);
982 MODULE_DESCRIPTION("Xceive xc2028/xc3028 tuner driver");
983 MODULE_AUTHOR("Michel Ludwig <michel.ludwig@gmail.com>");
984 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
985 MODULE_LICENSE("GPL");