1 /*
2  * stv0900_priv.h
3  *
4  * Driver for ST STV0900 satellite demodulator IC.
5  *
6  * Copyright (C) ST Microelectronics.
7  * Copyright (C) 2009 NetUP Inc.
8  * Copyright (C) 2009 Igor M. Liplianin <liplianin@netup.ru>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *
19  * GNU General Public License for more details.
20  */
21 
22 #ifndef STV0900_PRIV_H
23 #define STV0900_PRIV_H
24 
25 #include <linux/i2c.h>
26 
27 #define ABS(X) ((X) < 0 ? (-1 * (X)) : (X))
28 #define INRANGE(X, Y, Z) ((((X) <= (Y)) && ((Y) <= (Z))) \
29 		|| (((Z) <= (Y)) && ((Y) <= (X))) ? 1 : 0)
30 
31 #ifndef MAKEWORD
32 #define MAKEWORD(X, Y) (((X) << 8) + (Y))
33 #endif
34 
35 #define LSB(X) (((X) & 0xFF))
36 #define MSB(Y) (((Y) >> 8) & 0xFF)
37 
38 #ifndef TRUE
39 #define TRUE (1 == 1)
40 #endif
41 #ifndef FALSE
42 #define FALSE (!TRUE)
43 #endif
44 
45 #define dprintk(args...) \
46 	do { \
47 		if (stvdebug) \
48 			printk(KERN_DEBUG args); \
49 	} while (0)
50 
51 #define STV0900_MAXLOOKUPSIZE 500
52 #define STV0900_BLIND_SEARCH_AGC2_TH 700
53 #define STV0900_BLIND_SEARCH_AGC2_TH_CUT30 1400
54 #define IQPOWER_THRESHOLD  30
55 
56 /* One point of the lookup table */
57 struct stv000_lookpoint {
58 	s32 realval;/* real value */
59 	s32 regval;/* binary value */
60 };
61 
62 /* Lookup table definition */
63 struct stv0900_table{
64 	s32 size;/* Size of the lookup table */
65 	struct stv000_lookpoint table[STV0900_MAXLOOKUPSIZE];/* Lookup table */
66 };
67 
68 enum fe_stv0900_error {
69 	STV0900_NO_ERROR = 0,
70 	STV0900_INVALID_HANDLE,
71 	STV0900_BAD_PARAMETER,
72 	STV0900_I2C_ERROR,
73 	STV0900_SEARCH_FAILED,
74 };
75 
76 enum fe_stv0900_clock_type {
77 	STV0900_USE_REGISTERS_DEFAULT,
78 	STV0900_SERIAL_PUNCT_CLOCK,/*Serial punctured clock */
79 	STV0900_SERIAL_CONT_CLOCK,/*Serial continues clock */
80 	STV0900_PARALLEL_PUNCT_CLOCK,/*Parallel punctured clock */
81 	STV0900_DVBCI_CLOCK/*Parallel continues clock : DVBCI */
82 };
83 
84 enum fe_stv0900_search_state {
85 	STV0900_SEARCH = 0,
86 	STV0900_PLH_DETECTED,
87 	STV0900_DVBS2_FOUND,
88 	STV0900_DVBS_FOUND
89 
90 };
91 
92 enum fe_stv0900_ldpc_state {
93 	STV0900_PATH1_OFF_PATH2_OFF = 0,
94 	STV0900_PATH1_ON_PATH2_OFF = 1,
95 	STV0900_PATH1_OFF_PATH2_ON = 2,
96 	STV0900_PATH1_ON_PATH2_ON = 3
97 };
98 
99 enum fe_stv0900_signal_type {
100 	STV0900_NOAGC1 = 0,
101 	STV0900_AGC1OK,
102 	STV0900_NOTIMING,
103 	STV0900_ANALOGCARRIER,
104 	STV0900_TIMINGOK,
105 	STV0900_NOAGC2,
106 	STV0900_AGC2OK,
107 	STV0900_NOCARRIER,
108 	STV0900_CARRIEROK,
109 	STV0900_NODATA,
110 	STV0900_DATAOK,
111 	STV0900_OUTOFRANGE,
112 	STV0900_RANGEOK
113 };
114 
115 enum fe_stv0900_demod_num {
116 	STV0900_DEMOD_1,
117 	STV0900_DEMOD_2
118 };
119 
120 enum fe_stv0900_tracking_standard {
121 	STV0900_DVBS1_STANDARD,/* Found Standard*/
122 	STV0900_DVBS2_STANDARD,
123 	STV0900_DSS_STANDARD,
124 	STV0900_TURBOCODE_STANDARD,
125 	STV0900_UNKNOWN_STANDARD
126 };
127 
128 enum fe_stv0900_search_standard {
129 	STV0900_AUTO_SEARCH,
130 	STV0900_SEARCH_DVBS1,/* Search Standard*/
131 	STV0900_SEARCH_DVBS2,
132 	STV0900_SEARCH_DSS,
133 	STV0900_SEARCH_TURBOCODE
134 };
135 
136 enum fe_stv0900_search_algo {
137 	STV0900_BLIND_SEARCH,/* offset freq and SR are Unknown */
138 	STV0900_COLD_START,/* only the SR is known */
139 	STV0900_WARM_START/* offset freq and SR are known */
140 };
141 
142 enum fe_stv0900_modulation {
143 	STV0900_QPSK,
144 	STV0900_8PSK,
145 	STV0900_16APSK,
146 	STV0900_32APSK,
147 	STV0900_UNKNOWN
148 };
149 
150 enum fe_stv0900_modcode {
151 	STV0900_DUMMY_PLF,
152 	STV0900_QPSK_14,
153 	STV0900_QPSK_13,
154 	STV0900_QPSK_25,
155 	STV0900_QPSK_12,
156 	STV0900_QPSK_35,
157 	STV0900_QPSK_23,
158 	STV0900_QPSK_34,
159 	STV0900_QPSK_45,
160 	STV0900_QPSK_56,
161 	STV0900_QPSK_89,
162 	STV0900_QPSK_910,
163 	STV0900_8PSK_35,
164 	STV0900_8PSK_23,
165 	STV0900_8PSK_34,
166 	STV0900_8PSK_56,
167 	STV0900_8PSK_89,
168 	STV0900_8PSK_910,
169 	STV0900_16APSK_23,
170 	STV0900_16APSK_34,
171 	STV0900_16APSK_45,
172 	STV0900_16APSK_56,
173 	STV0900_16APSK_89,
174 	STV0900_16APSK_910,
175 	STV0900_32APSK_34,
176 	STV0900_32APSK_45,
177 	STV0900_32APSK_56,
178 	STV0900_32APSK_89,
179 	STV0900_32APSK_910,
180 	STV0900_MODCODE_UNKNOWN
181 };
182 
183 enum fe_stv0900_fec {/*DVBS1, DSS and turbo code puncture rate*/
184 	STV0900_FEC_1_2 = 0,
185 	STV0900_FEC_2_3,
186 	STV0900_FEC_3_4,
187 	STV0900_FEC_4_5,/*for turbo code only*/
188 	STV0900_FEC_5_6,
189 	STV0900_FEC_6_7,/*for DSS only */
190 	STV0900_FEC_7_8,
191 	STV0900_FEC_8_9,/*for turbo code only*/
192 	STV0900_FEC_UNKNOWN
193 };
194 
195 enum fe_stv0900_frame_length {
196 	STV0900_LONG_FRAME,
197 	STV0900_SHORT_FRAME
198 };
199 
200 enum fe_stv0900_pilot {
201 	STV0900_PILOTS_OFF,
202 	STV0900_PILOTS_ON
203 };
204 
205 enum fe_stv0900_rolloff {
206 	STV0900_35,
207 	STV0900_25,
208 	STV0900_20
209 };
210 
211 enum fe_stv0900_search_iq {
212 	STV0900_IQ_AUTO,
213 	STV0900_IQ_AUTO_NORMAL_FIRST,
214 	STV0900_IQ_FORCE_NORMAL,
215 	STV0900_IQ_FORCE_SWAPPED
216 };
217 
218 enum stv0900_iq_inversion {
219 	STV0900_IQ_NORMAL,
220 	STV0900_IQ_SWAPPED
221 };
222 
223 enum fe_stv0900_diseqc_mode {
224 	STV0900_22KHZ_Continues = 0,
225 	STV0900_DISEQC_2_3_PWM = 2,
226 	STV0900_DISEQC_3_3_PWM = 3,
227 	STV0900_DISEQC_2_3_ENVELOP = 4,
228 	STV0900_DISEQC_3_3_ENVELOP = 5
229 };
230 
231 enum fe_stv0900_demod_mode {
232 	STV0900_SINGLE = 0,
233 	STV0900_DUAL
234 };
235 
236 struct stv0900_init_params{
237 	u32	dmd_ref_clk;/* Reference,Input clock for the demod in Hz */
238 
239 	/* Demodulator Type (single demod or dual demod) */
240 	enum fe_stv0900_demod_mode	demod_mode;
241 	enum fe_stv0900_rolloff		rolloff;
242 	enum fe_stv0900_clock_type	path1_ts_clock;
243 
244 	u8	tun1_maddress;
245 	int	tuner1_adc;
246 	int 	tuner1_type;
247 
248 	/* IQ from the tuner1 to the demod */
249 	enum stv0900_iq_inversion	tun1_iq_inv;
250 	enum fe_stv0900_clock_type	path2_ts_clock;
251 
252 	u8	tun2_maddress;
253 	int	tuner2_adc;
254 	int	tuner2_type;
255 
256 	/* IQ from the tuner2 to the demod */
257 	enum stv0900_iq_inversion	tun2_iq_inv;
258 	struct stv0900_reg		*ts_config;
259 };
260 
261 struct stv0900_search_params {
262 	enum fe_stv0900_demod_num	path;/* Path Used demod1 or 2 */
263 
264 	u32	frequency;/* Transponder frequency (in KHz) */
265 	u32	symbol_rate;/* Transponder symbol rate  (in bds)*/
266 	u32	search_range;/* Range of the search (in Hz) */
267 
268 	enum fe_stv0900_search_standard	standard;
269 	enum fe_stv0900_modulation	modulation;
270 	enum fe_stv0900_fec		fec;
271 	enum fe_stv0900_modcode		modcode;
272 	enum fe_stv0900_search_iq	iq_inversion;
273 	enum fe_stv0900_search_algo	search_algo;
274 
275 };
276 
277 struct stv0900_signal_info {
278 	int	locked;/* Transponder locked */
279 	u32	frequency;/* Transponder frequency (in KHz) */
280 	u32	symbol_rate;/* Transponder symbol rate  (in Mbds) */
281 
282 	enum fe_stv0900_tracking_standard	standard;
283 	enum fe_stv0900_fec			fec;
284 	enum fe_stv0900_modcode			modcode;
285 	enum fe_stv0900_modulation		modulation;
286 	enum fe_stv0900_pilot			pilot;
287 	enum fe_stv0900_frame_length		frame_len;
288 	enum stv0900_iq_inversion		spectrum;
289 	enum fe_stv0900_rolloff			rolloff;
290 
291 	s32 Power;/* Power of the RF signal (dBm) */
292 	s32 C_N;/* Carrier to noise ratio (dB x10)*/
293 	u32 BER;/* Bit error rate (x10^7) */
294 
295 };
296 
297 struct stv0900_internal{
298 	s32	quartz;
299 	s32	mclk;
300 	/* manual RollOff for DVBS1/DSS only */
301 	enum fe_stv0900_rolloff		rolloff;
302 	/* Demodulator use for single demod or for dual demod) */
303 	enum fe_stv0900_demod_mode	demod_mode;
304 
305 	/*Demods */
306 	s32	freq[2];
307 	s32	bw[2];
308 	s32	symbol_rate[2];
309 	s32	srch_range[2];
310 	/* for software/auto tuner */
311 	int	tuner_type[2];
312 
313 	/* algorithm for search Blind, Cold or Warm*/
314 	enum fe_stv0900_search_algo	srch_algo[2];
315 	/* search standard: Auto, DVBS1/DSS only or DVBS2 only*/
316 	enum fe_stv0900_search_standard	srch_standard[2];
317 	/* inversion search : auto, auto norma first, normal or inverted */
318 	enum fe_stv0900_search_iq	srch_iq_inv[2];
319 	enum fe_stv0900_modcode		modcode[2];
320 	enum fe_stv0900_modulation	modulation[2];
321 	enum fe_stv0900_fec		fec[2];
322 
323 	struct stv0900_signal_info	result[2];
324 	enum fe_stv0900_error		err[2];
325 
326 
327 	struct i2c_adapter	*i2c_adap;
328 	u8			i2c_addr;
329 	u8			clkmode;/* 0 for CLKI, 2 for XTALI */
330 	u8			chip_id;
331 	struct stv0900_reg	*ts_config;
332 	enum fe_stv0900_error	errs;
333 	int dmds_used;
334 };
335 
336 /* state for each demod */
337 struct stv0900_state {
338 	/* pointer for internal params, one for each pair of demods */
339 	struct stv0900_internal		*internal;
340 	struct i2c_adapter		*i2c_adap;
341 	const struct stv0900_config	*config;
342 	struct dvb_frontend		frontend;
343 	int demod;
344 };
345 
346 extern int stvdebug;
347 
348 extern s32 ge2comp(s32 a, s32 width);
349 
350 extern void stv0900_write_reg(struct stv0900_internal *i_params,
351 				u16 reg_addr, u8 reg_data);
352 
353 extern u8 stv0900_read_reg(struct stv0900_internal *i_params,
354 				u16 reg_addr);
355 
356 extern void stv0900_write_bits(struct stv0900_internal *i_params,
357 				u32 label, u8 val);
358 
359 extern u8 stv0900_get_bits(struct stv0900_internal *i_params,
360 				u32 label);
361 
362 extern int stv0900_get_demod_lock(struct stv0900_internal *i_params,
363 				enum fe_stv0900_demod_num demod, s32 time_out);
364 extern int stv0900_check_signal_presence(struct stv0900_internal *i_params,
365 				enum fe_stv0900_demod_num demod);
366 
367 extern enum fe_stv0900_signal_type stv0900_algo(struct dvb_frontend *fe);
368 
369 extern void stv0900_set_tuner(struct dvb_frontend *fe, u32 frequency,
370 				u32 bandwidth);
371 extern void stv0900_set_bandwidth(struct dvb_frontend *fe, u32 bandwidth);
372 
373 extern void stv0900_start_search(struct stv0900_internal *i_params,
374 				enum fe_stv0900_demod_num demod);
375 
376 extern u8 stv0900_get_optim_carr_loop(s32 srate,
377 				enum fe_stv0900_modcode modcode,
378 				s32 pilot, u8 chip_id);
379 
380 extern u8 stv0900_get_optim_short_carr_loop(s32 srate,
381 				enum fe_stv0900_modulation modulation,
382 				u8 chip_id);
383 
384 extern void stv0900_stop_all_s2_modcod(struct stv0900_internal *i_params,
385 				enum fe_stv0900_demod_num demod);
386 
387 extern void stv0900_activate_s2_modcod(struct stv0900_internal *i_params,
388 				enum fe_stv0900_demod_num demod);
389 
390 extern void stv0900_activate_s2_modcod_single(struct stv0900_internal *i_params,
391 				enum fe_stv0900_demod_num demod);
392 
393 extern enum
394 fe_stv0900_tracking_standard stv0900_get_standard(struct dvb_frontend *fe,
395 				enum fe_stv0900_demod_num demod);
396 
397 extern u32
398 stv0900_get_freq_auto(struct stv0900_internal *intp, int demod);
399 
400 extern void
401 stv0900_set_tuner_auto(struct stv0900_internal *intp, u32 Frequency,
402 						u32 Bandwidth, int demod);
403 
404 #endif
405