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 int xc2028_get_reg(struct xc2028_data
*priv
, u16 reg
)
99 tuner_info("%s called\n", __FUNCTION__
);
103 i2c_send(rc
, priv
, buf
, sizeof(buf
));
107 i2c_rcv(rc
, priv
, buf
, 2);
111 return (buf
[1]) | (buf
[0] << 8);
114 static void free_firmware(struct xc2028_data
*priv
)
121 for (i
= 0; i
< priv
->firm_size
; i
++)
122 kfree(priv
->firm
[i
].ptr
);
127 priv
->need_load_generic
= 1;
130 static int load_all_firmwares(struct dvb_frontend
*fe
)
132 struct xc2028_data
*priv
= fe
->tuner_priv
;
133 const struct firmware
*fw
= NULL
;
134 unsigned char *p
, *endp
;
139 tuner_info("%s called\n", __FUNCTION__
);
141 tuner_info("Loading firmware %s\n", priv
->ctrl
.fname
);
142 rc
= request_firmware(&fw
, priv
->ctrl
.fname
, priv
->dev
);
145 tuner_info("Error: firmware %s not found.\n",
148 tuner_info("Error %d while requesting firmware %s \n",
149 rc
, priv
->ctrl
.fname
);
156 if (fw
->size
< sizeof(name
) - 1 + 2) {
157 tuner_info("Error: firmware size is zero!\n");
162 memcpy(name
, p
, sizeof(name
) - 1);
163 name
[sizeof(name
) - 1] = 0;
164 p
+= sizeof(name
) - 1;
166 priv
->version
= le16_to_cpu(*(__u16
*) p
);
169 tuner_info("firmware: %s, ver %d.%d\n", name
,
170 priv
->version
>> 8, priv
->version
& 0xff);
175 n_array
= le16_to_cpu(*(__u16
*) p
);
178 tuner_info("there are %d firmwares at %s\n", n_array
, priv
->ctrl
.fname
);
180 priv
->firm
= kzalloc(sizeof(*priv
->firm
) * n_array
, GFP_KERNEL
);
183 tuner_info("Not enough memory for loading firmware.\n");
188 priv
->firm_size
= n_array
;
196 tuner_info("Too much firmwares at the file\n");
200 /* Checks if there's enough bytes to read */
201 if (p
+ sizeof(type
) + sizeof(id
) + sizeof(size
) > endp
) {
202 tuner_info("Lost firmware!\n");
206 type
= le32_to_cpu(*(__u32
*) p
);
209 id
= le64_to_cpu(*(v4l2_std_id
*) p
);
212 size
= le32_to_cpu(*(v4l2_std_id
*) p
);
215 if ((!size
) || (size
+ p
> endp
)) {
216 tuner_info("Firmware type %x, id %lx corrupt\n",
217 type
, (unsigned long)id
);
221 priv
->firm
[n
].ptr
= kzalloc(size
, GFP_KERNEL
);
222 if (!priv
->firm
[n
].ptr
) {
223 tuner_info("Not enough memory.\n");
227 tuner_info("Loading firmware type %x, id %lx, size=%d.\n",
228 type
, (unsigned long)id
, size
);
230 memcpy(priv
->firm
[n
].ptr
, p
, size
);
231 priv
->firm
[n
].type
= type
;
232 priv
->firm
[n
].id
= id
;
233 priv
->firm
[n
].size
= size
;
238 if (n
+ 1 != priv
->firm_size
) {
239 tuner_info("Firmware file is incomplete!\n");
247 tuner_info("Error: firmware file is corrupted!\n");
250 tuner_info("Releasing loaded firmware file.\n");
255 release_firmware(fw
);
256 tuner_info("Firmware files loaded.\n");
261 static int load_firmware(struct dvb_frontend
*fe
, unsigned int type
,
264 struct xc2028_data
*priv
= fe
->tuner_priv
;
266 unsigned char *p
, *endp
, buf
[priv
->max_len
];
268 tuner_info("%s called\n", __FUNCTION__
);
271 printk(KERN_ERR PREFIX
"Error! firmware not loaded\n");
275 if ((type
== 0) && (*id
== 0))
278 /* Seek for exact match */
279 for (i
= 0; i
< priv
->firm_size
; i
++) {
280 if ((type
== priv
->firm
[i
].type
) && (*id
== priv
->firm
[i
].id
))
284 /* Seek for generic video standard match */
285 for (i
= 0; i
< priv
->firm_size
; i
++) {
286 if ((type
== priv
->firm
[i
].type
) && (*id
& priv
->firm
[i
].id
))
290 /*FIXME: Would make sense to seek for type "hint" match ? */
292 tuner_info("Can't find firmware for type=%x, id=%lx\n", type
,
297 *id
= priv
->firm
[i
].id
;
298 tuner_info("Found firmware for type=%x, id=%lx\n", type
, (long int)*id
);
300 p
= priv
->firm
[i
].ptr
;
303 printk(KERN_ERR PREFIX
"Firmware pointer were freed!");
306 endp
= p
+ priv
->firm
[i
].size
;
311 /* Checks if there's enough bytes to read */
312 if (p
+ sizeof(size
) > endp
) {
313 tuner_info("missing bytes\n");
317 size
= le16_to_cpu(*(__u16
*) p
);
324 /* Special callback command received */
325 rc
= priv
->tuner_callback(priv
->video_dev
,
326 XC2028_TUNER_RESET
, 0);
328 tuner_info("Error at RESET code %d\n",
335 /* Checks for a sleep command */
337 msleep(size
& 0x7fff);
341 if ((size
+ p
> endp
)) {
342 tuner_info("missing bytes: need %d, have %d\n",
343 size
, (int)(endp
- p
));
351 /* Sends message chunks */
353 int len
= (size
< priv
->max_len
- 1) ?
354 size
: priv
->max_len
- 1;
356 memcpy(buf
+ 1, p
, len
);
358 i2c_send(rc
, priv
, buf
, len
+ 1);
360 tuner_info("%d returned from send\n", rc
);
371 static int check_firmware(struct dvb_frontend
*fe
, enum tuner_mode new_mode
,
372 v4l2_std_id std
, fe_bandwidth_t bandwidth
)
374 struct xc2028_data
*priv
= fe
->tuner_priv
;
376 v4l2_std_id std0
= 0;
377 unsigned int type0
= 0, type
= 0;
378 int change_digital_bandwidth
;
380 tuner_info("%s called\n", __FUNCTION__
);
383 if (!priv
->ctrl
.fname
)
386 rc
= load_all_firmwares(fe
);
391 tuner_info("I am in mode %u and I should switch to mode %i\n",
392 priv
->mode
, new_mode
);
394 /* first of all, determine whether we have switched the mode */
395 if (new_mode
!= priv
->mode
) {
396 priv
->mode
= new_mode
;
397 priv
->need_load_generic
= 1;
400 change_digital_bandwidth
= (priv
->mode
== T_DIGITAL_TV
401 && bandwidth
!= priv
->bandwidth
) ? 1 : 0;
402 tuner_info("old bandwidth %u, new bandwidth %u\n", priv
->bandwidth
,
405 if (priv
->need_load_generic
) {
406 /* Reset is needed before loading firmware */
407 rc
= priv
->tuner_callback(priv
->video_dev
,
408 XC2028_TUNER_RESET
, 0);
414 if (priv
->ctrl
.type
== XC2028_FIRM_MTS
)
417 if (priv
->bandwidth
== 8)
420 /* FIXME: How to load FM and FM|INPUT1 firmwares? */
422 rc
= load_firmware(fe
, type0
, &std0
);
424 tuner_info("Error %d while loading generic firmware\n",
429 priv
->need_load_generic
= 0;
431 if (priv
->mode
== T_DIGITAL_TV
)
432 change_digital_bandwidth
= 1;
435 tuner_info("I should change bandwidth %u\n", change_digital_bandwidth
);
437 if (change_digital_bandwidth
) {
439 /*FIXME: Should allow selecting between D2620 and D2633 */
442 /* FIXME: When should select a DTV78 firmware?
445 case BANDWIDTH_8_MHZ
:
448 case BANDWIDTH_7_MHZ
:
451 case BANDWIDTH_6_MHZ
:
452 /* FIXME: Should allow select also ATSC */
457 tuner_info("error: bandwidth not supported.\n");
459 priv
->bandwidth
= bandwidth
;
462 /* Load INIT1, if needed */
463 tuner_info("Trying to load init1 firmware\n");
464 type0
= BASE
| INIT1
| priv
->ctrl
.type
;
465 if (priv
->ctrl
.type
== XC2028_FIRM_MTS
)
468 /* FIXME: Should handle errors - if INIT1 found */
469 rc
= load_firmware(fe
, type0
, &std0
);
471 /* FIXME: Should add support for FM radio
474 if (priv
->ctrl
.type
== XC2028_FIRM_MTS
)
477 tuner_info("firmware standard to load: %08lx\n", (unsigned long)std
);
478 if (priv
->firm_type
& std
) {
479 tuner_info("no need to load a std-specific firmware.\n");
483 rc
= load_firmware(fe
, type
, &std
);
487 version
= xc2028_get_reg(priv
, 0x4);
488 tuner_info("Firmware version is %d.%d\n",
489 (version
>> 4) & 0x0f, (version
) & 0x0f);
491 priv
->firm_type
= std
;
496 static int xc2028_signal(struct dvb_frontend
*fe
, u16
*strength
)
498 struct xc2028_data
*priv
= fe
->tuner_priv
;
499 int frq_lock
, signal
= 0;
501 tuner_info("%s called\n", __FUNCTION__
);
503 mutex_lock(&priv
->lock
);
507 frq_lock
= xc2028_get_reg(priv
, 0x2);
511 /* Frequency is locked. Return signal quality */
513 signal
= xc2028_get_reg(priv
, 0x40);
519 mutex_unlock(&priv
->lock
);
528 static int generic_set_tv_freq(struct dvb_frontend
*fe
, u32 freq
/* in Hz */ ,
529 enum tuner_mode new_mode
,
530 v4l2_std_id std
, fe_bandwidth_t bandwidth
)
532 struct xc2028_data
*priv
= fe
->tuner_priv
;
534 unsigned char buf
[5];
537 tuner_info("%s called\n", __FUNCTION__
);
539 mutex_lock(&priv
->lock
);
541 /* HACK: It seems that specific firmware need to be reloaded
542 when freq is changed */
547 rc
= priv
->tuner_callback(priv
->video_dev
, XC2028_TUNER_RESET
, 0);
552 tuner_info("should set frequency %d kHz)\n", freq
/ 1000);
554 if (check_firmware(fe
, new_mode
, std
, bandwidth
) < 0)
557 if (new_mode
== T_DIGITAL_TV
)
560 div
= (freq
- offset
+ DIV
/ 2) / DIV
;
562 /* CMD= Set frequency */
564 if (priv
->version
< 0x0202) {
565 send_seq(priv
, {0x00, 0x02, 0x00, 0x00});
567 send_seq(priv
, {0x80, 0x02, 0x00, 0x00});
570 rc
= priv
->tuner_callback(priv
->video_dev
, XC2028_RESET_CLK
, 1);
576 buf
[0] = 0xff & (div
>> 24);
577 buf
[1] = 0xff & (div
>> 16);
578 buf
[2] = 0xff & (div
>> 8);
579 buf
[3] = 0xff & (div
);
582 i2c_send(rc
, priv
, buf
, sizeof(buf
));
587 priv
->frequency
= freq
;
589 printk("divider= %02x %02x %02x %02x (freq=%d.%02d)\n",
590 buf
[1], buf
[2], buf
[3], buf
[4],
591 freq
/ 1000000, (freq
% 1000000) / 10000);
596 mutex_unlock(&priv
->lock
);
601 static int xc2028_set_tv_freq(struct dvb_frontend
*fe
,
602 struct analog_parameters
*p
)
604 struct xc2028_data
*priv
= fe
->tuner_priv
;
606 tuner_info("%s called\n", __FUNCTION__
);
608 return generic_set_tv_freq(fe
, 62500l * p
->frequency
, T_ANALOG_TV
,
609 p
->std
, BANDWIDTH_8_MHZ
/* NOT USED */);
612 static int xc2028_set_params(struct dvb_frontend
*fe
,
613 struct dvb_frontend_parameters
*p
)
615 struct xc2028_data
*priv
= fe
->tuner_priv
;
617 tuner_info("%s called\n", __FUNCTION__
);
619 /* FIXME: Only OFDM implemented */
620 if (fe
->ops
.info
.type
!= FE_OFDM
) {
621 tuner_info("DTV type not implemented.\n");
625 return generic_set_tv_freq(fe
, p
->frequency
, T_DIGITAL_TV
,
627 p
->u
.ofdm
.bandwidth
);
631 static int xc2028_dvb_release(struct dvb_frontend
*fe
)
633 struct xc2028_data
*priv
= fe
->tuner_priv
;
635 tuner_info("%s called\n", __FUNCTION__
);
640 list_del(&priv
->xc2028_list
);
642 kfree(priv
->ctrl
.fname
);
651 static int xc2028_get_frequency(struct dvb_frontend
*fe
, u32
*frequency
)
653 struct xc2028_data
*priv
= fe
->tuner_priv
;
655 tuner_info("%s called\n", __FUNCTION__
);
657 *frequency
= priv
->frequency
;
662 static int xc2028_set_config(struct dvb_frontend
*fe
, void *priv_cfg
)
664 struct xc2028_data
*priv
= fe
->tuner_priv
;
665 struct xc2028_ctrl
*p
= priv_cfg
;
667 tuner_info("%s called\n", __FUNCTION__
);
669 priv
->ctrl
.type
= p
->type
;
672 kfree(priv
->ctrl
.fname
);
674 priv
->ctrl
.fname
= kmalloc(strlen(p
->fname
) + 1, GFP_KERNEL
);
675 if (!priv
->ctrl
.fname
)
679 strcpy(priv
->ctrl
.fname
, p
->fname
);
683 priv
->max_len
= p
->max_len
;
685 tuner_info("%s OK\n", __FUNCTION__
);
690 static const struct dvb_tuner_ops xc2028_dvb_tuner_ops
= {
692 .name
= "Xceive XC3028",
693 .frequency_min
= 42000000,
694 .frequency_max
= 864000000,
695 .frequency_step
= 50000,
698 .set_config
= xc2028_set_config
,
699 .set_analog_params
= xc2028_set_tv_freq
,
700 .release
= xc2028_dvb_release
,
701 .get_frequency
= xc2028_get_frequency
,
702 .get_rf_strength
= xc2028_signal
,
703 .set_params
= xc2028_set_params
,
707 int xc2028_attach(struct dvb_frontend
*fe
, struct i2c_adapter
*i2c_adap
,
708 u8 i2c_addr
, struct device
*dev
, void *video_dev
,
709 int (*tuner_callback
) (void *dev
, int command
, int arg
))
711 struct xc2028_data
*priv
;
713 printk(KERN_INFO PREFIX
"Xcv2028/3028 init called!\n");
718 if (NULL
== video_dev
)
721 if (!tuner_callback
) {
722 printk(KERN_ERR PREFIX
"No tuner callback!\n");
726 list_for_each_entry(priv
, &xc2028_list
, xc2028_list
) {
727 if (priv
->dev
== dev
)
732 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
736 fe
->tuner_priv
= priv
;
738 priv
->bandwidth
= BANDWIDTH_6_MHZ
;
739 priv
->need_load_generic
= 1;
740 priv
->mode
= T_UNINITIALIZED
;
741 priv
->i2c_props
.addr
= i2c_addr
;
742 priv
->i2c_props
.adap
= i2c_adap
;
744 priv
->video_dev
= video_dev
;
745 priv
->tuner_callback
= tuner_callback
;
749 mutex_init(&priv
->lock
);
751 list_add_tail(&priv
->xc2028_list
, &xc2028_list
);
755 memcpy(&fe
->ops
.tuner_ops
, &xc2028_dvb_tuner_ops
,
756 sizeof(xc2028_dvb_tuner_ops
));
758 tuner_info("type set to %s\n", "XCeive xc2028/xc3028 tuner");
762 EXPORT_SYMBOL(xc2028_attach
);
764 MODULE_DESCRIPTION("Xceive xc2028/xc3028 tuner driver");
765 MODULE_AUTHOR("Michel Ludwig <michel.ludwig@gmail.com>");
766 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
767 MODULE_LICENSE("GPL");