1 /* Frontend part of the Linux driver for the Afatech 9005
2 * USB1.1 DVB-T receiver.
4 * Copyright (C) 2007 Luca Olivetti (luca@ventoso.org)
6 * Thanks to Afatech who kindly provided information.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 * see Documentation/dvb/README.dvb-usb for more information
25 #include "af9005-script.h"
28 #include <asm/div64.h>
30 struct af9005_fe_state {
31 struct dvb_usb_device *d;
32 struct dvb_frontend *tuner;
36 /* retraining parameters */
41 u16 original_aci0_if_top;
42 u16 original_aci1_if_top;
43 u16 original_aci0_if_min;
44 u8 original_if_unplug_th;
45 u8 original_rf_unplug_th;
46 u8 original_dtop_if_unplug_th;
47 u8 original_dtop_rf_unplug_th;
50 u32 pre_vit_error_count;
51 u32 pre_vit_bit_count;
53 u32 post_vit_error_count;
54 u32 post_vit_bit_count;
60 unsigned long next_status_check;
61 struct dvb_frontend frontend;
64 static int af9005_write_word_agc(struct dvb_usb_device *d, u16 reghi,
65 u16 reglo, u8 pos, u8 len, u16 value)
70 if ((ret = af9005_write_ofdm_register(d, reglo, (u8) (value & 0xff))))
72 temp = (u8) ((value & 0x0300) >> 8);
73 return af9005_write_register_bits(d, reghi, pos, len,
74 (u8) ((value & 0x300) >> 8));
77 static int af9005_read_word_agc(struct dvb_usb_device *d, u16 reghi,
78 u16 reglo, u8 pos, u8 len, u16 * value)
83 if ((ret = af9005_read_ofdm_register(d, reglo, &temp0)))
85 if ((ret = af9005_read_ofdm_register(d, reghi, &temp1)))
89 *value = ((u16) (temp1 & 0x03) << 8) + (u16) temp0;
92 *value = ((u16) (temp1 & 0x0C) << 6) + (u16) temp0;
95 *value = ((u16) (temp1 & 0x30) << 4) + (u16) temp0;
98 *value = ((u16) (temp1 & 0xC0) << 2) + (u16) temp0;
101 err("invalid pos in read word agc");
108 static int af9005_is_fecmon_available(struct dvb_frontend *fe, int *available)
110 struct af9005_fe_state *state = fe->demodulator_priv;
116 ret = af9005_read_register_bits(state->d, xd_p_fec_vtb_rsd_mon_en,
117 fec_vtb_rsd_mon_en_pos,
118 fec_vtb_rsd_mon_en_len, &temp);
123 af9005_read_register_bits(state->d,
124 xd_p_reg_ofsm_read_rbc_en,
125 reg_ofsm_read_rbc_en_pos,
126 reg_ofsm_read_rbc_en_len, &temp);
136 static int af9005_get_post_vit_err_cw_count(struct dvb_frontend *fe,
137 u32 * post_err_count,
141 struct af9005_fe_state *state = fe->demodulator_priv;
145 u8 temp, temp0, temp1, temp2;
151 /* check if error bit count is ready */
153 af9005_read_register_bits(state->d, xd_r_fec_rsd_ber_rdy,
154 fec_rsd_ber_rdy_pos, fec_rsd_ber_rdy_len,
159 deb_info("rsd counter not ready\n");
162 /* get abort count */
164 af9005_read_ofdm_register(state->d,
165 xd_r_fec_rsd_abort_packet_cnt_7_0,
170 af9005_read_ofdm_register(state->d,
171 xd_r_fec_rsd_abort_packet_cnt_15_8,
175 loc_abort_count = ((u16) temp1 << 8) + temp0;
177 /* get error count */
179 af9005_read_ofdm_register(state->d, xd_r_fec_rsd_bit_err_cnt_7_0,
184 af9005_read_ofdm_register(state->d, xd_r_fec_rsd_bit_err_cnt_15_8,
189 af9005_read_ofdm_register(state->d, xd_r_fec_rsd_bit_err_cnt_23_16,
193 err_count = ((u32) temp2 << 16) + ((u32) temp1 << 8) + temp0;
194 *post_err_count = err_count - (u32) loc_abort_count *8 * 8;
196 /* get RSD packet number */
198 af9005_read_ofdm_register(state->d, xd_p_fec_rsd_packet_unit_7_0,
203 af9005_read_ofdm_register(state->d, xd_p_fec_rsd_packet_unit_15_8,
207 cw_count = ((u32) temp1 << 8) + temp0;
209 err("wrong RSD packet count");
212 deb_info("POST abort count %d err count %d rsd packets %d\n",
213 loc_abort_count, err_count, cw_count);
214 *post_cw_count = cw_count - (u32) loc_abort_count;
215 *abort_count = loc_abort_count;
220 static int af9005_get_post_vit_ber(struct dvb_frontend *fe,
221 u32 * post_err_count, u32 * post_cw_count,
224 u32 loc_cw_count = 0, loc_err_count;
229 af9005_get_post_vit_err_cw_count(fe, &loc_err_count, &loc_cw_count,
233 *post_err_count = loc_err_count;
234 *post_cw_count = loc_cw_count * 204 * 8;
235 *abort_count = loc_abort_count;
240 static int af9005_get_pre_vit_err_bit_count(struct dvb_frontend *fe,
244 struct af9005_fe_state *state = fe->demodulator_priv;
245 u8 temp, temp0, temp1, temp2;
246 u32 super_frame_count, x, bits;
250 af9005_read_register_bits(state->d, xd_r_fec_vtb_ber_rdy,
251 fec_vtb_ber_rdy_pos, fec_vtb_ber_rdy_len,
256 deb_info("viterbi counter not ready\n");
257 return 101; /* ERR_APO_VTB_COUNTER_NOT_READY; */
260 af9005_read_ofdm_register(state->d, xd_r_fec_vtb_err_bit_cnt_7_0,
265 af9005_read_ofdm_register(state->d, xd_r_fec_vtb_err_bit_cnt_15_8,
270 af9005_read_ofdm_register(state->d, xd_r_fec_vtb_err_bit_cnt_23_16,
274 *pre_err_count = ((u32) temp2 << 16) + ((u32) temp1 << 8) + temp0;
277 af9005_read_ofdm_register(state->d, xd_p_fec_super_frm_unit_7_0,
282 af9005_read_ofdm_register(state->d, xd_p_fec_super_frm_unit_15_8,
286 super_frame_count = ((u32) temp1 << 8) + temp0;
287 if (super_frame_count == 0) {
288 deb_info("super frame count 0\n");
294 af9005_read_register_bits(state->d, xd_g_reg_tpsd_txmod,
295 reg_tpsd_txmod_pos, reg_tpsd_txmod_len,
302 } else if (temp == 1) {
306 err("Invalid fft mode");
310 /* read constellation mode */
312 af9005_read_register_bits(state->d, xd_g_reg_tpsd_const,
313 reg_tpsd_const_pos, reg_tpsd_const_len,
328 err("invalid constellation mode");
331 *pre_bit_count = super_frame_count * 68 * 4 * x * bits;
332 deb_info("PRE err count %d frame count %d bit count %d\n",
333 *pre_err_count, super_frame_count, *pre_bit_count);
337 static int af9005_reset_pre_viterbi(struct dvb_frontend *fe)
339 struct af9005_fe_state *state = fe->demodulator_priv;
342 /* set super frame count to 1 */
344 af9005_write_ofdm_register(state->d, xd_p_fec_super_frm_unit_7_0,
348 af9005_write_ofdm_register(state->d, xd_p_fec_super_frm_unit_15_8,
352 /* reset pre viterbi error count */
354 af9005_write_register_bits(state->d, xd_p_fec_vtb_ber_rst,
355 fec_vtb_ber_rst_pos, fec_vtb_ber_rst_len,
361 static int af9005_reset_post_viterbi(struct dvb_frontend *fe)
363 struct af9005_fe_state *state = fe->demodulator_priv;
366 /* set packet unit */
368 af9005_write_ofdm_register(state->d, xd_p_fec_rsd_packet_unit_7_0,
373 af9005_write_ofdm_register(state->d, xd_p_fec_rsd_packet_unit_15_8,
377 /* reset post viterbi error count */
379 af9005_write_register_bits(state->d, xd_p_fec_rsd_ber_rst,
380 fec_rsd_ber_rst_pos, fec_rsd_ber_rst_len,
386 static int af9005_get_statistic(struct dvb_frontend *fe)
388 struct af9005_fe_state *state = fe->demodulator_priv;
389 int ret, fecavailable;
390 u64 numerator, denominator;
392 deb_info("GET STATISTIC\n");
393 ret = af9005_is_fecmon_available(fe, &fecavailable);
397 deb_info("fecmon not available\n");
401 ret = af9005_get_pre_vit_err_bit_count(fe, &state->pre_vit_error_count,
402 &state->pre_vit_bit_count);
404 af9005_reset_pre_viterbi(fe);
405 if (state->pre_vit_bit_count > 0) {
406 /* according to v 0.0.4 of the dvb api ber should be a multiple
407 of 10E-9 so we have to multiply the error count by
410 (u64) state->pre_vit_error_count * (u64) 1000000000;
411 denominator = (u64) state->pre_vit_bit_count;
412 state->ber = do_div(numerator, denominator);
414 state->ber = 0xffffffff;
418 ret = af9005_get_post_vit_ber(fe, &state->post_vit_error_count,
419 &state->post_vit_bit_count,
420 &state->abort_count);
422 ret = af9005_reset_post_viterbi(fe);
423 state->unc += state->abort_count;
430 static int af9005_fe_refresh_state(struct dvb_frontend *fe)
432 struct af9005_fe_state *state = fe->demodulator_priv;
433 if (time_after(jiffies, state->next_status_check)) {
434 deb_info("REFRESH STATE\n");
437 if (af9005_get_statistic(fe))
438 err("get_statistic_failed");
439 state->next_status_check = jiffies + 250 * HZ / 1000;
444 static int af9005_fe_read_status(struct dvb_frontend *fe, fe_status_t * stat)
446 struct af9005_fe_state *state = fe->demodulator_priv;
450 if (state->tuner == NULL)
454 ret = af9005_read_register_bits(state->d, xd_p_agc_lock,
455 agc_lock_pos, agc_lock_len, &temp);
459 *stat |= FE_HAS_SIGNAL;
461 ret = af9005_read_register_bits(state->d, xd_p_fd_tpsd_lock,
462 fd_tpsd_lock_pos, fd_tpsd_lock_len,
467 *stat |= FE_HAS_CARRIER;
469 ret = af9005_read_register_bits(state->d,
470 xd_r_mp2if_sync_byte_locked,
471 mp2if_sync_byte_locked_pos,
472 mp2if_sync_byte_locked_pos, &temp);
476 *stat |= FE_HAS_SYNC | FE_HAS_VITERBI | FE_HAS_LOCK;
478 af9005_led_control(state->d, *stat & FE_HAS_LOCK);
481 af9005_read_register_bits(state->d, xd_p_reg_strong_sginal_detected,
482 reg_strong_sginal_detected_pos,
483 reg_strong_sginal_detected_len, &temp);
486 if (temp != state->strong) {
487 deb_info("adjust for strong signal %d\n", temp);
488 state->strong = temp;
493 static int af9005_fe_read_ber(struct dvb_frontend *fe, u32 * ber)
495 struct af9005_fe_state *state = fe->demodulator_priv;
496 if (state->tuner == NULL)
498 af9005_fe_refresh_state(fe);
503 static int af9005_fe_read_unc_blocks(struct dvb_frontend *fe, u32 * unc)
505 struct af9005_fe_state *state = fe->demodulator_priv;
506 if (state->tuner == NULL)
508 af9005_fe_refresh_state(fe);
513 static int af9005_fe_read_signal_strength(struct dvb_frontend *fe,
516 struct af9005_fe_state *state = fe->demodulator_priv;
520 if (state->tuner == NULL)
523 af9005_read_ofdm_register(state->d, xd_r_reg_aagc_rf_gain,
528 af9005_read_ofdm_register(state->d, xd_r_reg_aagc_if_gain,
532 /* this value has no real meaning, but i don't have the tables that relate
533 the rf and if gain with the dbm, so I just scale the value */
534 *strength = (512 - rf_gain - if_gain) << 7;
538 static int af9005_fe_read_snr(struct dvb_frontend *fe, u16 * snr)
540 /* the snr can be derived from the ber and the constellation
541 but I don't think this kind of complex calculations belong
542 in the driver. I may be wrong.... */
546 static int af9005_fe_program_cfoe(struct dvb_usb_device *d, fe_bandwidth_t bw)
548 u8 temp0, temp1, temp2, temp3, buf[4];
550 u32 NS_coeff1_2048Nu;
551 u32 NS_coeff1_8191Nu;
552 u32 NS_coeff1_8192Nu;
553 u32 NS_coeff1_8193Nu;
558 case BANDWIDTH_6_MHZ:
559 NS_coeff1_2048Nu = 0x2ADB6DC;
560 NS_coeff1_8191Nu = 0xAB7313;
561 NS_coeff1_8192Nu = 0xAB6DB7;
562 NS_coeff1_8193Nu = 0xAB685C;
563 NS_coeff2_2k = 0x156DB6E;
564 NS_coeff2_8k = 0x55B6DC;
567 case BANDWIDTH_7_MHZ:
568 NS_coeff1_2048Nu = 0x3200001;
569 NS_coeff1_8191Nu = 0xC80640;
570 NS_coeff1_8192Nu = 0xC80000;
571 NS_coeff1_8193Nu = 0xC7F9C0;
572 NS_coeff2_2k = 0x1900000;
573 NS_coeff2_8k = 0x640000;
576 case BANDWIDTH_8_MHZ:
577 NS_coeff1_2048Nu = 0x3924926;
578 NS_coeff1_8191Nu = 0xE4996E;
579 NS_coeff1_8192Nu = 0xE49249;
580 NS_coeff1_8193Nu = 0xE48B25;
581 NS_coeff2_2k = 0x1C92493;
582 NS_coeff2_8k = 0x724925;
585 err("Invalid bandwith %d.", bw);
590 * write NS_coeff1_2048Nu
593 temp0 = (u8) (NS_coeff1_2048Nu & 0x000000FF);
594 temp1 = (u8) ((NS_coeff1_2048Nu & 0x0000FF00) >> 8);
595 temp2 = (u8) ((NS_coeff1_2048Nu & 0x00FF0000) >> 16);
596 temp3 = (u8) ((NS_coeff1_2048Nu & 0x03000000) >> 24);
598 /* big endian to make 8051 happy */
604 /* cfoe_NS_2k_coeff1_25_24 */
605 ret = af9005_write_ofdm_register(d, 0xAE00, buf[0]);
609 /* cfoe_NS_2k_coeff1_23_16 */
610 ret = af9005_write_ofdm_register(d, 0xAE01, buf[1]);
614 /* cfoe_NS_2k_coeff1_15_8 */
615 ret = af9005_write_ofdm_register(d, 0xAE02, buf[2]);
619 /* cfoe_NS_2k_coeff1_7_0 */
620 ret = af9005_write_ofdm_register(d, 0xAE03, buf[3]);
628 temp0 = (u8) ((NS_coeff2_2k & 0x0000003F));
629 temp1 = (u8) ((NS_coeff2_2k & 0x00003FC0) >> 6);
630 temp2 = (u8) ((NS_coeff2_2k & 0x003FC000) >> 14);
631 temp3 = (u8) ((NS_coeff2_2k & 0x01C00000) >> 22);
633 /* big endian to make 8051 happy */
639 ret = af9005_write_ofdm_register(d, 0xAE04, buf[0]);
643 ret = af9005_write_ofdm_register(d, 0xAE05, buf[1]);
647 ret = af9005_write_ofdm_register(d, 0xAE06, buf[2]);
651 ret = af9005_write_ofdm_register(d, 0xAE07, buf[3]);
656 * write NS_coeff1_8191Nu
659 temp0 = (u8) ((NS_coeff1_8191Nu & 0x000000FF));
660 temp1 = (u8) ((NS_coeff1_8191Nu & 0x0000FF00) >> 8);
661 temp2 = (u8) ((NS_coeff1_8191Nu & 0x00FFC000) >> 16);
662 temp3 = (u8) ((NS_coeff1_8191Nu & 0x03000000) >> 24);
664 /* big endian to make 8051 happy */
670 ret = af9005_write_ofdm_register(d, 0xAE08, buf[0]);
674 ret = af9005_write_ofdm_register(d, 0xAE09, buf[1]);
678 ret = af9005_write_ofdm_register(d, 0xAE0A, buf[2]);
682 ret = af9005_write_ofdm_register(d, 0xAE0B, buf[3]);
687 * write NS_coeff1_8192Nu
690 temp0 = (u8) (NS_coeff1_8192Nu & 0x000000FF);
691 temp1 = (u8) ((NS_coeff1_8192Nu & 0x0000FF00) >> 8);
692 temp2 = (u8) ((NS_coeff1_8192Nu & 0x00FFC000) >> 16);
693 temp3 = (u8) ((NS_coeff1_8192Nu & 0x03000000) >> 24);
695 /* big endian to make 8051 happy */
701 ret = af9005_write_ofdm_register(d, 0xAE0C, buf[0]);
705 ret = af9005_write_ofdm_register(d, 0xAE0D, buf[1]);
709 ret = af9005_write_ofdm_register(d, 0xAE0E, buf[2]);
713 ret = af9005_write_ofdm_register(d, 0xAE0F, buf[3]);
718 * write NS_coeff1_8193Nu
721 temp0 = (u8) ((NS_coeff1_8193Nu & 0x000000FF));
722 temp1 = (u8) ((NS_coeff1_8193Nu & 0x0000FF00) >> 8);
723 temp2 = (u8) ((NS_coeff1_8193Nu & 0x00FFC000) >> 16);
724 temp3 = (u8) ((NS_coeff1_8193Nu & 0x03000000) >> 24);
726 /* big endian to make 8051 happy */
732 ret = af9005_write_ofdm_register(d, 0xAE10, buf[0]);
736 ret = af9005_write_ofdm_register(d, 0xAE11, buf[1]);
740 ret = af9005_write_ofdm_register(d, 0xAE12, buf[2]);
744 ret = af9005_write_ofdm_register(d, 0xAE13, buf[3]);
752 temp0 = (u8) ((NS_coeff2_8k & 0x0000003F));
753 temp1 = (u8) ((NS_coeff2_8k & 0x00003FC0) >> 6);
754 temp2 = (u8) ((NS_coeff2_8k & 0x003FC000) >> 14);
755 temp3 = (u8) ((NS_coeff2_8k & 0x01C00000) >> 22);
757 /* big endian to make 8051 happy */
763 ret = af9005_write_ofdm_register(d, 0xAE14, buf[0]);
767 ret = af9005_write_ofdm_register(d, 0xAE15, buf[1]);
771 ret = af9005_write_ofdm_register(d, 0xAE16, buf[2]);
775 ret = af9005_write_ofdm_register(d, 0xAE17, buf[3]);
780 static int af9005_fe_select_bw(struct dvb_usb_device *d, fe_bandwidth_t bw)
784 case BANDWIDTH_6_MHZ:
787 case BANDWIDTH_7_MHZ:
790 case BANDWIDTH_8_MHZ:
794 err("Invalid bandwith %d.", bw);
797 return af9005_write_register_bits(d, xd_g_reg_bw, reg_bw_pos,
801 static int af9005_fe_power(struct dvb_frontend *fe, int on)
803 struct af9005_fe_state *state = fe->demodulator_priv;
806 deb_info("power %s tuner\n", on ? "on" : "off");
807 ret = af9005_send_command(state->d, 0x03, &temp, 1, NULL, 0);
811 static struct mt2060_config af9005_mt2060_config = {
815 static struct qt1010_config af9005_qt1010_config = {
819 static int af9005_fe_init(struct dvb_frontend *fe)
821 struct af9005_fe_state *state = fe->demodulator_priv;
822 struct dvb_usb_adapter *adap = fe->dvb->priv;
823 int ret, i, scriptlen;
824 u8 temp, temp0 = 0, temp1 = 0, temp2 = 0;
828 deb_info("in af9005_fe_init\n");
833 af9005_write_register_bits(state->d, xd_I2C_reg_ofdm_rst_en,
836 if ((ret = af9005_write_ofdm_register(state->d, APO_REG_RESET, 0)))
838 /* clear ofdm reset */
839 deb_info("clear ofdm reset\n");
840 for (i = 0; i < 150; i++) {
842 af9005_read_ofdm_register(state->d,
843 xd_I2C_reg_ofdm_rst, &temp)))
845 if (temp & (regmask[reg_ofdm_rst_len - 1] << reg_ofdm_rst_pos))
854 write xd_g_reg_ofsm_clk 7
859 write xd_g_reg_ofsm_clk 0
862 ret = af9005_write_ofdm_register(state->d, 0xb200, 0xa9);
865 ret = af9005_write_ofdm_register(state->d, xd_g_reg_ofsm_clk, 0x07);
869 ret = af9005_send_command(state->d, 0x03, &temp, 1, NULL, 0);
872 ret = af9005_write_ofdm_register(state->d, xd_g_reg_ofsm_clk, 0x00);
875 ret = af9005_write_ofdm_register(state->d, 0xb200, 0xa1);
879 temp = regmask[reg_ofdm_rst_len - 1] << reg_ofdm_rst_pos;
881 af9005_write_register_bits(state->d, xd_I2C_reg_ofdm_rst,
882 reg_ofdm_rst_pos, reg_ofdm_rst_len, 1)))
885 af9005_write_register_bits(state->d, xd_I2C_reg_ofdm_rst,
886 reg_ofdm_rst_pos, reg_ofdm_rst_len, 0)))
891 /* don't know what register aefc is, but this is what the windows driver does */
892 ret = af9005_write_ofdm_register(state->d, 0xaefc, 0);
896 /* set stand alone chip */
897 deb_info("set stand alone chip\n");
899 af9005_write_register_bits(state->d, xd_p_reg_dca_stand_alone,
900 reg_dca_stand_alone_pos,
901 reg_dca_stand_alone_len, 1)))
904 /* set dca upper & lower chip */
905 deb_info("set dca upper & lower chip\n");
907 af9005_write_register_bits(state->d, xd_p_reg_dca_upper_chip,
908 reg_dca_upper_chip_pos,
909 reg_dca_upper_chip_len, 0)))
912 af9005_write_register_bits(state->d, xd_p_reg_dca_lower_chip,
913 reg_dca_lower_chip_pos,
914 reg_dca_lower_chip_len, 0)))
917 /* set 2wire master clock to 0x14 (for 60KHz) */
918 deb_info("set 2wire master clock to 0x14 (for 60KHz)\n");
920 af9005_write_ofdm_register(state->d, xd_I2C_i2c_m_period, 0x14)))
923 /* clear dca enable chip */
924 deb_info("clear dca enable chip\n");
926 af9005_write_register_bits(state->d, xd_p_reg_dca_en,
927 reg_dca_en_pos, reg_dca_en_len, 0)))
929 /* FIXME these are register bits, but I don't know which ones */
930 ret = af9005_write_ofdm_register(state->d, 0xa16c, 1);
933 ret = af9005_write_ofdm_register(state->d, 0xa3c1, 0);
937 /* init other parameters: program cfoe and select bandwith */
938 deb_info("program cfoe\n");
939 if ((ret = af9005_fe_program_cfoe(state->d, BANDWIDTH_6_MHZ)))
941 /* set read-update bit for constellation */
942 deb_info("set read-update bit for constellation\n");
944 af9005_write_register_bits(state->d, xd_p_reg_feq_read_update,
945 reg_feq_read_update_pos,
946 reg_feq_read_update_len, 1)))
949 /* sample code has a set MPEG TS code here
950 but sniffing reveals that it doesn't do it */
952 /* set read-update bit to 1 for DCA constellation */
953 deb_info("set read-update bit 1 for DCA constellation\n");
955 af9005_write_register_bits(state->d, xd_p_reg_dca_read_update,
956 reg_dca_read_update_pos,
957 reg_dca_read_update_len, 1)))
960 /* enable fec monitor */
961 deb_info("enable fec monitor\n");
963 af9005_write_register_bits(state->d, xd_p_fec_vtb_rsd_mon_en,
964 fec_vtb_rsd_mon_en_pos,
965 fec_vtb_rsd_mon_en_len, 1)))
968 /* FIXME should be register bits, I don't know which ones */
969 ret = af9005_write_ofdm_register(state->d, 0xa601, 0);
971 /* set api_retrain_never_freeze */
972 deb_info("set api_retrain_never_freeze\n");
973 if ((ret = af9005_write_ofdm_register(state->d, 0xaefb, 0x01)))
976 /* load init script */
977 deb_info("load init script\n");
978 scriptlen = sizeof(script) / sizeof(RegDesc);
979 for (i = 0; i < scriptlen; i++) {
981 af9005_write_register_bits(state->d, script[i].reg,
983 script[i].len, script[i].val)))
985 /* save 3 bytes of original fcw */
986 if (script[i].reg == 0xae18)
987 temp2 = script[i].val;
988 if (script[i].reg == 0xae19)
989 temp1 = script[i].val;
990 if (script[i].reg == 0xae1a)
991 temp0 = script[i].val;
993 /* save original unplug threshold */
994 if (script[i].reg == xd_p_reg_unplug_th)
995 state->original_if_unplug_th = script[i].val;
996 if (script[i].reg == xd_p_reg_unplug_rf_gain_th)
997 state->original_rf_unplug_th = script[i].val;
998 if (script[i].reg == xd_p_reg_unplug_dtop_if_gain_th)
999 state->original_dtop_if_unplug_th = script[i].val;
1000 if (script[i].reg == xd_p_reg_unplug_dtop_rf_gain_th)
1001 state->original_dtop_rf_unplug_th = script[i].val;
1004 state->original_fcw =
1005 ((u32) temp2 << 16) + ((u32) temp1 << 8) + (u32) temp0;
1008 /* save original TOPs */
1009 deb_info("save original TOPs\n");
1013 af9005_read_word_agc(state->d,
1014 xd_p_reg_aagc_rf_top_numerator_9_8,
1015 xd_p_reg_aagc_rf_top_numerator_7_0, 0, 2,
1016 &state->original_rf_top);
1022 af9005_read_word_agc(state->d,
1023 xd_p_reg_aagc_if_top_numerator_9_8,
1024 xd_p_reg_aagc_if_top_numerator_7_0, 0, 2,
1025 &state->original_if_top);
1031 af9005_read_word_agc(state->d, 0xA60E, 0xA60A, 4, 2,
1032 &state->original_aci0_if_top);
1038 af9005_read_word_agc(state->d, 0xA60E, 0xA60B, 6, 2,
1039 &state->original_aci1_if_top);
1043 /* attach tuner and init */
1044 if (state->tuner == NULL) {
1045 /* read tuner and board id from eeprom */
1046 ret = af9005_read_eeprom(adap->dev, 0xc6, buf, 2);
1048 err("Impossible to read EEPROM\n");
1051 deb_info("Tuner id %d, board id %d\n", buf[0], buf[1]);
1053 case 2: /* MT2060 */
1054 /* read if1 from eeprom */
1055 ret = af9005_read_eeprom(adap->dev, 0xc8, buf, 2);
1057 err("Impossible to read EEPROM\n");
1060 if1 = (u16) (buf[0] << 8) + buf[1];
1062 dvb_attach(mt2060_attach, fe, &adap->dev->i2c_adap,
1063 &af9005_mt2060_config, if1);
1064 if (state->tuner == NULL) {
1065 deb_info("MT2060 attach failed\n");
1069 case 3: /* QT1010 */
1070 case 9: /* QT1010B */
1072 dvb_attach(qt1010_attach, fe, &adap->dev->i2c_adap,
1073 &af9005_qt1010_config);
1074 if (state->tuner == NULL) {
1075 deb_info("QT1010 attach failed\n");
1080 err("Unsupported tuner type %d", buf[0]);
1083 ret = state->tuner->ops.tuner_ops.init(state->tuner);
1088 deb_info("profit!\n");
1092 static int af9005_fe_sleep(struct dvb_frontend *fe)
1094 return af9005_fe_power(fe, 0);
1097 static int af9005_ts_bus_ctrl(struct dvb_frontend *fe, int acquire)
1099 struct af9005_fe_state *state = fe->demodulator_priv;
1107 af9005_led_control(state->d, 0);
1112 static int af9005_fe_set_frontend(struct dvb_frontend *fe,
1113 struct dvb_frontend_parameters *fep)
1115 struct af9005_fe_state *state = fe->demodulator_priv;
1117 u8 temp, temp0, temp1, temp2;
1119 deb_info("af9005_fe_set_frontend freq %d bw %d\n", fep->frequency,
1120 fep->u.ofdm.bandwidth);
1121 if (state->tuner == NULL) {
1122 err("Tuner not attached");
1126 deb_info("turn off led\n");
1127 /* not in the log */
1128 ret = af9005_led_control(state->d, 0);
1131 /* not sure about the bits */
1132 ret = af9005_write_register_bits(state->d, XD_MP2IF_MISC, 2, 1, 0);
1136 /* set FCW to default value */
1137 deb_info("set FCW to default value\n");
1138 temp0 = (u8) (state->original_fcw & 0x000000ff);
1139 temp1 = (u8) ((state->original_fcw & 0x0000ff00) >> 8);
1140 temp2 = (u8) ((state->original_fcw & 0x00ff0000) >> 16);
1141 ret = af9005_write_ofdm_register(state->d, 0xae1a, temp0);
1144 ret = af9005_write_ofdm_register(state->d, 0xae19, temp1);
1147 ret = af9005_write_ofdm_register(state->d, 0xae18, temp2);
1151 /* restore original TOPs */
1152 deb_info("restore original TOPs\n");
1154 af9005_write_word_agc(state->d,
1155 xd_p_reg_aagc_rf_top_numerator_9_8,
1156 xd_p_reg_aagc_rf_top_numerator_7_0, 0, 2,
1157 state->original_rf_top);
1161 af9005_write_word_agc(state->d,
1162 xd_p_reg_aagc_if_top_numerator_9_8,
1163 xd_p_reg_aagc_if_top_numerator_7_0, 0, 2,
1164 state->original_if_top);
1168 af9005_write_word_agc(state->d, 0xA60E, 0xA60A, 4, 2,
1169 state->original_aci0_if_top);
1173 af9005_write_word_agc(state->d, 0xA60E, 0xA60B, 6, 2,
1174 state->original_aci1_if_top);
1178 /* select bandwith */
1179 deb_info("select bandwidth");
1180 ret = af9005_fe_select_bw(state->d, fep->u.ofdm.bandwidth);
1183 ret = af9005_fe_program_cfoe(state->d, fep->u.ofdm.bandwidth);
1187 /* clear easy mode flag */
1188 deb_info("clear easy mode flag\n");
1189 ret = af9005_write_ofdm_register(state->d, 0xaefd, 0);
1193 /* set unplug threshold to original value */
1194 deb_info("set unplug threshold to original value\n");
1196 af9005_write_ofdm_register(state->d, xd_p_reg_unplug_th,
1197 state->original_if_unplug_th);
1201 deb_info("set tuner\n");
1202 ret = state->tuner->ops.tuner_ops.set_params(state->tuner, fep);
1207 deb_info("trigger ofsm\n");
1209 ret = af9005_write_tuner_registers(state->d, 0xffff, &temp, 1);
1213 /* clear retrain and freeze flag */
1214 deb_info("clear retrain and freeze flag\n");
1216 af9005_write_register_bits(state->d,
1217 xd_p_reg_api_retrain_request,
1218 reg_api_retrain_request_pos, 2, 0);
1222 /* reset pre viterbi and post viterbi registers and statistics */
1223 af9005_reset_pre_viterbi(fe);
1224 af9005_reset_post_viterbi(fe);
1225 state->pre_vit_error_count = 0;
1226 state->pre_vit_bit_count = 0;
1228 state->post_vit_error_count = 0;
1229 /* state->unc = 0; commented out since it should be ever increasing */
1230 state->abort_count = 0;
1232 state->next_status_check = jiffies;
1238 static int af9005_fe_get_frontend(struct dvb_frontend *fe,
1239 struct dvb_frontend_parameters *fep)
1241 struct af9005_fe_state *state = fe->demodulator_priv;
1247 af9005_read_register_bits(state->d, xd_g_reg_tpsd_const,
1248 reg_tpsd_const_pos, reg_tpsd_const_len,
1252 deb_info("===== fe_get_frontend ==============\n");
1253 deb_info("CONSTELLATION ");
1256 fep->u.ofdm.constellation = QPSK;
1260 fep->u.ofdm.constellation = QAM_16;
1261 deb_info("QAM_16\n");
1264 fep->u.ofdm.constellation = QAM_64;
1265 deb_info("QAM_64\n");
1269 /* tps hierarchy and alpha value */
1271 af9005_read_register_bits(state->d, xd_g_reg_tpsd_hier,
1272 reg_tpsd_hier_pos, reg_tpsd_hier_len,
1276 deb_info("HIERARCHY ");
1279 fep->u.ofdm.hierarchy_information = HIERARCHY_NONE;
1283 fep->u.ofdm.hierarchy_information = HIERARCHY_1;
1287 fep->u.ofdm.hierarchy_information = HIERARCHY_2;
1291 fep->u.ofdm.hierarchy_information = HIERARCHY_4;
1296 /* high/low priority */
1298 af9005_read_register_bits(state->d, xd_g_reg_dec_pri,
1299 reg_dec_pri_pos, reg_dec_pri_len, &temp);
1302 /* if temp is set = high priority */
1303 deb_info("PRIORITY %s\n", temp ? "high" : "low");
1307 af9005_read_register_bits(state->d, xd_g_reg_tpsd_hpcr,
1308 reg_tpsd_hpcr_pos, reg_tpsd_hpcr_len,
1312 deb_info("CODERATE HP ");
1315 fep->u.ofdm.code_rate_HP = FEC_1_2;
1316 deb_info("FEC_1_2\n");
1319 fep->u.ofdm.code_rate_HP = FEC_2_3;
1320 deb_info("FEC_2_3\n");
1323 fep->u.ofdm.code_rate_HP = FEC_3_4;
1324 deb_info("FEC_3_4\n");
1327 fep->u.ofdm.code_rate_HP = FEC_5_6;
1328 deb_info("FEC_5_6\n");
1331 fep->u.ofdm.code_rate_HP = FEC_7_8;
1332 deb_info("FEC_7_8\n");
1338 af9005_read_register_bits(state->d, xd_g_reg_tpsd_lpcr,
1339 reg_tpsd_lpcr_pos, reg_tpsd_lpcr_len,
1343 deb_info("CODERATE LP ");
1346 fep->u.ofdm.code_rate_LP = FEC_1_2;
1347 deb_info("FEC_1_2\n");
1350 fep->u.ofdm.code_rate_LP = FEC_2_3;
1351 deb_info("FEC_2_3\n");
1354 fep->u.ofdm.code_rate_LP = FEC_3_4;
1355 deb_info("FEC_3_4\n");
1358 fep->u.ofdm.code_rate_LP = FEC_5_6;
1359 deb_info("FEC_5_6\n");
1362 fep->u.ofdm.code_rate_LP = FEC_7_8;
1363 deb_info("FEC_7_8\n");
1367 /* guard interval */
1369 af9005_read_register_bits(state->d, xd_g_reg_tpsd_gi,
1370 reg_tpsd_gi_pos, reg_tpsd_gi_len, &temp);
1373 deb_info("GUARD INTERVAL ");
1376 fep->u.ofdm.guard_interval = GUARD_INTERVAL_1_32;
1380 fep->u.ofdm.guard_interval = GUARD_INTERVAL_1_16;
1384 fep->u.ofdm.guard_interval = GUARD_INTERVAL_1_8;
1388 fep->u.ofdm.guard_interval = GUARD_INTERVAL_1_4;
1395 af9005_read_register_bits(state->d, xd_g_reg_tpsd_txmod,
1396 reg_tpsd_txmod_pos, reg_tpsd_txmod_len,
1400 deb_info("TRANSMISSION MODE ");
1403 fep->u.ofdm.transmission_mode = TRANSMISSION_MODE_2K;
1407 fep->u.ofdm.transmission_mode = TRANSMISSION_MODE_8K;
1414 af9005_read_register_bits(state->d, xd_g_reg_bw, reg_bw_pos,
1416 deb_info("BANDWIDTH ");
1419 fep->u.ofdm.bandwidth = BANDWIDTH_6_MHZ;
1423 fep->u.ofdm.bandwidth = BANDWIDTH_7_MHZ;
1427 fep->u.ofdm.bandwidth = BANDWIDTH_8_MHZ;
1434 static void af9005_fe_release(struct dvb_frontend *fe)
1436 struct af9005_fe_state *state =
1437 (struct af9005_fe_state *)fe->demodulator_priv;
1438 if (state->tuner != NULL && state->tuner->ops.tuner_ops.release != NULL) {
1439 state->tuner->ops.tuner_ops.release(state->tuner);
1440 #ifdef CONFIG_DVB_CORE_ATTACH
1441 symbol_put_addr(state->tuner->ops.tuner_ops.release);
1447 static struct dvb_frontend_ops af9005_fe_ops;
1449 struct dvb_frontend *af9005_fe_attach(struct dvb_usb_device *d)
1451 struct af9005_fe_state *state = NULL;
1453 /* allocate memory for the internal state */
1454 state = kzalloc(sizeof(struct af9005_fe_state), GFP_KERNEL);
1458 deb_info("attaching frontend af9005\n");
1461 state->tuner = NULL;
1464 memcpy(&state->frontend.ops, &af9005_fe_ops,
1465 sizeof(struct dvb_frontend_ops));
1466 state->frontend.demodulator_priv = state;
1468 return &state->frontend;
1473 static struct dvb_frontend_ops af9005_fe_ops = {
1475 .name = "AF9005 USB DVB-T",
1477 .frequency_min = 44250000,
1478 .frequency_max = 867250000,
1479 .frequency_stepsize = 250000,
1480 .caps = FE_CAN_INVERSION_AUTO |
1481 FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
1482 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
1483 FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 |
1484 FE_CAN_QAM_AUTO | FE_CAN_TRANSMISSION_MODE_AUTO |
1485 FE_CAN_GUARD_INTERVAL_AUTO | FE_CAN_RECOVER |
1486 FE_CAN_HIERARCHY_AUTO,
1489 .release = af9005_fe_release,
1491 .init = af9005_fe_init,
1492 .sleep = af9005_fe_sleep,
1493 .ts_bus_ctrl = af9005_ts_bus_ctrl,
1495 .set_frontend = af9005_fe_set_frontend,
1496 .get_frontend = af9005_fe_get_frontend,
1498 .read_status = af9005_fe_read_status,
1499 .read_ber = af9005_fe_read_ber,
1500 .read_signal_strength = af9005_fe_read_signal_strength,
1501 .read_snr = af9005_fe_read_snr,
1502 .read_ucblocks = af9005_fe_read_unc_blocks,