11 #include <linux/i2c.h>
12 #include <asm/div64.h>
14 #include <linux/videodev2.h>
18 #include <linux/slab.h>
19 #include <asm/unaligned.h>
28 #define XREG_INIT 0x00
29 #define XREG_RF_FREQ 0x02
30 #define XREG_POWER_DOWN 0x08
33 #define XREG_FREQ_ERROR 0x01
34 #define XREG_LOCK 0x02
35 #define XREG_VERSION 0x04
36 #define XREG_PRODUCT_ID 0x08
37 #define XREG_HSYNC_FREQ 0x10
38 #define XREG_FRAME_LINES 0x20
41 #define XREG_ADC_ENV 0x0100
47 static int no_poweroff;
49 MODULE_PARM_DESC(no_poweroff,
"0 (default) powers device off when not used.\n"
50 "1 keep device energized and with tuner ready all the times.\n"
51 " Faster, but consumes more power and keeps the device hotter\n");
53 static char audio_std[8];
56 "Audio standard. XC3028 audio decoder explicitly "
57 "needs to know what audio\n"
58 "standard is needed for some video standards with audio A2 or NICAM.\n"
59 "The valid values are:\n"
67 static char firmware_name[30];
70 "default firmware name\n");
72 static LIST_HEAD(hybrid_tuner_instance_list);
123 #define i2c_send(priv, buf, size) ({ \
125 _rc = tuner_i2c_xfer_send(&priv->i2c_props, buf, size); \
127 tuner_info("i2c output error: rc = %d (should be %d)\n",\
129 if (priv->ctrl.msleep) \
130 msleep(priv->ctrl.msleep); \
134 #define i2c_rcv(priv, buf, size) ({ \
136 _rc = tuner_i2c_xfer_recv(&priv->i2c_props, buf, size); \
138 tuner_err("i2c input error: rc = %d (should be %d)\n", \
143 #define i2c_send_recv(priv, obuf, osize, ibuf, isize) ({ \
145 _rc = tuner_i2c_xfer_send_recv(&priv->i2c_props, obuf, osize, \
148 tuner_err("i2c input error: rc = %d (should be %d)\n", \
150 if (priv->ctrl.msleep) \
151 msleep(priv->ctrl.msleep); \
155 #define send_seq(priv, data...) ({ \
156 static u8 _val[] = data; \
158 if (sizeof(_val) != \
159 (_rc = tuner_i2c_xfer_send(&priv->i2c_props, \
160 _val, sizeof(_val)))) { \
161 tuner_err("Error on line %d: %d\n", __LINE__, _rc); \
162 } else if (priv->ctrl.msleep) \
163 msleep(priv->ctrl.msleep); \
169 unsigned char buf[2];
170 unsigned char ibuf[2];
172 tuner_dbg(
"%s %04x called\n", __func__, reg);
175 buf[1] = (
unsigned char) reg;
180 *val = (ibuf[1]) | (ibuf[0] << 8);
184 #define dump_firm_type(t) dump_firm_type_and_int_freq(t, 0)
185 static void dump_firm_type_and_int_freq(
unsigned int type,
u16 int_freq)
248 printk(
"HAS_IF_%d ", int_freq);
271 switch (priv->
state) {
308 const unsigned char *
p, *endp;
318 if (fw->
size <
sizeof(name) - 1 + 2 + 2) {
319 tuner_err(
"Error: firmware file %s has invalid size!\n",
324 memcpy(name, p,
sizeof(name) - 1);
325 name[
sizeof(
name) - 1] = 0;
326 p +=
sizeof(
name) - 1;
331 n_array = get_unaligned_le16(p);
334 tuner_info(
"Loading %d firmware images from %s, type: %s, ver %d.%d\n",
335 n_array, priv->
fname, name,
340 tuner_err(
"Not enough memory to load firmware file.\n");
354 tuner_err(
"More firmware images in file than "
360 if (endp - p <
sizeof(type) +
sizeof(
id) +
sizeof(size))
370 int_freq = get_unaligned_le16(p);
371 p +=
sizeof(int_freq);
372 if (endp - p <
sizeof(size))
379 if (!size || size > endp - p) {
382 printk(
"(%x), id %llx is corrupted "
383 "(size=%d, expected %d)\n",
384 type, (
unsigned long long)
id,
385 (
unsigned)(endp - p), size);
391 tuner_err(
"Not enough memory to load firmware file.\n");
397 dump_firm_type_and_int_freq(type, int_freq);
398 printk(
"(%x), id %llx, size=%d.\n",
399 type, (
unsigned long long)
id, size);
406 priv->
firm[
n].int_freq = int_freq;
412 tuner_err(
"Firmware file is incomplete!\n");
419 tuner_err(
"Firmware header is incomplete!\n");
422 tuner_err(
"Error: firmware file is corrupted!\n");
425 tuner_info(
"Releasing partially loaded firmware file.\n");
437 static int seek_firmware(
struct dvb_frontend *fe,
unsigned int type,
441 int i, best_i = -1, best_nr_matches = 0;
444 tuner_dbg(
"%s called, want type=", __func__);
447 printk(
"(%x), id %016llx.\n", type, (
unsigned long long)*
id);
451 tuner_err(
"Error! firmware not loaded\n");
455 if (((type & ~
SCODE) == 0) && (*
id == 0))
460 else if (type &
SCODE) {
475 if ((type == (priv->
firm[i].type & type_mask)) &&
476 (*
id == priv->
firm[i].id))
485 if (type != (priv->
firm[i].type & type_mask))
488 match_mask = *
id & priv->
firm[
i].id;
492 if ((*
id & match_mask) == *
id)
496 if (nr_matches > best_nr_matches) {
497 best_nr_matches = nr_matches;
502 if (best_nr_matches > 0) {
503 tuner_dbg(
"Selecting best matching firmware (%d bits) for "
504 "type=", best_nr_matches);
506 printk(
"(%x), id %016llx:\n", type, (
unsigned long long)*
id);
517 *
id = priv->
firm[
i].id;
520 tuner_dbg(
"%s firmware for type=", (i < 0) ?
"Can't find" :
"Found");
523 printk(
"(%x), id %016llx.\n", type, (
unsigned long long)*
id);
545 static int load_firmware(
struct dvb_frontend *fe,
unsigned int type,
550 unsigned char *
p, *endp,
buf[priv->
ctrl.max_len];
554 pos = seek_firmware(fe, type,
id);
560 printk(
"(%x), id %016llx.\n", priv->
firm[pos].type,
561 (
unsigned long long)*
id);
564 endp = p + priv->
firm[
pos].size;
570 if (p +
sizeof(size) > endp) {
571 tuner_err(
"Firmware chunk size is wrong\n");
591 if (size >= 0xff00) {
616 if ((size + p > endp)) {
617 tuner_err(
"missing bytes: need %d, have %d\n",
618 size, (
int)(endp - p));
628 int len = (size < priv->
ctrl.max_len - 1) ?
629 size : priv->
ctrl.max_len - 1;
635 tuner_err(
"%d returned from send\n", rc);
645 if ((rc < 0) && (rc != -
EINVAL)) {
646 tuner_err(
"error executing flush: %d\n", rc);
653 static int load_scode(
struct dvb_frontend *fe,
unsigned int type,
663 pos = seek_firmware(fe, type,
id);
667 for (pos = 0; pos < priv->
firm_size; pos++) {
668 if ((priv->
firm[pos].int_freq == int_freq) &&
669 (priv->
firm[pos].type & HAS_IF))
678 if (priv->
firm[pos].type & HAS_IF) {
679 if (priv->
firm[pos].size != 12 * 16 || scode >= 16)
685 if (priv->
firm[pos].size != 14 * 16 || scode >= 16 ||
692 dump_firm_type_and_int_freq(priv->
firm[pos].type,
693 priv->
firm[pos].int_freq);
694 printk(
"(%x), id %016llx.\n", priv->
firm[pos].type,
695 (
unsigned long long)*
id);
698 rc =
send_seq(priv, {0x20, 0x00, 0x00, 0x00});
700 rc =
send_seq(priv, {0xa0, 0x00, 0x00, 0x00});
715 static int check_firmware(
struct dvb_frontend *fe,
unsigned int type,
726 rc = check_device_status(priv);
730 if (priv->
ctrl.mts && !(type &
FM))
736 new_fw.std_req =
std;
737 new_fw.scode_table = SCODE | priv->
ctrl.scode_table;
741 tuner_dbg(
"checking firmware, user requested type=");
744 printk(
"(%x), id %016llx, ", new_fw.type,
745 (
unsigned long long)new_fw.std_req);
751 printk(
"int_freq %d, ", new_fw.int_freq);
752 printk(
"scode_nr %d\n", new_fw.scode_nr);
762 tuner_dbg(
"BASE firmware not changed.\n");
776 rc = load_firmware(fe, BASE | new_fw.type, &std0);
778 tuner_err(
"Error %d while loading base firmware\n",
784 tuner_dbg(
"Load init1 firmware, if exists\n");
786 rc = load_firmware(fe, BASE |
INIT1 | new_fw.type, &std0);
788 rc = load_firmware(fe, (BASE |
INIT1 | new_fw.type) & ~
F8MHZ,
790 if (rc < 0 && rc != -
ENOENT) {
791 tuner_err(
"Error %d while loading init1 firmware\n",
801 if (priv->
cur_fw.type == (BASE | new_fw.type) &&
802 priv->
cur_fw.std_req == std) {
803 tuner_dbg(
"Std-specific firmware already loaded.\n");
804 goto skip_std_specific;
808 priv->
cur_fw.scode_table = 0;
810 rc = load_firmware(fe, new_fw.type, &new_fw.
id);
812 rc = load_firmware(fe, new_fw.type & ~
F8MHZ, &new_fw.id);
818 if (priv->
cur_fw.scode_table == new_fw.scode_table &&
819 priv->
cur_fw.scode_nr == new_fw.scode_nr) {
820 tuner_dbg(
"SCODE firmware already loaded.\n");
824 if (new_fw.type & FM)
828 tuner_dbg(
"Trying to load scode %d\n", new_fw.scode_nr);
830 rc = load_scode(fe, new_fw.type | new_fw.scode_table, &new_fw.
id,
831 new_fw.int_freq, new_fw.scode_nr);
834 if (xc2028_get_reg(priv, 0x0004, &version) < 0 ||
835 xc2028_get_reg(priv, 0x0008, &hwmodel) < 0) {
836 tuner_err(
"Unable to read tuner registers.\n");
840 tuner_dbg(
"Device is Xceive %d version %d.%d, "
841 "firmware version %d.%d\n",
842 hwmodel, (version & 0xf000) >> 12, (version & 0xf00) >> 8,
843 (version & 0xf0) >> 4, version & 0xf);
846 if (priv->
ctrl.read_not_reliable)
847 goto read_not_reliable;
850 if (priv->
firm_version != ((version & 0xf0) << 4 | (version & 0x0f))) {
851 if (!priv->
ctrl.read_not_reliable) {
852 tuner_err(
"Incorrect readback of firmware version.\n");
855 tuner_err(
"Returned an incorrect version. However, "
856 "read is not reliable enough. Ignoring it.\n");
862 if (priv->
hwmodel == 0 && (hwmodel == 2028 || hwmodel == 3028)) {
864 priv->
hwvers = version & 0xff00;
866 priv->
hwvers != (version & 0xff00)) {
867 tuner_err(
"Read invalid device hardware information - tuner "
890 if (retry_count < 8) {
905 u16 frq_lock, signal = 0;
910 rc = check_device_status(priv);
917 for (i = 0; i < 3; i++) {
918 rc = xc2028_get_reg(priv,
XREG_LOCK, &frq_lock);
932 rc = xc2028_get_reg(priv,
XREG_SNR, &signal);
938 signal = ((1 << 12) - 1) | ((signal & 0x07) << 12);
945 tuner_dbg(
"signal strength is %d\n", signal);
957 rc = check_device_status(priv);
964 for (i = 0; i < 3; i++) {
965 rc = xc2028_get_reg(priv,
XREG_LOCK, &frq_lock);
983 *afc = afc_reg * 15625;
1003 unsigned char buf[4];
1010 tuner_dbg(
"should set frequency %d kHz\n", freq / 1000);
1012 if (check_firmware(fe, type, std, int_freq) < 0)
1091 else if (type !=
ATSC)
1097 div = (freq - offset +
DIV / 2) /
DIV;
1111 if (priv->
ctrl.msleep)
1117 buf[0] = 0xff & (div >> 24);
1118 buf[1] = 0xff & (div >> 16);
1119 buf[2] = 0xff & (div >> 8);
1120 buf[3] = 0xff & (
div);
1122 rc =
i2c_send(priv, buf,
sizeof(buf));
1129 tuner_dbg(
"divisor= %*ph (freq=%d.%03d)\n", 4, buf,
1130 freq / 1000000, (freq % 1000000) / 1000);
1140 static int xc2028_set_analog_freq(
struct dvb_frontend *fe,
1144 unsigned int type=0;
1150 if (priv->
ctrl.input1)
1152 return generic_set_freq(fe, (625
l * p->
frequency) / 10,
1165 p->
std |= parse_audio_std_option();
1167 return generic_set_freq(fe, 62500
l * p->
frequency,
1178 unsigned int type = 0;
1183 rc = check_device_status(priv);
1198 switch (priv->
ctrl.type) {
1223 if (bw <= 6000000) {
1225 priv->
ctrl.vhfbw7 = 0;
1226 priv->
ctrl.uhfbw8 = 0;
1227 }
else if (bw <= 7000000) {
1229 priv->
ctrl.vhfbw7 = 1;
1231 priv->
ctrl.uhfbw8 = 0;
1236 priv->
ctrl.vhfbw7 = 0;
1238 priv->
ctrl.uhfbw8 = 1;
1244 if (priv->
ctrl.demod) {
1245 demod = priv->
ctrl.demod;
1265 return generic_set_freq(fe, c->
frequency,
1274 rc = check_device_status(priv);
1279 if (no_poweroff || priv->
ctrl.disable_power_mgmt)
1282 tuner_dbg(
"Putting xc2028/3028 into poweroff mode.\n");
1284 tuner_dbg(
"Printing sleep stack trace:\n");
1334 rc = check_device_status(priv);
1343 static void load_firmware_cb(
const struct firmware *fw,
1350 tuner_dbg(
"request_firmware_nowait(): %s\n", fw ?
"OK" :
"error");
1357 rc = load_all_firmwares(fe, fw);
1366 static int xc2028_set_config(
struct dvb_frontend *fe,
void *priv_cfg)
1381 if (priv->
ctrl.fname)
1394 if (!firmware_name[0] && p->
fname &&
1398 if (priv->
ctrl.max_len < 9)
1399 priv->
ctrl.max_len = 13;
1402 if (!firmware_name[0])
1405 priv->
fname = firmware_name;
1411 fe, load_firmware_cb);
1413 tuner_err(
"Failed to request firmware %s\n",
1426 .name =
"Xceive XC3028",
1427 .frequency_min = 42000000,
1428 .frequency_max = 864000000,
1429 .frequency_step = 50000,
1432 .set_config = xc2028_set_config,
1433 .set_analog_params = xc2028_set_analog_freq,
1434 .release = xc2028_dvb_release,
1435 .get_frequency = xc2028_get_frequency,
1436 .get_rf_strength = xc2028_signal,
1437 .get_afc = xc2028_get_afc,
1438 .set_params = xc2028_set_params,
1439 .sleep = xc2028_sleep,
1472 priv->
ctrl.max_len = 13;
1484 memcpy(&fe->
ops.tuner_ops, &xc2028_dvb_tuner_ops,
1485 sizeof(xc2028_dvb_tuner_ops));
1487 tuner_info(
"type set to %s\n",
"XCeive xc2028/xc3028 tuner");
1490 xc2028_set_config(fe, cfg->
ctrl);
1498 xc2028_dvb_release(fe);