1af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti/* Frontend part of the Linux driver for the Afatech 9005
2af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti * USB1.1 DVB-T receiver.
3af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti *
4af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti * Copyright (C) 2007 Luca Olivetti (luca@ventoso.org)
5af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti *
6af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti * Thanks to Afatech who kindly provided information.
7af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti *
8af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti * This program is free software; you can redistribute it and/or modify
9af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti * it under the terms of the GNU General Public License as published by
10af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti * the Free Software Foundation; either version 2 of the License, or
11af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti * (at your option) any later version.
12af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti *
13af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti * This program is distributed in the hope that it will be useful,
14af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti * but WITHOUT ANY WARRANTY; without even the implied warranty of
15af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti * GNU General Public License for more details.
17af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti *
18af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti * You should have received a copy of the GNU General Public License
19af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti * along with this program; if not, write to the Free Software
20af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti *
22af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti * see Documentation/dvb/README.dvb-usb for more information
23af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti */
24af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti#include "af9005.h"
25af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti#include "af9005-script.h"
26af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti#include "mt2060.h"
27af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti#include "qt1010.h"
28af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti#include <asm/div64.h>
29af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
30af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivettistruct af9005_fe_state {
31af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	struct dvb_usb_device *d;
32af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	fe_status_t stat;
33af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
34af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	/* retraining parameters */
35af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	u32 original_fcw;
36af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	u16 original_rf_top;
37af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	u16 original_if_top;
38af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	u16 original_if_min;
39af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	u16 original_aci0_if_top;
40af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	u16 original_aci1_if_top;
41af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	u16 original_aci0_if_min;
42af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	u8 original_if_unplug_th;
43af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	u8 original_rf_unplug_th;
44af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	u8 original_dtop_if_unplug_th;
45af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	u8 original_dtop_rf_unplug_th;
46af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
47af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	/* statistics */
48af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	u32 pre_vit_error_count;
49af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	u32 pre_vit_bit_count;
50af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	u32 ber;
51af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	u32 post_vit_error_count;
52af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	u32 post_vit_bit_count;
53af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	u32 unc;
54af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	u16 abort_count;
55af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
56af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	int opened;
57af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	int strong;
58af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	unsigned long next_status_check;
59af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	struct dvb_frontend frontend;
60af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti};
61af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
62af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivettistatic int af9005_write_word_agc(struct dvb_usb_device *d, u16 reghi,
63af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti				 u16 reglo, u8 pos, u8 len, u16 value)
64af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti{
65af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	int ret;
66af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
67af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if ((ret = af9005_write_ofdm_register(d, reglo, (u8) (value & 0xff))))
68af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
69af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	return af9005_write_register_bits(d, reghi, pos, len,
70af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti					  (u8) ((value & 0x300) >> 8));
71af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti}
72af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
73af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivettistatic int af9005_read_word_agc(struct dvb_usb_device *d, u16 reghi,
74af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti				u16 reglo, u8 pos, u8 len, u16 * value)
75af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti{
76af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	int ret;
77af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	u8 temp0, temp1;
78af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
79af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if ((ret = af9005_read_ofdm_register(d, reglo, &temp0)))
80af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
81af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if ((ret = af9005_read_ofdm_register(d, reghi, &temp1)))
82af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
83af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	switch (pos) {
84af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	case 0:
85af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		*value = ((u16) (temp1 & 0x03) << 8) + (u16) temp0;
86af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		break;
87af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	case 2:
88af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		*value = ((u16) (temp1 & 0x0C) << 6) + (u16) temp0;
89af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		break;
90af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	case 4:
91af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		*value = ((u16) (temp1 & 0x30) << 4) + (u16) temp0;
92af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		break;
93af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	case 6:
94af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		*value = ((u16) (temp1 & 0xC0) << 2) + (u16) temp0;
95af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		break;
96af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	default:
97af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		err("invalid pos in read word agc");
98af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return -EINVAL;
99af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	}
100af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	return 0;
101af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
102af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti}
103af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
104af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivettistatic int af9005_is_fecmon_available(struct dvb_frontend *fe, int *available)
105af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti{
106af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	struct af9005_fe_state *state = fe->demodulator_priv;
107af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	int ret;
108af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	u8 temp;
109af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
110af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	*available = false;
111af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
112af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret = af9005_read_register_bits(state->d, xd_p_fec_vtb_rsd_mon_en,
113af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti					fec_vtb_rsd_mon_en_pos,
114af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti					fec_vtb_rsd_mon_en_len, &temp);
115af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
116af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
117af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (temp & 1) {
118af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		ret =
119af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		    af9005_read_register_bits(state->d,
120af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti					      xd_p_reg_ofsm_read_rbc_en,
121af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti					      reg_ofsm_read_rbc_en_pos,
122af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti					      reg_ofsm_read_rbc_en_len, &temp);
123af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		if (ret)
124af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti			return ret;
125af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		if ((temp & 1) == 0)
126af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti			*available = true;
127af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
128af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	}
129af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	return 0;
130af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti}
131af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
132af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivettistatic int af9005_get_post_vit_err_cw_count(struct dvb_frontend *fe,
133af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti					    u32 * post_err_count,
134af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti					    u32 * post_cw_count,
135af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti					    u16 * abort_count)
136af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti{
137af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	struct af9005_fe_state *state = fe->demodulator_priv;
138af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	int ret;
139af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	u32 err_count;
140af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	u32 cw_count;
141af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	u8 temp, temp0, temp1, temp2;
142af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	u16 loc_abort_count;
143af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
144af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	*post_err_count = 0;
145af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	*post_cw_count = 0;
146af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
147af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	/* check if error bit count is ready */
148af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret =
149af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	    af9005_read_register_bits(state->d, xd_r_fec_rsd_ber_rdy,
150af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti				      fec_rsd_ber_rdy_pos, fec_rsd_ber_rdy_len,
151af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti				      &temp);
152af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
153af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
154af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (!temp) {
155af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		deb_info("rsd counter not ready\n");
156af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return 100;
157af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	}
158af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	/* get abort count */
159af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret =
160af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	    af9005_read_ofdm_register(state->d,
161af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti				      xd_r_fec_rsd_abort_packet_cnt_7_0,
162af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti				      &temp0);
163af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
164af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
165af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret =
166af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	    af9005_read_ofdm_register(state->d,
167af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti				      xd_r_fec_rsd_abort_packet_cnt_15_8,
168af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti				      &temp1);
169af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
170af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
171af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	loc_abort_count = ((u16) temp1 << 8) + temp0;
172af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
173af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	/* get error count */
174af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret =
175af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	    af9005_read_ofdm_register(state->d, xd_r_fec_rsd_bit_err_cnt_7_0,
176af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti				      &temp0);
177af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
178af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
179af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret =
180af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	    af9005_read_ofdm_register(state->d, xd_r_fec_rsd_bit_err_cnt_15_8,
181af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti				      &temp1);
182af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
183af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
184af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret =
185af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	    af9005_read_ofdm_register(state->d, xd_r_fec_rsd_bit_err_cnt_23_16,
186af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti				      &temp2);
187af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
188af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
189af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	err_count = ((u32) temp2 << 16) + ((u32) temp1 << 8) + temp0;
190af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	*post_err_count = err_count - (u32) loc_abort_count *8 * 8;
191af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
192af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	/* get RSD packet number */
193af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret =
194af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	    af9005_read_ofdm_register(state->d, xd_p_fec_rsd_packet_unit_7_0,
195af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti				      &temp0);
196af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
197af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
198af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret =
199af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	    af9005_read_ofdm_register(state->d, xd_p_fec_rsd_packet_unit_15_8,
200af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti				      &temp1);
201af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
202af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
203af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	cw_count = ((u32) temp1 << 8) + temp0;
204af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (cw_count == 0) {
205af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		err("wrong RSD packet count");
206af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return -EIO;
207af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	}
208af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	deb_info("POST abort count %d err count %d rsd packets %d\n",
209af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		 loc_abort_count, err_count, cw_count);
210af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	*post_cw_count = cw_count - (u32) loc_abort_count;
211af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	*abort_count = loc_abort_count;
212af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	return 0;
213af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
214af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti}
215af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
216af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivettistatic int af9005_get_post_vit_ber(struct dvb_frontend *fe,
217af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti				   u32 * post_err_count, u32 * post_cw_count,
218af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti				   u16 * abort_count)
219af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti{
220af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	u32 loc_cw_count = 0, loc_err_count;
221cf8e193a48879a02f55b53c0cf2ec98a784baaa5Hans Verkuil	u16 loc_abort_count = 0;
222af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	int ret;
223af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
224af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret =
225af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	    af9005_get_post_vit_err_cw_count(fe, &loc_err_count, &loc_cw_count,
226af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti					     &loc_abort_count);
227af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
228af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
229af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	*post_err_count = loc_err_count;
230af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	*post_cw_count = loc_cw_count * 204 * 8;
231af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	*abort_count = loc_abort_count;
232af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
233af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	return 0;
234af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti}
235af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
236af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivettistatic int af9005_get_pre_vit_err_bit_count(struct dvb_frontend *fe,
237af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti					    u32 * pre_err_count,
238af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti					    u32 * pre_bit_count)
239af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti{
240af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	struct af9005_fe_state *state = fe->demodulator_priv;
241af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	u8 temp, temp0, temp1, temp2;
242af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	u32 super_frame_count, x, bits;
243af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	int ret;
244af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
245af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret =
246af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	    af9005_read_register_bits(state->d, xd_r_fec_vtb_ber_rdy,
247af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti				      fec_vtb_ber_rdy_pos, fec_vtb_ber_rdy_len,
248af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti				      &temp);
249af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
250af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
251af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (!temp) {
252af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		deb_info("viterbi counter not ready\n");
253af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return 101;	/* ERR_APO_VTB_COUNTER_NOT_READY; */
254af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	}
255af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret =
256af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	    af9005_read_ofdm_register(state->d, xd_r_fec_vtb_err_bit_cnt_7_0,
257af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti				      &temp0);
258af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
259af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
260af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret =
261af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	    af9005_read_ofdm_register(state->d, xd_r_fec_vtb_err_bit_cnt_15_8,
262af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti				      &temp1);
263af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
264af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
265af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret =
266af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	    af9005_read_ofdm_register(state->d, xd_r_fec_vtb_err_bit_cnt_23_16,
267af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti				      &temp2);
268af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
269af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
270af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	*pre_err_count = ((u32) temp2 << 16) + ((u32) temp1 << 8) + temp0;
271af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
272af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret =
273af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	    af9005_read_ofdm_register(state->d, xd_p_fec_super_frm_unit_7_0,
274af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti				      &temp0);
275af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
276af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
277af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret =
278af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	    af9005_read_ofdm_register(state->d, xd_p_fec_super_frm_unit_15_8,
279af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti				      &temp1);
280af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
281af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
282af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	super_frame_count = ((u32) temp1 << 8) + temp0;
283af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (super_frame_count == 0) {
284af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		deb_info("super frame count 0\n");
285af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return 102;
286af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	}
287af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
288af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	/* read fft mode */
289af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret =
290af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	    af9005_read_register_bits(state->d, xd_g_reg_tpsd_txmod,
291af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti				      reg_tpsd_txmod_pos, reg_tpsd_txmod_len,
292af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti				      &temp);
293af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
294af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
295af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (temp == 0) {
296af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		/* 2K */
297af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		x = 1512;
298af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	} else if (temp == 1) {
299af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		/* 8k */
300af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		x = 6048;
301af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	} else {
302af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		err("Invalid fft mode");
303af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return -EINVAL;
304af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	}
305af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
306f9e54512fd16379812bcff86d95d0a7d78028b20Mauro Carvalho Chehab	/* read modulation mode */
307af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret =
308af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	    af9005_read_register_bits(state->d, xd_g_reg_tpsd_const,
309af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti				      reg_tpsd_const_pos, reg_tpsd_const_len,
310af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti				      &temp);
311af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
312af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
313af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	switch (temp) {
314af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	case 0:		/* QPSK */
315af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		bits = 2;
316af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		break;
317af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	case 1:		/* QAM_16 */
318af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		bits = 4;
319af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		break;
320af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	case 2:		/* QAM_64 */
321af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		bits = 6;
322af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		break;
323af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	default:
324f9e54512fd16379812bcff86d95d0a7d78028b20Mauro Carvalho Chehab		err("invalid modulation mode");
325af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return -EINVAL;
326af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	}
327af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	*pre_bit_count = super_frame_count * 68 * 4 * x * bits;
328af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	deb_info("PRE err count %d frame count %d bit count %d\n",
329af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		 *pre_err_count, super_frame_count, *pre_bit_count);
330af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	return 0;
331af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti}
332af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
333af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivettistatic int af9005_reset_pre_viterbi(struct dvb_frontend *fe)
334af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti{
335af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	struct af9005_fe_state *state = fe->demodulator_priv;
336af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	int ret;
337af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
338af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	/* set super frame count to 1 */
339af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret =
340af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	    af9005_write_ofdm_register(state->d, xd_p_fec_super_frm_unit_7_0,
341af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti				       1 & 0xff);
342af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
343af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
3444bc4365291bc27798d760c87f3aada39d9a74f43Adrian Bunk	ret = af9005_write_ofdm_register(state->d, xd_p_fec_super_frm_unit_15_8,
3454bc4365291bc27798d760c87f3aada39d9a74f43Adrian Bunk					 1 >> 8);
346af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
347af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
348af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	/* reset pre viterbi error count */
349af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret =
350af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	    af9005_write_register_bits(state->d, xd_p_fec_vtb_ber_rst,
351af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti				       fec_vtb_ber_rst_pos, fec_vtb_ber_rst_len,
352af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti				       1);
353af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
354af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	return ret;
355af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti}
356af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
357af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivettistatic int af9005_reset_post_viterbi(struct dvb_frontend *fe)
358af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti{
359af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	struct af9005_fe_state *state = fe->demodulator_priv;
360af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	int ret;
361af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
362af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	/* set packet unit */
363af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret =
364af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	    af9005_write_ofdm_register(state->d, xd_p_fec_rsd_packet_unit_7_0,
365af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti				       10000 & 0xff);
366af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
367af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
368af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret =
369af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	    af9005_write_ofdm_register(state->d, xd_p_fec_rsd_packet_unit_15_8,
370af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti				       10000 >> 8);
371af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
372af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
373af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	/* reset post viterbi error count */
374af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret =
375af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	    af9005_write_register_bits(state->d, xd_p_fec_rsd_ber_rst,
376af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti				       fec_rsd_ber_rst_pos, fec_rsd_ber_rst_len,
377af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti				       1);
378af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
379af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	return ret;
380af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti}
381af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
382af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivettistatic int af9005_get_statistic(struct dvb_frontend *fe)
383af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti{
384af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	struct af9005_fe_state *state = fe->demodulator_priv;
385af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	int ret, fecavailable;
386af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	u64 numerator, denominator;
387af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
388af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	deb_info("GET STATISTIC\n");
389af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret = af9005_is_fecmon_available(fe, &fecavailable);
390af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
391af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
392af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (!fecavailable) {
393af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		deb_info("fecmon not available\n");
394af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return 0;
395af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	}
396af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
397af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret = af9005_get_pre_vit_err_bit_count(fe, &state->pre_vit_error_count,
398af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti					       &state->pre_vit_bit_count);
399af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret == 0) {
400af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		af9005_reset_pre_viterbi(fe);
401af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		if (state->pre_vit_bit_count > 0) {
402af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti			/* according to v 0.0.4 of the dvb api ber should be a multiple
403af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti			   of 10E-9 so we have to multiply the error count by
404af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti			   10E9=1000000000 */
405af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti			numerator =
406af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti			    (u64) state->pre_vit_error_count * (u64) 1000000000;
407af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti			denominator = (u64) state->pre_vit_bit_count;
408af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti			state->ber = do_div(numerator, denominator);
409af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		} else {
410af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti			state->ber = 0xffffffff;
411af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		}
412af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	}
413af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
414af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret = af9005_get_post_vit_ber(fe, &state->post_vit_error_count,
415af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti				      &state->post_vit_bit_count,
416af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti				      &state->abort_count);
417af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret == 0) {
418af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		ret = af9005_reset_post_viterbi(fe);
419af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		state->unc += state->abort_count;
420af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		if (ret)
421af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti			return ret;
422af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	}
423af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	return 0;
424af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti}
425af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
426af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivettistatic int af9005_fe_refresh_state(struct dvb_frontend *fe)
427af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti{
428af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	struct af9005_fe_state *state = fe->demodulator_priv;
429af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (time_after(jiffies, state->next_status_check)) {
430af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		deb_info("REFRESH STATE\n");
431af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
432af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		/* statistics */
433af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		if (af9005_get_statistic(fe))
434af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti			err("get_statistic_failed");
435af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		state->next_status_check = jiffies + 250 * HZ / 1000;
436af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	}
437af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	return 0;
438af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti}
439af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
440af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivettistatic int af9005_fe_read_status(struct dvb_frontend *fe, fe_status_t * stat)
441af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti{
442af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	struct af9005_fe_state *state = fe->demodulator_priv;
443af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	u8 temp;
444af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	int ret;
445af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
446639ffd2d9038d34725b752147a0f1a8536146851Luca Olivetti	if (fe->ops.tuner_ops.release == NULL)
447af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return -ENODEV;
448af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
449af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	*stat = 0;
450af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret = af9005_read_register_bits(state->d, xd_p_agc_lock,
451af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti					agc_lock_pos, agc_lock_len, &temp);
452af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
453af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
454af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (temp)
455af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		*stat |= FE_HAS_SIGNAL;
456af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
457af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret = af9005_read_register_bits(state->d, xd_p_fd_tpsd_lock,
458af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti					fd_tpsd_lock_pos, fd_tpsd_lock_len,
459af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti					&temp);
460af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
461af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
462af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (temp)
463af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		*stat |= FE_HAS_CARRIER;
464af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
465af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret = af9005_read_register_bits(state->d,
466af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti					xd_r_mp2if_sync_byte_locked,
467af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti					mp2if_sync_byte_locked_pos,
468af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti					mp2if_sync_byte_locked_pos, &temp);
469af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
470af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
471af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (temp)
472af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		*stat |= FE_HAS_SYNC | FE_HAS_VITERBI | FE_HAS_LOCK;
473af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (state->opened)
474af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		af9005_led_control(state->d, *stat & FE_HAS_LOCK);
475af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
476af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret =
477af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	    af9005_read_register_bits(state->d, xd_p_reg_strong_sginal_detected,
478af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti				      reg_strong_sginal_detected_pos,
479af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti				      reg_strong_sginal_detected_len, &temp);
480af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
481af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
482af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (temp != state->strong) {
483af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		deb_info("adjust for strong signal %d\n", temp);
484af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti			state->strong = temp;
485af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	}
486af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	return 0;
487af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti}
488af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
489af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivettistatic int af9005_fe_read_ber(struct dvb_frontend *fe, u32 * ber)
490af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti{
491af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	struct af9005_fe_state *state = fe->demodulator_priv;
492639ffd2d9038d34725b752147a0f1a8536146851Luca Olivetti	if (fe->ops.tuner_ops.release  == NULL)
493af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return -ENODEV;
494af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	af9005_fe_refresh_state(fe);
495af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	*ber = state->ber;
496af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	return 0;
497af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti}
498af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
499af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivettistatic int af9005_fe_read_unc_blocks(struct dvb_frontend *fe, u32 * unc)
500af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti{
501af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	struct af9005_fe_state *state = fe->demodulator_priv;
502639ffd2d9038d34725b752147a0f1a8536146851Luca Olivetti	if (fe->ops.tuner_ops.release == NULL)
503af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return -ENODEV;
504af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	af9005_fe_refresh_state(fe);
505af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	*unc = state->unc;
506af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	return 0;
507af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti}
508af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
509af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivettistatic int af9005_fe_read_signal_strength(struct dvb_frontend *fe,
510af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti					  u16 * strength)
511af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti{
512af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	struct af9005_fe_state *state = fe->demodulator_priv;
513af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	int ret;
514af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	u8 if_gain, rf_gain;
515af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
516639ffd2d9038d34725b752147a0f1a8536146851Luca Olivetti	if (fe->ops.tuner_ops.release == NULL)
517af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return -ENODEV;
518af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret =
519af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	    af9005_read_ofdm_register(state->d, xd_r_reg_aagc_rf_gain,
520af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti				      &rf_gain);
521af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
522af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
523af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret =
524af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	    af9005_read_ofdm_register(state->d, xd_r_reg_aagc_if_gain,
525af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti				      &if_gain);
526af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
527af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
528af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	/* this value has no real meaning, but i don't have the tables that relate
529af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	   the rf and if gain with the dbm, so I just scale the value */
530af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	*strength = (512 - rf_gain - if_gain) << 7;
531af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	return 0;
532af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti}
533af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
534af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivettistatic int af9005_fe_read_snr(struct dvb_frontend *fe, u16 * snr)
535af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti{
536f9e54512fd16379812bcff86d95d0a7d78028b20Mauro Carvalho Chehab	/* the snr can be derived from the ber and the modulation
537af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	   but I don't think this kind of complex calculations belong
538af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	   in the driver. I may be wrong.... */
539af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	return -ENOSYS;
540af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti}
541af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
542f9e54512fd16379812bcff86d95d0a7d78028b20Mauro Carvalho Chehabstatic int af9005_fe_program_cfoe(struct dvb_usb_device *d, u32 bw)
543af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti{
544af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	u8 temp0, temp1, temp2, temp3, buf[4];
545af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	int ret;
546af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	u32 NS_coeff1_2048Nu;
547af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	u32 NS_coeff1_8191Nu;
548af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	u32 NS_coeff1_8192Nu;
549af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	u32 NS_coeff1_8193Nu;
550af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	u32 NS_coeff2_2k;
551af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	u32 NS_coeff2_8k;
552af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
553af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	switch (bw) {
554f9e54512fd16379812bcff86d95d0a7d78028b20Mauro Carvalho Chehab	case 6000000:
555af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		NS_coeff1_2048Nu = 0x2ADB6DC;
556af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		NS_coeff1_8191Nu = 0xAB7313;
557af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		NS_coeff1_8192Nu = 0xAB6DB7;
558af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		NS_coeff1_8193Nu = 0xAB685C;
559af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		NS_coeff2_2k = 0x156DB6E;
560af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		NS_coeff2_8k = 0x55B6DC;
561af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		break;
562af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
563f9e54512fd16379812bcff86d95d0a7d78028b20Mauro Carvalho Chehab	case 7000000:
564af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		NS_coeff1_2048Nu = 0x3200001;
565af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		NS_coeff1_8191Nu = 0xC80640;
566af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		NS_coeff1_8192Nu = 0xC80000;
567af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		NS_coeff1_8193Nu = 0xC7F9C0;
568af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		NS_coeff2_2k = 0x1900000;
569af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		NS_coeff2_8k = 0x640000;
570af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		break;
571af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
572f9e54512fd16379812bcff86d95d0a7d78028b20Mauro Carvalho Chehab	case 8000000:
573af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		NS_coeff1_2048Nu = 0x3924926;
574af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		NS_coeff1_8191Nu = 0xE4996E;
575af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		NS_coeff1_8192Nu = 0xE49249;
576af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		NS_coeff1_8193Nu = 0xE48B25;
577af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		NS_coeff2_2k = 0x1C92493;
578af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		NS_coeff2_8k = 0x724925;
579af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		break;
580af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	default:
58125985edcedea6396277003854657b5f3cb31a628Lucas De Marchi		err("Invalid bandwidth %d.", bw);
582af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return -EINVAL;
583af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	}
584af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
585af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	/*
586af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	 *  write NS_coeff1_2048Nu
587af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	 */
588af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
589af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	temp0 = (u8) (NS_coeff1_2048Nu & 0x000000FF);
590af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	temp1 = (u8) ((NS_coeff1_2048Nu & 0x0000FF00) >> 8);
591af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	temp2 = (u8) ((NS_coeff1_2048Nu & 0x00FF0000) >> 16);
592af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	temp3 = (u8) ((NS_coeff1_2048Nu & 0x03000000) >> 24);
593af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
594af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	/*  big endian to make 8051 happy */
595af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	buf[0] = temp3;
596af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	buf[1] = temp2;
597af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	buf[2] = temp1;
598af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	buf[3] = temp0;
599af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
600af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	/*  cfoe_NS_2k_coeff1_25_24 */
601af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret = af9005_write_ofdm_register(d, 0xAE00, buf[0]);
602af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
603af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
604af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
605af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	/*  cfoe_NS_2k_coeff1_23_16 */
606af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret = af9005_write_ofdm_register(d, 0xAE01, buf[1]);
607af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
608af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
609af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
610af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	/*  cfoe_NS_2k_coeff1_15_8 */
611af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret = af9005_write_ofdm_register(d, 0xAE02, buf[2]);
612af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
613af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
614af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
615af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	/*  cfoe_NS_2k_coeff1_7_0 */
616af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret = af9005_write_ofdm_register(d, 0xAE03, buf[3]);
617af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
618af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
619af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
620af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	/*
621af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	 *  write NS_coeff2_2k
622af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	 */
623af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
624af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	temp0 = (u8) ((NS_coeff2_2k & 0x0000003F));
625af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	temp1 = (u8) ((NS_coeff2_2k & 0x00003FC0) >> 6);
626af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	temp2 = (u8) ((NS_coeff2_2k & 0x003FC000) >> 14);
627af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	temp3 = (u8) ((NS_coeff2_2k & 0x01C00000) >> 22);
628af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
629af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	/*  big endian to make 8051 happy */
630af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	buf[0] = temp3;
631af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	buf[1] = temp2;
632af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	buf[2] = temp1;
633af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	buf[3] = temp0;
634af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
635af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret = af9005_write_ofdm_register(d, 0xAE04, buf[0]);
636af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
637af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
638af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
639af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret = af9005_write_ofdm_register(d, 0xAE05, buf[1]);
640af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
641af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
642af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
643af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret = af9005_write_ofdm_register(d, 0xAE06, buf[2]);
644af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
645af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
646af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
647af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret = af9005_write_ofdm_register(d, 0xAE07, buf[3]);
648af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
649af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
650af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
651af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	/*
652af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	 *  write NS_coeff1_8191Nu
653af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	 */
654af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
655af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	temp0 = (u8) ((NS_coeff1_8191Nu & 0x000000FF));
656af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	temp1 = (u8) ((NS_coeff1_8191Nu & 0x0000FF00) >> 8);
657af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	temp2 = (u8) ((NS_coeff1_8191Nu & 0x00FFC000) >> 16);
658af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	temp3 = (u8) ((NS_coeff1_8191Nu & 0x03000000) >> 24);
659af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
660af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	/*  big endian to make 8051 happy */
661af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	buf[0] = temp3;
662af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	buf[1] = temp2;
663af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	buf[2] = temp1;
664af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	buf[3] = temp0;
665af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
666af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret = af9005_write_ofdm_register(d, 0xAE08, buf[0]);
667af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
668af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
669af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
670af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret = af9005_write_ofdm_register(d, 0xAE09, buf[1]);
671af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
672af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
673af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
674af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret = af9005_write_ofdm_register(d, 0xAE0A, buf[2]);
675af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
676af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
677af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
678af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret = af9005_write_ofdm_register(d, 0xAE0B, buf[3]);
679af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
680af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
681af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
682af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	/*
683af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	 *  write NS_coeff1_8192Nu
684af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	 */
685af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
686af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	temp0 = (u8) (NS_coeff1_8192Nu & 0x000000FF);
687af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	temp1 = (u8) ((NS_coeff1_8192Nu & 0x0000FF00) >> 8);
688af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	temp2 = (u8) ((NS_coeff1_8192Nu & 0x00FFC000) >> 16);
689af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	temp3 = (u8) ((NS_coeff1_8192Nu & 0x03000000) >> 24);
690af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
691af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	/*  big endian to make 8051 happy */
692af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	buf[0] = temp3;
693af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	buf[1] = temp2;
694af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	buf[2] = temp1;
695af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	buf[3] = temp0;
696af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
697af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret = af9005_write_ofdm_register(d, 0xAE0C, buf[0]);
698af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
699af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
700af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
701af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret = af9005_write_ofdm_register(d, 0xAE0D, buf[1]);
702af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
703af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
704af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
705af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret = af9005_write_ofdm_register(d, 0xAE0E, buf[2]);
706af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
707af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
708af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
709af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret = af9005_write_ofdm_register(d, 0xAE0F, buf[3]);
710af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
711af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
712af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
713af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	/*
714af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	 *  write NS_coeff1_8193Nu
715af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	 */
716af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
717af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	temp0 = (u8) ((NS_coeff1_8193Nu & 0x000000FF));
718af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	temp1 = (u8) ((NS_coeff1_8193Nu & 0x0000FF00) >> 8);
719af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	temp2 = (u8) ((NS_coeff1_8193Nu & 0x00FFC000) >> 16);
720af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	temp3 = (u8) ((NS_coeff1_8193Nu & 0x03000000) >> 24);
721af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
722af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	/*  big endian to make 8051 happy */
723af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	buf[0] = temp3;
724af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	buf[1] = temp2;
725af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	buf[2] = temp1;
726af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	buf[3] = temp0;
727af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
728af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret = af9005_write_ofdm_register(d, 0xAE10, buf[0]);
729af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
730af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
731af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
732af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret = af9005_write_ofdm_register(d, 0xAE11, buf[1]);
733af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
734af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
735af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
736af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret = af9005_write_ofdm_register(d, 0xAE12, buf[2]);
737af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
738af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
739af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
740af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret = af9005_write_ofdm_register(d, 0xAE13, buf[3]);
741af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
742af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
743af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
744af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	/*
745af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	 *  write NS_coeff2_8k
746af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	 */
747af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
748af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	temp0 = (u8) ((NS_coeff2_8k & 0x0000003F));
749af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	temp1 = (u8) ((NS_coeff2_8k & 0x00003FC0) >> 6);
750af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	temp2 = (u8) ((NS_coeff2_8k & 0x003FC000) >> 14);
751af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	temp3 = (u8) ((NS_coeff2_8k & 0x01C00000) >> 22);
752af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
753af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	/*  big endian to make 8051 happy */
754af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	buf[0] = temp3;
755af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	buf[1] = temp2;
756af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	buf[2] = temp1;
757af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	buf[3] = temp0;
758af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
759af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret = af9005_write_ofdm_register(d, 0xAE14, buf[0]);
760af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
761af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
762af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
763af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret = af9005_write_ofdm_register(d, 0xAE15, buf[1]);
764af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
765af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
766af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
767af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret = af9005_write_ofdm_register(d, 0xAE16, buf[2]);
768af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
769af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
770af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
771af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret = af9005_write_ofdm_register(d, 0xAE17, buf[3]);
772af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	return ret;
773af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
774af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti}
775af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
776f9e54512fd16379812bcff86d95d0a7d78028b20Mauro Carvalho Chehabstatic int af9005_fe_select_bw(struct dvb_usb_device *d, u32 bw)
777af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti{
778af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	u8 temp;
779af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	switch (bw) {
780f9e54512fd16379812bcff86d95d0a7d78028b20Mauro Carvalho Chehab	case 6000000:
781af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		temp = 0;
782af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		break;
783f9e54512fd16379812bcff86d95d0a7d78028b20Mauro Carvalho Chehab	case 7000000:
784af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		temp = 1;
785af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		break;
786f9e54512fd16379812bcff86d95d0a7d78028b20Mauro Carvalho Chehab	case 8000000:
787af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		temp = 2;
788af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		break;
789af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	default:
79025985edcedea6396277003854657b5f3cb31a628Lucas De Marchi		err("Invalid bandwidth %d.", bw);
791af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return -EINVAL;
792af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	}
793af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	return af9005_write_register_bits(d, xd_g_reg_bw, reg_bw_pos,
794af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti					  reg_bw_len, temp);
795af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti}
796af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
797af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivettistatic int af9005_fe_power(struct dvb_frontend *fe, int on)
798af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti{
799af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	struct af9005_fe_state *state = fe->demodulator_priv;
800af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	u8 temp = on;
801af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	int ret;
802af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	deb_info("power %s tuner\n", on ? "on" : "off");
803af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret = af9005_send_command(state->d, 0x03, &temp, 1, NULL, 0);
804af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	return ret;
805af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti}
806af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
807af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivettistatic struct mt2060_config af9005_mt2060_config = {
808af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	0xC0
809af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti};
810af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
811af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivettistatic struct qt1010_config af9005_qt1010_config = {
812af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	0xC4
813af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti};
814af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
815af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivettistatic int af9005_fe_init(struct dvb_frontend *fe)
816af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti{
817af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	struct af9005_fe_state *state = fe->demodulator_priv;
818af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	struct dvb_usb_adapter *adap = fe->dvb->priv;
819af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	int ret, i, scriptlen;
820af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	u8 temp, temp0 = 0, temp1 = 0, temp2 = 0;
821af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	u8 buf[2];
822af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	u16 if1;
823af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
824af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	deb_info("in af9005_fe_init\n");
825af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
826af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	/* reset */
827af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	deb_info("reset\n");
828af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if ((ret =
829af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	     af9005_write_register_bits(state->d, xd_I2C_reg_ofdm_rst_en,
830af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti					4, 1, 0x01)))
831af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
832af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if ((ret = af9005_write_ofdm_register(state->d, APO_REG_RESET, 0)))
833af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
834af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	/* clear ofdm reset */
835af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	deb_info("clear ofdm reset\n");
836af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	for (i = 0; i < 150; i++) {
837af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		if ((ret =
838af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		     af9005_read_ofdm_register(state->d,
839af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti					       xd_I2C_reg_ofdm_rst, &temp)))
840af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti			return ret;
841af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		if (temp & (regmask[reg_ofdm_rst_len - 1] << reg_ofdm_rst_pos))
842af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti			break;
843af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		msleep(10);
844af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	}
845af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (i == 150)
846af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return -ETIMEDOUT;
847af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
848af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	/*FIXME in the dump
849af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	   write B200 A9
850af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	   write xd_g_reg_ofsm_clk 7
851af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	   read eepr c6 (2)
852af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	   read eepr c7 (2)
853af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	   misc ctrl 3 -> 1
854af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	   read eepr ca (6)
855af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	   write xd_g_reg_ofsm_clk 0
856af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	   write B200 a1
857af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	 */
858af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret = af9005_write_ofdm_register(state->d, 0xb200, 0xa9);
859af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
860af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
861af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret = af9005_write_ofdm_register(state->d, xd_g_reg_ofsm_clk, 0x07);
862af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
863af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
864af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	temp = 0x01;
865af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret = af9005_send_command(state->d, 0x03, &temp, 1, NULL, 0);
866af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
867af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
868af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret = af9005_write_ofdm_register(state->d, xd_g_reg_ofsm_clk, 0x00);
869af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
870af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
871af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret = af9005_write_ofdm_register(state->d, 0xb200, 0xa1);
872af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
873af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
874af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
875af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	temp = regmask[reg_ofdm_rst_len - 1] << reg_ofdm_rst_pos;
876af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if ((ret =
877af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	     af9005_write_register_bits(state->d, xd_I2C_reg_ofdm_rst,
878af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti					reg_ofdm_rst_pos, reg_ofdm_rst_len, 1)))
879af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
8804bc4365291bc27798d760c87f3aada39d9a74f43Adrian Bunk	ret = af9005_write_register_bits(state->d, xd_I2C_reg_ofdm_rst,
8814bc4365291bc27798d760c87f3aada39d9a74f43Adrian Bunk					 reg_ofdm_rst_pos, reg_ofdm_rst_len, 0);
882af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
883af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
884af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
885af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	/* don't know what register aefc is, but this is what the windows driver does */
886af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret = af9005_write_ofdm_register(state->d, 0xaefc, 0);
887af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
888af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
889af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
890af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	/* set stand alone chip */
891af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	deb_info("set stand alone chip\n");
892af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if ((ret =
893af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	     af9005_write_register_bits(state->d, xd_p_reg_dca_stand_alone,
894af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti					reg_dca_stand_alone_pos,
895af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti					reg_dca_stand_alone_len, 1)))
896af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
897af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
898af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	/* set dca upper & lower chip */
899af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	deb_info("set dca upper & lower chip\n");
900af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if ((ret =
901af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	     af9005_write_register_bits(state->d, xd_p_reg_dca_upper_chip,
902af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti					reg_dca_upper_chip_pos,
903af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti					reg_dca_upper_chip_len, 0)))
904af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
905af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if ((ret =
906af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	     af9005_write_register_bits(state->d, xd_p_reg_dca_lower_chip,
907af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti					reg_dca_lower_chip_pos,
908af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti					reg_dca_lower_chip_len, 0)))
909af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
910af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
911af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	/* set 2wire master clock to 0x14 (for 60KHz) */
912af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	deb_info("set 2wire master clock to 0x14 (for 60KHz)\n");
913af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if ((ret =
914af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	     af9005_write_ofdm_register(state->d, xd_I2C_i2c_m_period, 0x14)))
915af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
916af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
917af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	/* clear dca enable chip */
918af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	deb_info("clear dca enable chip\n");
919af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if ((ret =
920af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	     af9005_write_register_bits(state->d, xd_p_reg_dca_en,
921af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti					reg_dca_en_pos, reg_dca_en_len, 0)))
922af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
923af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	/* FIXME these are register bits, but I don't know which ones */
924af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret = af9005_write_ofdm_register(state->d, 0xa16c, 1);
925af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
926af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
927af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret = af9005_write_ofdm_register(state->d, 0xa3c1, 0);
928af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
929af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
930af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
93125985edcedea6396277003854657b5f3cb31a628Lucas De Marchi	/* init other parameters: program cfoe and select bandwidth */
932af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	deb_info("program cfoe\n");
933f9e54512fd16379812bcff86d95d0a7d78028b20Mauro Carvalho Chehab	ret = af9005_fe_program_cfoe(state->d, 6000000);
934f9e54512fd16379812bcff86d95d0a7d78028b20Mauro Carvalho Chehab	if (ret)
935af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
936f9e54512fd16379812bcff86d95d0a7d78028b20Mauro Carvalho Chehab	/* set read-update bit for modulation */
937f9e54512fd16379812bcff86d95d0a7d78028b20Mauro Carvalho Chehab	deb_info("set read-update bit for modulation\n");
938af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if ((ret =
939af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	     af9005_write_register_bits(state->d, xd_p_reg_feq_read_update,
940af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti					reg_feq_read_update_pos,
941af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti					reg_feq_read_update_len, 1)))
942af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
943af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
944af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	/* sample code has a set MPEG TS code here
945af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	   but sniffing reveals that it doesn't do it */
946af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
947f9e54512fd16379812bcff86d95d0a7d78028b20Mauro Carvalho Chehab	/* set read-update bit to 1 for DCA modulation */
948f9e54512fd16379812bcff86d95d0a7d78028b20Mauro Carvalho Chehab	deb_info("set read-update bit 1 for DCA modulation\n");
949af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if ((ret =
950af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	     af9005_write_register_bits(state->d, xd_p_reg_dca_read_update,
951af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti					reg_dca_read_update_pos,
952af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti					reg_dca_read_update_len, 1)))
953af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
954af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
955af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	/* enable fec monitor */
956af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	deb_info("enable fec monitor\n");
957af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if ((ret =
958af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	     af9005_write_register_bits(state->d, xd_p_fec_vtb_rsd_mon_en,
959af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti					fec_vtb_rsd_mon_en_pos,
960af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti					fec_vtb_rsd_mon_en_len, 1)))
961af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
962af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
963af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	/* FIXME should be register bits, I don't know which ones */
964af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret = af9005_write_ofdm_register(state->d, 0xa601, 0);
965af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
966af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	/* set api_retrain_never_freeze */
967af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	deb_info("set api_retrain_never_freeze\n");
968af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if ((ret = af9005_write_ofdm_register(state->d, 0xaefb, 0x01)))
969af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
970af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
971af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	/* load init script */
972af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	deb_info("load init script\n");
973af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	scriptlen = sizeof(script) / sizeof(RegDesc);
974af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	for (i = 0; i < scriptlen; i++) {
975af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		if ((ret =
976af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		     af9005_write_register_bits(state->d, script[i].reg,
977af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti						script[i].pos,
978af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti						script[i].len, script[i].val)))
979af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti			return ret;
980af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		/* save 3 bytes of original fcw */
981af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		if (script[i].reg == 0xae18)
982af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti			temp2 = script[i].val;
983af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		if (script[i].reg == 0xae19)
984af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti			temp1 = script[i].val;
985af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		if (script[i].reg == 0xae1a)
986af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti			temp0 = script[i].val;
987af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
988af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		/* save original unplug threshold */
989af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		if (script[i].reg == xd_p_reg_unplug_th)
990af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti			state->original_if_unplug_th = script[i].val;
991af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		if (script[i].reg == xd_p_reg_unplug_rf_gain_th)
992af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti			state->original_rf_unplug_th = script[i].val;
993af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		if (script[i].reg == xd_p_reg_unplug_dtop_if_gain_th)
994af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti			state->original_dtop_if_unplug_th = script[i].val;
995af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		if (script[i].reg == xd_p_reg_unplug_dtop_rf_gain_th)
996af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti			state->original_dtop_rf_unplug_th = script[i].val;
997af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
998af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	}
999af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	state->original_fcw =
1000af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	    ((u32) temp2 << 16) + ((u32) temp1 << 8) + (u32) temp0;
1001af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
1002af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
1003af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	/* save original TOPs */
1004af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	deb_info("save original TOPs\n");
1005af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
1006af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	/*  RF TOP */
1007af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret =
1008af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	    af9005_read_word_agc(state->d,
1009af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti				 xd_p_reg_aagc_rf_top_numerator_9_8,
1010af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti				 xd_p_reg_aagc_rf_top_numerator_7_0, 0, 2,
1011af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti				 &state->original_rf_top);
1012af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
1013af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
1014af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
1015af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	/*  IF TOP */
1016af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret =
1017af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	    af9005_read_word_agc(state->d,
1018af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti				 xd_p_reg_aagc_if_top_numerator_9_8,
1019af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti				 xd_p_reg_aagc_if_top_numerator_7_0, 0, 2,
1020af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti				 &state->original_if_top);
1021af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
1022af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
1023af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
1024af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	/*  ACI 0 IF TOP */
1025af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret =
1026af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	    af9005_read_word_agc(state->d, 0xA60E, 0xA60A, 4, 2,
1027af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti				 &state->original_aci0_if_top);
1028af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
1029af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
1030af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
1031af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	/*  ACI 1 IF TOP */
1032af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret =
1033af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	    af9005_read_word_agc(state->d, 0xA60E, 0xA60B, 6, 2,
1034af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti				 &state->original_aci1_if_top);
1035af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
1036af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
1037af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
1038af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	/* attach tuner and init */
1039639ffd2d9038d34725b752147a0f1a8536146851Luca Olivetti	if (fe->ops.tuner_ops.release == NULL) {
1040af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		/* read tuner and board id from eeprom */
1041af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		ret = af9005_read_eeprom(adap->dev, 0xc6, buf, 2);
1042af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		if (ret) {
1043af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti			err("Impossible to read EEPROM\n");
1044af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti			return ret;
1045af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		}
1046af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		deb_info("Tuner id %d, board id %d\n", buf[0], buf[1]);
1047af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		switch (buf[0]) {
1048af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		case 2:	/* MT2060 */
1049af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti			/* read if1 from eeprom */
1050af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti			ret = af9005_read_eeprom(adap->dev, 0xc8, buf, 2);
1051af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti			if (ret) {
1052af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti				err("Impossible to read EEPROM\n");
1053af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti				return ret;
1054af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti			}
1055af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti			if1 = (u16) (buf[0] << 8) + buf[1];
1056639ffd2d9038d34725b752147a0f1a8536146851Luca Olivetti			if (dvb_attach(mt2060_attach, fe, &adap->dev->i2c_adap,
1057639ffd2d9038d34725b752147a0f1a8536146851Luca Olivetti					 &af9005_mt2060_config, if1) == NULL) {
1058af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti				deb_info("MT2060 attach failed\n");
1059af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti				return -ENODEV;
1060af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti			}
1061af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti			break;
1062af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		case 3:	/* QT1010 */
1063af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		case 9:	/* QT1010B */
1064639ffd2d9038d34725b752147a0f1a8536146851Luca Olivetti			if (dvb_attach(qt1010_attach, fe, &adap->dev->i2c_adap,
1065639ffd2d9038d34725b752147a0f1a8536146851Luca Olivetti					&af9005_qt1010_config) ==NULL) {
1066af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti				deb_info("QT1010 attach failed\n");
1067af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti				return -ENODEV;
1068af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti			}
1069af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti			break;
1070af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		default:
1071af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti			err("Unsupported tuner type %d", buf[0]);
1072af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti			return -ENODEV;
1073af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		}
1074639ffd2d9038d34725b752147a0f1a8536146851Luca Olivetti		ret = fe->ops.tuner_ops.init(fe);
1075af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		if (ret)
1076af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti			return ret;
1077af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	}
1078af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
1079af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	deb_info("profit!\n");
1080af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	return 0;
1081af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti}
1082af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
1083af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivettistatic int af9005_fe_sleep(struct dvb_frontend *fe)
1084af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti{
1085af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	return af9005_fe_power(fe, 0);
1086af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti}
1087af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
1088af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivettistatic int af9005_ts_bus_ctrl(struct dvb_frontend *fe, int acquire)
1089af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti{
1090af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	struct af9005_fe_state *state = fe->demodulator_priv;
1091af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
1092af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (acquire) {
1093af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		state->opened++;
1094af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	} else {
1095af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
1096af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		state->opened--;
1097af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		if (!state->opened)
1098af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti			af9005_led_control(state->d, 0);
1099af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	}
1100af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	return 0;
1101af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti}
1102af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
1103f9e54512fd16379812bcff86d95d0a7d78028b20Mauro Carvalho Chehabstatic int af9005_fe_set_frontend(struct dvb_frontend *fe)
1104af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti{
1105f9e54512fd16379812bcff86d95d0a7d78028b20Mauro Carvalho Chehab	struct dtv_frontend_properties *fep = &fe->dtv_property_cache;
1106af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	struct af9005_fe_state *state = fe->demodulator_priv;
1107af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	int ret;
1108af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	u8 temp, temp0, temp1, temp2;
1109af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
1110af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	deb_info("af9005_fe_set_frontend freq %d bw %d\n", fep->frequency,
1111f9e54512fd16379812bcff86d95d0a7d78028b20Mauro Carvalho Chehab		 fep->bandwidth_hz);
1112639ffd2d9038d34725b752147a0f1a8536146851Luca Olivetti	if (fe->ops.tuner_ops.release == NULL) {
1113af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		err("Tuner not attached");
1114af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return -ENODEV;
1115af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	}
1116af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
1117af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	deb_info("turn off led\n");
1118af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	/* not in the log */
1119af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret = af9005_led_control(state->d, 0);
1120af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
1121af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
1122af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	/* not sure about the bits */
1123af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret = af9005_write_register_bits(state->d, XD_MP2IF_MISC, 2, 1, 0);
1124af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
1125af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
1126af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
1127af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	/* set FCW to default value */
1128af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	deb_info("set FCW to default value\n");
1129af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	temp0 = (u8) (state->original_fcw & 0x000000ff);
1130af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	temp1 = (u8) ((state->original_fcw & 0x0000ff00) >> 8);
1131af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	temp2 = (u8) ((state->original_fcw & 0x00ff0000) >> 16);
1132af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret = af9005_write_ofdm_register(state->d, 0xae1a, temp0);
1133af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
1134af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
1135af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret = af9005_write_ofdm_register(state->d, 0xae19, temp1);
1136af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
1137af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
1138af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret = af9005_write_ofdm_register(state->d, 0xae18, temp2);
1139af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
1140af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
1141af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
1142af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	/* restore original TOPs */
1143af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	deb_info("restore original TOPs\n");
1144af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret =
1145af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	    af9005_write_word_agc(state->d,
1146af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti				  xd_p_reg_aagc_rf_top_numerator_9_8,
1147af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti				  xd_p_reg_aagc_rf_top_numerator_7_0, 0, 2,
1148af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti				  state->original_rf_top);
1149af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
1150af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
1151af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret =
1152af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	    af9005_write_word_agc(state->d,
1153af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti				  xd_p_reg_aagc_if_top_numerator_9_8,
1154af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti				  xd_p_reg_aagc_if_top_numerator_7_0, 0, 2,
1155af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti				  state->original_if_top);
1156af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
1157af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
1158af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret =
1159af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	    af9005_write_word_agc(state->d, 0xA60E, 0xA60A, 4, 2,
1160af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti				  state->original_aci0_if_top);
1161af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
1162af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
1163af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret =
1164af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	    af9005_write_word_agc(state->d, 0xA60E, 0xA60B, 6, 2,
1165af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti				  state->original_aci1_if_top);
1166af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
1167af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
1168af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
116925985edcedea6396277003854657b5f3cb31a628Lucas De Marchi	/* select bandwidth */
1170af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	deb_info("select bandwidth");
1171f9e54512fd16379812bcff86d95d0a7d78028b20Mauro Carvalho Chehab	ret = af9005_fe_select_bw(state->d, fep->bandwidth_hz);
1172af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
1173af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
1174f9e54512fd16379812bcff86d95d0a7d78028b20Mauro Carvalho Chehab	ret = af9005_fe_program_cfoe(state->d, fep->bandwidth_hz);
1175af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
1176af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
1177af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
1178af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	/* clear easy mode flag */
1179af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	deb_info("clear easy mode flag\n");
1180af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret = af9005_write_ofdm_register(state->d, 0xaefd, 0);
1181af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
1182af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
1183af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
1184af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	/* set unplug threshold to original value */
1185af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	deb_info("set unplug threshold to original value\n");
1186af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret =
1187af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	    af9005_write_ofdm_register(state->d, xd_p_reg_unplug_th,
1188af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti				       state->original_if_unplug_th);
1189af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
1190af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
1191af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	/* set tuner */
1192af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	deb_info("set tuner\n");
119314d24d148c7521b2b88b396652e36f55d061e195Mauro Carvalho Chehab	ret = fe->ops.tuner_ops.set_params(fe);
1194af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
1195af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
1196af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
1197af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	/* trigger ofsm */
1198af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	deb_info("trigger ofsm\n");
1199af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	temp = 0;
1200af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret = af9005_write_tuner_registers(state->d, 0xffff, &temp, 1);
1201af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
1202af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
1203af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
1204af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	/* clear retrain and freeze flag */
1205af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	deb_info("clear retrain and freeze flag\n");
1206af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret =
1207af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	    af9005_write_register_bits(state->d,
1208af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti				       xd_p_reg_api_retrain_request,
1209af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti				       reg_api_retrain_request_pos, 2, 0);
1210af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
1211af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
1212af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
1213af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	/* reset pre viterbi and post viterbi registers and statistics */
1214af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	af9005_reset_pre_viterbi(fe);
1215af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	af9005_reset_post_viterbi(fe);
1216af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	state->pre_vit_error_count = 0;
1217af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	state->pre_vit_bit_count = 0;
1218af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	state->ber = 0;
1219af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	state->post_vit_error_count = 0;
1220af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	/* state->unc = 0; commented out since it should be ever increasing */
1221af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	state->abort_count = 0;
1222af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
1223af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	state->next_status_check = jiffies;
1224af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	state->strong = -1;
1225af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
1226af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	return 0;
1227af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti}
1228af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
12297c61d80a9bcfc3fdec8ffd75756cad6a64678229Mauro Carvalho Chehabstatic int af9005_fe_get_frontend(struct dvb_frontend *fe)
1230af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti{
12317c61d80a9bcfc3fdec8ffd75756cad6a64678229Mauro Carvalho Chehab	struct dtv_frontend_properties *fep = &fe->dtv_property_cache;
1232af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	struct af9005_fe_state *state = fe->demodulator_priv;
1233af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	int ret;
1234af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	u8 temp;
1235af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
1236af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	/* mode */
1237af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret =
1238af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	    af9005_read_register_bits(state->d, xd_g_reg_tpsd_const,
1239af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti				      reg_tpsd_const_pos, reg_tpsd_const_len,
1240af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti				      &temp);
1241af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
1242af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
1243a689e3657d7e82c2271008553c709fc79fb2e038Mauro Carvalho Chehab	deb_info("===== fe_get_frontend_legacy = =============\n");
1244af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	deb_info("CONSTELLATION ");
1245af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	switch (temp) {
1246af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	case 0:
1247f9e54512fd16379812bcff86d95d0a7d78028b20Mauro Carvalho Chehab		fep->modulation = QPSK;
1248af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		deb_info("QPSK\n");
1249af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		break;
1250af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	case 1:
1251f9e54512fd16379812bcff86d95d0a7d78028b20Mauro Carvalho Chehab		fep->modulation = QAM_16;
1252af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		deb_info("QAM_16\n");
1253af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		break;
1254af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	case 2:
1255f9e54512fd16379812bcff86d95d0a7d78028b20Mauro Carvalho Chehab		fep->modulation = QAM_64;
1256af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		deb_info("QAM_64\n");
1257af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		break;
1258af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	}
1259af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
1260af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	/* tps hierarchy and alpha value */
1261af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret =
1262af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	    af9005_read_register_bits(state->d, xd_g_reg_tpsd_hier,
1263af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti				      reg_tpsd_hier_pos, reg_tpsd_hier_len,
1264af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti				      &temp);
1265af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
1266af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
1267af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	deb_info("HIERARCHY ");
1268af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	switch (temp) {
1269af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	case 0:
1270f9e54512fd16379812bcff86d95d0a7d78028b20Mauro Carvalho Chehab		fep->hierarchy = HIERARCHY_NONE;
1271af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		deb_info("NONE\n");
1272af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		break;
1273af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	case 1:
1274f9e54512fd16379812bcff86d95d0a7d78028b20Mauro Carvalho Chehab		fep->hierarchy = HIERARCHY_1;
1275af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		deb_info("1\n");
1276af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		break;
1277af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	case 2:
1278f9e54512fd16379812bcff86d95d0a7d78028b20Mauro Carvalho Chehab		fep->hierarchy = HIERARCHY_2;
1279af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		deb_info("2\n");
1280af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		break;
1281af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	case 3:
1282f9e54512fd16379812bcff86d95d0a7d78028b20Mauro Carvalho Chehab		fep->hierarchy = HIERARCHY_4;
1283af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		deb_info("4\n");
1284af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		break;
1285af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	}
1286af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
1287af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	/*  high/low priority     */
1288af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret =
1289af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	    af9005_read_register_bits(state->d, xd_g_reg_dec_pri,
1290af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti				      reg_dec_pri_pos, reg_dec_pri_len, &temp);
1291af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
1292af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
1293af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	/* if temp is set = high priority */
1294af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	deb_info("PRIORITY %s\n", temp ? "high" : "low");
1295af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
1296af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	/* high coderate */
1297af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret =
1298af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	    af9005_read_register_bits(state->d, xd_g_reg_tpsd_hpcr,
1299af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti				      reg_tpsd_hpcr_pos, reg_tpsd_hpcr_len,
1300af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti				      &temp);
1301af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
1302af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
1303af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	deb_info("CODERATE HP ");
1304af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	switch (temp) {
1305af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	case 0:
1306f9e54512fd16379812bcff86d95d0a7d78028b20Mauro Carvalho Chehab		fep->code_rate_HP = FEC_1_2;
1307af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		deb_info("FEC_1_2\n");
1308af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		break;
1309af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	case 1:
1310f9e54512fd16379812bcff86d95d0a7d78028b20Mauro Carvalho Chehab		fep->code_rate_HP = FEC_2_3;
1311af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		deb_info("FEC_2_3\n");
1312af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		break;
1313af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	case 2:
1314f9e54512fd16379812bcff86d95d0a7d78028b20Mauro Carvalho Chehab		fep->code_rate_HP = FEC_3_4;
1315af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		deb_info("FEC_3_4\n");
1316af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		break;
1317af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	case 3:
1318f9e54512fd16379812bcff86d95d0a7d78028b20Mauro Carvalho Chehab		fep->code_rate_HP = FEC_5_6;
1319af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		deb_info("FEC_5_6\n");
1320af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		break;
1321af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	case 4:
1322f9e54512fd16379812bcff86d95d0a7d78028b20Mauro Carvalho Chehab		fep->code_rate_HP = FEC_7_8;
1323af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		deb_info("FEC_7_8\n");
1324af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		break;
1325af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	}
1326af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
1327af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	/* low coderate */
1328af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret =
1329af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	    af9005_read_register_bits(state->d, xd_g_reg_tpsd_lpcr,
1330af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti				      reg_tpsd_lpcr_pos, reg_tpsd_lpcr_len,
1331af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti				      &temp);
1332af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
1333af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
1334af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	deb_info("CODERATE LP ");
1335af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	switch (temp) {
1336af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	case 0:
1337f9e54512fd16379812bcff86d95d0a7d78028b20Mauro Carvalho Chehab		fep->code_rate_LP = FEC_1_2;
1338af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		deb_info("FEC_1_2\n");
1339af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		break;
1340af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	case 1:
1341f9e54512fd16379812bcff86d95d0a7d78028b20Mauro Carvalho Chehab		fep->code_rate_LP = FEC_2_3;
1342af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		deb_info("FEC_2_3\n");
1343af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		break;
1344af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	case 2:
1345f9e54512fd16379812bcff86d95d0a7d78028b20Mauro Carvalho Chehab		fep->code_rate_LP = FEC_3_4;
1346af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		deb_info("FEC_3_4\n");
1347af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		break;
1348af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	case 3:
1349f9e54512fd16379812bcff86d95d0a7d78028b20Mauro Carvalho Chehab		fep->code_rate_LP = FEC_5_6;
1350af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		deb_info("FEC_5_6\n");
1351af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		break;
1352af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	case 4:
1353f9e54512fd16379812bcff86d95d0a7d78028b20Mauro Carvalho Chehab		fep->code_rate_LP = FEC_7_8;
1354af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		deb_info("FEC_7_8\n");
1355af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		break;
1356af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	}
1357af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
1358af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	/* guard interval */
1359af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret =
1360af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	    af9005_read_register_bits(state->d, xd_g_reg_tpsd_gi,
1361af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti				      reg_tpsd_gi_pos, reg_tpsd_gi_len, &temp);
1362af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
1363af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
1364af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	deb_info("GUARD INTERVAL ");
1365af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	switch (temp) {
1366af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	case 0:
1367f9e54512fd16379812bcff86d95d0a7d78028b20Mauro Carvalho Chehab		fep->guard_interval = GUARD_INTERVAL_1_32;
1368af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		deb_info("1_32\n");
1369af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		break;
1370af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	case 1:
1371f9e54512fd16379812bcff86d95d0a7d78028b20Mauro Carvalho Chehab		fep->guard_interval = GUARD_INTERVAL_1_16;
1372af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		deb_info("1_16\n");
1373af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		break;
1374af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	case 2:
1375f9e54512fd16379812bcff86d95d0a7d78028b20Mauro Carvalho Chehab		fep->guard_interval = GUARD_INTERVAL_1_8;
1376af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		deb_info("1_8\n");
1377af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		break;
1378af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	case 3:
1379f9e54512fd16379812bcff86d95d0a7d78028b20Mauro Carvalho Chehab		fep->guard_interval = GUARD_INTERVAL_1_4;
1380af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		deb_info("1_4\n");
1381af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		break;
1382af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	}
1383af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
1384af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	/* fft */
1385af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret =
1386af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	    af9005_read_register_bits(state->d, xd_g_reg_tpsd_txmod,
1387af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti				      reg_tpsd_txmod_pos, reg_tpsd_txmod_len,
1388af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti				      &temp);
1389af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (ret)
1390af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		return ret;
1391af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	deb_info("TRANSMISSION MODE ");
1392af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	switch (temp) {
1393af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	case 0:
1394f9e54512fd16379812bcff86d95d0a7d78028b20Mauro Carvalho Chehab		fep->transmission_mode = TRANSMISSION_MODE_2K;
1395af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		deb_info("2K\n");
1396af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		break;
1397af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	case 1:
1398f9e54512fd16379812bcff86d95d0a7d78028b20Mauro Carvalho Chehab		fep->transmission_mode = TRANSMISSION_MODE_8K;
1399af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		deb_info("8K\n");
1400af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		break;
1401af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	}
1402af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
1403af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	/* bandwidth      */
1404af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	ret =
1405af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	    af9005_read_register_bits(state->d, xd_g_reg_bw, reg_bw_pos,
1406af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti				      reg_bw_len, &temp);
1407af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	deb_info("BANDWIDTH ");
1408af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	switch (temp) {
1409af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	case 0:
1410f9e54512fd16379812bcff86d95d0a7d78028b20Mauro Carvalho Chehab		fep->bandwidth_hz = 6000000;
1411af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		deb_info("6\n");
1412af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		break;
1413af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	case 1:
1414f9e54512fd16379812bcff86d95d0a7d78028b20Mauro Carvalho Chehab		fep->bandwidth_hz = 7000000;
1415af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		deb_info("7\n");
1416af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		break;
1417af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	case 2:
1418f9e54512fd16379812bcff86d95d0a7d78028b20Mauro Carvalho Chehab		fep->bandwidth_hz = 8000000;
1419af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		deb_info("8\n");
1420af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		break;
1421af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	}
1422af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	return 0;
1423af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti}
1424af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
1425af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivettistatic void af9005_fe_release(struct dvb_frontend *fe)
1426af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti{
1427af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	struct af9005_fe_state *state =
1428af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	    (struct af9005_fe_state *)fe->demodulator_priv;
1429af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	kfree(state);
1430af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti}
1431af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
1432af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivettistatic struct dvb_frontend_ops af9005_fe_ops;
1433af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
1434af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivettistruct dvb_frontend *af9005_fe_attach(struct dvb_usb_device *d)
1435af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti{
1436af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	struct af9005_fe_state *state = NULL;
1437af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
1438af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	/* allocate memory for the internal state */
1439af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	state = kzalloc(sizeof(struct af9005_fe_state), GFP_KERNEL);
1440af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	if (state == NULL)
1441af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		goto error;
1442af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
1443af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	deb_info("attaching frontend af9005\n");
1444af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
1445af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	state->d = d;
1446af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	state->opened = 0;
1447af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
1448af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	memcpy(&state->frontend.ops, &af9005_fe_ops,
1449af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	       sizeof(struct dvb_frontend_ops));
1450af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	state->frontend.demodulator_priv = state;
1451af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
1452af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	return &state->frontend;
1453af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti      error:
1454af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	return NULL;
1455af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti}
1456af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
1457af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivettistatic struct dvb_frontend_ops af9005_fe_ops = {
1458f9e54512fd16379812bcff86d95d0a7d78028b20Mauro Carvalho Chehab	.delsys = { SYS_DVBT },
1459af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	.info = {
1460af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		 .name = "AF9005 USB DVB-T",
1461af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		 .frequency_min = 44250000,
1462af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		 .frequency_max = 867250000,
1463af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		 .frequency_stepsize = 250000,
1464af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		 .caps = FE_CAN_INVERSION_AUTO |
1465af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		 FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
1466af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
1467af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		 FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 |
1468af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		 FE_CAN_QAM_AUTO | FE_CAN_TRANSMISSION_MODE_AUTO |
1469af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		 FE_CAN_GUARD_INTERVAL_AUTO | FE_CAN_RECOVER |
1470af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		 FE_CAN_HIERARCHY_AUTO,
1471af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti		 },
1472af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
1473af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	.release = af9005_fe_release,
1474af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
1475af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	.init = af9005_fe_init,
1476af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	.sleep = af9005_fe_sleep,
1477af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	.ts_bus_ctrl = af9005_ts_bus_ctrl,
1478af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
1479f9e54512fd16379812bcff86d95d0a7d78028b20Mauro Carvalho Chehab	.set_frontend = af9005_fe_set_frontend,
1480f9e54512fd16379812bcff86d95d0a7d78028b20Mauro Carvalho Chehab	.get_frontend = af9005_fe_get_frontend,
1481af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti
1482af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	.read_status = af9005_fe_read_status,
1483af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	.read_ber = af9005_fe_read_ber,
1484af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	.read_signal_strength = af9005_fe_read_signal_strength,
1485af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	.read_snr = af9005_fe_read_snr,
1486af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti	.read_ucblocks = af9005_fe_read_unc_blocks,
1487af4e067e1dcf926d9523dff11e46c45fd9fa9da2Luca Olivetti};
1488