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"
27 static LIST_HEAD(xc2028_list);
28 /* struct for storing firmware table */
29 struct firmware_description {
37 struct list_head xc2028_list;
38 struct tuner_i2c_props i2c_props;
39 int (*tuner_callback) (void *dev,
40 int command, int arg);
46 struct firmware_description *firm;
51 struct xc2028_ctrl ctrl;
53 v4l2_std_id firm_type; /* video stds supported
54 by current firmware */
55 fe_bandwidth_t bandwidth; /* Firmware bandwidth:
57 int need_load_generic; /* The generic firmware
60 int max_len; /* Max firmware chunk */
63 struct i2c_client *i2c_client;
68 #define i2c_send(rc, priv, buf, size) do { \
69 rc = tuner_i2c_xfer_send(&priv->i2c_props, buf, size); \
71 tuner_info("i2c output error: rc = %d (should be %d)\n",\
75 #define i2c_rcv(rc, priv, buf, size) do { \
76 rc = tuner_i2c_xfer_recv(&priv->i2c_props, buf, size); \
78 tuner_info("i2c input error: rc = %d (should be %d)\n", \
82 #define send_seq(priv, data...) do { \
84 static u8 _val[] = data; \
86 (rc = tuner_i2c_xfer_send(&priv->i2c_props, \
87 _val, sizeof(_val)))) { \
88 tuner_info("Error on line %d: %d\n", __LINE__, rc); \
94 static unsigned int xc2028_get_reg(struct xc2028_data *priv, u16 reg)
99 tuner_info("%s called\n", __FUNCTION__);
102 buf[1] = (unsigned char) reg;
104 i2c_send(rc, priv, buf, 2);
108 i2c_rcv(rc, priv, buf, 2);
112 return (buf[1]) | (buf[0] << 8);
115 void dump_firm_type(unsigned int type)
161 if (type & TOYOTA388)
162 printk("TOYOTA388 ");
163 if (type & TOYOTA794)
164 printk("TOYOTA794 ");
167 if (type & ZARLINK456)
168 printk("ZARLINK456 ");
179 static void free_firmware(struct xc2028_data *priv)
186 for (i = 0; i < priv->firm_size; i++)
187 kfree(priv->firm[i].ptr);
192 priv->need_load_generic = 1;
195 static int load_all_firmwares(struct dvb_frontend *fe)
197 struct xc2028_data *priv = fe->tuner_priv;
198 const struct firmware *fw = NULL;
199 unsigned char *p, *endp;
204 tuner_info("%s called\n", __FUNCTION__);
206 tuner_info("Reading firmware %s\n", priv->ctrl.fname);
207 rc = request_firmware(&fw, priv->ctrl.fname, priv->dev);
210 tuner_info("Error: firmware %s not found.\n",
213 tuner_info("Error %d while requesting firmware %s \n",
214 rc, priv->ctrl.fname);
221 if (fw->size < sizeof(name) - 1 + 2) {
222 tuner_info("Error: firmware size is zero!\n");
227 memcpy(name, p, sizeof(name) - 1);
228 name[sizeof(name) - 1] = 0;
229 p += sizeof(name) - 1;
231 priv->version = le16_to_cpu(*(__u16 *) p);
234 tuner_info("firmware: %s, ver %d.%d\n", name,
235 priv->version >> 8, priv->version & 0xff);
240 n_array = le16_to_cpu(*(__u16 *) p);
243 tuner_info("there are %d firmwares at %s\n", n_array, priv->ctrl.fname);
245 priv->firm = kzalloc(sizeof(*priv->firm) * n_array, GFP_KERNEL);
248 tuner_info("Not enough memory for loading firmware.\n");
253 priv->firm_size = n_array;
261 tuner_info("Too much firmwares at the file\n");
265 /* Checks if there's enough bytes to read */
266 if (p + sizeof(type) + sizeof(id) + sizeof(size) > endp) {
267 tuner_info("Lost firmware!\n");
271 type = le32_to_cpu(*(__u32 *) p);
274 id = le64_to_cpu(*(v4l2_std_id *) p);
277 size = le32_to_cpu(*(v4l2_std_id *) p);
280 if ((!size) || (size + p > endp)) {
281 tuner_info("Firmware type ");
282 dump_firm_type(type);
283 printk("(%x), id %lx corrupt (size=%ld, expected %d)\n",
284 type, (unsigned long)id, endp - p, size);
288 priv->firm[n].ptr = kzalloc(size, GFP_KERNEL);
289 if (!priv->firm[n].ptr) {
290 tuner_info("Not enough memory.\n");
294 tuner_info("Loading firmware type ");
295 dump_firm_type(type);
296 printk("(%x), id %lx, size=%d.\n",
297 type, (unsigned long)id, size);
299 memcpy(priv->firm[n].ptr, p, size);
300 priv->firm[n].type = type;
301 priv->firm[n].id = id;
302 priv->firm[n].size = size;
307 if (n + 1 != priv->firm_size) {
308 tuner_info("Firmware file is incomplete!\n");
316 tuner_info("Error: firmware file is corrupted!\n");
319 tuner_info("Releasing loaded firmware file.\n");
324 release_firmware(fw);
325 tuner_info("Firmware files loaded.\n");
330 static int seek_firmware(struct dvb_frontend *fe, unsigned int type,
333 struct xc2028_data *priv = fe->tuner_priv;
336 tuner_info("%s called\n", __FUNCTION__);
339 printk(KERN_ERR PREFIX "Error! firmware not loaded\n");
343 if (((type & ~SCODE) == 0) && (*id == 0))
346 /* Seek for exact match */
347 for (i = 0; i < priv->firm_size; i++) {
348 if ((type == priv->firm[i].type) && (*id == priv->firm[i].id))
352 /* Seek for generic video standard match */
353 for (i = 0; i < priv->firm_size; i++) {
354 if ((type == priv->firm[i].type) && (*id & priv->firm[i].id))
358 /*FIXME: Would make sense to seek for type "hint" match ? */
364 *id = priv->firm[i].id;
367 tuner_info("%s firmware for type=", (i < 0)? "Can't find": "Found");
368 dump_firm_type(type);
369 printk("(%x), id %08lx.\n", type, (unsigned long)*id);
374 static int load_firmware(struct dvb_frontend *fe, unsigned int type,
377 struct xc2028_data *priv = fe->tuner_priv;
379 unsigned char *p, *endp, buf[priv->max_len];
381 tuner_info("%s called\n", __FUNCTION__);
383 pos = seek_firmware(fe, type, id);
387 p = priv->firm[pos].ptr;
390 printk(KERN_ERR PREFIX "Firmware pointer were freed!");
393 endp = p + priv->firm[pos].size;
398 /* Checks if there's enough bytes to read */
399 if (p + sizeof(size) > endp) {
400 tuner_info("missing bytes\n");
404 size = le16_to_cpu(*(__u16 *) p);
411 /* Special callback command received */
412 rc = priv->tuner_callback(priv->video_dev,
413 XC2028_TUNER_RESET, 0);
415 tuner_info("Error at RESET code %d\n",
422 /* Checks for a sleep command */
424 msleep(size & 0x7fff);
428 if ((size + p > endp)) {
429 tuner_info("missing bytes: need %d, have %d\n",
430 size, (int)(endp - p));
438 /* Sends message chunks */
440 int len = (size < priv->max_len - 1) ?
441 size : priv->max_len - 1;
443 memcpy(buf + 1, p, len);
445 i2c_send(rc, priv, buf, len + 1);
447 tuner_info("%d returned from send\n", rc);
458 static int load_scode(struct dvb_frontend *fe, unsigned int type,
459 v4l2_std_id *id, int scode)
461 struct xc2028_data *priv = fe->tuner_priv;
465 tuner_info("%s called\n", __FUNCTION__);
467 pos = seek_firmware(fe, type, id);
471 p = priv->firm[pos].ptr;
474 printk(KERN_ERR PREFIX "Firmware pointer were freed!");
478 if ((priv->firm[pos].size != 12 * 16) || (scode >= 16))
481 if (priv->version < 0x0202) {
482 send_seq(priv, {0x20, 0x00, 0x00, 0x00});
484 send_seq(priv, {0xa0, 0x00, 0x00, 0x00});
487 i2c_send(rc, priv, p + 12 * scode, 12);
489 send_seq(priv, {0x00, 0x8c});
494 static int check_firmware(struct dvb_frontend *fe, enum tuner_mode new_mode,
495 v4l2_std_id std, fe_bandwidth_t bandwidth)
497 struct xc2028_data *priv = fe->tuner_priv;
498 int rc, version, hwmodel;
499 v4l2_std_id std0 = 0;
500 unsigned int type0 = 0, type = 0;
501 int change_digital_bandwidth;
503 tuner_info("%s called\n", __FUNCTION__);
506 if (!priv->ctrl.fname)
509 rc = load_all_firmwares(fe);
514 tuner_info("I am in mode %u and I should switch to mode %i\n",
515 priv->mode, new_mode);
517 /* first of all, determine whether we have switched the mode */
518 if (new_mode != priv->mode) {
519 priv->mode = new_mode;
520 priv->need_load_generic = 1;
523 change_digital_bandwidth = (priv->mode == T_DIGITAL_TV
524 && bandwidth != priv->bandwidth) ? 1 : 0;
525 tuner_info("old bandwidth %u, new bandwidth %u\n", priv->bandwidth,
528 if (priv->need_load_generic) {
529 /* Reset is needed before loading firmware */
530 rc = priv->tuner_callback(priv->video_dev,
531 XC2028_TUNER_RESET, 0);
537 if (priv->ctrl.type == XC2028_FIRM_MTS)
540 if (priv->bandwidth == 8)
543 /* FIXME: How to load FM and FM|INPUT1 firmwares? */
545 rc = load_firmware(fe, type0, &std0);
547 tuner_info("Error %d while loading generic firmware\n",
552 priv->need_load_generic = 0;
554 if (priv->mode == T_DIGITAL_TV)
555 change_digital_bandwidth = 1;
558 tuner_info("I should change bandwidth %u\n", change_digital_bandwidth);
560 if (change_digital_bandwidth) {
562 /*FIXME: Should allow selecting between D2620 and D2633 */
565 /* FIXME: When should select a DTV78 firmware?
568 case BANDWIDTH_8_MHZ:
571 case BANDWIDTH_7_MHZ:
574 case BANDWIDTH_6_MHZ:
575 /* FIXME: Should allow select also ATSC */
580 tuner_info("error: bandwidth not supported.\n");
582 priv->bandwidth = bandwidth;
585 /* Load INIT1, if needed */
586 tuner_info("Load init1 firmware, if exists\n");
587 type0 = BASE | INIT1;
588 if (priv->ctrl.type == XC2028_FIRM_MTS)
591 /* FIXME: Should handle errors - if INIT1 found */
592 rc = load_firmware(fe, type0, &std0);
594 /* FIXME: Should add support for FM radio
597 if (priv->ctrl.type == XC2028_FIRM_MTS)
600 tuner_info("Firmware standard to load: %08lx\n", (unsigned long)std);
601 if (priv->firm_type & std) {
602 tuner_info("Std-specific firmware already loaded.\n");
606 rc = load_firmware(fe, type, &std);
610 /* Load SCODE firmware, if exists */
611 tuner_info("Trying to load scode 0\n");
614 rc = load_scode(fe, type, &std, 0);
616 version = xc2028_get_reg(priv, 0x0004);
617 hwmodel = xc2028_get_reg(priv, 0x0008);
619 tuner_info("Device is Xceive %d version %d.%d, "
620 "firmware version %d.%d\n",
621 hwmodel, (version & 0xf000) >> 12, (version & 0xf00) >> 8,
622 (version & 0xf0) >> 4, version & 0xf);
624 priv->firm_type = std;
629 static int xc2028_signal(struct dvb_frontend *fe, u16 *strength)
631 struct xc2028_data *priv = fe->tuner_priv;
632 int frq_lock, signal = 0;
634 tuner_info("%s called\n", __FUNCTION__);
636 mutex_lock(&priv->lock);
640 /* Sync Lock Indicator */
641 frq_lock = xc2028_get_reg(priv, 0x0002);
645 /* Frequency is locked. Return signal quality */
647 /* Get SNR of the video signal */
648 signal = xc2028_get_reg(priv, 0x0040);
654 mutex_unlock(&priv->lock);
663 static int generic_set_tv_freq(struct dvb_frontend *fe, u32 freq /* in Hz */ ,
664 enum tuner_mode new_mode,
665 v4l2_std_id std, fe_bandwidth_t bandwidth)
667 struct xc2028_data *priv = fe->tuner_priv;
669 unsigned char buf[5];
672 tuner_info("%s called\n", __FUNCTION__);
674 mutex_lock(&priv->lock);
676 /* HACK: It seems that specific firmware need to be reloaded
677 when freq is changed */
682 rc = priv->tuner_callback(priv->video_dev, XC2028_TUNER_RESET, 0);
687 tuner_info("should set frequency %d kHz)\n", freq / 1000);
689 if (check_firmware(fe, new_mode, std, bandwidth) < 0)
692 if (new_mode == T_DIGITAL_TV)
695 div = (freq - offset + DIV / 2) / DIV;
697 /* CMD= Set frequency */
699 if (priv->version < 0x0202) {
700 send_seq(priv, {0x00, 0x02, 0x00, 0x00});
702 send_seq(priv, {0x80, 0x02, 0x00, 0x00});
705 rc = priv->tuner_callback(priv->video_dev, XC2028_RESET_CLK, 1);
711 buf[0] = 0xff & (div >> 24);
712 buf[1] = 0xff & (div >> 16);
713 buf[2] = 0xff & (div >> 8);
714 buf[3] = 0xff & (div);
717 i2c_send(rc, priv, buf, sizeof(buf));
722 priv->frequency = freq;
724 printk("divider= %02x %02x %02x %02x (freq=%d.%02d)\n",
725 buf[1], buf[2], buf[3], buf[4],
726 freq / 1000000, (freq % 1000000) / 10000);
731 mutex_unlock(&priv->lock);
736 static int xc2028_set_tv_freq(struct dvb_frontend *fe,
737 struct analog_parameters *p)
739 struct xc2028_data *priv = fe->tuner_priv;
741 tuner_info("%s called\n", __FUNCTION__);
743 return generic_set_tv_freq(fe, 62500l * p->frequency, T_ANALOG_TV,
744 p->std, BANDWIDTH_8_MHZ /* NOT USED */);
747 static int xc2028_set_params(struct dvb_frontend *fe,
748 struct dvb_frontend_parameters *p)
750 struct xc2028_data *priv = fe->tuner_priv;
752 tuner_info("%s called\n", __FUNCTION__);
754 /* FIXME: Only OFDM implemented */
755 if (fe->ops.info.type != FE_OFDM) {
756 tuner_info("DTV type not implemented.\n");
760 return generic_set_tv_freq(fe, p->frequency, T_DIGITAL_TV,
762 p->u.ofdm.bandwidth);
766 static int xc2028_dvb_release(struct dvb_frontend *fe)
768 struct xc2028_data *priv = fe->tuner_priv;
770 tuner_info("%s called\n", __FUNCTION__);
775 list_del(&priv->xc2028_list);
777 kfree(priv->ctrl.fname);
786 static int xc2028_get_frequency(struct dvb_frontend *fe, u32 *frequency)
788 struct xc2028_data *priv = fe->tuner_priv;
790 tuner_info("%s called\n", __FUNCTION__);
792 *frequency = priv->frequency;
797 static int xc2028_set_config(struct dvb_frontend *fe, void *priv_cfg)
799 struct xc2028_data *priv = fe->tuner_priv;
800 struct xc2028_ctrl *p = priv_cfg;
802 tuner_info("%s called\n", __FUNCTION__);
804 priv->ctrl.type = p->type;
807 kfree(priv->ctrl.fname);
809 priv->ctrl.fname = kmalloc(strlen(p->fname) + 1, GFP_KERNEL);
810 if (!priv->ctrl.fname)
814 strcpy(priv->ctrl.fname, p->fname);
818 priv->max_len = p->max_len;
820 tuner_info("%s OK\n", __FUNCTION__);
825 static const struct dvb_tuner_ops xc2028_dvb_tuner_ops = {
827 .name = "Xceive XC3028",
828 .frequency_min = 42000000,
829 .frequency_max = 864000000,
830 .frequency_step = 50000,
833 .set_config = xc2028_set_config,
834 .set_analog_params = xc2028_set_tv_freq,
835 .release = xc2028_dvb_release,
836 .get_frequency = xc2028_get_frequency,
837 .get_rf_strength = xc2028_signal,
838 .set_params = xc2028_set_params,
842 int xc2028_attach(struct dvb_frontend *fe, struct i2c_adapter *i2c_adap,
843 u8 i2c_addr, struct device *dev, void *video_dev,
844 int (*tuner_callback) (void *dev, int command, int arg))
846 struct xc2028_data *priv;
848 printk(KERN_INFO PREFIX "Xcv2028/3028 init called!\n");
853 if (NULL == video_dev)
856 if (!tuner_callback) {
857 printk(KERN_ERR PREFIX "No tuner callback!\n");
861 list_for_each_entry(priv, &xc2028_list, xc2028_list) {
862 if (priv->dev == dev)
867 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
871 fe->tuner_priv = priv;
873 priv->bandwidth = BANDWIDTH_6_MHZ;
874 priv->need_load_generic = 1;
875 priv->mode = T_UNINITIALIZED;
876 priv->i2c_props.addr = i2c_addr;
877 priv->i2c_props.adap = i2c_adap;
879 priv->video_dev = video_dev;
880 priv->tuner_callback = tuner_callback;
884 mutex_init(&priv->lock);
886 list_add_tail(&priv->xc2028_list, &xc2028_list);
890 memcpy(&fe->ops.tuner_ops, &xc2028_dvb_tuner_ops,
891 sizeof(xc2028_dvb_tuner_ops));
893 tuner_info("type set to %s\n", "XCeive xc2028/xc3028 tuner");
897 EXPORT_SYMBOL(xc2028_attach);
899 MODULE_DESCRIPTION("Xceive xc2028/xc3028 tuner driver");
900 MODULE_AUTHOR("Michel Ludwig <michel.ludwig@gmail.com>");
901 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
902 MODULE_LICENSE("GPL");