50 const u8 *
val,
int len)
56 .addr = priv->
config.i2c_addr,
63 buf[0] = (reg >> 8) & 0xff;
64 buf[1] = (reg >> 0) & 0xff;
72 dev_warn(&priv->
i2c->dev,
"%s: i2c wr failed=%d reg=%04x " \
73 "len=%d\n", KBUILD_MODNAME, ret, reg, len);
92 .addr = priv->
config.i2c_addr,
99 buf[0] = (reg >> 8) & 0xff;
100 buf[1] = (reg >> 0) & 0xff;
107 dev_warn(&priv->
i2c->dev,
"%s: i2c rd failed=%d reg=%04x " \
108 "len=%d\n", KBUILD_MODNAME, ret, reg, len);
119 u8 mbox = (0 << 7)|(0 << 6)|(1 << 1)|(1 << 0);
122 ((reg & 0xff00) != 0xff00) && ((reg & 0xff00) != 0xae00)) {
123 mbox |= ((len - 1) << 2);
124 ret = af9013_wr_regs_i2c(priv, mbox, reg, val, len);
126 for (i = 0; i <
len; i++) {
127 ret = af9013_wr_regs_i2c(priv, mbox, reg+i, val+i, 1);
141 u8 mbox = (0 << 7)|(0 << 6)|(1 << 1)|(0 << 0);
144 ((reg & 0xff00) != 0xff00) && ((reg & 0xff00) != 0xae00)) {
145 mbox |= ((len - 1) << 2);
146 ret = af9013_rd_regs_i2c(priv, mbox, reg, val, len);
148 for (i = 0; i <
len; i++) {
149 ret = af9013_rd_regs_i2c(priv, mbox, reg+i, val+i, 1);
162 return af9013_wr_regs(priv, reg, &val, 1);
168 return af9013_rd_regs(priv, reg, val, 1);
174 u8 mbox = (1 << 7)|(1 << 6)|((len - 1) << 2)|(1 << 1)|(1 << 0);
175 return af9013_wr_regs_i2c(state, mbox, reg, val, len);
186 ret = af9013_rd_reg(state, reg, &tmp);
190 mask = (0xff >> (8 -
len)) <<
pos;
196 return af9013_wr_reg(state, reg, val);
199 static int af9013_rd_reg_bits(
struct af9013_state *state,
u16 reg,
int pos,
205 ret = af9013_rd_reg(state, reg, &tmp);
210 *val &= (0xff >> (8 -
len));
221 dev_dbg(&state->
i2c->dev,
"%s: gpio=%d gpioval=%02x\n",
222 __func__, gpio, gpioval);
240 dev_err(&state->
i2c->dev,
"%s: invalid gpio=%d\n",
241 KBUILD_MODNAME, gpio);
258 ret = af9013_wr_reg_bits(state, addr, pos, 4, gpioval);
264 dev_dbg(&state->
i2c->dev,
"%s: failed=%d\n", __func__, ret);
272 dev_dbg(&state->
i2c->dev,
"%s: a=%d b=%d x=%d\n", __func__, a, b, x);
279 for (i = 0; i <
x; i++) {
287 r = (
c << (
u32)x) +
r;
289 dev_dbg(&state->
i2c->dev,
"%s: a=%d b=%d x=%d r=%d r=%x\n",
290 __func__, a, b, x, r, r);
300 dev_dbg(&state->
i2c->dev,
"%s: onoff=%d\n", __func__, onoff);
303 ret = af9013_wr_reg_bits(state, 0xd417, 4, 1, 1);
308 ret = af9013_wr_reg(state, 0xaeff, 1);
313 for (i = 0; i < 150; i++) {
314 ret = af9013_rd_reg_bits(state, 0xd417, 1, 1, &tmp);
329 ret = af9013_wr_reg_bits(state, 0xd417, 1, 1, 0);
334 ret = af9013_wr_reg_bits(state, 0xd417, 4, 1, 0);
337 ret = af9013_wr_reg_bits(state, 0xd73a, 3, 1, 0);
340 ret = af9013_wr_reg_bits(state, 0xd73a, 3, 1, 1);
345 dev_dbg(&state->
i2c->dev,
"%s: failed=%d\n", __func__, ret);
349 static int af9013_statistics_ber_unc_start(
struct dvb_frontend *fe)
357 ret = af9013_wr_reg_bits(state, 0xd391, 4, 1, 1);
363 dev_dbg(&state->
i2c->dev,
"%s: failed=%d\n", __func__, ret);
367 static int af9013_statistics_ber_unc_result(
struct dvb_frontend *fe)
376 ret = af9013_rd_reg_bits(state, 0xd391, 4, 1, &buf[0]);
381 dev_dbg(&state->
i2c->dev,
"%s: not ready\n", __func__);
385 ret = af9013_rd_regs(state, 0xd387, buf, 5);
389 state->
ber = (buf[2] << 16) | (buf[1] << 8) | buf[0];
390 state->
ucblocks += (buf[4] << 8) | buf[3];
394 dev_dbg(&state->
i2c->dev,
"%s: failed=%d\n", __func__, ret);
398 static int af9013_statistics_snr_start(
struct dvb_frontend *fe)
406 ret = af9013_wr_reg_bits(state, 0xd2e1, 3, 1, 1);
412 dev_dbg(&state->
i2c->dev,
"%s: failed=%d\n", __func__, ret);
416 static int af9013_statistics_snr_result(
struct dvb_frontend *fe)
427 ret = af9013_rd_reg_bits(state, 0xd2e1, 3, 1, &tmp);
432 dev_dbg(&state->
i2c->dev,
"%s: not ready\n", __func__);
437 ret = af9013_rd_regs(state, 0xd2e3, buf, 3);
441 snr_val = (buf[2] << 16) | (buf[1] << 8) | buf[0];
444 ret = af9013_rd_reg(state, 0xd3c1, &tmp);
448 switch ((tmp >> 6) & 3) {
451 snr_lut = qpsk_snr_lut;
455 snr_lut = qam16_snr_lut;
459 snr_lut = qam64_snr_lut;
466 for (i = 0; i < len; i++) {
467 tmp = snr_lut[
i].snr;
469 if (snr_val < snr_lut[i].val)
472 state->
snr = tmp * 10;
476 dev_dbg(&state->
i2c->dev,
"%s: failed=%d\n", __func__, ret);
480 static int af9013_statistics_signal_strength(
struct dvb_frontend *fe)
484 u8 buf[2], rf_gain, if_gain;
492 ret = af9013_rd_regs(state, 0xd07c, buf, 2);
499 signal_strength = (0xffff / \
502 (10 * (rf_gain + if_gain) - \
504 if (signal_strength < 0)
506 else if (signal_strength > 0xffff)
507 signal_strength = 0xffff;
513 dev_dbg(&state->
i2c->dev,
"%s: failed=%d\n", __func__, ret);
521 unsigned int next_msec;
534 af9013_statistics_signal_strength(&state->
fe);
539 af9013_statistics_snr_start(&state->
fe);
544 af9013_statistics_ber_unc_start(&state->
fe);
549 af9013_statistics_snr_result(&state->
fe);
554 af9013_statistics_ber_unc_result(&state->
fe);
564 static int af9013_get_tune_settings(
struct dvb_frontend *fe,
579 bool auto_mode, spec_inv;
581 u32 if_frequency, freq_cw;
583 dev_dbg(&state->
i2c->dev,
"%s: frequency=%d bandwidth_hz=%d\n",
587 if (fe->
ops.tuner_ops.set_params)
588 fe->
ops.tuner_ops.set_params(fe);
599 ret = af9013_wr_regs(state, 0xae00, coeff_lut[i].val,
600 sizeof(coeff_lut[i].val));
606 if (fe->
ops.tuner_ops.get_if_frequency)
607 fe->
ops.tuner_ops.get_if_frequency(fe, &if_frequency);
609 if_frequency = state->
config.if_frequency;
611 dev_dbg(&state->
i2c->dev,
"%s: if_frequency=%d\n",
612 __func__, if_frequency);
614 sampling_freq = if_frequency;
616 while (sampling_freq > (state->
config.clock / 2))
617 sampling_freq -= state->
config.clock;
619 if (sampling_freq < 0) {
621 spec_inv = state->
config.spec_inv;
623 spec_inv = !state->
config.spec_inv;
626 freq_cw = af9013_div(state, sampling_freq, state->
config.clock,
630 freq_cw = 0x800000 - freq_cw;
632 buf[0] = (freq_cw >> 0) & 0xff;
633 buf[1] = (freq_cw >> 8) & 0xff;
634 buf[2] = (freq_cw >> 16) & 0x7f;
636 freq_cw = 0x800000 - freq_cw;
638 buf[3] = (freq_cw >> 0) & 0xff;
639 buf[4] = (freq_cw >> 8) & 0xff;
640 buf[5] = (freq_cw >> 16) & 0x7f;
642 ret = af9013_wr_regs(state, 0xd140, buf, 3);
646 ret = af9013_wr_regs(state, 0x9be7, buf, 6);
652 ret = af9013_wr_reg_bits(state, 0xd330, 3, 1, 1);
657 ret = af9013_wr_reg_bits(state, 0xd507, 6, 1, 0);
662 ret = af9013_wr_reg_bits(state, 0x9bfe, 0, 1, 0);
667 ret = af9013_wr_reg_bits(state, 0x9bc2, 0, 1, 0);
685 dev_dbg(&state->
i2c->dev,
"%s: invalid transmission_mode\n",
706 dev_dbg(&state->
i2c->dev,
"%s: invalid guard_interval\n",
727 dev_dbg(&state->
i2c->dev,
"%s: invalid hierarchy\n", __func__);
744 dev_dbg(&state->
i2c->dev,
"%s: invalid modulation\n", __func__);
770 dev_dbg(&state->
i2c->dev,
"%s: invalid code_rate_HP\n",
796 dev_dbg(&state->
i2c->dev,
"%s: invalid code_rate_LP\n",
811 dev_dbg(&state->
i2c->dev,
"%s: invalid bandwidth_hz\n",
817 ret = af9013_wr_regs(state, 0xd3c0, buf, 3);
823 ret = af9013_wr_reg(state, 0xaefd, 0);
827 dev_dbg(&state->
i2c->dev,
"%s: auto params\n", __func__);
830 ret = af9013_wr_reg(state, 0xaefd, 1);
834 ret = af9013_wr_reg(state, 0xaefe, 0);
838 dev_dbg(&state->
i2c->dev,
"%s: manual params\n", __func__);
842 ret = af9013_wr_reg(state, 0xffff, 0);
852 dev_dbg(&state->
i2c->dev,
"%s: failed=%d\n", __func__, ret);
865 ret = af9013_rd_regs(state, 0xd3c0, buf, 3);
869 switch ((buf[1] >> 6) & 3) {
881 switch ((buf[0] >> 0) & 3) {
889 switch ((buf[0] >> 2) & 3) {
904 switch ((buf[0] >> 4) & 7) {
919 switch ((buf[2] >> 0) & 7) {
937 switch ((buf[2] >> 3) & 7) {
955 switch ((buf[1] >> 2) & 3) {
969 dev_dbg(&state->
i2c->dev,
"%s: failed=%d\n", __func__, ret);
995 ret = af9013_rd_reg_bits(state, 0xd507, 6, 1, &tmp);
1005 ret = af9013_rd_reg_bits(state, 0xd330, 3, 1, &tmp);
1019 dev_dbg(&state->
i2c->dev,
"%s: failed=%d\n", __func__, ret);
1030 static int af9013_read_signal_strength(
struct dvb_frontend *fe,
u16 *strength)
1059 dev_dbg(&state->
i2c->dev,
"%s:\n", __func__);
1062 ret = af9013_power_ctrl(state, 1);
1067 ret = af9013_wr_reg(state, 0xd73a, 0xa4);
1072 ret = af9013_wr_regs(state, 0x9bf2, state->
config.api_version, 4);
1077 switch (state->
config.clock) {
1091 dev_err(&state->
i2c->dev,
"%s: invalid clock\n",
1096 adc_cw = af9013_div(state, state->
config.clock, 1000000ul, 19);
1097 buf[0] = (adc_cw >> 0) & 0xff;
1098 buf[1] = (adc_cw >> 8) & 0xff;
1099 buf[2] = (adc_cw >> 16) & 0xff;
1101 ret = af9013_wr_regs(state, 0xd180, buf, 3);
1105 ret = af9013_wr_reg_bits(state, 0x9bd2, 0, 4, tmp);
1110 ret = af9013_wr_reg(state, 0xd416, 0x14);
1115 ret = af9013_wr_reg_bits(state, 0xd700, 1, 1, 1);
1120 ret = af9013_wr_reg_bits(state, 0xd700, 2, 1, 0);
1125 ret = af9013_wr_reg_bits(state, 0xd371, 1, 1, 1);
1132 ret = af9013_wr_reg_bits(state, 0xd50b, 2, 1, 1);
1137 ret = af9013_wr_reg_bits(state, 0xd500, 3, 1, 1);
1142 ret = af9013_wr_reg_bits(state, 0xd502, 4, 1, 1);
1147 ret = af9013_wr_reg_bits(state, 0xd520, 4, 1, 1);
1152 dev_dbg(&state->
i2c->dev,
"%s: load ofsm settings\n", __func__);
1155 for (i = 0; i <
len; i++) {
1156 ret = af9013_wr_reg_bits(state, init[i].addr, init[i].pos,
1157 init[i].len, init[i].val);
1163 dev_dbg(&state->
i2c->dev,
"%s: load tuner specific settings\n",
1165 switch (state->
config.tuner) {
1168 init = tuner_init_mxl5003d;
1174 init = tuner_init_mxl5005;
1178 init = tuner_init_env77h11d5;
1182 init = tuner_init_mt2060;
1186 init = tuner_init_mc44s803;
1191 init = tuner_init_qt1010;
1195 init = tuner_init_mt2060_2;
1200 init = tuner_init_tda18271;
1205 init = tuner_init_unknown;
1209 for (i = 0; i <
len; i++) {
1210 ret = af9013_wr_reg_bits(state, init[i].addr, init[i].pos,
1211 init[i].len, init[i].val);
1217 ret = af9013_wr_reg_bits(state, 0xd500, 1, 2, state->
config.ts_mode);
1222 ret = af9013_wr_reg_bits(state, 0xd730, 0, 1, 1);
1228 ret = af9013_rd_reg_bits(state, 0x9bee, 0, 1,
1236 ret = af9013_rd_reg(state, 0x9bbd, &state->
rf_50);
1240 ret = af9013_rd_reg(state, 0x9bd0, &state->
rf_80);
1244 ret = af9013_rd_reg(state, 0x9be2, &state->
if_50);
1248 ret = af9013_rd_reg(state, 0x9be4, &state->
if_80);
1254 ret = af9013_wr_reg(state, 0xd2e2, 1);
1259 buf[0] = (10000 >> 0) & 0xff;
1260 buf[1] = (10000 >> 8) & 0xff;
1261 ret = af9013_wr_regs(state, 0xd385, buf, 2);
1266 ret = af9013_wr_reg_bits(state, 0xd392, 1, 1, 1);
1275 dev_dbg(&state->
i2c->dev,
"%s: failed=%d\n", __func__, ret);
1284 dev_dbg(&state->
i2c->dev,
"%s:\n", __func__);
1290 ret = af9013_wr_reg_bits(state, 0xd730, 0, 1, 0);
1295 ret = af9013_power_ctrl(state, 0);
1301 dev_dbg(&state->
i2c->dev,
"%s: failed=%d\n", __func__, ret);
1310 dev_dbg(&state->
i2c->dev,
"%s: enable=%d\n", __func__, enable);
1317 ret = af9013_wr_reg_bits(state, 0xd417, 3, 1, enable);
1319 ret = af9013_wr_reg_bits(state, 0xd607, 2, 1, enable);
1327 dev_dbg(&state->
i2c->dev,
"%s: failed=%d\n", __func__, ret);
1339 static int af9013_download_firmware(
struct af9013_state *state)
1341 int i, len, remaining,
ret;
1350 ret = af9013_rd_reg(state, 0x98be, &val);
1354 dev_dbg(&state->
i2c->dev,
"%s: firmware status=%02x\n",
1360 dev_info(&state->
i2c->dev,
"%s: found a '%s' in cold state, will try " \
1361 "to load a firmware\n",
1362 KBUILD_MODNAME, af9013_ops.info.name);
1367 dev_info(&state->
i2c->dev,
"%s: did not find the firmware " \
1368 "file. (%s) Please see linux/Documentation/dvb/ for " \
1369 "more details on firmware-problems. (%d)\n",
1370 KBUILD_MODNAME, fw_file, ret);
1374 dev_info(&state->
i2c->dev,
"%s: downloading firmware from file '%s'\n",
1375 KBUILD_MODNAME, fw_file);
1378 for (i = 0; i < fw->
size; i++)
1379 checksum += fw->
data[i];
1381 fw_params[0] = checksum >> 8;
1382 fw_params[1] = checksum & 0xff;
1383 fw_params[2] = fw->
size >> 8;
1384 fw_params[3] = fw->
size & 0xff;
1387 ret = af9013_write_ofsm_regs(state, 0x50fc,
1388 fw_params,
sizeof(fw_params));
1392 #define FW_ADDR 0x5100
1394 for (remaining = fw->
size; remaining > 0; remaining -= LEN_MAX) {
1399 ret = af9013_write_ofsm_regs(state,
1400 FW_ADDR + fw->
size - remaining,
1401 (
u8 *) &fw->
data[fw->
size - remaining], len);
1404 "%s: firmware download failed=%d\n",
1405 KBUILD_MODNAME, ret);
1411 ret = af9013_wr_reg(state, 0xe205, 1);
1415 for (i = 0; i < 15; i++) {
1419 ret = af9013_rd_reg(state, 0x98be, &val);
1423 dev_dbg(&state->
i2c->dev,
"%s: firmware status=%02x\n",
1426 if (val == 0x0c || val == 0x04)
1431 dev_err(&state->
i2c->dev,
"%s: firmware did not run\n",
1434 }
else if (val != 0x0c) {
1435 dev_err(&state->
i2c->dev,
"%s: firmware boot timeout\n",
1445 dev_info(&state->
i2c->dev,
"%s: found a '%s' in warm state\n",
1446 KBUILD_MODNAME, af9013_ops.info.name);
1468 ret = af9013_download_firmware(state);
1474 ret = af9013_rd_regs(state, 0x5103, buf, 4);
1478 dev_info(&state->
i2c->dev,
"%s: firmware version %d.%d.%d.%d\n",
1479 KBUILD_MODNAME, buf[0], buf[1], buf[2], buf[3]);
1482 for (i = 0; i <
sizeof(state->
config.gpio); i++) {
1483 ret = af9013_set_gpio(state, i, state->
config.gpio[i]);
1489 memcpy(&state->
fe.ops, &af9013_ops,
1491 state->
fe.demodulator_priv =
state;
1505 .name =
"Afatech AF9013",
1506 .frequency_min = 174000000,
1507 .frequency_max = 862000000,
1508 .frequency_stepsize = 250000,
1509 .frequency_tolerance = 0,
1527 .release = af9013_release,
1529 .init = af9013_init,
1530 .sleep = af9013_sleep,
1532 .get_tune_settings = af9013_get_tune_settings,
1533 .set_frontend = af9013_set_frontend,
1534 .get_frontend = af9013_get_frontend,
1536 .read_status = af9013_read_status,
1537 .read_snr = af9013_read_snr,
1538 .read_signal_strength = af9013_read_signal_strength,
1539 .read_ber = af9013_read_ber,
1540 .read_ucblocks = af9013_read_ucblocks,
1542 .i2c_gate_ctrl = af9013_i2c_gate_ctrl,