1/* SPDX-License-Identifier: GPL-2.0-or-later */
2/*
3	STB0899 Multistandard Frontend driver
4	Copyright (C) Manu Abraham (abraham.manu@gmail.com)
5
6	Copyright (C) ST Microelectronics
7
8*/
9
10#ifndef __STB0899_PRIV_H
11#define __STB0899_PRIV_H
12
13#include <media/dvb_frontend.h>
14#include "stb0899_drv.h"
15
16#define FE_ERROR				0
17#define FE_NOTICE				1
18#define FE_INFO					2
19#define FE_DEBUG				3
20#define FE_DEBUGREG				4
21
22#define dprintk(x, y, z, format, arg...) do {						\
23	if (z) {									\
24		if	((*x > FE_ERROR) && (*x > y))					\
25			printk(KERN_ERR "%s: " format "\n", __func__ , ##arg);		\
26		else if	((*x > FE_NOTICE) && (*x > y))					\
27			printk(KERN_NOTICE "%s: " format "\n", __func__ , ##arg);	\
28		else if ((*x > FE_INFO) && (*x > y))					\
29			printk(KERN_INFO "%s: " format "\n", __func__ , ##arg);		\
30		else if ((*x > FE_DEBUG) && (*x > y))					\
31			printk(KERN_DEBUG "%s: " format "\n", __func__ , ##arg);	\
32	} else {									\
33		if (*x > y)								\
34			printk(format, ##arg);						\
35	}										\
36} while(0)
37
38#define INRANGE(val, x, y)			(((x <= val) && (val <= y)) ||		\
39						 ((y <= val) && (val <= x)) ? 1 : 0)
40
41#define BYTE0					0
42#define BYTE1					8
43#define BYTE2					16
44#define BYTE3					24
45
46#define GETBYTE(x, y)				(((x) >> (y)) & 0xff)
47#define MAKEWORD32(a, b, c, d)			(((a) << 24) | ((b) << 16) | ((c) << 8) | (d))
48#define MAKEWORD16(a, b)			(((a) << 8) | (b))
49
50#define LSB(x)					((x & 0xff))
51#define MSB(y)					((y >> 8) & 0xff)
52
53
54#define STB0899_GETFIELD(bitf, val)		((val >> STB0899_OFFST_##bitf) & ((1 << STB0899_WIDTH_##bitf) - 1))
55
56
57#define STB0899_SETFIELD(mask, val, width, offset)      (mask & (~(((1 << width) - 1) <<	\
58							 offset))) | ((val &			\
59							 ((1 << width) - 1)) << offset)
60
61#define STB0899_SETFIELD_VAL(bitf, mask, val)	(mask = (mask & (~(((1 << STB0899_WIDTH_##bitf) - 1) <<\
62							 STB0899_OFFST_##bitf))) | \
63							 (val << STB0899_OFFST_##bitf))
64
65
66enum stb0899_status {
67	NOAGC1	= 0,
68	AGC1OK,
69	NOTIMING,
70	ANALOGCARRIER,
71	TIMINGOK,
72	NOAGC2,
73	AGC2OK,
74	NOCARRIER,
75	CARRIEROK,
76	NODATA,
77	FALSELOCK,
78	DATAOK,
79	OUTOFRANGE,
80	RANGEOK,
81	DVBS2_DEMOD_LOCK,
82	DVBS2_DEMOD_NOLOCK,
83	DVBS2_FEC_LOCK,
84	DVBS2_FEC_NOLOCK
85};
86
87enum stb0899_modcod {
88	STB0899_DUMMY_PLF,
89	STB0899_QPSK_14,
90	STB0899_QPSK_13,
91	STB0899_QPSK_25,
92	STB0899_QPSK_12,
93	STB0899_QPSK_35,
94	STB0899_QPSK_23,
95	STB0899_QPSK_34,
96	STB0899_QPSK_45,
97	STB0899_QPSK_56,
98	STB0899_QPSK_89,
99	STB0899_QPSK_910,
100	STB0899_8PSK_35,
101	STB0899_8PSK_23,
102	STB0899_8PSK_34,
103	STB0899_8PSK_56,
104	STB0899_8PSK_89,
105	STB0899_8PSK_910,
106	STB0899_16APSK_23,
107	STB0899_16APSK_34,
108	STB0899_16APSK_45,
109	STB0899_16APSK_56,
110	STB0899_16APSK_89,
111	STB0899_16APSK_910,
112	STB0899_32APSK_34,
113	STB0899_32APSK_45,
114	STB0899_32APSK_56,
115	STB0899_32APSK_89,
116	STB0899_32APSK_910
117};
118
119enum stb0899_frame {
120	STB0899_LONG_FRAME,
121	STB0899_SHORT_FRAME
122};
123
124enum stb0899_alpha {
125	RRC_20,
126	RRC_25,
127	RRC_35
128};
129
130struct stb0899_tab {
131	s32 real;
132	s32 read;
133};
134
135enum stb0899_fec {
136	STB0899_FEC_1_2			= 13,
137	STB0899_FEC_2_3			= 18,
138	STB0899_FEC_3_4			= 21,
139	STB0899_FEC_5_6			= 24,
140	STB0899_FEC_6_7			= 25,
141	STB0899_FEC_7_8			= 26
142};
143
144struct stb0899_params {
145	u32	freq;					/* Frequency	*/
146	u32	srate;					/* Symbol rate	*/
147	enum fe_code_rate fecrate;
148};
149
150struct stb0899_internal {
151	u32			master_clk;
152	u32			freq;			/* Demod internal Frequency		*/
153	u32			srate;			/* Demod internal Symbol rate		*/
154	enum stb0899_fec	fecrate;		/* Demod internal FEC rate		*/
155	s32			srch_range;		/* Demod internal Search Range		*/
156	s32			sub_range;		/* Demod current sub range (Hz)		*/
157	s32			tuner_step;		/* Tuner step (Hz)			*/
158	s32			tuner_offst;		/* Relative offset to carrier (Hz)	*/
159	u32			tuner_bw;		/* Current bandwidth of the tuner (Hz)	*/
160
161	s32			mclk;			/* Masterclock Divider factor (binary)	*/
162	s32			rolloff;		/* Current RollOff of the filter (x100)	*/
163
164	s16			derot_freq;		/* Current derotator frequency (Hz)	*/
165	s16			derot_percent;
166
167	s16			direction;		/* Current derotator search direction	*/
168	s16			derot_step;		/* Derotator step (binary value)	*/
169	s16			t_derot;		/* Derotator time constant (ms)		*/
170	s16			t_data;			/* Data recovery time constant (ms)	*/
171	s16			sub_dir;		/* Direction of the next sub range	*/
172
173	s16			t_agc1;			/* Agc1 time constant (ms)		*/
174	s16			t_agc2;			/* Agc2 time constant (ms)		*/
175
176	u32			lock;			/* Demod internal lock state		*/
177	enum stb0899_status	status;			/* Demod internal status		*/
178
179	/* DVB-S2 */
180	s32			agc_gain;		/* RF AGC Gain				*/
181	s32			center_freq;		/* Nominal carrier frequency		*/
182	s32			av_frame_coarse;	/* Coarse carrier freq search frames	*/
183	s32			av_frame_fine;		/* Fine carrier freq search frames	*/
184
185	s16			step_size;		/* Carrier frequency search step size	*/
186
187	enum stb0899_alpha	rrc_alpha;
188	enum stb0899_inversion	inversion;
189	enum stb0899_modcod	modcod;
190	u8			pilots;			/* Pilots found				*/
191
192	enum stb0899_frame	frame_length;
193	u8			v_status;		/* VSTATUS				*/
194	u8			err_ctrl;		/* ERRCTRLn				*/
195};
196
197struct stb0899_state {
198	struct i2c_adapter		*i2c;
199	struct stb0899_config		*config;
200	struct dvb_frontend		frontend;
201
202	u32				*verbose;	/* Cached module verbosity level	*/
203
204	struct stb0899_internal		internal;	/* Device internal parameters		*/
205
206	/*	cached params from API	*/
207	enum fe_delivery_system		delsys;
208	struct stb0899_params		params;
209
210	u32				rx_freq;	/* DiSEqC 2.0 receiver freq		*/
211	struct mutex			search_lock;
212};
213/* stb0899.c		*/
214extern int stb0899_read_reg(struct stb0899_state *state,
215			    unsigned int reg);
216
217extern u32 _stb0899_read_s2reg(struct stb0899_state *state,
218			       u32 stb0899_i2cdev,
219			       u32 stb0899_base_addr,
220			       u16 stb0899_reg_offset);
221
222extern int stb0899_read_regs(struct stb0899_state *state,
223			     unsigned int reg, u8 *buf,
224			     u32 count);
225
226extern int stb0899_write_regs(struct stb0899_state *state,
227			      unsigned int reg, u8 *data,
228			      u32 count);
229
230extern int stb0899_write_reg(struct stb0899_state *state,
231			     unsigned int reg,
232			     u8 data);
233
234extern int stb0899_write_s2reg(struct stb0899_state *state,
235			       u32 stb0899_i2cdev,
236			       u32 stb0899_base_addr,
237			       u16 stb0899_reg_offset,
238			       u32 stb0899_data);
239
240extern int stb0899_i2c_gate_ctrl(struct dvb_frontend *fe, int enable);
241
242
243#define STB0899_READ_S2REG(DEVICE, REG)		(_stb0899_read_s2reg(state, DEVICE, STB0899_BASE_##REG, STB0899_OFF0_##REG))
244//#define STB0899_WRITE_S2REG(DEVICE, REG, DATA)	(_stb0899_write_s2reg(state, DEVICE, STB0899_BASE_##REG, STB0899_OFF0_##REG, DATA))
245
246/* stb0899_algo.c	*/
247extern enum stb0899_status stb0899_dvbs_algo(struct stb0899_state *state);
248extern enum stb0899_status stb0899_dvbs2_algo(struct stb0899_state *state);
249extern long stb0899_carr_width(struct stb0899_state *state);
250
251#endif //__STB0899_PRIV_H
252