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 * see Documentation/media/dvb-drivers/dvb-usb.rst for more information
21 #include "af9005-script.h"
24 #include <asm/div64.h>
26 struct af9005_fe_state
{
27 struct dvb_usb_device
*d
;
30 /* retraining parameters */
35 u16 original_aci0_if_top
;
36 u16 original_aci1_if_top
;
37 u16 original_aci0_if_min
;
38 u8 original_if_unplug_th
;
39 u8 original_rf_unplug_th
;
40 u8 original_dtop_if_unplug_th
;
41 u8 original_dtop_rf_unplug_th
;
44 u32 pre_vit_error_count
;
45 u32 pre_vit_bit_count
;
47 u32 post_vit_error_count
;
48 u32 post_vit_bit_count
;
54 unsigned long next_status_check
;
55 struct dvb_frontend frontend
;
58 static int af9005_write_word_agc(struct dvb_usb_device
*d
, u16 reghi
,
59 u16 reglo
, u8 pos
, u8 len
, u16 value
)
63 if ((ret
= af9005_write_ofdm_register(d
, reglo
, (u8
) (value
& 0xff))))
65 return af9005_write_register_bits(d
, reghi
, pos
, len
,
66 (u8
) ((value
& 0x300) >> 8));
69 static int af9005_read_word_agc(struct dvb_usb_device
*d
, u16 reghi
,
70 u16 reglo
, u8 pos
, u8 len
, u16
* value
)
75 if ((ret
= af9005_read_ofdm_register(d
, reglo
, &temp0
)))
77 if ((ret
= af9005_read_ofdm_register(d
, reghi
, &temp1
)))
81 *value
= ((u16
) (temp1
& 0x03) << 8) + (u16
) temp0
;
84 *value
= ((u16
) (temp1
& 0x0C) << 6) + (u16
) temp0
;
87 *value
= ((u16
) (temp1
& 0x30) << 4) + (u16
) temp0
;
90 *value
= ((u16
) (temp1
& 0xC0) << 2) + (u16
) temp0
;
93 err("invalid pos in read word agc");
100 static int af9005_is_fecmon_available(struct dvb_frontend
*fe
, int *available
)
102 struct af9005_fe_state
*state
= fe
->demodulator_priv
;
108 ret
= af9005_read_register_bits(state
->d
, xd_p_fec_vtb_rsd_mon_en
,
109 fec_vtb_rsd_mon_en_pos
,
110 fec_vtb_rsd_mon_en_len
, &temp
);
115 af9005_read_register_bits(state
->d
,
116 xd_p_reg_ofsm_read_rbc_en
,
117 reg_ofsm_read_rbc_en_pos
,
118 reg_ofsm_read_rbc_en_len
, &temp
);
128 static int af9005_get_post_vit_err_cw_count(struct dvb_frontend
*fe
,
129 u32
* post_err_count
,
133 struct af9005_fe_state
*state
= fe
->demodulator_priv
;
137 u8 temp
, temp0
, temp1
, temp2
;
143 /* check if error bit count is ready */
145 af9005_read_register_bits(state
->d
, xd_r_fec_rsd_ber_rdy
,
146 fec_rsd_ber_rdy_pos
, fec_rsd_ber_rdy_len
,
151 deb_info("rsd counter not ready\n");
154 /* get abort count */
156 af9005_read_ofdm_register(state
->d
,
157 xd_r_fec_rsd_abort_packet_cnt_7_0
,
162 af9005_read_ofdm_register(state
->d
,
163 xd_r_fec_rsd_abort_packet_cnt_15_8
,
167 loc_abort_count
= ((u16
) temp1
<< 8) + temp0
;
169 /* get error count */
171 af9005_read_ofdm_register(state
->d
, xd_r_fec_rsd_bit_err_cnt_7_0
,
176 af9005_read_ofdm_register(state
->d
, xd_r_fec_rsd_bit_err_cnt_15_8
,
181 af9005_read_ofdm_register(state
->d
, xd_r_fec_rsd_bit_err_cnt_23_16
,
185 err_count
= ((u32
) temp2
<< 16) + ((u32
) temp1
<< 8) + temp0
;
186 *post_err_count
= err_count
- (u32
) loc_abort_count
*8 * 8;
188 /* get RSD packet number */
190 af9005_read_ofdm_register(state
->d
, xd_p_fec_rsd_packet_unit_7_0
,
195 af9005_read_ofdm_register(state
->d
, xd_p_fec_rsd_packet_unit_15_8
,
199 cw_count
= ((u32
) temp1
<< 8) + temp0
;
201 err("wrong RSD packet count");
204 deb_info("POST abort count %d err count %d rsd packets %d\n",
205 loc_abort_count
, err_count
, cw_count
);
206 *post_cw_count
= cw_count
- (u32
) loc_abort_count
;
207 *abort_count
= loc_abort_count
;
212 static int af9005_get_post_vit_ber(struct dvb_frontend
*fe
,
213 u32
* post_err_count
, u32
* post_cw_count
,
216 u32 loc_cw_count
= 0, loc_err_count
;
217 u16 loc_abort_count
= 0;
221 af9005_get_post_vit_err_cw_count(fe
, &loc_err_count
, &loc_cw_count
,
225 *post_err_count
= loc_err_count
;
226 *post_cw_count
= loc_cw_count
* 204 * 8;
227 *abort_count
= loc_abort_count
;
232 static int af9005_get_pre_vit_err_bit_count(struct dvb_frontend
*fe
,
236 struct af9005_fe_state
*state
= fe
->demodulator_priv
;
237 u8 temp
, temp0
, temp1
, temp2
;
238 u32 super_frame_count
, x
, bits
;
242 af9005_read_register_bits(state
->d
, xd_r_fec_vtb_ber_rdy
,
243 fec_vtb_ber_rdy_pos
, fec_vtb_ber_rdy_len
,
248 deb_info("viterbi counter not ready\n");
249 return 101; /* ERR_APO_VTB_COUNTER_NOT_READY; */
252 af9005_read_ofdm_register(state
->d
, xd_r_fec_vtb_err_bit_cnt_7_0
,
257 af9005_read_ofdm_register(state
->d
, xd_r_fec_vtb_err_bit_cnt_15_8
,
262 af9005_read_ofdm_register(state
->d
, xd_r_fec_vtb_err_bit_cnt_23_16
,
266 *pre_err_count
= ((u32
) temp2
<< 16) + ((u32
) temp1
<< 8) + temp0
;
269 af9005_read_ofdm_register(state
->d
, xd_p_fec_super_frm_unit_7_0
,
274 af9005_read_ofdm_register(state
->d
, xd_p_fec_super_frm_unit_15_8
,
278 super_frame_count
= ((u32
) temp1
<< 8) + temp0
;
279 if (super_frame_count
== 0) {
280 deb_info("super frame count 0\n");
286 af9005_read_register_bits(state
->d
, xd_g_reg_tpsd_txmod
,
287 reg_tpsd_txmod_pos
, reg_tpsd_txmod_len
,
294 } else if (temp
== 1) {
298 err("Invalid fft mode");
302 /* read modulation mode */
304 af9005_read_register_bits(state
->d
, xd_g_reg_tpsd_const
,
305 reg_tpsd_const_pos
, reg_tpsd_const_len
,
320 err("invalid modulation mode");
323 *pre_bit_count
= super_frame_count
* 68 * 4 * x
* bits
;
324 deb_info("PRE err count %d frame count %d bit count %d\n",
325 *pre_err_count
, super_frame_count
, *pre_bit_count
);
329 static int af9005_reset_pre_viterbi(struct dvb_frontend
*fe
)
331 struct af9005_fe_state
*state
= fe
->demodulator_priv
;
334 /* set super frame count to 1 */
336 af9005_write_ofdm_register(state
->d
, xd_p_fec_super_frm_unit_7_0
,
340 ret
= af9005_write_ofdm_register(state
->d
, xd_p_fec_super_frm_unit_15_8
,
344 /* reset pre viterbi error count */
346 af9005_write_register_bits(state
->d
, xd_p_fec_vtb_ber_rst
,
347 fec_vtb_ber_rst_pos
, fec_vtb_ber_rst_len
,
353 static int af9005_reset_post_viterbi(struct dvb_frontend
*fe
)
355 struct af9005_fe_state
*state
= fe
->demodulator_priv
;
358 /* set packet unit */
360 af9005_write_ofdm_register(state
->d
, xd_p_fec_rsd_packet_unit_7_0
,
365 af9005_write_ofdm_register(state
->d
, xd_p_fec_rsd_packet_unit_15_8
,
369 /* reset post viterbi error count */
371 af9005_write_register_bits(state
->d
, xd_p_fec_rsd_ber_rst
,
372 fec_rsd_ber_rst_pos
, fec_rsd_ber_rst_len
,
378 static int af9005_get_statistic(struct dvb_frontend
*fe
)
380 struct af9005_fe_state
*state
= fe
->demodulator_priv
;
381 int ret
, fecavailable
;
382 u64 numerator
, denominator
;
384 deb_info("GET STATISTIC\n");
385 ret
= af9005_is_fecmon_available(fe
, &fecavailable
);
389 deb_info("fecmon not available\n");
393 ret
= af9005_get_pre_vit_err_bit_count(fe
, &state
->pre_vit_error_count
,
394 &state
->pre_vit_bit_count
);
396 af9005_reset_pre_viterbi(fe
);
397 if (state
->pre_vit_bit_count
> 0) {
398 /* according to v 0.0.4 of the dvb api ber should be a multiple
399 of 10E-9 so we have to multiply the error count by
402 (u64
) state
->pre_vit_error_count
* (u64
) 1000000000;
403 denominator
= (u64
) state
->pre_vit_bit_count
;
404 state
->ber
= do_div(numerator
, denominator
);
406 state
->ber
= 0xffffffff;
410 ret
= af9005_get_post_vit_ber(fe
, &state
->post_vit_error_count
,
411 &state
->post_vit_bit_count
,
412 &state
->abort_count
);
414 ret
= af9005_reset_post_viterbi(fe
);
415 state
->unc
+= state
->abort_count
;
422 static int af9005_fe_refresh_state(struct dvb_frontend
*fe
)
424 struct af9005_fe_state
*state
= fe
->demodulator_priv
;
425 if (time_after(jiffies
, state
->next_status_check
)) {
426 deb_info("REFRESH STATE\n");
429 if (af9005_get_statistic(fe
))
430 err("get_statistic_failed");
431 state
->next_status_check
= jiffies
+ 250 * HZ
/ 1000;
436 static int af9005_fe_read_status(struct dvb_frontend
*fe
,
437 enum fe_status
*stat
)
439 struct af9005_fe_state
*state
= fe
->demodulator_priv
;
443 if (fe
->ops
.tuner_ops
.release
== NULL
)
447 ret
= af9005_read_register_bits(state
->d
, xd_p_agc_lock
,
448 agc_lock_pos
, agc_lock_len
, &temp
);
452 *stat
|= FE_HAS_SIGNAL
;
454 ret
= af9005_read_register_bits(state
->d
, xd_p_fd_tpsd_lock
,
455 fd_tpsd_lock_pos
, fd_tpsd_lock_len
,
460 *stat
|= FE_HAS_CARRIER
;
462 ret
= af9005_read_register_bits(state
->d
,
463 xd_r_mp2if_sync_byte_locked
,
464 mp2if_sync_byte_locked_pos
,
465 mp2if_sync_byte_locked_pos
, &temp
);
469 *stat
|= FE_HAS_SYNC
| FE_HAS_VITERBI
| FE_HAS_LOCK
;
471 af9005_led_control(state
->d
, *stat
& FE_HAS_LOCK
);
474 af9005_read_register_bits(state
->d
, xd_p_reg_strong_sginal_detected
,
475 reg_strong_sginal_detected_pos
,
476 reg_strong_sginal_detected_len
, &temp
);
479 if (temp
!= state
->strong
) {
480 deb_info("adjust for strong signal %d\n", temp
);
481 state
->strong
= temp
;
486 static int af9005_fe_read_ber(struct dvb_frontend
*fe
, u32
* ber
)
488 struct af9005_fe_state
*state
= fe
->demodulator_priv
;
489 if (fe
->ops
.tuner_ops
.release
== NULL
)
491 af9005_fe_refresh_state(fe
);
496 static int af9005_fe_read_unc_blocks(struct dvb_frontend
*fe
, u32
* unc
)
498 struct af9005_fe_state
*state
= fe
->demodulator_priv
;
499 if (fe
->ops
.tuner_ops
.release
== NULL
)
501 af9005_fe_refresh_state(fe
);
506 static int af9005_fe_read_signal_strength(struct dvb_frontend
*fe
,
509 struct af9005_fe_state
*state
= fe
->demodulator_priv
;
513 if (fe
->ops
.tuner_ops
.release
== NULL
)
516 af9005_read_ofdm_register(state
->d
, xd_r_reg_aagc_rf_gain
,
521 af9005_read_ofdm_register(state
->d
, xd_r_reg_aagc_if_gain
,
525 /* this value has no real meaning, but i don't have the tables that relate
526 the rf and if gain with the dbm, so I just scale the value */
527 *strength
= (512 - rf_gain
- if_gain
) << 7;
531 static int af9005_fe_read_snr(struct dvb_frontend
*fe
, u16
* snr
)
533 /* the snr can be derived from the ber and the modulation
534 but I don't think this kind of complex calculations belong
535 in the driver. I may be wrong.... */
539 static int af9005_fe_program_cfoe(struct dvb_usb_device
*d
, u32 bw
)
541 u8 temp0
, temp1
, temp2
, temp3
, buf
[4];
543 u32 NS_coeff1_2048Nu
;
544 u32 NS_coeff1_8191Nu
;
545 u32 NS_coeff1_8192Nu
;
546 u32 NS_coeff1_8193Nu
;
552 NS_coeff1_2048Nu
= 0x2ADB6DC;
553 NS_coeff1_8191Nu
= 0xAB7313;
554 NS_coeff1_8192Nu
= 0xAB6DB7;
555 NS_coeff1_8193Nu
= 0xAB685C;
556 NS_coeff2_2k
= 0x156DB6E;
557 NS_coeff2_8k
= 0x55B6DC;
561 NS_coeff1_2048Nu
= 0x3200001;
562 NS_coeff1_8191Nu
= 0xC80640;
563 NS_coeff1_8192Nu
= 0xC80000;
564 NS_coeff1_8193Nu
= 0xC7F9C0;
565 NS_coeff2_2k
= 0x1900000;
566 NS_coeff2_8k
= 0x640000;
570 NS_coeff1_2048Nu
= 0x3924926;
571 NS_coeff1_8191Nu
= 0xE4996E;
572 NS_coeff1_8192Nu
= 0xE49249;
573 NS_coeff1_8193Nu
= 0xE48B25;
574 NS_coeff2_2k
= 0x1C92493;
575 NS_coeff2_8k
= 0x724925;
578 err("Invalid bandwidth %d.", bw
);
583 * write NS_coeff1_2048Nu
586 temp0
= (u8
) (NS_coeff1_2048Nu
& 0x000000FF);
587 temp1
= (u8
) ((NS_coeff1_2048Nu
& 0x0000FF00) >> 8);
588 temp2
= (u8
) ((NS_coeff1_2048Nu
& 0x00FF0000) >> 16);
589 temp3
= (u8
) ((NS_coeff1_2048Nu
& 0x03000000) >> 24);
591 /* big endian to make 8051 happy */
597 /* cfoe_NS_2k_coeff1_25_24 */
598 ret
= af9005_write_ofdm_register(d
, 0xAE00, buf
[0]);
602 /* cfoe_NS_2k_coeff1_23_16 */
603 ret
= af9005_write_ofdm_register(d
, 0xAE01, buf
[1]);
607 /* cfoe_NS_2k_coeff1_15_8 */
608 ret
= af9005_write_ofdm_register(d
, 0xAE02, buf
[2]);
612 /* cfoe_NS_2k_coeff1_7_0 */
613 ret
= af9005_write_ofdm_register(d
, 0xAE03, buf
[3]);
621 temp0
= (u8
) ((NS_coeff2_2k
& 0x0000003F));
622 temp1
= (u8
) ((NS_coeff2_2k
& 0x00003FC0) >> 6);
623 temp2
= (u8
) ((NS_coeff2_2k
& 0x003FC000) >> 14);
624 temp3
= (u8
) ((NS_coeff2_2k
& 0x01C00000) >> 22);
626 /* big endian to make 8051 happy */
632 ret
= af9005_write_ofdm_register(d
, 0xAE04, buf
[0]);
636 ret
= af9005_write_ofdm_register(d
, 0xAE05, buf
[1]);
640 ret
= af9005_write_ofdm_register(d
, 0xAE06, buf
[2]);
644 ret
= af9005_write_ofdm_register(d
, 0xAE07, buf
[3]);
649 * write NS_coeff1_8191Nu
652 temp0
= (u8
) ((NS_coeff1_8191Nu
& 0x000000FF));
653 temp1
= (u8
) ((NS_coeff1_8191Nu
& 0x0000FF00) >> 8);
654 temp2
= (u8
) ((NS_coeff1_8191Nu
& 0x00FFC000) >> 16);
655 temp3
= (u8
) ((NS_coeff1_8191Nu
& 0x03000000) >> 24);
657 /* big endian to make 8051 happy */
663 ret
= af9005_write_ofdm_register(d
, 0xAE08, buf
[0]);
667 ret
= af9005_write_ofdm_register(d
, 0xAE09, buf
[1]);
671 ret
= af9005_write_ofdm_register(d
, 0xAE0A, buf
[2]);
675 ret
= af9005_write_ofdm_register(d
, 0xAE0B, buf
[3]);
680 * write NS_coeff1_8192Nu
683 temp0
= (u8
) (NS_coeff1_8192Nu
& 0x000000FF);
684 temp1
= (u8
) ((NS_coeff1_8192Nu
& 0x0000FF00) >> 8);
685 temp2
= (u8
) ((NS_coeff1_8192Nu
& 0x00FFC000) >> 16);
686 temp3
= (u8
) ((NS_coeff1_8192Nu
& 0x03000000) >> 24);
688 /* big endian to make 8051 happy */
694 ret
= af9005_write_ofdm_register(d
, 0xAE0C, buf
[0]);
698 ret
= af9005_write_ofdm_register(d
, 0xAE0D, buf
[1]);
702 ret
= af9005_write_ofdm_register(d
, 0xAE0E, buf
[2]);
706 ret
= af9005_write_ofdm_register(d
, 0xAE0F, buf
[3]);
711 * write NS_coeff1_8193Nu
714 temp0
= (u8
) ((NS_coeff1_8193Nu
& 0x000000FF));
715 temp1
= (u8
) ((NS_coeff1_8193Nu
& 0x0000FF00) >> 8);
716 temp2
= (u8
) ((NS_coeff1_8193Nu
& 0x00FFC000) >> 16);
717 temp3
= (u8
) ((NS_coeff1_8193Nu
& 0x03000000) >> 24);
719 /* big endian to make 8051 happy */
725 ret
= af9005_write_ofdm_register(d
, 0xAE10, buf
[0]);
729 ret
= af9005_write_ofdm_register(d
, 0xAE11, buf
[1]);
733 ret
= af9005_write_ofdm_register(d
, 0xAE12, buf
[2]);
737 ret
= af9005_write_ofdm_register(d
, 0xAE13, buf
[3]);
745 temp0
= (u8
) ((NS_coeff2_8k
& 0x0000003F));
746 temp1
= (u8
) ((NS_coeff2_8k
& 0x00003FC0) >> 6);
747 temp2
= (u8
) ((NS_coeff2_8k
& 0x003FC000) >> 14);
748 temp3
= (u8
) ((NS_coeff2_8k
& 0x01C00000) >> 22);
750 /* big endian to make 8051 happy */
756 ret
= af9005_write_ofdm_register(d
, 0xAE14, buf
[0]);
760 ret
= af9005_write_ofdm_register(d
, 0xAE15, buf
[1]);
764 ret
= af9005_write_ofdm_register(d
, 0xAE16, buf
[2]);
768 ret
= af9005_write_ofdm_register(d
, 0xAE17, buf
[3]);
773 static int af9005_fe_select_bw(struct dvb_usb_device
*d
, u32 bw
)
787 err("Invalid bandwidth %d.", bw
);
790 return af9005_write_register_bits(d
, xd_g_reg_bw
, reg_bw_pos
,
794 static int af9005_fe_power(struct dvb_frontend
*fe
, int on
)
796 struct af9005_fe_state
*state
= fe
->demodulator_priv
;
799 deb_info("power %s tuner\n", on
? "on" : "off");
800 ret
= af9005_send_command(state
->d
, 0x03, &temp
, 1, NULL
, 0);
804 static struct mt2060_config af9005_mt2060_config
= {
808 static struct qt1010_config af9005_qt1010_config
= {
812 static int af9005_fe_init(struct dvb_frontend
*fe
)
814 struct af9005_fe_state
*state
= fe
->demodulator_priv
;
815 struct dvb_usb_adapter
*adap
= fe
->dvb
->priv
;
816 int ret
, i
, scriptlen
;
817 u8 temp
, temp0
= 0, temp1
= 0, temp2
= 0;
821 deb_info("in af9005_fe_init\n");
826 af9005_write_register_bits(state
->d
, xd_I2C_reg_ofdm_rst_en
,
829 if ((ret
= af9005_write_ofdm_register(state
->d
, APO_REG_RESET
, 0)))
831 /* clear ofdm reset */
832 deb_info("clear ofdm reset\n");
833 for (i
= 0; i
< 150; i
++) {
835 af9005_read_ofdm_register(state
->d
,
836 xd_I2C_reg_ofdm_rst
, &temp
)))
838 if (temp
& (regmask
[reg_ofdm_rst_len
- 1] << reg_ofdm_rst_pos
))
847 write xd_g_reg_ofsm_clk 7
852 write xd_g_reg_ofsm_clk 0
855 ret
= af9005_write_ofdm_register(state
->d
, 0xb200, 0xa9);
858 ret
= af9005_write_ofdm_register(state
->d
, xd_g_reg_ofsm_clk
, 0x07);
862 ret
= af9005_send_command(state
->d
, 0x03, &temp
, 1, NULL
, 0);
865 ret
= af9005_write_ofdm_register(state
->d
, xd_g_reg_ofsm_clk
, 0x00);
868 ret
= af9005_write_ofdm_register(state
->d
, 0xb200, 0xa1);
872 temp
= regmask
[reg_ofdm_rst_len
- 1] << reg_ofdm_rst_pos
;
874 af9005_write_register_bits(state
->d
, xd_I2C_reg_ofdm_rst
,
875 reg_ofdm_rst_pos
, reg_ofdm_rst_len
, 1)))
877 ret
= af9005_write_register_bits(state
->d
, xd_I2C_reg_ofdm_rst
,
878 reg_ofdm_rst_pos
, reg_ofdm_rst_len
, 0);
882 /* don't know what register aefc is, but this is what the windows driver does */
883 ret
= af9005_write_ofdm_register(state
->d
, 0xaefc, 0);
887 /* set stand alone chip */
888 deb_info("set stand alone chip\n");
890 af9005_write_register_bits(state
->d
, xd_p_reg_dca_stand_alone
,
891 reg_dca_stand_alone_pos
,
892 reg_dca_stand_alone_len
, 1)))
895 /* set dca upper & lower chip */
896 deb_info("set dca upper & lower chip\n");
898 af9005_write_register_bits(state
->d
, xd_p_reg_dca_upper_chip
,
899 reg_dca_upper_chip_pos
,
900 reg_dca_upper_chip_len
, 0)))
903 af9005_write_register_bits(state
->d
, xd_p_reg_dca_lower_chip
,
904 reg_dca_lower_chip_pos
,
905 reg_dca_lower_chip_len
, 0)))
908 /* set 2wire master clock to 0x14 (for 60KHz) */
909 deb_info("set 2wire master clock to 0x14 (for 60KHz)\n");
911 af9005_write_ofdm_register(state
->d
, xd_I2C_i2c_m_period
, 0x14)))
914 /* clear dca enable chip */
915 deb_info("clear dca enable chip\n");
917 af9005_write_register_bits(state
->d
, xd_p_reg_dca_en
,
918 reg_dca_en_pos
, reg_dca_en_len
, 0)))
920 /* FIXME these are register bits, but I don't know which ones */
921 ret
= af9005_write_ofdm_register(state
->d
, 0xa16c, 1);
924 ret
= af9005_write_ofdm_register(state
->d
, 0xa3c1, 0);
928 /* init other parameters: program cfoe and select bandwidth */
929 deb_info("program cfoe\n");
930 ret
= af9005_fe_program_cfoe(state
->d
, 6000000);
933 /* set read-update bit for modulation */
934 deb_info("set read-update bit for modulation\n");
936 af9005_write_register_bits(state
->d
, xd_p_reg_feq_read_update
,
937 reg_feq_read_update_pos
,
938 reg_feq_read_update_len
, 1)))
941 /* sample code has a set MPEG TS code here
942 but sniffing reveals that it doesn't do it */
944 /* set read-update bit to 1 for DCA modulation */
945 deb_info("set read-update bit 1 for DCA modulation\n");
947 af9005_write_register_bits(state
->d
, xd_p_reg_dca_read_update
,
948 reg_dca_read_update_pos
,
949 reg_dca_read_update_len
, 1)))
952 /* enable fec monitor */
953 deb_info("enable fec monitor\n");
955 af9005_write_register_bits(state
->d
, xd_p_fec_vtb_rsd_mon_en
,
956 fec_vtb_rsd_mon_en_pos
,
957 fec_vtb_rsd_mon_en_len
, 1)))
960 /* FIXME should be register bits, I don't know which ones */
961 ret
= af9005_write_ofdm_register(state
->d
, 0xa601, 0);
963 /* set api_retrain_never_freeze */
964 deb_info("set api_retrain_never_freeze\n");
965 if ((ret
= af9005_write_ofdm_register(state
->d
, 0xaefb, 0x01)))
968 /* load init script */
969 deb_info("load init script\n");
970 scriptlen
= sizeof(script
) / sizeof(RegDesc
);
971 for (i
= 0; i
< scriptlen
; i
++) {
973 af9005_write_register_bits(state
->d
, script
[i
].reg
,
975 script
[i
].len
, script
[i
].val
)))
977 /* save 3 bytes of original fcw */
978 if (script
[i
].reg
== 0xae18)
979 temp2
= script
[i
].val
;
980 if (script
[i
].reg
== 0xae19)
981 temp1
= script
[i
].val
;
982 if (script
[i
].reg
== 0xae1a)
983 temp0
= script
[i
].val
;
985 /* save original unplug threshold */
986 if (script
[i
].reg
== xd_p_reg_unplug_th
)
987 state
->original_if_unplug_th
= script
[i
].val
;
988 if (script
[i
].reg
== xd_p_reg_unplug_rf_gain_th
)
989 state
->original_rf_unplug_th
= script
[i
].val
;
990 if (script
[i
].reg
== xd_p_reg_unplug_dtop_if_gain_th
)
991 state
->original_dtop_if_unplug_th
= script
[i
].val
;
992 if (script
[i
].reg
== xd_p_reg_unplug_dtop_rf_gain_th
)
993 state
->original_dtop_rf_unplug_th
= script
[i
].val
;
996 state
->original_fcw
=
997 ((u32
) temp2
<< 16) + ((u32
) temp1
<< 8) + (u32
) temp0
;
1000 /* save original TOPs */
1001 deb_info("save original TOPs\n");
1005 af9005_read_word_agc(state
->d
,
1006 xd_p_reg_aagc_rf_top_numerator_9_8
,
1007 xd_p_reg_aagc_rf_top_numerator_7_0
, 0, 2,
1008 &state
->original_rf_top
);
1014 af9005_read_word_agc(state
->d
,
1015 xd_p_reg_aagc_if_top_numerator_9_8
,
1016 xd_p_reg_aagc_if_top_numerator_7_0
, 0, 2,
1017 &state
->original_if_top
);
1023 af9005_read_word_agc(state
->d
, 0xA60E, 0xA60A, 4, 2,
1024 &state
->original_aci0_if_top
);
1030 af9005_read_word_agc(state
->d
, 0xA60E, 0xA60B, 6, 2,
1031 &state
->original_aci1_if_top
);
1035 /* attach tuner and init */
1036 if (fe
->ops
.tuner_ops
.release
== NULL
) {
1037 /* read tuner and board id from eeprom */
1038 ret
= af9005_read_eeprom(adap
->dev
, 0xc6, buf
, 2);
1040 err("Impossible to read EEPROM\n");
1043 deb_info("Tuner id %d, board id %d\n", buf
[0], buf
[1]);
1045 case 2: /* MT2060 */
1046 /* read if1 from eeprom */
1047 ret
= af9005_read_eeprom(adap
->dev
, 0xc8, buf
, 2);
1049 err("Impossible to read EEPROM\n");
1052 if1
= (u16
) (buf
[0] << 8) + buf
[1];
1053 if (dvb_attach(mt2060_attach
, fe
, &adap
->dev
->i2c_adap
,
1054 &af9005_mt2060_config
, if1
) == NULL
) {
1055 deb_info("MT2060 attach failed\n");
1059 case 3: /* QT1010 */
1060 case 9: /* QT1010B */
1061 if (dvb_attach(qt1010_attach
, fe
, &adap
->dev
->i2c_adap
,
1062 &af9005_qt1010_config
) ==NULL
) {
1063 deb_info("QT1010 attach failed\n");
1068 err("Unsupported tuner type %d", buf
[0]);
1071 ret
= fe
->ops
.tuner_ops
.init(fe
);
1076 deb_info("profit!\n");
1080 static int af9005_fe_sleep(struct dvb_frontend
*fe
)
1082 return af9005_fe_power(fe
, 0);
1085 static int af9005_ts_bus_ctrl(struct dvb_frontend
*fe
, int acquire
)
1087 struct af9005_fe_state
*state
= fe
->demodulator_priv
;
1095 af9005_led_control(state
->d
, 0);
1100 static int af9005_fe_set_frontend(struct dvb_frontend
*fe
)
1102 struct dtv_frontend_properties
*fep
= &fe
->dtv_property_cache
;
1103 struct af9005_fe_state
*state
= fe
->demodulator_priv
;
1105 u8 temp
, temp0
, temp1
, temp2
;
1107 deb_info("af9005_fe_set_frontend freq %d bw %d\n", fep
->frequency
,
1109 if (fe
->ops
.tuner_ops
.release
== NULL
) {
1110 err("Tuner not attached");
1114 deb_info("turn off led\n");
1115 /* not in the log */
1116 ret
= af9005_led_control(state
->d
, 0);
1119 /* not sure about the bits */
1120 ret
= af9005_write_register_bits(state
->d
, XD_MP2IF_MISC
, 2, 1, 0);
1124 /* set FCW to default value */
1125 deb_info("set FCW to default value\n");
1126 temp0
= (u8
) (state
->original_fcw
& 0x000000ff);
1127 temp1
= (u8
) ((state
->original_fcw
& 0x0000ff00) >> 8);
1128 temp2
= (u8
) ((state
->original_fcw
& 0x00ff0000) >> 16);
1129 ret
= af9005_write_ofdm_register(state
->d
, 0xae1a, temp0
);
1132 ret
= af9005_write_ofdm_register(state
->d
, 0xae19, temp1
);
1135 ret
= af9005_write_ofdm_register(state
->d
, 0xae18, temp2
);
1139 /* restore original TOPs */
1140 deb_info("restore original TOPs\n");
1142 af9005_write_word_agc(state
->d
,
1143 xd_p_reg_aagc_rf_top_numerator_9_8
,
1144 xd_p_reg_aagc_rf_top_numerator_7_0
, 0, 2,
1145 state
->original_rf_top
);
1149 af9005_write_word_agc(state
->d
,
1150 xd_p_reg_aagc_if_top_numerator_9_8
,
1151 xd_p_reg_aagc_if_top_numerator_7_0
, 0, 2,
1152 state
->original_if_top
);
1156 af9005_write_word_agc(state
->d
, 0xA60E, 0xA60A, 4, 2,
1157 state
->original_aci0_if_top
);
1161 af9005_write_word_agc(state
->d
, 0xA60E, 0xA60B, 6, 2,
1162 state
->original_aci1_if_top
);
1166 /* select bandwidth */
1167 deb_info("select bandwidth");
1168 ret
= af9005_fe_select_bw(state
->d
, fep
->bandwidth_hz
);
1171 ret
= af9005_fe_program_cfoe(state
->d
, fep
->bandwidth_hz
);
1175 /* clear easy mode flag */
1176 deb_info("clear easy mode flag\n");
1177 ret
= af9005_write_ofdm_register(state
->d
, 0xaefd, 0);
1181 /* set unplug threshold to original value */
1182 deb_info("set unplug threshold to original value\n");
1184 af9005_write_ofdm_register(state
->d
, xd_p_reg_unplug_th
,
1185 state
->original_if_unplug_th
);
1189 deb_info("set tuner\n");
1190 ret
= fe
->ops
.tuner_ops
.set_params(fe
);
1195 deb_info("trigger ofsm\n");
1197 ret
= af9005_write_tuner_registers(state
->d
, 0xffff, &temp
, 1);
1201 /* clear retrain and freeze flag */
1202 deb_info("clear retrain and freeze flag\n");
1204 af9005_write_register_bits(state
->d
,
1205 xd_p_reg_api_retrain_request
,
1206 reg_api_retrain_request_pos
, 2, 0);
1210 /* reset pre viterbi and post viterbi registers and statistics */
1211 af9005_reset_pre_viterbi(fe
);
1212 af9005_reset_post_viterbi(fe
);
1213 state
->pre_vit_error_count
= 0;
1214 state
->pre_vit_bit_count
= 0;
1216 state
->post_vit_error_count
= 0;
1217 /* state->unc = 0; commented out since it should be ever increasing */
1218 state
->abort_count
= 0;
1220 state
->next_status_check
= jiffies
;
1226 static int af9005_fe_get_frontend(struct dvb_frontend
*fe
,
1227 struct dtv_frontend_properties
*fep
)
1229 struct af9005_fe_state
*state
= fe
->demodulator_priv
;
1235 af9005_read_register_bits(state
->d
, xd_g_reg_tpsd_const
,
1236 reg_tpsd_const_pos
, reg_tpsd_const_len
,
1240 deb_info("===== fe_get_frontend_legacy = =============\n");
1241 deb_info("CONSTELLATION ");
1244 fep
->modulation
= QPSK
;
1248 fep
->modulation
= QAM_16
;
1249 deb_info("QAM_16\n");
1252 fep
->modulation
= QAM_64
;
1253 deb_info("QAM_64\n");
1257 /* tps hierarchy and alpha value */
1259 af9005_read_register_bits(state
->d
, xd_g_reg_tpsd_hier
,
1260 reg_tpsd_hier_pos
, reg_tpsd_hier_len
,
1264 deb_info("HIERARCHY ");
1267 fep
->hierarchy
= HIERARCHY_NONE
;
1271 fep
->hierarchy
= HIERARCHY_1
;
1275 fep
->hierarchy
= HIERARCHY_2
;
1279 fep
->hierarchy
= HIERARCHY_4
;
1284 /* high/low priority */
1286 af9005_read_register_bits(state
->d
, xd_g_reg_dec_pri
,
1287 reg_dec_pri_pos
, reg_dec_pri_len
, &temp
);
1290 /* if temp is set = high priority */
1291 deb_info("PRIORITY %s\n", temp
? "high" : "low");
1295 af9005_read_register_bits(state
->d
, xd_g_reg_tpsd_hpcr
,
1296 reg_tpsd_hpcr_pos
, reg_tpsd_hpcr_len
,
1300 deb_info("CODERATE HP ");
1303 fep
->code_rate_HP
= FEC_1_2
;
1304 deb_info("FEC_1_2\n");
1307 fep
->code_rate_HP
= FEC_2_3
;
1308 deb_info("FEC_2_3\n");
1311 fep
->code_rate_HP
= FEC_3_4
;
1312 deb_info("FEC_3_4\n");
1315 fep
->code_rate_HP
= FEC_5_6
;
1316 deb_info("FEC_5_6\n");
1319 fep
->code_rate_HP
= FEC_7_8
;
1320 deb_info("FEC_7_8\n");
1326 af9005_read_register_bits(state
->d
, xd_g_reg_tpsd_lpcr
,
1327 reg_tpsd_lpcr_pos
, reg_tpsd_lpcr_len
,
1331 deb_info("CODERATE LP ");
1334 fep
->code_rate_LP
= FEC_1_2
;
1335 deb_info("FEC_1_2\n");
1338 fep
->code_rate_LP
= FEC_2_3
;
1339 deb_info("FEC_2_3\n");
1342 fep
->code_rate_LP
= FEC_3_4
;
1343 deb_info("FEC_3_4\n");
1346 fep
->code_rate_LP
= FEC_5_6
;
1347 deb_info("FEC_5_6\n");
1350 fep
->code_rate_LP
= FEC_7_8
;
1351 deb_info("FEC_7_8\n");
1355 /* guard interval */
1357 af9005_read_register_bits(state
->d
, xd_g_reg_tpsd_gi
,
1358 reg_tpsd_gi_pos
, reg_tpsd_gi_len
, &temp
);
1361 deb_info("GUARD INTERVAL ");
1364 fep
->guard_interval
= GUARD_INTERVAL_1_32
;
1368 fep
->guard_interval
= GUARD_INTERVAL_1_16
;
1372 fep
->guard_interval
= GUARD_INTERVAL_1_8
;
1376 fep
->guard_interval
= GUARD_INTERVAL_1_4
;
1383 af9005_read_register_bits(state
->d
, xd_g_reg_tpsd_txmod
,
1384 reg_tpsd_txmod_pos
, reg_tpsd_txmod_len
,
1388 deb_info("TRANSMISSION MODE ");
1391 fep
->transmission_mode
= TRANSMISSION_MODE_2K
;
1395 fep
->transmission_mode
= TRANSMISSION_MODE_8K
;
1402 af9005_read_register_bits(state
->d
, xd_g_reg_bw
, reg_bw_pos
,
1404 deb_info("BANDWIDTH ");
1407 fep
->bandwidth_hz
= 6000000;
1411 fep
->bandwidth_hz
= 7000000;
1415 fep
->bandwidth_hz
= 8000000;
1422 static void af9005_fe_release(struct dvb_frontend
*fe
)
1424 struct af9005_fe_state
*state
=
1425 (struct af9005_fe_state
*)fe
->demodulator_priv
;
1429 static const struct dvb_frontend_ops af9005_fe_ops
;
1431 struct dvb_frontend
*af9005_fe_attach(struct dvb_usb_device
*d
)
1433 struct af9005_fe_state
*state
= NULL
;
1435 /* allocate memory for the internal state */
1436 state
= kzalloc(sizeof(struct af9005_fe_state
), GFP_KERNEL
);
1440 deb_info("attaching frontend af9005\n");
1445 memcpy(&state
->frontend
.ops
, &af9005_fe_ops
,
1446 sizeof(struct dvb_frontend_ops
));
1447 state
->frontend
.demodulator_priv
= state
;
1449 return &state
->frontend
;
1454 static const struct dvb_frontend_ops af9005_fe_ops
= {
1455 .delsys
= { SYS_DVBT
},
1457 .name
= "AF9005 USB DVB-T",
1458 .frequency_min_hz
= 44250 * kHz
,
1459 .frequency_max_hz
= 867250 * kHz
,
1460 .frequency_stepsize_hz
= 250 * kHz
,
1461 .caps
= FE_CAN_INVERSION_AUTO
|
1462 FE_CAN_FEC_1_2
| FE_CAN_FEC_2_3
| FE_CAN_FEC_3_4
|
1463 FE_CAN_FEC_5_6
| FE_CAN_FEC_7_8
| FE_CAN_FEC_AUTO
|
1464 FE_CAN_QPSK
| FE_CAN_QAM_16
| FE_CAN_QAM_64
|
1465 FE_CAN_QAM_AUTO
| FE_CAN_TRANSMISSION_MODE_AUTO
|
1466 FE_CAN_GUARD_INTERVAL_AUTO
| FE_CAN_RECOVER
|
1467 FE_CAN_HIERARCHY_AUTO
,
1470 .release
= af9005_fe_release
,
1472 .init
= af9005_fe_init
,
1473 .sleep
= af9005_fe_sleep
,
1474 .ts_bus_ctrl
= af9005_ts_bus_ctrl
,
1476 .set_frontend
= af9005_fe_set_frontend
,
1477 .get_frontend
= af9005_fe_get_frontend
,
1479 .read_status
= af9005_fe_read_status
,
1480 .read_ber
= af9005_fe_read_ber
,
1481 .read_signal_strength
= af9005_fe_read_signal_strength
,
1482 .read_snr
= af9005_fe_read_snr
,
1483 .read_ucblocks
= af9005_fe_read_unc_blocks
,