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 static void free_firmware(struct xc2028_data
*priv
)
122 for (i
= 0; i
< priv
->firm_size
; i
++)
123 kfree(priv
->firm
[i
].ptr
);
128 priv
->need_load_generic
= 1;
131 static int load_all_firmwares(struct dvb_frontend
*fe
)
133 struct xc2028_data
*priv
= fe
->tuner_priv
;
134 const struct firmware
*fw
= NULL
;
135 unsigned char *p
, *endp
;
140 tuner_info("%s called\n", __FUNCTION__
);
142 tuner_info("Loading firmware %s\n", priv
->ctrl
.fname
);
143 rc
= request_firmware(&fw
, priv
->ctrl
.fname
, priv
->dev
);
146 tuner_info("Error: firmware %s not found.\n",
149 tuner_info("Error %d while requesting firmware %s \n",
150 rc
, priv
->ctrl
.fname
);
157 if (fw
->size
< sizeof(name
) - 1 + 2) {
158 tuner_info("Error: firmware size is zero!\n");
163 memcpy(name
, p
, sizeof(name
) - 1);
164 name
[sizeof(name
) - 1] = 0;
165 p
+= sizeof(name
) - 1;
167 priv
->version
= le16_to_cpu(*(__u16
*) p
);
170 tuner_info("firmware: %s, ver %d.%d\n", name
,
171 priv
->version
>> 8, priv
->version
& 0xff);
176 n_array
= le16_to_cpu(*(__u16
*) p
);
179 tuner_info("there are %d firmwares at %s\n", n_array
, priv
->ctrl
.fname
);
181 priv
->firm
= kzalloc(sizeof(*priv
->firm
) * n_array
, GFP_KERNEL
);
184 tuner_info("Not enough memory for loading firmware.\n");
189 priv
->firm_size
= n_array
;
197 tuner_info("Too much firmwares at the file\n");
201 /* Checks if there's enough bytes to read */
202 if (p
+ sizeof(type
) + sizeof(id
) + sizeof(size
) > endp
) {
203 tuner_info("Lost firmware!\n");
207 type
= le32_to_cpu(*(__u32
*) p
);
210 id
= le64_to_cpu(*(v4l2_std_id
*) p
);
213 size
= le32_to_cpu(*(v4l2_std_id
*) p
);
216 if ((!size
) || (size
+ p
> endp
)) {
217 tuner_info("Firmware type %x, id %lx corrupt\n",
218 type
, (unsigned long)id
);
222 priv
->firm
[n
].ptr
= kzalloc(size
, GFP_KERNEL
);
223 if (!priv
->firm
[n
].ptr
) {
224 tuner_info("Not enough memory.\n");
228 tuner_info("Loading firmware type %x, id %lx, size=%d.\n",
229 type
, (unsigned long)id
, size
);
231 memcpy(priv
->firm
[n
].ptr
, p
, size
);
232 priv
->firm
[n
].type
= type
;
233 priv
->firm
[n
].id
= id
;
234 priv
->firm
[n
].size
= size
;
239 if (n
+ 1 != priv
->firm_size
) {
240 tuner_info("Firmware file is incomplete!\n");
248 tuner_info("Error: firmware file is corrupted!\n");
251 tuner_info("Releasing loaded firmware file.\n");
256 release_firmware(fw
);
257 tuner_info("Firmware files loaded.\n");
262 static int load_firmware(struct dvb_frontend
*fe
, unsigned int type
,
265 struct xc2028_data
*priv
= fe
->tuner_priv
;
267 unsigned char *p
, *endp
, buf
[priv
->max_len
];
269 tuner_info("%s called\n", __FUNCTION__
);
272 printk(KERN_ERR PREFIX
"Error! firmware not loaded\n");
276 if ((type
== 0) && (*id
== 0))
279 /* Seek for exact match */
280 for (i
= 0; i
< priv
->firm_size
; i
++) {
281 if ((type
== priv
->firm
[i
].type
) && (*id
== priv
->firm
[i
].id
))
285 /* Seek for generic video standard match */
286 for (i
= 0; i
< priv
->firm_size
; i
++) {
287 if ((type
== priv
->firm
[i
].type
) && (*id
& priv
->firm
[i
].id
))
291 /*FIXME: Would make sense to seek for type "hint" match ? */
293 tuner_info("Can't find firmware for type=%x, id=%lx\n", type
,
298 *id
= priv
->firm
[i
].id
;
299 tuner_info("Found firmware for type=%x, id=%lx\n", type
, (long int)*id
);
301 p
= priv
->firm
[i
].ptr
;
304 printk(KERN_ERR PREFIX
"Firmware pointer were freed!");
307 endp
= p
+ priv
->firm
[i
].size
;
312 /* Checks if there's enough bytes to read */
313 if (p
+ sizeof(size
) > endp
) {
314 tuner_info("missing bytes\n");
318 size
= le16_to_cpu(*(__u16
*) p
);
325 /* Special callback command received */
326 rc
= priv
->tuner_callback(priv
->video_dev
,
327 XC2028_TUNER_RESET
, 0);
329 tuner_info("Error at RESET code %d\n",
336 /* Checks for a sleep command */
338 msleep(size
& 0x7fff);
342 if ((size
+ p
> endp
)) {
343 tuner_info("missing bytes: need %d, have %d\n",
344 size
, (int)(endp
- p
));
352 /* Sends message chunks */
354 int len
= (size
< priv
->max_len
- 1) ?
355 size
: priv
->max_len
- 1;
357 memcpy(buf
+ 1, p
, len
);
359 i2c_send(rc
, priv
, buf
, len
+ 1);
361 tuner_info("%d returned from send\n", rc
);
372 static int check_firmware(struct dvb_frontend
*fe
, enum tuner_mode new_mode
,
373 v4l2_std_id std
, fe_bandwidth_t bandwidth
)
375 struct xc2028_data
*priv
= fe
->tuner_priv
;
376 int rc
, version
, hwmodel
;
377 v4l2_std_id std0
= 0;
378 unsigned int type0
= 0, type
= 0;
379 int change_digital_bandwidth
;
381 tuner_info("%s called\n", __FUNCTION__
);
384 if (!priv
->ctrl
.fname
)
387 rc
= load_all_firmwares(fe
);
392 tuner_info("I am in mode %u and I should switch to mode %i\n",
393 priv
->mode
, new_mode
);
395 /* first of all, determine whether we have switched the mode */
396 if (new_mode
!= priv
->mode
) {
397 priv
->mode
= new_mode
;
398 priv
->need_load_generic
= 1;
401 change_digital_bandwidth
= (priv
->mode
== T_DIGITAL_TV
402 && bandwidth
!= priv
->bandwidth
) ? 1 : 0;
403 tuner_info("old bandwidth %u, new bandwidth %u\n", priv
->bandwidth
,
406 if (priv
->need_load_generic
) {
407 /* Reset is needed before loading firmware */
408 rc
= priv
->tuner_callback(priv
->video_dev
,
409 XC2028_TUNER_RESET
, 0);
415 if (priv
->ctrl
.type
== XC2028_FIRM_MTS
)
418 if (priv
->bandwidth
== 8)
421 /* FIXME: How to load FM and FM|INPUT1 firmwares? */
423 rc
= load_firmware(fe
, type0
, &std0
);
425 tuner_info("Error %d while loading generic firmware\n",
430 priv
->need_load_generic
= 0;
432 if (priv
->mode
== T_DIGITAL_TV
)
433 change_digital_bandwidth
= 1;
436 tuner_info("I should change bandwidth %u\n", change_digital_bandwidth
);
438 if (change_digital_bandwidth
) {
440 /*FIXME: Should allow selecting between D2620 and D2633 */
443 /* FIXME: When should select a DTV78 firmware?
446 case BANDWIDTH_8_MHZ
:
449 case BANDWIDTH_7_MHZ
:
452 case BANDWIDTH_6_MHZ
:
453 /* FIXME: Should allow select also ATSC */
458 tuner_info("error: bandwidth not supported.\n");
460 priv
->bandwidth
= bandwidth
;
463 /* Load INIT1, if needed */
464 tuner_info("Trying to load init1 firmware\n");
465 type0
= BASE
| INIT1
| priv
->ctrl
.type
;
466 if (priv
->ctrl
.type
== XC2028_FIRM_MTS
)
469 /* FIXME: Should handle errors - if INIT1 found */
470 rc
= load_firmware(fe
, type0
, &std0
);
472 /* FIXME: Should add support for FM radio
475 if (priv
->ctrl
.type
== XC2028_FIRM_MTS
)
478 tuner_info("firmware standard to load: %08lx\n", (unsigned long)std
);
479 if (priv
->firm_type
& std
) {
480 tuner_info("no need to load a std-specific firmware.\n");
484 rc
= load_firmware(fe
, type
, &std
);
488 version
= xc2028_get_reg(priv
, 0x0004);
489 hwmodel
= xc2028_get_reg(priv
, 0x0008);
491 tuner_info("Device is Xceive %d version %d.%d, "
492 "firmware version %d.%d\n",
493 hwmodel
, (version
& 0xf000) >> 12, (version
& 0xf00) >> 8,
494 (version
& 0xf0) >> 4, version
& 0xf);
496 priv
->firm_type
= std
;
501 static int xc2028_signal(struct dvb_frontend
*fe
, u16
*strength
)
503 struct xc2028_data
*priv
= fe
->tuner_priv
;
504 int frq_lock
, signal
= 0;
506 tuner_info("%s called\n", __FUNCTION__
);
508 mutex_lock(&priv
->lock
);
512 /* Sync Lock Indicator */
513 frq_lock
= xc2028_get_reg(priv
, 0x0002);
517 /* Frequency is locked. Return signal quality */
519 /* Get SNR of the video signal */
520 signal
= xc2028_get_reg(priv
, 0x0040);
526 mutex_unlock(&priv
->lock
);
535 static int generic_set_tv_freq(struct dvb_frontend
*fe
, u32 freq
/* in Hz */ ,
536 enum tuner_mode new_mode
,
537 v4l2_std_id std
, fe_bandwidth_t bandwidth
)
539 struct xc2028_data
*priv
= fe
->tuner_priv
;
541 unsigned char buf
[5];
544 tuner_info("%s called\n", __FUNCTION__
);
546 mutex_lock(&priv
->lock
);
548 /* HACK: It seems that specific firmware need to be reloaded
549 when freq is changed */
554 rc
= priv
->tuner_callback(priv
->video_dev
, XC2028_TUNER_RESET
, 0);
559 tuner_info("should set frequency %d kHz)\n", freq
/ 1000);
561 if (check_firmware(fe
, new_mode
, std
, bandwidth
) < 0)
564 if (new_mode
== T_DIGITAL_TV
)
567 div
= (freq
- offset
+ DIV
/ 2) / DIV
;
569 /* CMD= Set frequency */
571 if (priv
->version
< 0x0202) {
572 send_seq(priv
, {0x00, 0x02, 0x00, 0x00});
574 send_seq(priv
, {0x80, 0x02, 0x00, 0x00});
577 rc
= priv
->tuner_callback(priv
->video_dev
, XC2028_RESET_CLK
, 1);
583 buf
[0] = 0xff & (div
>> 24);
584 buf
[1] = 0xff & (div
>> 16);
585 buf
[2] = 0xff & (div
>> 8);
586 buf
[3] = 0xff & (div
);
589 i2c_send(rc
, priv
, buf
, sizeof(buf
));
594 priv
->frequency
= freq
;
596 printk("divider= %02x %02x %02x %02x (freq=%d.%02d)\n",
597 buf
[1], buf
[2], buf
[3], buf
[4],
598 freq
/ 1000000, (freq
% 1000000) / 10000);
603 mutex_unlock(&priv
->lock
);
608 static int xc2028_set_tv_freq(struct dvb_frontend
*fe
,
609 struct analog_parameters
*p
)
611 struct xc2028_data
*priv
= fe
->tuner_priv
;
613 tuner_info("%s called\n", __FUNCTION__
);
615 return generic_set_tv_freq(fe
, 62500l * p
->frequency
, T_ANALOG_TV
,
616 p
->std
, BANDWIDTH_8_MHZ
/* NOT USED */);
619 static int xc2028_set_params(struct dvb_frontend
*fe
,
620 struct dvb_frontend_parameters
*p
)
622 struct xc2028_data
*priv
= fe
->tuner_priv
;
624 tuner_info("%s called\n", __FUNCTION__
);
626 /* FIXME: Only OFDM implemented */
627 if (fe
->ops
.info
.type
!= FE_OFDM
) {
628 tuner_info("DTV type not implemented.\n");
632 return generic_set_tv_freq(fe
, p
->frequency
, T_DIGITAL_TV
,
634 p
->u
.ofdm
.bandwidth
);
638 static int xc2028_dvb_release(struct dvb_frontend
*fe
)
640 struct xc2028_data
*priv
= fe
->tuner_priv
;
642 tuner_info("%s called\n", __FUNCTION__
);
647 list_del(&priv
->xc2028_list
);
649 kfree(priv
->ctrl
.fname
);
658 static int xc2028_get_frequency(struct dvb_frontend
*fe
, u32
*frequency
)
660 struct xc2028_data
*priv
= fe
->tuner_priv
;
662 tuner_info("%s called\n", __FUNCTION__
);
664 *frequency
= priv
->frequency
;
669 static int xc2028_set_config(struct dvb_frontend
*fe
, void *priv_cfg
)
671 struct xc2028_data
*priv
= fe
->tuner_priv
;
672 struct xc2028_ctrl
*p
= priv_cfg
;
674 tuner_info("%s called\n", __FUNCTION__
);
676 priv
->ctrl
.type
= p
->type
;
679 kfree(priv
->ctrl
.fname
);
681 priv
->ctrl
.fname
= kmalloc(strlen(p
->fname
) + 1, GFP_KERNEL
);
682 if (!priv
->ctrl
.fname
)
686 strcpy(priv
->ctrl
.fname
, p
->fname
);
690 priv
->max_len
= p
->max_len
;
692 tuner_info("%s OK\n", __FUNCTION__
);
697 static const struct dvb_tuner_ops xc2028_dvb_tuner_ops
= {
699 .name
= "Xceive XC3028",
700 .frequency_min
= 42000000,
701 .frequency_max
= 864000000,
702 .frequency_step
= 50000,
705 .set_config
= xc2028_set_config
,
706 .set_analog_params
= xc2028_set_tv_freq
,
707 .release
= xc2028_dvb_release
,
708 .get_frequency
= xc2028_get_frequency
,
709 .get_rf_strength
= xc2028_signal
,
710 .set_params
= xc2028_set_params
,
714 int xc2028_attach(struct dvb_frontend
*fe
, struct i2c_adapter
*i2c_adap
,
715 u8 i2c_addr
, struct device
*dev
, void *video_dev
,
716 int (*tuner_callback
) (void *dev
, int command
, int arg
))
718 struct xc2028_data
*priv
;
720 printk(KERN_INFO PREFIX
"Xcv2028/3028 init called!\n");
725 if (NULL
== video_dev
)
728 if (!tuner_callback
) {
729 printk(KERN_ERR PREFIX
"No tuner callback!\n");
733 list_for_each_entry(priv
, &xc2028_list
, xc2028_list
) {
734 if (priv
->dev
== dev
)
739 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
743 fe
->tuner_priv
= priv
;
745 priv
->bandwidth
= BANDWIDTH_6_MHZ
;
746 priv
->need_load_generic
= 1;
747 priv
->mode
= T_UNINITIALIZED
;
748 priv
->i2c_props
.addr
= i2c_addr
;
749 priv
->i2c_props
.adap
= i2c_adap
;
751 priv
->video_dev
= video_dev
;
752 priv
->tuner_callback
= tuner_callback
;
756 mutex_init(&priv
->lock
);
758 list_add_tail(&priv
->xc2028_list
, &xc2028_list
);
762 memcpy(&fe
->ops
.tuner_ops
, &xc2028_dvb_tuner_ops
,
763 sizeof(xc2028_dvb_tuner_ops
));
765 tuner_info("type set to %s\n", "XCeive xc2028/xc3028 tuner");
769 EXPORT_SYMBOL(xc2028_attach
);
771 MODULE_DESCRIPTION("Xceive xc2028/xc3028 tuner driver");
772 MODULE_AUTHOR("Michel Ludwig <michel.ludwig@gmail.com>");
773 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
774 MODULE_LICENSE("GPL");