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
;
34 /* retraining parameters */
39 u16 original_aci0_if_top
;
40 u16 original_aci1_if_top
;
41 u16 original_aci0_if_min
;
42 u8 original_if_unplug_th
;
43 u8 original_rf_unplug_th
;
44 u8 original_dtop_if_unplug_th
;
45 u8 original_dtop_rf_unplug_th
;
48 u32 pre_vit_error_count
;
49 u32 pre_vit_bit_count
;
51 u32 post_vit_error_count
;
52 u32 post_vit_bit_count
;
58 unsigned long next_status_check
;
59 struct dvb_frontend frontend
;
62 static int af9005_write_word_agc(struct dvb_usb_device
*d
, u16 reghi
,
63 u16 reglo
, u8 pos
, u8 len
, u16 value
)
68 if ((ret
= af9005_write_ofdm_register(d
, reglo
, (u8
) (value
& 0xff))))
70 temp
= (u8
) ((value
& 0x0300) >> 8);
71 return af9005_write_register_bits(d
, reghi
, pos
, len
,
72 (u8
) ((value
& 0x300) >> 8));
75 static int af9005_read_word_agc(struct dvb_usb_device
*d
, u16 reghi
,
76 u16 reglo
, u8 pos
, u8 len
, u16
* value
)
81 if ((ret
= af9005_read_ofdm_register(d
, reglo
, &temp0
)))
83 if ((ret
= af9005_read_ofdm_register(d
, reghi
, &temp1
)))
87 *value
= ((u16
) (temp1
& 0x03) << 8) + (u16
) temp0
;
90 *value
= ((u16
) (temp1
& 0x0C) << 6) + (u16
) temp0
;
93 *value
= ((u16
) (temp1
& 0x30) << 4) + (u16
) temp0
;
96 *value
= ((u16
) (temp1
& 0xC0) << 2) + (u16
) temp0
;
99 err("invalid pos in read word agc");
106 static int af9005_is_fecmon_available(struct dvb_frontend
*fe
, int *available
)
108 struct af9005_fe_state
*state
= fe
->demodulator_priv
;
114 ret
= af9005_read_register_bits(state
->d
, xd_p_fec_vtb_rsd_mon_en
,
115 fec_vtb_rsd_mon_en_pos
,
116 fec_vtb_rsd_mon_en_len
, &temp
);
121 af9005_read_register_bits(state
->d
,
122 xd_p_reg_ofsm_read_rbc_en
,
123 reg_ofsm_read_rbc_en_pos
,
124 reg_ofsm_read_rbc_en_len
, &temp
);
134 static int af9005_get_post_vit_err_cw_count(struct dvb_frontend
*fe
,
135 u32
* post_err_count
,
139 struct af9005_fe_state
*state
= fe
->demodulator_priv
;
143 u8 temp
, temp0
, temp1
, temp2
;
149 /* check if error bit count is ready */
151 af9005_read_register_bits(state
->d
, xd_r_fec_rsd_ber_rdy
,
152 fec_rsd_ber_rdy_pos
, fec_rsd_ber_rdy_len
,
157 deb_info("rsd counter not ready\n");
160 /* get abort count */
162 af9005_read_ofdm_register(state
->d
,
163 xd_r_fec_rsd_abort_packet_cnt_7_0
,
168 af9005_read_ofdm_register(state
->d
,
169 xd_r_fec_rsd_abort_packet_cnt_15_8
,
173 loc_abort_count
= ((u16
) temp1
<< 8) + temp0
;
175 /* get error count */
177 af9005_read_ofdm_register(state
->d
, xd_r_fec_rsd_bit_err_cnt_7_0
,
182 af9005_read_ofdm_register(state
->d
, xd_r_fec_rsd_bit_err_cnt_15_8
,
187 af9005_read_ofdm_register(state
->d
, xd_r_fec_rsd_bit_err_cnt_23_16
,
191 err_count
= ((u32
) temp2
<< 16) + ((u32
) temp1
<< 8) + temp0
;
192 *post_err_count
= err_count
- (u32
) loc_abort_count
*8 * 8;
194 /* get RSD packet number */
196 af9005_read_ofdm_register(state
->d
, xd_p_fec_rsd_packet_unit_7_0
,
201 af9005_read_ofdm_register(state
->d
, xd_p_fec_rsd_packet_unit_15_8
,
205 cw_count
= ((u32
) temp1
<< 8) + temp0
;
207 err("wrong RSD packet count");
210 deb_info("POST abort count %d err count %d rsd packets %d\n",
211 loc_abort_count
, err_count
, cw_count
);
212 *post_cw_count
= cw_count
- (u32
) loc_abort_count
;
213 *abort_count
= loc_abort_count
;
218 static int af9005_get_post_vit_ber(struct dvb_frontend
*fe
,
219 u32
* post_err_count
, u32
* post_cw_count
,
222 u32 loc_cw_count
= 0, loc_err_count
;
223 u16 loc_abort_count
= 0;
227 af9005_get_post_vit_err_cw_count(fe
, &loc_err_count
, &loc_cw_count
,
231 *post_err_count
= loc_err_count
;
232 *post_cw_count
= loc_cw_count
* 204 * 8;
233 *abort_count
= loc_abort_count
;
238 static int af9005_get_pre_vit_err_bit_count(struct dvb_frontend
*fe
,
242 struct af9005_fe_state
*state
= fe
->demodulator_priv
;
243 u8 temp
, temp0
, temp1
, temp2
;
244 u32 super_frame_count
, x
, bits
;
248 af9005_read_register_bits(state
->d
, xd_r_fec_vtb_ber_rdy
,
249 fec_vtb_ber_rdy_pos
, fec_vtb_ber_rdy_len
,
254 deb_info("viterbi counter not ready\n");
255 return 101; /* ERR_APO_VTB_COUNTER_NOT_READY; */
258 af9005_read_ofdm_register(state
->d
, xd_r_fec_vtb_err_bit_cnt_7_0
,
263 af9005_read_ofdm_register(state
->d
, xd_r_fec_vtb_err_bit_cnt_15_8
,
268 af9005_read_ofdm_register(state
->d
, xd_r_fec_vtb_err_bit_cnt_23_16
,
272 *pre_err_count
= ((u32
) temp2
<< 16) + ((u32
) temp1
<< 8) + temp0
;
275 af9005_read_ofdm_register(state
->d
, xd_p_fec_super_frm_unit_7_0
,
280 af9005_read_ofdm_register(state
->d
, xd_p_fec_super_frm_unit_15_8
,
284 super_frame_count
= ((u32
) temp1
<< 8) + temp0
;
285 if (super_frame_count
== 0) {
286 deb_info("super frame count 0\n");
292 af9005_read_register_bits(state
->d
, xd_g_reg_tpsd_txmod
,
293 reg_tpsd_txmod_pos
, reg_tpsd_txmod_len
,
300 } else if (temp
== 1) {
304 err("Invalid fft mode");
308 /* read constellation mode */
310 af9005_read_register_bits(state
->d
, xd_g_reg_tpsd_const
,
311 reg_tpsd_const_pos
, reg_tpsd_const_len
,
326 err("invalid constellation mode");
329 *pre_bit_count
= super_frame_count
* 68 * 4 * x
* bits
;
330 deb_info("PRE err count %d frame count %d bit count %d\n",
331 *pre_err_count
, super_frame_count
, *pre_bit_count
);
335 static int af9005_reset_pre_viterbi(struct dvb_frontend
*fe
)
337 struct af9005_fe_state
*state
= fe
->demodulator_priv
;
340 /* set super frame count to 1 */
342 af9005_write_ofdm_register(state
->d
, xd_p_fec_super_frm_unit_7_0
,
346 ret
= af9005_write_ofdm_register(state
->d
, xd_p_fec_super_frm_unit_15_8
,
350 /* reset pre viterbi error count */
352 af9005_write_register_bits(state
->d
, xd_p_fec_vtb_ber_rst
,
353 fec_vtb_ber_rst_pos
, fec_vtb_ber_rst_len
,
359 static int af9005_reset_post_viterbi(struct dvb_frontend
*fe
)
361 struct af9005_fe_state
*state
= fe
->demodulator_priv
;
364 /* set packet unit */
366 af9005_write_ofdm_register(state
->d
, xd_p_fec_rsd_packet_unit_7_0
,
371 af9005_write_ofdm_register(state
->d
, xd_p_fec_rsd_packet_unit_15_8
,
375 /* reset post viterbi error count */
377 af9005_write_register_bits(state
->d
, xd_p_fec_rsd_ber_rst
,
378 fec_rsd_ber_rst_pos
, fec_rsd_ber_rst_len
,
384 static int af9005_get_statistic(struct dvb_frontend
*fe
)
386 struct af9005_fe_state
*state
= fe
->demodulator_priv
;
387 int ret
, fecavailable
;
388 u64 numerator
, denominator
;
390 deb_info("GET STATISTIC\n");
391 ret
= af9005_is_fecmon_available(fe
, &fecavailable
);
395 deb_info("fecmon not available\n");
399 ret
= af9005_get_pre_vit_err_bit_count(fe
, &state
->pre_vit_error_count
,
400 &state
->pre_vit_bit_count
);
402 af9005_reset_pre_viterbi(fe
);
403 if (state
->pre_vit_bit_count
> 0) {
404 /* according to v 0.0.4 of the dvb api ber should be a multiple
405 of 10E-9 so we have to multiply the error count by
408 (u64
) state
->pre_vit_error_count
* (u64
) 1000000000;
409 denominator
= (u64
) state
->pre_vit_bit_count
;
410 state
->ber
= do_div(numerator
, denominator
);
412 state
->ber
= 0xffffffff;
416 ret
= af9005_get_post_vit_ber(fe
, &state
->post_vit_error_count
,
417 &state
->post_vit_bit_count
,
418 &state
->abort_count
);
420 ret
= af9005_reset_post_viterbi(fe
);
421 state
->unc
+= state
->abort_count
;
428 static int af9005_fe_refresh_state(struct dvb_frontend
*fe
)
430 struct af9005_fe_state
*state
= fe
->demodulator_priv
;
431 if (time_after(jiffies
, state
->next_status_check
)) {
432 deb_info("REFRESH STATE\n");
435 if (af9005_get_statistic(fe
))
436 err("get_statistic_failed");
437 state
->next_status_check
= jiffies
+ 250 * HZ
/ 1000;
442 static int af9005_fe_read_status(struct dvb_frontend
*fe
, fe_status_t
* stat
)
444 struct af9005_fe_state
*state
= fe
->demodulator_priv
;
448 if (fe
->ops
.tuner_ops
.release
== NULL
)
452 ret
= af9005_read_register_bits(state
->d
, xd_p_agc_lock
,
453 agc_lock_pos
, agc_lock_len
, &temp
);
457 *stat
|= FE_HAS_SIGNAL
;
459 ret
= af9005_read_register_bits(state
->d
, xd_p_fd_tpsd_lock
,
460 fd_tpsd_lock_pos
, fd_tpsd_lock_len
,
465 *stat
|= FE_HAS_CARRIER
;
467 ret
= af9005_read_register_bits(state
->d
,
468 xd_r_mp2if_sync_byte_locked
,
469 mp2if_sync_byte_locked_pos
,
470 mp2if_sync_byte_locked_pos
, &temp
);
474 *stat
|= FE_HAS_SYNC
| FE_HAS_VITERBI
| FE_HAS_LOCK
;
476 af9005_led_control(state
->d
, *stat
& FE_HAS_LOCK
);
479 af9005_read_register_bits(state
->d
, xd_p_reg_strong_sginal_detected
,
480 reg_strong_sginal_detected_pos
,
481 reg_strong_sginal_detected_len
, &temp
);
484 if (temp
!= state
->strong
) {
485 deb_info("adjust for strong signal %d\n", temp
);
486 state
->strong
= temp
;
491 static int af9005_fe_read_ber(struct dvb_frontend
*fe
, u32
* ber
)
493 struct af9005_fe_state
*state
= fe
->demodulator_priv
;
494 if (fe
->ops
.tuner_ops
.release
== NULL
)
496 af9005_fe_refresh_state(fe
);
501 static int af9005_fe_read_unc_blocks(struct dvb_frontend
*fe
, u32
* unc
)
503 struct af9005_fe_state
*state
= fe
->demodulator_priv
;
504 if (fe
->ops
.tuner_ops
.release
== NULL
)
506 af9005_fe_refresh_state(fe
);
511 static int af9005_fe_read_signal_strength(struct dvb_frontend
*fe
,
514 struct af9005_fe_state
*state
= fe
->demodulator_priv
;
518 if (fe
->ops
.tuner_ops
.release
== NULL
)
521 af9005_read_ofdm_register(state
->d
, xd_r_reg_aagc_rf_gain
,
526 af9005_read_ofdm_register(state
->d
, xd_r_reg_aagc_if_gain
,
530 /* this value has no real meaning, but i don't have the tables that relate
531 the rf and if gain with the dbm, so I just scale the value */
532 *strength
= (512 - rf_gain
- if_gain
) << 7;
536 static int af9005_fe_read_snr(struct dvb_frontend
*fe
, u16
* snr
)
538 /* the snr can be derived from the ber and the constellation
539 but I don't think this kind of complex calculations belong
540 in the driver. I may be wrong.... */
544 static int af9005_fe_program_cfoe(struct dvb_usb_device
*d
, fe_bandwidth_t bw
)
546 u8 temp0
, temp1
, temp2
, temp3
, buf
[4];
548 u32 NS_coeff1_2048Nu
;
549 u32 NS_coeff1_8191Nu
;
550 u32 NS_coeff1_8192Nu
;
551 u32 NS_coeff1_8193Nu
;
556 case BANDWIDTH_6_MHZ
:
557 NS_coeff1_2048Nu
= 0x2ADB6DC;
558 NS_coeff1_8191Nu
= 0xAB7313;
559 NS_coeff1_8192Nu
= 0xAB6DB7;
560 NS_coeff1_8193Nu
= 0xAB685C;
561 NS_coeff2_2k
= 0x156DB6E;
562 NS_coeff2_8k
= 0x55B6DC;
565 case BANDWIDTH_7_MHZ
:
566 NS_coeff1_2048Nu
= 0x3200001;
567 NS_coeff1_8191Nu
= 0xC80640;
568 NS_coeff1_8192Nu
= 0xC80000;
569 NS_coeff1_8193Nu
= 0xC7F9C0;
570 NS_coeff2_2k
= 0x1900000;
571 NS_coeff2_8k
= 0x640000;
574 case BANDWIDTH_8_MHZ
:
575 NS_coeff1_2048Nu
= 0x3924926;
576 NS_coeff1_8191Nu
= 0xE4996E;
577 NS_coeff1_8192Nu
= 0xE49249;
578 NS_coeff1_8193Nu
= 0xE48B25;
579 NS_coeff2_2k
= 0x1C92493;
580 NS_coeff2_8k
= 0x724925;
583 err("Invalid bandwith %d.", bw
);
588 * write NS_coeff1_2048Nu
591 temp0
= (u8
) (NS_coeff1_2048Nu
& 0x000000FF);
592 temp1
= (u8
) ((NS_coeff1_2048Nu
& 0x0000FF00) >> 8);
593 temp2
= (u8
) ((NS_coeff1_2048Nu
& 0x00FF0000) >> 16);
594 temp3
= (u8
) ((NS_coeff1_2048Nu
& 0x03000000) >> 24);
596 /* big endian to make 8051 happy */
602 /* cfoe_NS_2k_coeff1_25_24 */
603 ret
= af9005_write_ofdm_register(d
, 0xAE00, buf
[0]);
607 /* cfoe_NS_2k_coeff1_23_16 */
608 ret
= af9005_write_ofdm_register(d
, 0xAE01, buf
[1]);
612 /* cfoe_NS_2k_coeff1_15_8 */
613 ret
= af9005_write_ofdm_register(d
, 0xAE02, buf
[2]);
617 /* cfoe_NS_2k_coeff1_7_0 */
618 ret
= af9005_write_ofdm_register(d
, 0xAE03, buf
[3]);
626 temp0
= (u8
) ((NS_coeff2_2k
& 0x0000003F));
627 temp1
= (u8
) ((NS_coeff2_2k
& 0x00003FC0) >> 6);
628 temp2
= (u8
) ((NS_coeff2_2k
& 0x003FC000) >> 14);
629 temp3
= (u8
) ((NS_coeff2_2k
& 0x01C00000) >> 22);
631 /* big endian to make 8051 happy */
637 ret
= af9005_write_ofdm_register(d
, 0xAE04, buf
[0]);
641 ret
= af9005_write_ofdm_register(d
, 0xAE05, buf
[1]);
645 ret
= af9005_write_ofdm_register(d
, 0xAE06, buf
[2]);
649 ret
= af9005_write_ofdm_register(d
, 0xAE07, buf
[3]);
654 * write NS_coeff1_8191Nu
657 temp0
= (u8
) ((NS_coeff1_8191Nu
& 0x000000FF));
658 temp1
= (u8
) ((NS_coeff1_8191Nu
& 0x0000FF00) >> 8);
659 temp2
= (u8
) ((NS_coeff1_8191Nu
& 0x00FFC000) >> 16);
660 temp3
= (u8
) ((NS_coeff1_8191Nu
& 0x03000000) >> 24);
662 /* big endian to make 8051 happy */
668 ret
= af9005_write_ofdm_register(d
, 0xAE08, buf
[0]);
672 ret
= af9005_write_ofdm_register(d
, 0xAE09, buf
[1]);
676 ret
= af9005_write_ofdm_register(d
, 0xAE0A, buf
[2]);
680 ret
= af9005_write_ofdm_register(d
, 0xAE0B, buf
[3]);
685 * write NS_coeff1_8192Nu
688 temp0
= (u8
) (NS_coeff1_8192Nu
& 0x000000FF);
689 temp1
= (u8
) ((NS_coeff1_8192Nu
& 0x0000FF00) >> 8);
690 temp2
= (u8
) ((NS_coeff1_8192Nu
& 0x00FFC000) >> 16);
691 temp3
= (u8
) ((NS_coeff1_8192Nu
& 0x03000000) >> 24);
693 /* big endian to make 8051 happy */
699 ret
= af9005_write_ofdm_register(d
, 0xAE0C, buf
[0]);
703 ret
= af9005_write_ofdm_register(d
, 0xAE0D, buf
[1]);
707 ret
= af9005_write_ofdm_register(d
, 0xAE0E, buf
[2]);
711 ret
= af9005_write_ofdm_register(d
, 0xAE0F, buf
[3]);
716 * write NS_coeff1_8193Nu
719 temp0
= (u8
) ((NS_coeff1_8193Nu
& 0x000000FF));
720 temp1
= (u8
) ((NS_coeff1_8193Nu
& 0x0000FF00) >> 8);
721 temp2
= (u8
) ((NS_coeff1_8193Nu
& 0x00FFC000) >> 16);
722 temp3
= (u8
) ((NS_coeff1_8193Nu
& 0x03000000) >> 24);
724 /* big endian to make 8051 happy */
730 ret
= af9005_write_ofdm_register(d
, 0xAE10, buf
[0]);
734 ret
= af9005_write_ofdm_register(d
, 0xAE11, buf
[1]);
738 ret
= af9005_write_ofdm_register(d
, 0xAE12, buf
[2]);
742 ret
= af9005_write_ofdm_register(d
, 0xAE13, buf
[3]);
750 temp0
= (u8
) ((NS_coeff2_8k
& 0x0000003F));
751 temp1
= (u8
) ((NS_coeff2_8k
& 0x00003FC0) >> 6);
752 temp2
= (u8
) ((NS_coeff2_8k
& 0x003FC000) >> 14);
753 temp3
= (u8
) ((NS_coeff2_8k
& 0x01C00000) >> 22);
755 /* big endian to make 8051 happy */
761 ret
= af9005_write_ofdm_register(d
, 0xAE14, buf
[0]);
765 ret
= af9005_write_ofdm_register(d
, 0xAE15, buf
[1]);
769 ret
= af9005_write_ofdm_register(d
, 0xAE16, buf
[2]);
773 ret
= af9005_write_ofdm_register(d
, 0xAE17, buf
[3]);
778 static int af9005_fe_select_bw(struct dvb_usb_device
*d
, fe_bandwidth_t bw
)
782 case BANDWIDTH_6_MHZ
:
785 case BANDWIDTH_7_MHZ
:
788 case BANDWIDTH_8_MHZ
:
792 err("Invalid bandwith %d.", bw
);
795 return af9005_write_register_bits(d
, xd_g_reg_bw
, reg_bw_pos
,
799 static int af9005_fe_power(struct dvb_frontend
*fe
, int on
)
801 struct af9005_fe_state
*state
= fe
->demodulator_priv
;
804 deb_info("power %s tuner\n", on
? "on" : "off");
805 ret
= af9005_send_command(state
->d
, 0x03, &temp
, 1, NULL
, 0);
809 static struct mt2060_config af9005_mt2060_config
= {
813 static struct qt1010_config af9005_qt1010_config
= {
817 static int af9005_fe_init(struct dvb_frontend
*fe
)
819 struct af9005_fe_state
*state
= fe
->demodulator_priv
;
820 struct dvb_usb_adapter
*adap
= fe
->dvb
->priv
;
821 int ret
, i
, scriptlen
;
822 u8 temp
, temp0
= 0, temp1
= 0, temp2
= 0;
826 deb_info("in af9005_fe_init\n");
831 af9005_write_register_bits(state
->d
, xd_I2C_reg_ofdm_rst_en
,
834 if ((ret
= af9005_write_ofdm_register(state
->d
, APO_REG_RESET
, 0)))
836 /* clear ofdm reset */
837 deb_info("clear ofdm reset\n");
838 for (i
= 0; i
< 150; i
++) {
840 af9005_read_ofdm_register(state
->d
,
841 xd_I2C_reg_ofdm_rst
, &temp
)))
843 if (temp
& (regmask
[reg_ofdm_rst_len
- 1] << reg_ofdm_rst_pos
))
852 write xd_g_reg_ofsm_clk 7
857 write xd_g_reg_ofsm_clk 0
860 ret
= af9005_write_ofdm_register(state
->d
, 0xb200, 0xa9);
863 ret
= af9005_write_ofdm_register(state
->d
, xd_g_reg_ofsm_clk
, 0x07);
867 ret
= af9005_send_command(state
->d
, 0x03, &temp
, 1, NULL
, 0);
870 ret
= af9005_write_ofdm_register(state
->d
, xd_g_reg_ofsm_clk
, 0x00);
873 ret
= af9005_write_ofdm_register(state
->d
, 0xb200, 0xa1);
877 temp
= regmask
[reg_ofdm_rst_len
- 1] << reg_ofdm_rst_pos
;
879 af9005_write_register_bits(state
->d
, xd_I2C_reg_ofdm_rst
,
880 reg_ofdm_rst_pos
, reg_ofdm_rst_len
, 1)))
882 ret
= af9005_write_register_bits(state
->d
, xd_I2C_reg_ofdm_rst
,
883 reg_ofdm_rst_pos
, reg_ofdm_rst_len
, 0);
887 /* don't know what register aefc is, but this is what the windows driver does */
888 ret
= af9005_write_ofdm_register(state
->d
, 0xaefc, 0);
892 /* set stand alone chip */
893 deb_info("set stand alone chip\n");
895 af9005_write_register_bits(state
->d
, xd_p_reg_dca_stand_alone
,
896 reg_dca_stand_alone_pos
,
897 reg_dca_stand_alone_len
, 1)))
900 /* set dca upper & lower chip */
901 deb_info("set dca upper & lower chip\n");
903 af9005_write_register_bits(state
->d
, xd_p_reg_dca_upper_chip
,
904 reg_dca_upper_chip_pos
,
905 reg_dca_upper_chip_len
, 0)))
908 af9005_write_register_bits(state
->d
, xd_p_reg_dca_lower_chip
,
909 reg_dca_lower_chip_pos
,
910 reg_dca_lower_chip_len
, 0)))
913 /* set 2wire master clock to 0x14 (for 60KHz) */
914 deb_info("set 2wire master clock to 0x14 (for 60KHz)\n");
916 af9005_write_ofdm_register(state
->d
, xd_I2C_i2c_m_period
, 0x14)))
919 /* clear dca enable chip */
920 deb_info("clear dca enable chip\n");
922 af9005_write_register_bits(state
->d
, xd_p_reg_dca_en
,
923 reg_dca_en_pos
, reg_dca_en_len
, 0)))
925 /* FIXME these are register bits, but I don't know which ones */
926 ret
= af9005_write_ofdm_register(state
->d
, 0xa16c, 1);
929 ret
= af9005_write_ofdm_register(state
->d
, 0xa3c1, 0);
933 /* init other parameters: program cfoe and select bandwith */
934 deb_info("program cfoe\n");
935 if ((ret
= af9005_fe_program_cfoe(state
->d
, BANDWIDTH_6_MHZ
)))
937 /* set read-update bit for constellation */
938 deb_info("set read-update bit for constellation\n");
940 af9005_write_register_bits(state
->d
, xd_p_reg_feq_read_update
,
941 reg_feq_read_update_pos
,
942 reg_feq_read_update_len
, 1)))
945 /* sample code has a set MPEG TS code here
946 but sniffing reveals that it doesn't do it */
948 /* set read-update bit to 1 for DCA constellation */
949 deb_info("set read-update bit 1 for DCA constellation\n");
951 af9005_write_register_bits(state
->d
, xd_p_reg_dca_read_update
,
952 reg_dca_read_update_pos
,
953 reg_dca_read_update_len
, 1)))
956 /* enable fec monitor */
957 deb_info("enable fec monitor\n");
959 af9005_write_register_bits(state
->d
, xd_p_fec_vtb_rsd_mon_en
,
960 fec_vtb_rsd_mon_en_pos
,
961 fec_vtb_rsd_mon_en_len
, 1)))
964 /* FIXME should be register bits, I don't know which ones */
965 ret
= af9005_write_ofdm_register(state
->d
, 0xa601, 0);
967 /* set api_retrain_never_freeze */
968 deb_info("set api_retrain_never_freeze\n");
969 if ((ret
= af9005_write_ofdm_register(state
->d
, 0xaefb, 0x01)))
972 /* load init script */
973 deb_info("load init script\n");
974 scriptlen
= sizeof(script
) / sizeof(RegDesc
);
975 for (i
= 0; i
< scriptlen
; i
++) {
977 af9005_write_register_bits(state
->d
, script
[i
].reg
,
979 script
[i
].len
, script
[i
].val
)))
981 /* save 3 bytes of original fcw */
982 if (script
[i
].reg
== 0xae18)
983 temp2
= script
[i
].val
;
984 if (script
[i
].reg
== 0xae19)
985 temp1
= script
[i
].val
;
986 if (script
[i
].reg
== 0xae1a)
987 temp0
= script
[i
].val
;
989 /* save original unplug threshold */
990 if (script
[i
].reg
== xd_p_reg_unplug_th
)
991 state
->original_if_unplug_th
= script
[i
].val
;
992 if (script
[i
].reg
== xd_p_reg_unplug_rf_gain_th
)
993 state
->original_rf_unplug_th
= script
[i
].val
;
994 if (script
[i
].reg
== xd_p_reg_unplug_dtop_if_gain_th
)
995 state
->original_dtop_if_unplug_th
= script
[i
].val
;
996 if (script
[i
].reg
== xd_p_reg_unplug_dtop_rf_gain_th
)
997 state
->original_dtop_rf_unplug_th
= script
[i
].val
;
1000 state
->original_fcw
=
1001 ((u32
) temp2
<< 16) + ((u32
) temp1
<< 8) + (u32
) temp0
;
1004 /* save original TOPs */
1005 deb_info("save original TOPs\n");
1009 af9005_read_word_agc(state
->d
,
1010 xd_p_reg_aagc_rf_top_numerator_9_8
,
1011 xd_p_reg_aagc_rf_top_numerator_7_0
, 0, 2,
1012 &state
->original_rf_top
);
1018 af9005_read_word_agc(state
->d
,
1019 xd_p_reg_aagc_if_top_numerator_9_8
,
1020 xd_p_reg_aagc_if_top_numerator_7_0
, 0, 2,
1021 &state
->original_if_top
);
1027 af9005_read_word_agc(state
->d
, 0xA60E, 0xA60A, 4, 2,
1028 &state
->original_aci0_if_top
);
1034 af9005_read_word_agc(state
->d
, 0xA60E, 0xA60B, 6, 2,
1035 &state
->original_aci1_if_top
);
1039 /* attach tuner and init */
1040 if (fe
->ops
.tuner_ops
.release
== NULL
) {
1041 /* read tuner and board id from eeprom */
1042 ret
= af9005_read_eeprom(adap
->dev
, 0xc6, buf
, 2);
1044 err("Impossible to read EEPROM\n");
1047 deb_info("Tuner id %d, board id %d\n", buf
[0], buf
[1]);
1049 case 2: /* MT2060 */
1050 /* read if1 from eeprom */
1051 ret
= af9005_read_eeprom(adap
->dev
, 0xc8, buf
, 2);
1053 err("Impossible to read EEPROM\n");
1056 if1
= (u16
) (buf
[0] << 8) + buf
[1];
1057 if (dvb_attach(mt2060_attach
, fe
, &adap
->dev
->i2c_adap
,
1058 &af9005_mt2060_config
, if1
) == NULL
) {
1059 deb_info("MT2060 attach failed\n");
1063 case 3: /* QT1010 */
1064 case 9: /* QT1010B */
1065 if (dvb_attach(qt1010_attach
, fe
, &adap
->dev
->i2c_adap
,
1066 &af9005_qt1010_config
) ==NULL
) {
1067 deb_info("QT1010 attach failed\n");
1072 err("Unsupported tuner type %d", buf
[0]);
1075 ret
= fe
->ops
.tuner_ops
.init(fe
);
1080 deb_info("profit!\n");
1084 static int af9005_fe_sleep(struct dvb_frontend
*fe
)
1086 return af9005_fe_power(fe
, 0);
1089 static int af9005_ts_bus_ctrl(struct dvb_frontend
*fe
, int acquire
)
1091 struct af9005_fe_state
*state
= fe
->demodulator_priv
;
1099 af9005_led_control(state
->d
, 0);
1104 static int af9005_fe_set_frontend(struct dvb_frontend
*fe
,
1105 struct dvb_frontend_parameters
*fep
)
1107 struct af9005_fe_state
*state
= fe
->demodulator_priv
;
1109 u8 temp
, temp0
, temp1
, temp2
;
1111 deb_info("af9005_fe_set_frontend freq %d bw %d\n", fep
->frequency
,
1112 fep
->u
.ofdm
.bandwidth
);
1113 if (fe
->ops
.tuner_ops
.release
== NULL
) {
1114 err("Tuner not attached");
1118 deb_info("turn off led\n");
1119 /* not in the log */
1120 ret
= af9005_led_control(state
->d
, 0);
1123 /* not sure about the bits */
1124 ret
= af9005_write_register_bits(state
->d
, XD_MP2IF_MISC
, 2, 1, 0);
1128 /* set FCW to default value */
1129 deb_info("set FCW to default value\n");
1130 temp0
= (u8
) (state
->original_fcw
& 0x000000ff);
1131 temp1
= (u8
) ((state
->original_fcw
& 0x0000ff00) >> 8);
1132 temp2
= (u8
) ((state
->original_fcw
& 0x00ff0000) >> 16);
1133 ret
= af9005_write_ofdm_register(state
->d
, 0xae1a, temp0
);
1136 ret
= af9005_write_ofdm_register(state
->d
, 0xae19, temp1
);
1139 ret
= af9005_write_ofdm_register(state
->d
, 0xae18, temp2
);
1143 /* restore original TOPs */
1144 deb_info("restore original TOPs\n");
1146 af9005_write_word_agc(state
->d
,
1147 xd_p_reg_aagc_rf_top_numerator_9_8
,
1148 xd_p_reg_aagc_rf_top_numerator_7_0
, 0, 2,
1149 state
->original_rf_top
);
1153 af9005_write_word_agc(state
->d
,
1154 xd_p_reg_aagc_if_top_numerator_9_8
,
1155 xd_p_reg_aagc_if_top_numerator_7_0
, 0, 2,
1156 state
->original_if_top
);
1160 af9005_write_word_agc(state
->d
, 0xA60E, 0xA60A, 4, 2,
1161 state
->original_aci0_if_top
);
1165 af9005_write_word_agc(state
->d
, 0xA60E, 0xA60B, 6, 2,
1166 state
->original_aci1_if_top
);
1170 /* select bandwith */
1171 deb_info("select bandwidth");
1172 ret
= af9005_fe_select_bw(state
->d
, fep
->u
.ofdm
.bandwidth
);
1175 ret
= af9005_fe_program_cfoe(state
->d
, fep
->u
.ofdm
.bandwidth
);
1179 /* clear easy mode flag */
1180 deb_info("clear easy mode flag\n");
1181 ret
= af9005_write_ofdm_register(state
->d
, 0xaefd, 0);
1185 /* set unplug threshold to original value */
1186 deb_info("set unplug threshold to original value\n");
1188 af9005_write_ofdm_register(state
->d
, xd_p_reg_unplug_th
,
1189 state
->original_if_unplug_th
);
1193 deb_info("set tuner\n");
1194 ret
= fe
->ops
.tuner_ops
.set_params(fe
, fep
);
1199 deb_info("trigger ofsm\n");
1201 ret
= af9005_write_tuner_registers(state
->d
, 0xffff, &temp
, 1);
1205 /* clear retrain and freeze flag */
1206 deb_info("clear retrain and freeze flag\n");
1208 af9005_write_register_bits(state
->d
,
1209 xd_p_reg_api_retrain_request
,
1210 reg_api_retrain_request_pos
, 2, 0);
1214 /* reset pre viterbi and post viterbi registers and statistics */
1215 af9005_reset_pre_viterbi(fe
);
1216 af9005_reset_post_viterbi(fe
);
1217 state
->pre_vit_error_count
= 0;
1218 state
->pre_vit_bit_count
= 0;
1220 state
->post_vit_error_count
= 0;
1221 /* state->unc = 0; commented out since it should be ever increasing */
1222 state
->abort_count
= 0;
1224 state
->next_status_check
= jiffies
;
1230 static int af9005_fe_get_frontend(struct dvb_frontend
*fe
,
1231 struct dvb_frontend_parameters
*fep
)
1233 struct af9005_fe_state
*state
= fe
->demodulator_priv
;
1239 af9005_read_register_bits(state
->d
, xd_g_reg_tpsd_const
,
1240 reg_tpsd_const_pos
, reg_tpsd_const_len
,
1244 deb_info("===== fe_get_frontend ==============\n");
1245 deb_info("CONSTELLATION ");
1248 fep
->u
.ofdm
.constellation
= QPSK
;
1252 fep
->u
.ofdm
.constellation
= QAM_16
;
1253 deb_info("QAM_16\n");
1256 fep
->u
.ofdm
.constellation
= QAM_64
;
1257 deb_info("QAM_64\n");
1261 /* tps hierarchy and alpha value */
1263 af9005_read_register_bits(state
->d
, xd_g_reg_tpsd_hier
,
1264 reg_tpsd_hier_pos
, reg_tpsd_hier_len
,
1268 deb_info("HIERARCHY ");
1271 fep
->u
.ofdm
.hierarchy_information
= HIERARCHY_NONE
;
1275 fep
->u
.ofdm
.hierarchy_information
= HIERARCHY_1
;
1279 fep
->u
.ofdm
.hierarchy_information
= HIERARCHY_2
;
1283 fep
->u
.ofdm
.hierarchy_information
= HIERARCHY_4
;
1288 /* high/low priority */
1290 af9005_read_register_bits(state
->d
, xd_g_reg_dec_pri
,
1291 reg_dec_pri_pos
, reg_dec_pri_len
, &temp
);
1294 /* if temp is set = high priority */
1295 deb_info("PRIORITY %s\n", temp
? "high" : "low");
1299 af9005_read_register_bits(state
->d
, xd_g_reg_tpsd_hpcr
,
1300 reg_tpsd_hpcr_pos
, reg_tpsd_hpcr_len
,
1304 deb_info("CODERATE HP ");
1307 fep
->u
.ofdm
.code_rate_HP
= FEC_1_2
;
1308 deb_info("FEC_1_2\n");
1311 fep
->u
.ofdm
.code_rate_HP
= FEC_2_3
;
1312 deb_info("FEC_2_3\n");
1315 fep
->u
.ofdm
.code_rate_HP
= FEC_3_4
;
1316 deb_info("FEC_3_4\n");
1319 fep
->u
.ofdm
.code_rate_HP
= FEC_5_6
;
1320 deb_info("FEC_5_6\n");
1323 fep
->u
.ofdm
.code_rate_HP
= FEC_7_8
;
1324 deb_info("FEC_7_8\n");
1330 af9005_read_register_bits(state
->d
, xd_g_reg_tpsd_lpcr
,
1331 reg_tpsd_lpcr_pos
, reg_tpsd_lpcr_len
,
1335 deb_info("CODERATE LP ");
1338 fep
->u
.ofdm
.code_rate_LP
= FEC_1_2
;
1339 deb_info("FEC_1_2\n");
1342 fep
->u
.ofdm
.code_rate_LP
= FEC_2_3
;
1343 deb_info("FEC_2_3\n");
1346 fep
->u
.ofdm
.code_rate_LP
= FEC_3_4
;
1347 deb_info("FEC_3_4\n");
1350 fep
->u
.ofdm
.code_rate_LP
= FEC_5_6
;
1351 deb_info("FEC_5_6\n");
1354 fep
->u
.ofdm
.code_rate_LP
= FEC_7_8
;
1355 deb_info("FEC_7_8\n");
1359 /* guard interval */
1361 af9005_read_register_bits(state
->d
, xd_g_reg_tpsd_gi
,
1362 reg_tpsd_gi_pos
, reg_tpsd_gi_len
, &temp
);
1365 deb_info("GUARD INTERVAL ");
1368 fep
->u
.ofdm
.guard_interval
= GUARD_INTERVAL_1_32
;
1372 fep
->u
.ofdm
.guard_interval
= GUARD_INTERVAL_1_16
;
1376 fep
->u
.ofdm
.guard_interval
= GUARD_INTERVAL_1_8
;
1380 fep
->u
.ofdm
.guard_interval
= GUARD_INTERVAL_1_4
;
1387 af9005_read_register_bits(state
->d
, xd_g_reg_tpsd_txmod
,
1388 reg_tpsd_txmod_pos
, reg_tpsd_txmod_len
,
1392 deb_info("TRANSMISSION MODE ");
1395 fep
->u
.ofdm
.transmission_mode
= TRANSMISSION_MODE_2K
;
1399 fep
->u
.ofdm
.transmission_mode
= TRANSMISSION_MODE_8K
;
1406 af9005_read_register_bits(state
->d
, xd_g_reg_bw
, reg_bw_pos
,
1408 deb_info("BANDWIDTH ");
1411 fep
->u
.ofdm
.bandwidth
= BANDWIDTH_6_MHZ
;
1415 fep
->u
.ofdm
.bandwidth
= BANDWIDTH_7_MHZ
;
1419 fep
->u
.ofdm
.bandwidth
= BANDWIDTH_8_MHZ
;
1426 static void af9005_fe_release(struct dvb_frontend
*fe
)
1428 struct af9005_fe_state
*state
=
1429 (struct af9005_fe_state
*)fe
->demodulator_priv
;
1433 static struct dvb_frontend_ops af9005_fe_ops
;
1435 struct dvb_frontend
*af9005_fe_attach(struct dvb_usb_device
*d
)
1437 struct af9005_fe_state
*state
= NULL
;
1439 /* allocate memory for the internal state */
1440 state
= kzalloc(sizeof(struct af9005_fe_state
), GFP_KERNEL
);
1444 deb_info("attaching frontend af9005\n");
1449 memcpy(&state
->frontend
.ops
, &af9005_fe_ops
,
1450 sizeof(struct dvb_frontend_ops
));
1451 state
->frontend
.demodulator_priv
= state
;
1453 return &state
->frontend
;
1458 static struct dvb_frontend_ops af9005_fe_ops
= {
1460 .name
= "AF9005 USB DVB-T",
1462 .frequency_min
= 44250000,
1463 .frequency_max
= 867250000,
1464 .frequency_stepsize
= 250000,
1465 .caps
= FE_CAN_INVERSION_AUTO
|
1466 FE_CAN_FEC_1_2
| FE_CAN_FEC_2_3
| FE_CAN_FEC_3_4
|
1467 FE_CAN_FEC_5_6
| FE_CAN_FEC_7_8
| FE_CAN_FEC_AUTO
|
1468 FE_CAN_QPSK
| FE_CAN_QAM_16
| FE_CAN_QAM_64
|
1469 FE_CAN_QAM_AUTO
| FE_CAN_TRANSMISSION_MODE_AUTO
|
1470 FE_CAN_GUARD_INTERVAL_AUTO
| FE_CAN_RECOVER
|
1471 FE_CAN_HIERARCHY_AUTO
,
1474 .release
= af9005_fe_release
,
1476 .init
= af9005_fe_init
,
1477 .sleep
= af9005_fe_sleep
,
1478 .ts_bus_ctrl
= af9005_ts_bus_ctrl
,
1480 .set_frontend
= af9005_fe_set_frontend
,
1481 .get_frontend
= af9005_fe_get_frontend
,
1483 .read_status
= af9005_fe_read_status
,
1484 .read_ber
= af9005_fe_read_ber
,
1485 .read_signal_strength
= af9005_fe_read_signal_strength
,
1486 .read_snr
= af9005_fe_read_snr
,
1487 .read_ucblocks
= af9005_fe_read_unc_blocks
,