xref: /OK3568_Linux_fs/kernel/drivers/media/dvb-frontends/cx24123.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  *   Conexant cx24123/cx24109 - DVB QPSK Satellite demod/tuner driver
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  *   Copyright (C) 2005 Steven Toth <stoth@linuxtv.org>
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  *   Support for KWorld DVB-S 100 by Vadim Catana <skystar@moldova.cc>
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  *   Support for CX24123/CX24113-NIM by Patrick Boettcher <pb@linuxtv.org>
10*4882a593Smuzhiyun  */
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun #include <linux/slab.h>
13*4882a593Smuzhiyun #include <linux/kernel.h>
14*4882a593Smuzhiyun #include <linux/module.h>
15*4882a593Smuzhiyun #include <linux/init.h>
16*4882a593Smuzhiyun #include <asm/div64.h>
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun #include <media/dvb_frontend.h>
19*4882a593Smuzhiyun #include "cx24123.h"
20*4882a593Smuzhiyun 
21*4882a593Smuzhiyun #define XTAL 10111000
22*4882a593Smuzhiyun 
23*4882a593Smuzhiyun static int force_band;
24*4882a593Smuzhiyun module_param(force_band, int, 0644);
25*4882a593Smuzhiyun MODULE_PARM_DESC(force_band, "Force a specific band select "\
26*4882a593Smuzhiyun 	"(1-9, default:off).");
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun static int debug;
29*4882a593Smuzhiyun module_param(debug, int, 0644);
30*4882a593Smuzhiyun MODULE_PARM_DESC(debug, "Activates frontend debugging (default:0)");
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun #define info(args...) do { printk(KERN_INFO "CX24123: " args); } while (0)
33*4882a593Smuzhiyun #define err(args...)  do { printk(KERN_ERR  "CX24123: " args); } while (0)
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun #define dprintk(args...) \
36*4882a593Smuzhiyun 	do { \
37*4882a593Smuzhiyun 		if (debug) { \
38*4882a593Smuzhiyun 			printk(KERN_DEBUG "CX24123: %s: ", __func__); \
39*4882a593Smuzhiyun 			printk(args); \
40*4882a593Smuzhiyun 		} \
41*4882a593Smuzhiyun 	} while (0)
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun struct cx24123_state {
44*4882a593Smuzhiyun 	struct i2c_adapter *i2c;
45*4882a593Smuzhiyun 	const struct cx24123_config *config;
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun 	struct dvb_frontend frontend;
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun 	/* Some PLL specifics for tuning */
50*4882a593Smuzhiyun 	u32 VCAarg;
51*4882a593Smuzhiyun 	u32 VGAarg;
52*4882a593Smuzhiyun 	u32 bandselectarg;
53*4882a593Smuzhiyun 	u32 pllarg;
54*4882a593Smuzhiyun 	u32 FILTune;
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun 	struct i2c_adapter tuner_i2c_adapter;
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun 	u8 demod_rev;
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun 	/* The Demod/Tuner can't easily provide these, we cache them */
61*4882a593Smuzhiyun 	u32 currentfreq;
62*4882a593Smuzhiyun 	u32 currentsymbolrate;
63*4882a593Smuzhiyun };
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun /* Various tuner defaults need to be established for a given symbol rate Sps */
66*4882a593Smuzhiyun static struct cx24123_AGC_val {
67*4882a593Smuzhiyun 	u32 symbolrate_low;
68*4882a593Smuzhiyun 	u32 symbolrate_high;
69*4882a593Smuzhiyun 	u32 VCAprogdata;
70*4882a593Smuzhiyun 	u32 VGAprogdata;
71*4882a593Smuzhiyun 	u32 FILTune;
72*4882a593Smuzhiyun } cx24123_AGC_vals[] =
73*4882a593Smuzhiyun {
74*4882a593Smuzhiyun 	{
75*4882a593Smuzhiyun 		.symbolrate_low		= 1000000,
76*4882a593Smuzhiyun 		.symbolrate_high	= 4999999,
77*4882a593Smuzhiyun 		/* the specs recommend other values for VGA offsets,
78*4882a593Smuzhiyun 		   but tests show they are wrong */
79*4882a593Smuzhiyun 		.VGAprogdata		= (1 << 19) | (0x180 << 9) | 0x1e0,
80*4882a593Smuzhiyun 		.VCAprogdata		= (2 << 19) | (0x07 << 9) | 0x07,
81*4882a593Smuzhiyun 		.FILTune		= 0x27f /* 0.41 V */
82*4882a593Smuzhiyun 	},
83*4882a593Smuzhiyun 	{
84*4882a593Smuzhiyun 		.symbolrate_low		=  5000000,
85*4882a593Smuzhiyun 		.symbolrate_high	= 14999999,
86*4882a593Smuzhiyun 		.VGAprogdata		= (1 << 19) | (0x180 << 9) | 0x1e0,
87*4882a593Smuzhiyun 		.VCAprogdata		= (2 << 19) | (0x07 << 9) | 0x1f,
88*4882a593Smuzhiyun 		.FILTune		= 0x317 /* 0.90 V */
89*4882a593Smuzhiyun 	},
90*4882a593Smuzhiyun 	{
91*4882a593Smuzhiyun 		.symbolrate_low		= 15000000,
92*4882a593Smuzhiyun 		.symbolrate_high	= 45000000,
93*4882a593Smuzhiyun 		.VGAprogdata		= (1 << 19) | (0x100 << 9) | 0x180,
94*4882a593Smuzhiyun 		.VCAprogdata		= (2 << 19) | (0x07 << 9) | 0x3f,
95*4882a593Smuzhiyun 		.FILTune		= 0x145 /* 2.70 V */
96*4882a593Smuzhiyun 	},
97*4882a593Smuzhiyun };
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun /*
100*4882a593Smuzhiyun  * Various tuner defaults need to be established for a given frequency kHz.
101*4882a593Smuzhiyun  * fixme: The bounds on the bands do not match the doc in real life.
102*4882a593Smuzhiyun  * fixme: Some of them have been moved, other might need adjustment.
103*4882a593Smuzhiyun  */
104*4882a593Smuzhiyun static struct cx24123_bandselect_val {
105*4882a593Smuzhiyun 	u32 freq_low;
106*4882a593Smuzhiyun 	u32 freq_high;
107*4882a593Smuzhiyun 	u32 VCOdivider;
108*4882a593Smuzhiyun 	u32 progdata;
109*4882a593Smuzhiyun } cx24123_bandselect_vals[] =
110*4882a593Smuzhiyun {
111*4882a593Smuzhiyun 	/* band 1 */
112*4882a593Smuzhiyun 	{
113*4882a593Smuzhiyun 		.freq_low	= 950000,
114*4882a593Smuzhiyun 		.freq_high	= 1074999,
115*4882a593Smuzhiyun 		.VCOdivider	= 4,
116*4882a593Smuzhiyun 		.progdata	= (0 << 19) | (0 << 9) | 0x40,
117*4882a593Smuzhiyun 	},
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun 	/* band 2 */
120*4882a593Smuzhiyun 	{
121*4882a593Smuzhiyun 		.freq_low	= 1075000,
122*4882a593Smuzhiyun 		.freq_high	= 1177999,
123*4882a593Smuzhiyun 		.VCOdivider	= 4,
124*4882a593Smuzhiyun 		.progdata	= (0 << 19) | (0 << 9) | 0x80,
125*4882a593Smuzhiyun 	},
126*4882a593Smuzhiyun 
127*4882a593Smuzhiyun 	/* band 3 */
128*4882a593Smuzhiyun 	{
129*4882a593Smuzhiyun 		.freq_low	= 1178000,
130*4882a593Smuzhiyun 		.freq_high	= 1295999,
131*4882a593Smuzhiyun 		.VCOdivider	= 2,
132*4882a593Smuzhiyun 		.progdata	= (0 << 19) | (1 << 9) | 0x01,
133*4882a593Smuzhiyun 	},
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun 	/* band 4 */
136*4882a593Smuzhiyun 	{
137*4882a593Smuzhiyun 		.freq_low	= 1296000,
138*4882a593Smuzhiyun 		.freq_high	= 1431999,
139*4882a593Smuzhiyun 		.VCOdivider	= 2,
140*4882a593Smuzhiyun 		.progdata	= (0 << 19) | (1 << 9) | 0x02,
141*4882a593Smuzhiyun 	},
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun 	/* band 5 */
144*4882a593Smuzhiyun 	{
145*4882a593Smuzhiyun 		.freq_low	= 1432000,
146*4882a593Smuzhiyun 		.freq_high	= 1575999,
147*4882a593Smuzhiyun 		.VCOdivider	= 2,
148*4882a593Smuzhiyun 		.progdata	= (0 << 19) | (1 << 9) | 0x04,
149*4882a593Smuzhiyun 	},
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun 	/* band 6 */
152*4882a593Smuzhiyun 	{
153*4882a593Smuzhiyun 		.freq_low	= 1576000,
154*4882a593Smuzhiyun 		.freq_high	= 1717999,
155*4882a593Smuzhiyun 		.VCOdivider	= 2,
156*4882a593Smuzhiyun 		.progdata	= (0 << 19) | (1 << 9) | 0x08,
157*4882a593Smuzhiyun 	},
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun 	/* band 7 */
160*4882a593Smuzhiyun 	{
161*4882a593Smuzhiyun 		.freq_low	= 1718000,
162*4882a593Smuzhiyun 		.freq_high	= 1855999,
163*4882a593Smuzhiyun 		.VCOdivider	= 2,
164*4882a593Smuzhiyun 		.progdata	= (0 << 19) | (1 << 9) | 0x10,
165*4882a593Smuzhiyun 	},
166*4882a593Smuzhiyun 
167*4882a593Smuzhiyun 	/* band 8 */
168*4882a593Smuzhiyun 	{
169*4882a593Smuzhiyun 		.freq_low	= 1856000,
170*4882a593Smuzhiyun 		.freq_high	= 2035999,
171*4882a593Smuzhiyun 		.VCOdivider	= 2,
172*4882a593Smuzhiyun 		.progdata	= (0 << 19) | (1 << 9) | 0x20,
173*4882a593Smuzhiyun 	},
174*4882a593Smuzhiyun 
175*4882a593Smuzhiyun 	/* band 9 */
176*4882a593Smuzhiyun 	{
177*4882a593Smuzhiyun 		.freq_low	= 2036000,
178*4882a593Smuzhiyun 		.freq_high	= 2150000,
179*4882a593Smuzhiyun 		.VCOdivider	= 2,
180*4882a593Smuzhiyun 		.progdata	= (0 << 19) | (1 << 9) | 0x40,
181*4882a593Smuzhiyun 	},
182*4882a593Smuzhiyun };
183*4882a593Smuzhiyun 
184*4882a593Smuzhiyun static struct {
185*4882a593Smuzhiyun 	u8 reg;
186*4882a593Smuzhiyun 	u8 data;
187*4882a593Smuzhiyun } cx24123_regdata[] =
188*4882a593Smuzhiyun {
189*4882a593Smuzhiyun 	{0x00, 0x03}, /* Reset system */
190*4882a593Smuzhiyun 	{0x00, 0x00}, /* Clear reset */
191*4882a593Smuzhiyun 	{0x03, 0x07}, /* QPSK, DVB, Auto Acquisition (default) */
192*4882a593Smuzhiyun 	{0x04, 0x10}, /* MPEG */
193*4882a593Smuzhiyun 	{0x05, 0x04}, /* MPEG */
194*4882a593Smuzhiyun 	{0x06, 0x31}, /* MPEG (default) */
195*4882a593Smuzhiyun 	{0x0b, 0x00}, /* Freq search start point (default) */
196*4882a593Smuzhiyun 	{0x0c, 0x00}, /* Demodulator sample gain (default) */
197*4882a593Smuzhiyun 	{0x0d, 0x7f}, /* Force driver to shift until the maximum (+-10 MHz) */
198*4882a593Smuzhiyun 	{0x0e, 0x03}, /* Default non-inverted, FEC 3/4 (default) */
199*4882a593Smuzhiyun 	{0x0f, 0xfe}, /* FEC search mask (all supported codes) */
200*4882a593Smuzhiyun 	{0x10, 0x01}, /* Default search inversion, no repeat (default) */
201*4882a593Smuzhiyun 	{0x16, 0x00}, /* Enable reading of frequency */
202*4882a593Smuzhiyun 	{0x17, 0x01}, /* Enable EsNO Ready Counter */
203*4882a593Smuzhiyun 	{0x1c, 0x80}, /* Enable error counter */
204*4882a593Smuzhiyun 	{0x20, 0x00}, /* Tuner burst clock rate = 500KHz */
205*4882a593Smuzhiyun 	{0x21, 0x15}, /* Tuner burst mode, word length = 0x15 */
206*4882a593Smuzhiyun 	{0x28, 0x00}, /* Enable FILTERV with positive pol., DiSEqC 2.x off */
207*4882a593Smuzhiyun 	{0x29, 0x00}, /* DiSEqC LNB_DC off */
208*4882a593Smuzhiyun 	{0x2a, 0xb0}, /* DiSEqC Parameters (default) */
209*4882a593Smuzhiyun 	{0x2b, 0x73}, /* DiSEqC Tone Frequency (default) */
210*4882a593Smuzhiyun 	{0x2c, 0x00}, /* DiSEqC Message (0x2c - 0x31) */
211*4882a593Smuzhiyun 	{0x2d, 0x00},
212*4882a593Smuzhiyun 	{0x2e, 0x00},
213*4882a593Smuzhiyun 	{0x2f, 0x00},
214*4882a593Smuzhiyun 	{0x30, 0x00},
215*4882a593Smuzhiyun 	{0x31, 0x00},
216*4882a593Smuzhiyun 	{0x32, 0x8c}, /* DiSEqC Parameters (default) */
217*4882a593Smuzhiyun 	{0x33, 0x00}, /* Interrupts off (0x33 - 0x34) */
218*4882a593Smuzhiyun 	{0x34, 0x00},
219*4882a593Smuzhiyun 	{0x35, 0x03}, /* DiSEqC Tone Amplitude (default) */
220*4882a593Smuzhiyun 	{0x36, 0x02}, /* DiSEqC Parameters (default) */
221*4882a593Smuzhiyun 	{0x37, 0x3a}, /* DiSEqC Parameters (default) */
222*4882a593Smuzhiyun 	{0x3a, 0x00}, /* Enable AGC accumulator (for signal strength) */
223*4882a593Smuzhiyun 	{0x44, 0x00}, /* Constellation (default) */
224*4882a593Smuzhiyun 	{0x45, 0x00}, /* Symbol count (default) */
225*4882a593Smuzhiyun 	{0x46, 0x0d}, /* Symbol rate estimator on (default) */
226*4882a593Smuzhiyun 	{0x56, 0xc1}, /* Error Counter = Viterbi BER */
227*4882a593Smuzhiyun 	{0x57, 0xff}, /* Error Counter Window (default) */
228*4882a593Smuzhiyun 	{0x5c, 0x20}, /* Acquisition AFC Expiration window (default is 0x10) */
229*4882a593Smuzhiyun 	{0x67, 0x83}, /* Non-DCII symbol clock */
230*4882a593Smuzhiyun };
231*4882a593Smuzhiyun 
cx24123_i2c_writereg(struct cx24123_state * state,u8 i2c_addr,int reg,int data)232*4882a593Smuzhiyun static int cx24123_i2c_writereg(struct cx24123_state *state,
233*4882a593Smuzhiyun 	u8 i2c_addr, int reg, int data)
234*4882a593Smuzhiyun {
235*4882a593Smuzhiyun 	u8 buf[] = { reg, data };
236*4882a593Smuzhiyun 	struct i2c_msg msg = {
237*4882a593Smuzhiyun 		.addr = i2c_addr, .flags = 0, .buf = buf, .len = 2
238*4882a593Smuzhiyun 	};
239*4882a593Smuzhiyun 	int err;
240*4882a593Smuzhiyun 
241*4882a593Smuzhiyun 	/* printk(KERN_DEBUG "wr(%02x): %02x %02x\n", i2c_addr, reg, data); */
242*4882a593Smuzhiyun 
243*4882a593Smuzhiyun 	err = i2c_transfer(state->i2c, &msg, 1);
244*4882a593Smuzhiyun 	if (err != 1) {
245*4882a593Smuzhiyun 		printk("%s: writereg error(err == %i, reg == 0x%02x, data == 0x%02x)\n",
246*4882a593Smuzhiyun 		       __func__, err, reg, data);
247*4882a593Smuzhiyun 		return err;
248*4882a593Smuzhiyun 	}
249*4882a593Smuzhiyun 
250*4882a593Smuzhiyun 	return 0;
251*4882a593Smuzhiyun }
252*4882a593Smuzhiyun 
cx24123_i2c_readreg(struct cx24123_state * state,u8 i2c_addr,u8 reg)253*4882a593Smuzhiyun static int cx24123_i2c_readreg(struct cx24123_state *state, u8 i2c_addr, u8 reg)
254*4882a593Smuzhiyun {
255*4882a593Smuzhiyun 	int ret;
256*4882a593Smuzhiyun 	u8 b = 0;
257*4882a593Smuzhiyun 	struct i2c_msg msg[] = {
258*4882a593Smuzhiyun 		{ .addr = i2c_addr, .flags = 0, .buf = &reg, .len = 1 },
259*4882a593Smuzhiyun 		{ .addr = i2c_addr, .flags = I2C_M_RD, .buf = &b, .len = 1 }
260*4882a593Smuzhiyun 	};
261*4882a593Smuzhiyun 
262*4882a593Smuzhiyun 	ret = i2c_transfer(state->i2c, msg, 2);
263*4882a593Smuzhiyun 
264*4882a593Smuzhiyun 	if (ret != 2) {
265*4882a593Smuzhiyun 		err("%s: reg=0x%x (error=%d)\n", __func__, reg, ret);
266*4882a593Smuzhiyun 		return ret;
267*4882a593Smuzhiyun 	}
268*4882a593Smuzhiyun 
269*4882a593Smuzhiyun 	/* printk(KERN_DEBUG "rd(%02x): %02x %02x\n", i2c_addr, reg, b); */
270*4882a593Smuzhiyun 
271*4882a593Smuzhiyun 	return b;
272*4882a593Smuzhiyun }
273*4882a593Smuzhiyun 
274*4882a593Smuzhiyun #define cx24123_readreg(state, reg) \
275*4882a593Smuzhiyun 	cx24123_i2c_readreg(state, state->config->demod_address, reg)
276*4882a593Smuzhiyun #define cx24123_writereg(state, reg, val) \
277*4882a593Smuzhiyun 	cx24123_i2c_writereg(state, state->config->demod_address, reg, val)
278*4882a593Smuzhiyun 
cx24123_set_inversion(struct cx24123_state * state,enum fe_spectral_inversion inversion)279*4882a593Smuzhiyun static int cx24123_set_inversion(struct cx24123_state *state,
280*4882a593Smuzhiyun 				 enum fe_spectral_inversion inversion)
281*4882a593Smuzhiyun {
282*4882a593Smuzhiyun 	u8 nom_reg = cx24123_readreg(state, 0x0e);
283*4882a593Smuzhiyun 	u8 auto_reg = cx24123_readreg(state, 0x10);
284*4882a593Smuzhiyun 
285*4882a593Smuzhiyun 	switch (inversion) {
286*4882a593Smuzhiyun 	case INVERSION_OFF:
287*4882a593Smuzhiyun 		dprintk("inversion off\n");
288*4882a593Smuzhiyun 		cx24123_writereg(state, 0x0e, nom_reg & ~0x80);
289*4882a593Smuzhiyun 		cx24123_writereg(state, 0x10, auto_reg | 0x80);
290*4882a593Smuzhiyun 		break;
291*4882a593Smuzhiyun 	case INVERSION_ON:
292*4882a593Smuzhiyun 		dprintk("inversion on\n");
293*4882a593Smuzhiyun 		cx24123_writereg(state, 0x0e, nom_reg | 0x80);
294*4882a593Smuzhiyun 		cx24123_writereg(state, 0x10, auto_reg | 0x80);
295*4882a593Smuzhiyun 		break;
296*4882a593Smuzhiyun 	case INVERSION_AUTO:
297*4882a593Smuzhiyun 		dprintk("inversion auto\n");
298*4882a593Smuzhiyun 		cx24123_writereg(state, 0x10, auto_reg & ~0x80);
299*4882a593Smuzhiyun 		break;
300*4882a593Smuzhiyun 	default:
301*4882a593Smuzhiyun 		return -EINVAL;
302*4882a593Smuzhiyun 	}
303*4882a593Smuzhiyun 
304*4882a593Smuzhiyun 	return 0;
305*4882a593Smuzhiyun }
306*4882a593Smuzhiyun 
cx24123_get_inversion(struct cx24123_state * state,enum fe_spectral_inversion * inversion)307*4882a593Smuzhiyun static int cx24123_get_inversion(struct cx24123_state *state,
308*4882a593Smuzhiyun 				 enum fe_spectral_inversion *inversion)
309*4882a593Smuzhiyun {
310*4882a593Smuzhiyun 	u8 val;
311*4882a593Smuzhiyun 
312*4882a593Smuzhiyun 	val = cx24123_readreg(state, 0x1b) >> 7;
313*4882a593Smuzhiyun 
314*4882a593Smuzhiyun 	if (val == 0) {
315*4882a593Smuzhiyun 		dprintk("read inversion off\n");
316*4882a593Smuzhiyun 		*inversion = INVERSION_OFF;
317*4882a593Smuzhiyun 	} else {
318*4882a593Smuzhiyun 		dprintk("read inversion on\n");
319*4882a593Smuzhiyun 		*inversion = INVERSION_ON;
320*4882a593Smuzhiyun 	}
321*4882a593Smuzhiyun 
322*4882a593Smuzhiyun 	return 0;
323*4882a593Smuzhiyun }
324*4882a593Smuzhiyun 
cx24123_set_fec(struct cx24123_state * state,enum fe_code_rate fec)325*4882a593Smuzhiyun static int cx24123_set_fec(struct cx24123_state *state, enum fe_code_rate fec)
326*4882a593Smuzhiyun {
327*4882a593Smuzhiyun 	u8 nom_reg = cx24123_readreg(state, 0x0e) & ~0x07;
328*4882a593Smuzhiyun 
329*4882a593Smuzhiyun 	if (((int)fec < FEC_NONE) || (fec > FEC_AUTO))
330*4882a593Smuzhiyun 		fec = FEC_AUTO;
331*4882a593Smuzhiyun 
332*4882a593Smuzhiyun 	/* Set the soft decision threshold */
333*4882a593Smuzhiyun 	if (fec == FEC_1_2)
334*4882a593Smuzhiyun 		cx24123_writereg(state, 0x43,
335*4882a593Smuzhiyun 			cx24123_readreg(state, 0x43) | 0x01);
336*4882a593Smuzhiyun 	else
337*4882a593Smuzhiyun 		cx24123_writereg(state, 0x43,
338*4882a593Smuzhiyun 			cx24123_readreg(state, 0x43) & ~0x01);
339*4882a593Smuzhiyun 
340*4882a593Smuzhiyun 	switch (fec) {
341*4882a593Smuzhiyun 	case FEC_1_2:
342*4882a593Smuzhiyun 		dprintk("set FEC to 1/2\n");
343*4882a593Smuzhiyun 		cx24123_writereg(state, 0x0e, nom_reg | 0x01);
344*4882a593Smuzhiyun 		cx24123_writereg(state, 0x0f, 0x02);
345*4882a593Smuzhiyun 		break;
346*4882a593Smuzhiyun 	case FEC_2_3:
347*4882a593Smuzhiyun 		dprintk("set FEC to 2/3\n");
348*4882a593Smuzhiyun 		cx24123_writereg(state, 0x0e, nom_reg | 0x02);
349*4882a593Smuzhiyun 		cx24123_writereg(state, 0x0f, 0x04);
350*4882a593Smuzhiyun 		break;
351*4882a593Smuzhiyun 	case FEC_3_4:
352*4882a593Smuzhiyun 		dprintk("set FEC to 3/4\n");
353*4882a593Smuzhiyun 		cx24123_writereg(state, 0x0e, nom_reg | 0x03);
354*4882a593Smuzhiyun 		cx24123_writereg(state, 0x0f, 0x08);
355*4882a593Smuzhiyun 		break;
356*4882a593Smuzhiyun 	case FEC_4_5:
357*4882a593Smuzhiyun 		dprintk("set FEC to 4/5\n");
358*4882a593Smuzhiyun 		cx24123_writereg(state, 0x0e, nom_reg | 0x04);
359*4882a593Smuzhiyun 		cx24123_writereg(state, 0x0f, 0x10);
360*4882a593Smuzhiyun 		break;
361*4882a593Smuzhiyun 	case FEC_5_6:
362*4882a593Smuzhiyun 		dprintk("set FEC to 5/6\n");
363*4882a593Smuzhiyun 		cx24123_writereg(state, 0x0e, nom_reg | 0x05);
364*4882a593Smuzhiyun 		cx24123_writereg(state, 0x0f, 0x20);
365*4882a593Smuzhiyun 		break;
366*4882a593Smuzhiyun 	case FEC_6_7:
367*4882a593Smuzhiyun 		dprintk("set FEC to 6/7\n");
368*4882a593Smuzhiyun 		cx24123_writereg(state, 0x0e, nom_reg | 0x06);
369*4882a593Smuzhiyun 		cx24123_writereg(state, 0x0f, 0x40);
370*4882a593Smuzhiyun 		break;
371*4882a593Smuzhiyun 	case FEC_7_8:
372*4882a593Smuzhiyun 		dprintk("set FEC to 7/8\n");
373*4882a593Smuzhiyun 		cx24123_writereg(state, 0x0e, nom_reg | 0x07);
374*4882a593Smuzhiyun 		cx24123_writereg(state, 0x0f, 0x80);
375*4882a593Smuzhiyun 		break;
376*4882a593Smuzhiyun 	case FEC_AUTO:
377*4882a593Smuzhiyun 		dprintk("set FEC to auto\n");
378*4882a593Smuzhiyun 		cx24123_writereg(state, 0x0f, 0xfe);
379*4882a593Smuzhiyun 		break;
380*4882a593Smuzhiyun 	default:
381*4882a593Smuzhiyun 		return -EOPNOTSUPP;
382*4882a593Smuzhiyun 	}
383*4882a593Smuzhiyun 
384*4882a593Smuzhiyun 	return 0;
385*4882a593Smuzhiyun }
386*4882a593Smuzhiyun 
cx24123_get_fec(struct cx24123_state * state,enum fe_code_rate * fec)387*4882a593Smuzhiyun static int cx24123_get_fec(struct cx24123_state *state, enum fe_code_rate *fec)
388*4882a593Smuzhiyun {
389*4882a593Smuzhiyun 	int ret;
390*4882a593Smuzhiyun 
391*4882a593Smuzhiyun 	ret = cx24123_readreg(state, 0x1b);
392*4882a593Smuzhiyun 	if (ret < 0)
393*4882a593Smuzhiyun 		return ret;
394*4882a593Smuzhiyun 	ret = ret & 0x07;
395*4882a593Smuzhiyun 
396*4882a593Smuzhiyun 	switch (ret) {
397*4882a593Smuzhiyun 	case 1:
398*4882a593Smuzhiyun 		*fec = FEC_1_2;
399*4882a593Smuzhiyun 		break;
400*4882a593Smuzhiyun 	case 2:
401*4882a593Smuzhiyun 		*fec = FEC_2_3;
402*4882a593Smuzhiyun 		break;
403*4882a593Smuzhiyun 	case 3:
404*4882a593Smuzhiyun 		*fec = FEC_3_4;
405*4882a593Smuzhiyun 		break;
406*4882a593Smuzhiyun 	case 4:
407*4882a593Smuzhiyun 		*fec = FEC_4_5;
408*4882a593Smuzhiyun 		break;
409*4882a593Smuzhiyun 	case 5:
410*4882a593Smuzhiyun 		*fec = FEC_5_6;
411*4882a593Smuzhiyun 		break;
412*4882a593Smuzhiyun 	case 6:
413*4882a593Smuzhiyun 		*fec = FEC_6_7;
414*4882a593Smuzhiyun 		break;
415*4882a593Smuzhiyun 	case 7:
416*4882a593Smuzhiyun 		*fec = FEC_7_8;
417*4882a593Smuzhiyun 		break;
418*4882a593Smuzhiyun 	default:
419*4882a593Smuzhiyun 		/* this can happen when there's no lock */
420*4882a593Smuzhiyun 		*fec = FEC_NONE;
421*4882a593Smuzhiyun 	}
422*4882a593Smuzhiyun 
423*4882a593Smuzhiyun 	return 0;
424*4882a593Smuzhiyun }
425*4882a593Smuzhiyun 
426*4882a593Smuzhiyun /* Approximation of closest integer of log2(a/b). It actually gives the
427*4882a593Smuzhiyun    lowest integer i such that 2^i >= round(a/b) */
cx24123_int_log2(u32 a,u32 b)428*4882a593Smuzhiyun static u32 cx24123_int_log2(u32 a, u32 b)
429*4882a593Smuzhiyun {
430*4882a593Smuzhiyun 	u32 exp, nearest = 0;
431*4882a593Smuzhiyun 	u32 div = a / b;
432*4882a593Smuzhiyun 	if (a % b >= b / 2)
433*4882a593Smuzhiyun 		++div;
434*4882a593Smuzhiyun 	if (div < (1UL << 31)) {
435*4882a593Smuzhiyun 		for (exp = 1; div > exp; nearest++)
436*4882a593Smuzhiyun 			exp += exp;
437*4882a593Smuzhiyun 	}
438*4882a593Smuzhiyun 	return nearest;
439*4882a593Smuzhiyun }
440*4882a593Smuzhiyun 
cx24123_set_symbolrate(struct cx24123_state * state,u32 srate)441*4882a593Smuzhiyun static int cx24123_set_symbolrate(struct cx24123_state *state, u32 srate)
442*4882a593Smuzhiyun {
443*4882a593Smuzhiyun 	u64 tmp;
444*4882a593Smuzhiyun 	u32 sample_rate, ratio, sample_gain;
445*4882a593Smuzhiyun 	u8 pll_mult;
446*4882a593Smuzhiyun 
447*4882a593Smuzhiyun 	/*  check if symbol rate is within limits */
448*4882a593Smuzhiyun 	if ((srate > state->frontend.ops.info.symbol_rate_max) ||
449*4882a593Smuzhiyun 	    (srate < state->frontend.ops.info.symbol_rate_min))
450*4882a593Smuzhiyun 		return -EOPNOTSUPP;
451*4882a593Smuzhiyun 
452*4882a593Smuzhiyun 	/* choose the sampling rate high enough for the required operation,
453*4882a593Smuzhiyun 	   while optimizing the power consumed by the demodulator */
454*4882a593Smuzhiyun 	if (srate < (XTAL*2)/2)
455*4882a593Smuzhiyun 		pll_mult = 2;
456*4882a593Smuzhiyun 	else if (srate < (XTAL*3)/2)
457*4882a593Smuzhiyun 		pll_mult = 3;
458*4882a593Smuzhiyun 	else if (srate < (XTAL*4)/2)
459*4882a593Smuzhiyun 		pll_mult = 4;
460*4882a593Smuzhiyun 	else if (srate < (XTAL*5)/2)
461*4882a593Smuzhiyun 		pll_mult = 5;
462*4882a593Smuzhiyun 	else if (srate < (XTAL*6)/2)
463*4882a593Smuzhiyun 		pll_mult = 6;
464*4882a593Smuzhiyun 	else if (srate < (XTAL*7)/2)
465*4882a593Smuzhiyun 		pll_mult = 7;
466*4882a593Smuzhiyun 	else if (srate < (XTAL*8)/2)
467*4882a593Smuzhiyun 		pll_mult = 8;
468*4882a593Smuzhiyun 	else
469*4882a593Smuzhiyun 		pll_mult = 9;
470*4882a593Smuzhiyun 
471*4882a593Smuzhiyun 
472*4882a593Smuzhiyun 	sample_rate = pll_mult * XTAL;
473*4882a593Smuzhiyun 
474*4882a593Smuzhiyun 	/* SYSSymbolRate[21:0] = (srate << 23) / sample_rate */
475*4882a593Smuzhiyun 
476*4882a593Smuzhiyun 	tmp = ((u64)srate) << 23;
477*4882a593Smuzhiyun 	do_div(tmp, sample_rate);
478*4882a593Smuzhiyun 	ratio = (u32) tmp;
479*4882a593Smuzhiyun 
480*4882a593Smuzhiyun 	cx24123_writereg(state, 0x01, pll_mult * 6);
481*4882a593Smuzhiyun 
482*4882a593Smuzhiyun 	cx24123_writereg(state, 0x08, (ratio >> 16) & 0x3f);
483*4882a593Smuzhiyun 	cx24123_writereg(state, 0x09, (ratio >> 8) & 0xff);
484*4882a593Smuzhiyun 	cx24123_writereg(state, 0x0a, ratio & 0xff);
485*4882a593Smuzhiyun 
486*4882a593Smuzhiyun 	/* also set the demodulator sample gain */
487*4882a593Smuzhiyun 	sample_gain = cx24123_int_log2(sample_rate, srate);
488*4882a593Smuzhiyun 	tmp = cx24123_readreg(state, 0x0c) & ~0xe0;
489*4882a593Smuzhiyun 	cx24123_writereg(state, 0x0c, tmp | sample_gain << 5);
490*4882a593Smuzhiyun 
491*4882a593Smuzhiyun 	dprintk("srate=%d, ratio=0x%08x, sample_rate=%i sample_gain=%d\n",
492*4882a593Smuzhiyun 		srate, ratio, sample_rate, sample_gain);
493*4882a593Smuzhiyun 
494*4882a593Smuzhiyun 	return 0;
495*4882a593Smuzhiyun }
496*4882a593Smuzhiyun 
497*4882a593Smuzhiyun /*
498*4882a593Smuzhiyun  * Based on the required frequency and symbolrate, the tuner AGC has
499*4882a593Smuzhiyun  * to be configured and the correct band selected.
500*4882a593Smuzhiyun  * Calculate those values.
501*4882a593Smuzhiyun  */
cx24123_pll_calculate(struct dvb_frontend * fe)502*4882a593Smuzhiyun static int cx24123_pll_calculate(struct dvb_frontend *fe)
503*4882a593Smuzhiyun {
504*4882a593Smuzhiyun 	struct dtv_frontend_properties *p = &fe->dtv_property_cache;
505*4882a593Smuzhiyun 	struct cx24123_state *state = fe->demodulator_priv;
506*4882a593Smuzhiyun 	u32 ndiv = 0, adiv = 0, vco_div = 0;
507*4882a593Smuzhiyun 	int i = 0;
508*4882a593Smuzhiyun 	int pump = 2;
509*4882a593Smuzhiyun 	int band = 0;
510*4882a593Smuzhiyun 	int num_bands = ARRAY_SIZE(cx24123_bandselect_vals);
511*4882a593Smuzhiyun 	struct cx24123_bandselect_val *bsv = NULL;
512*4882a593Smuzhiyun 	struct cx24123_AGC_val *agcv = NULL;
513*4882a593Smuzhiyun 
514*4882a593Smuzhiyun 	/* Defaults for low freq, low rate */
515*4882a593Smuzhiyun 	state->VCAarg = cx24123_AGC_vals[0].VCAprogdata;
516*4882a593Smuzhiyun 	state->VGAarg = cx24123_AGC_vals[0].VGAprogdata;
517*4882a593Smuzhiyun 	state->bandselectarg = cx24123_bandselect_vals[0].progdata;
518*4882a593Smuzhiyun 	vco_div = cx24123_bandselect_vals[0].VCOdivider;
519*4882a593Smuzhiyun 
520*4882a593Smuzhiyun 	/* For the given symbol rate, determine the VCA, VGA and
521*4882a593Smuzhiyun 	 * FILTUNE programming bits */
522*4882a593Smuzhiyun 	for (i = 0; i < ARRAY_SIZE(cx24123_AGC_vals); i++) {
523*4882a593Smuzhiyun 		agcv = &cx24123_AGC_vals[i];
524*4882a593Smuzhiyun 		if ((agcv->symbolrate_low <= p->symbol_rate) &&
525*4882a593Smuzhiyun 		    (agcv->symbolrate_high >= p->symbol_rate)) {
526*4882a593Smuzhiyun 			state->VCAarg = agcv->VCAprogdata;
527*4882a593Smuzhiyun 			state->VGAarg = agcv->VGAprogdata;
528*4882a593Smuzhiyun 			state->FILTune = agcv->FILTune;
529*4882a593Smuzhiyun 		}
530*4882a593Smuzhiyun 	}
531*4882a593Smuzhiyun 
532*4882a593Smuzhiyun 	/* determine the band to use */
533*4882a593Smuzhiyun 	if (force_band < 1 || force_band > num_bands) {
534*4882a593Smuzhiyun 		for (i = 0; i < num_bands; i++) {
535*4882a593Smuzhiyun 			bsv = &cx24123_bandselect_vals[i];
536*4882a593Smuzhiyun 			if ((bsv->freq_low <= p->frequency) &&
537*4882a593Smuzhiyun 				(bsv->freq_high >= p->frequency))
538*4882a593Smuzhiyun 				band = i;
539*4882a593Smuzhiyun 		}
540*4882a593Smuzhiyun 	} else
541*4882a593Smuzhiyun 		band = force_band - 1;
542*4882a593Smuzhiyun 
543*4882a593Smuzhiyun 	state->bandselectarg = cx24123_bandselect_vals[band].progdata;
544*4882a593Smuzhiyun 	vco_div = cx24123_bandselect_vals[band].VCOdivider;
545*4882a593Smuzhiyun 
546*4882a593Smuzhiyun 	/* determine the charge pump current */
547*4882a593Smuzhiyun 	if (p->frequency < (cx24123_bandselect_vals[band].freq_low +
548*4882a593Smuzhiyun 		cx24123_bandselect_vals[band].freq_high) / 2)
549*4882a593Smuzhiyun 		pump = 0x01;
550*4882a593Smuzhiyun 	else
551*4882a593Smuzhiyun 		pump = 0x02;
552*4882a593Smuzhiyun 
553*4882a593Smuzhiyun 	/* Determine the N/A dividers for the requested lband freq (in kHz). */
554*4882a593Smuzhiyun 	/* Note: the reference divider R=10, frequency is in KHz,
555*4882a593Smuzhiyun 	 * XTAL is in Hz */
556*4882a593Smuzhiyun 	ndiv = (((p->frequency * vco_div * 10) /
557*4882a593Smuzhiyun 		(2 * XTAL / 1000)) / 32) & 0x1ff;
558*4882a593Smuzhiyun 	adiv = (((p->frequency * vco_div * 10) /
559*4882a593Smuzhiyun 		(2 * XTAL / 1000)) % 32) & 0x1f;
560*4882a593Smuzhiyun 
561*4882a593Smuzhiyun 	if (adiv == 0 && ndiv > 0)
562*4882a593Smuzhiyun 		ndiv--;
563*4882a593Smuzhiyun 
564*4882a593Smuzhiyun 	/* control bits 11, refdiv 11, charge pump polarity 1,
565*4882a593Smuzhiyun 	 * charge pump current, ndiv, adiv */
566*4882a593Smuzhiyun 	state->pllarg = (3 << 19) | (3 << 17) | (1 << 16) |
567*4882a593Smuzhiyun 		(pump << 14) | (ndiv << 5) | adiv;
568*4882a593Smuzhiyun 
569*4882a593Smuzhiyun 	return 0;
570*4882a593Smuzhiyun }
571*4882a593Smuzhiyun 
572*4882a593Smuzhiyun /*
573*4882a593Smuzhiyun  * Tuner data is 21 bits long, must be left-aligned in data.
574*4882a593Smuzhiyun  * Tuner cx24109 is written through a dedicated 3wire interface
575*4882a593Smuzhiyun  * on the demod chip.
576*4882a593Smuzhiyun  */
cx24123_pll_writereg(struct dvb_frontend * fe,u32 data)577*4882a593Smuzhiyun static int cx24123_pll_writereg(struct dvb_frontend *fe, u32 data)
578*4882a593Smuzhiyun {
579*4882a593Smuzhiyun 	struct cx24123_state *state = fe->demodulator_priv;
580*4882a593Smuzhiyun 	unsigned long timeout;
581*4882a593Smuzhiyun 
582*4882a593Smuzhiyun 	dprintk("pll writereg called, data=0x%08x\n", data);
583*4882a593Smuzhiyun 
584*4882a593Smuzhiyun 	/* align the 21 bytes into to bit23 boundary */
585*4882a593Smuzhiyun 	data = data << 3;
586*4882a593Smuzhiyun 
587*4882a593Smuzhiyun 	/* Reset the demod pll word length to 0x15 bits */
588*4882a593Smuzhiyun 	cx24123_writereg(state, 0x21, 0x15);
589*4882a593Smuzhiyun 
590*4882a593Smuzhiyun 	/* write the msb 8 bits, wait for the send to be completed */
591*4882a593Smuzhiyun 	timeout = jiffies + msecs_to_jiffies(40);
592*4882a593Smuzhiyun 	cx24123_writereg(state, 0x22, (data >> 16) & 0xff);
593*4882a593Smuzhiyun 	while ((cx24123_readreg(state, 0x20) & 0x40) == 0) {
594*4882a593Smuzhiyun 		if (time_after(jiffies, timeout)) {
595*4882a593Smuzhiyun 			err("%s:  demodulator is not responding, "\
596*4882a593Smuzhiyun 				"possibly hung, aborting.\n", __func__);
597*4882a593Smuzhiyun 			return -EREMOTEIO;
598*4882a593Smuzhiyun 		}
599*4882a593Smuzhiyun 		msleep(10);
600*4882a593Smuzhiyun 	}
601*4882a593Smuzhiyun 
602*4882a593Smuzhiyun 	/* send another 8 bytes, wait for the send to be completed */
603*4882a593Smuzhiyun 	timeout = jiffies + msecs_to_jiffies(40);
604*4882a593Smuzhiyun 	cx24123_writereg(state, 0x22, (data >> 8) & 0xff);
605*4882a593Smuzhiyun 	while ((cx24123_readreg(state, 0x20) & 0x40) == 0) {
606*4882a593Smuzhiyun 		if (time_after(jiffies, timeout)) {
607*4882a593Smuzhiyun 			err("%s:  demodulator is not responding, "\
608*4882a593Smuzhiyun 				"possibly hung, aborting.\n", __func__);
609*4882a593Smuzhiyun 			return -EREMOTEIO;
610*4882a593Smuzhiyun 		}
611*4882a593Smuzhiyun 		msleep(10);
612*4882a593Smuzhiyun 	}
613*4882a593Smuzhiyun 
614*4882a593Smuzhiyun 	/* send the lower 5 bits of this byte, padded with 3 LBB,
615*4882a593Smuzhiyun 	 * wait for the send to be completed */
616*4882a593Smuzhiyun 	timeout = jiffies + msecs_to_jiffies(40);
617*4882a593Smuzhiyun 	cx24123_writereg(state, 0x22, (data) & 0xff);
618*4882a593Smuzhiyun 	while ((cx24123_readreg(state, 0x20) & 0x80)) {
619*4882a593Smuzhiyun 		if (time_after(jiffies, timeout)) {
620*4882a593Smuzhiyun 			err("%s:  demodulator is not responding," \
621*4882a593Smuzhiyun 				"possibly hung, aborting.\n", __func__);
622*4882a593Smuzhiyun 			return -EREMOTEIO;
623*4882a593Smuzhiyun 		}
624*4882a593Smuzhiyun 		msleep(10);
625*4882a593Smuzhiyun 	}
626*4882a593Smuzhiyun 
627*4882a593Smuzhiyun 	/* Trigger the demod to configure the tuner */
628*4882a593Smuzhiyun 	cx24123_writereg(state, 0x20, cx24123_readreg(state, 0x20) | 2);
629*4882a593Smuzhiyun 	cx24123_writereg(state, 0x20, cx24123_readreg(state, 0x20) & 0xfd);
630*4882a593Smuzhiyun 
631*4882a593Smuzhiyun 	return 0;
632*4882a593Smuzhiyun }
633*4882a593Smuzhiyun 
cx24123_pll_tune(struct dvb_frontend * fe)634*4882a593Smuzhiyun static int cx24123_pll_tune(struct dvb_frontend *fe)
635*4882a593Smuzhiyun {
636*4882a593Smuzhiyun 	struct dtv_frontend_properties *p = &fe->dtv_property_cache;
637*4882a593Smuzhiyun 	struct cx24123_state *state = fe->demodulator_priv;
638*4882a593Smuzhiyun 	u8 val;
639*4882a593Smuzhiyun 
640*4882a593Smuzhiyun 	dprintk("frequency=%i\n", p->frequency);
641*4882a593Smuzhiyun 
642*4882a593Smuzhiyun 	if (cx24123_pll_calculate(fe) != 0) {
643*4882a593Smuzhiyun 		err("%s: cx24123_pll_calculate failed\n", __func__);
644*4882a593Smuzhiyun 		return -EINVAL;
645*4882a593Smuzhiyun 	}
646*4882a593Smuzhiyun 
647*4882a593Smuzhiyun 	/* Write the new VCO/VGA */
648*4882a593Smuzhiyun 	cx24123_pll_writereg(fe, state->VCAarg);
649*4882a593Smuzhiyun 	cx24123_pll_writereg(fe, state->VGAarg);
650*4882a593Smuzhiyun 
651*4882a593Smuzhiyun 	/* Write the new bandselect and pll args */
652*4882a593Smuzhiyun 	cx24123_pll_writereg(fe, state->bandselectarg);
653*4882a593Smuzhiyun 	cx24123_pll_writereg(fe, state->pllarg);
654*4882a593Smuzhiyun 
655*4882a593Smuzhiyun 	/* set the FILTUNE voltage */
656*4882a593Smuzhiyun 	val = cx24123_readreg(state, 0x28) & ~0x3;
657*4882a593Smuzhiyun 	cx24123_writereg(state, 0x27, state->FILTune >> 2);
658*4882a593Smuzhiyun 	cx24123_writereg(state, 0x28, val | (state->FILTune & 0x3));
659*4882a593Smuzhiyun 
660*4882a593Smuzhiyun 	dprintk("pll tune VCA=%d, band=%d, pll=%d\n", state->VCAarg,
661*4882a593Smuzhiyun 			state->bandselectarg, state->pllarg);
662*4882a593Smuzhiyun 
663*4882a593Smuzhiyun 	return 0;
664*4882a593Smuzhiyun }
665*4882a593Smuzhiyun 
666*4882a593Smuzhiyun 
667*4882a593Smuzhiyun /*
668*4882a593Smuzhiyun  * 0x23:
669*4882a593Smuzhiyun  *    [7:7] = BTI enabled
670*4882a593Smuzhiyun  *    [6:6] = I2C repeater enabled
671*4882a593Smuzhiyun  *    [5:5] = I2C repeater start
672*4882a593Smuzhiyun  *    [0:0] = BTI start
673*4882a593Smuzhiyun  */
674*4882a593Smuzhiyun 
675*4882a593Smuzhiyun /* mode == 1 -> i2c-repeater, 0 -> bti */
cx24123_repeater_mode(struct cx24123_state * state,u8 mode,u8 start)676*4882a593Smuzhiyun static int cx24123_repeater_mode(struct cx24123_state *state, u8 mode, u8 start)
677*4882a593Smuzhiyun {
678*4882a593Smuzhiyun 	u8 r = cx24123_readreg(state, 0x23) & 0x1e;
679*4882a593Smuzhiyun 	if (mode)
680*4882a593Smuzhiyun 		r |= (1 << 6) | (start << 5);
681*4882a593Smuzhiyun 	else
682*4882a593Smuzhiyun 		r |= (1 << 7) | (start);
683*4882a593Smuzhiyun 	return cx24123_writereg(state, 0x23, r);
684*4882a593Smuzhiyun }
685*4882a593Smuzhiyun 
cx24123_initfe(struct dvb_frontend * fe)686*4882a593Smuzhiyun static int cx24123_initfe(struct dvb_frontend *fe)
687*4882a593Smuzhiyun {
688*4882a593Smuzhiyun 	struct cx24123_state *state = fe->demodulator_priv;
689*4882a593Smuzhiyun 	int i;
690*4882a593Smuzhiyun 
691*4882a593Smuzhiyun 	dprintk("init frontend\n");
692*4882a593Smuzhiyun 
693*4882a593Smuzhiyun 	/* Configure the demod to a good set of defaults */
694*4882a593Smuzhiyun 	for (i = 0; i < ARRAY_SIZE(cx24123_regdata); i++)
695*4882a593Smuzhiyun 		cx24123_writereg(state, cx24123_regdata[i].reg,
696*4882a593Smuzhiyun 			cx24123_regdata[i].data);
697*4882a593Smuzhiyun 
698*4882a593Smuzhiyun 	/* Set the LNB polarity */
699*4882a593Smuzhiyun 	if (state->config->lnb_polarity)
700*4882a593Smuzhiyun 		cx24123_writereg(state, 0x32,
701*4882a593Smuzhiyun 			cx24123_readreg(state, 0x32) | 0x02);
702*4882a593Smuzhiyun 
703*4882a593Smuzhiyun 	if (state->config->dont_use_pll)
704*4882a593Smuzhiyun 		cx24123_repeater_mode(state, 1, 0);
705*4882a593Smuzhiyun 
706*4882a593Smuzhiyun 	return 0;
707*4882a593Smuzhiyun }
708*4882a593Smuzhiyun 
cx24123_set_voltage(struct dvb_frontend * fe,enum fe_sec_voltage voltage)709*4882a593Smuzhiyun static int cx24123_set_voltage(struct dvb_frontend *fe,
710*4882a593Smuzhiyun 			       enum fe_sec_voltage voltage)
711*4882a593Smuzhiyun {
712*4882a593Smuzhiyun 	struct cx24123_state *state = fe->demodulator_priv;
713*4882a593Smuzhiyun 	u8 val;
714*4882a593Smuzhiyun 
715*4882a593Smuzhiyun 	val = cx24123_readreg(state, 0x29) & ~0x40;
716*4882a593Smuzhiyun 
717*4882a593Smuzhiyun 	switch (voltage) {
718*4882a593Smuzhiyun 	case SEC_VOLTAGE_13:
719*4882a593Smuzhiyun 		dprintk("setting voltage 13V\n");
720*4882a593Smuzhiyun 		return cx24123_writereg(state, 0x29, val & 0x7f);
721*4882a593Smuzhiyun 	case SEC_VOLTAGE_18:
722*4882a593Smuzhiyun 		dprintk("setting voltage 18V\n");
723*4882a593Smuzhiyun 		return cx24123_writereg(state, 0x29, val | 0x80);
724*4882a593Smuzhiyun 	case SEC_VOLTAGE_OFF:
725*4882a593Smuzhiyun 		/* already handled in cx88-dvb */
726*4882a593Smuzhiyun 		return 0;
727*4882a593Smuzhiyun 	default:
728*4882a593Smuzhiyun 		return -EINVAL;
729*4882a593Smuzhiyun 	}
730*4882a593Smuzhiyun 
731*4882a593Smuzhiyun 	return 0;
732*4882a593Smuzhiyun }
733*4882a593Smuzhiyun 
734*4882a593Smuzhiyun /* wait for diseqc queue to become ready (or timeout) */
cx24123_wait_for_diseqc(struct cx24123_state * state)735*4882a593Smuzhiyun static void cx24123_wait_for_diseqc(struct cx24123_state *state)
736*4882a593Smuzhiyun {
737*4882a593Smuzhiyun 	unsigned long timeout = jiffies + msecs_to_jiffies(200);
738*4882a593Smuzhiyun 	while (!(cx24123_readreg(state, 0x29) & 0x40)) {
739*4882a593Smuzhiyun 		if (time_after(jiffies, timeout)) {
740*4882a593Smuzhiyun 			err("%s: diseqc queue not ready, " \
741*4882a593Smuzhiyun 				"command may be lost.\n", __func__);
742*4882a593Smuzhiyun 			break;
743*4882a593Smuzhiyun 		}
744*4882a593Smuzhiyun 		msleep(10);
745*4882a593Smuzhiyun 	}
746*4882a593Smuzhiyun }
747*4882a593Smuzhiyun 
cx24123_send_diseqc_msg(struct dvb_frontend * fe,struct dvb_diseqc_master_cmd * cmd)748*4882a593Smuzhiyun static int cx24123_send_diseqc_msg(struct dvb_frontend *fe,
749*4882a593Smuzhiyun 	struct dvb_diseqc_master_cmd *cmd)
750*4882a593Smuzhiyun {
751*4882a593Smuzhiyun 	struct cx24123_state *state = fe->demodulator_priv;
752*4882a593Smuzhiyun 	int i, val, tone;
753*4882a593Smuzhiyun 
754*4882a593Smuzhiyun 	dprintk("\n");
755*4882a593Smuzhiyun 
756*4882a593Smuzhiyun 	/* stop continuous tone if enabled */
757*4882a593Smuzhiyun 	tone = cx24123_readreg(state, 0x29);
758*4882a593Smuzhiyun 	if (tone & 0x10)
759*4882a593Smuzhiyun 		cx24123_writereg(state, 0x29, tone & ~0x50);
760*4882a593Smuzhiyun 
761*4882a593Smuzhiyun 	/* wait for diseqc queue ready */
762*4882a593Smuzhiyun 	cx24123_wait_for_diseqc(state);
763*4882a593Smuzhiyun 
764*4882a593Smuzhiyun 	/* select tone mode */
765*4882a593Smuzhiyun 	cx24123_writereg(state, 0x2a, cx24123_readreg(state, 0x2a) & 0xfb);
766*4882a593Smuzhiyun 
767*4882a593Smuzhiyun 	for (i = 0; i < cmd->msg_len; i++)
768*4882a593Smuzhiyun 		cx24123_writereg(state, 0x2C + i, cmd->msg[i]);
769*4882a593Smuzhiyun 
770*4882a593Smuzhiyun 	val = cx24123_readreg(state, 0x29);
771*4882a593Smuzhiyun 	cx24123_writereg(state, 0x29, ((val & 0x90) | 0x40) |
772*4882a593Smuzhiyun 		((cmd->msg_len-3) & 3));
773*4882a593Smuzhiyun 
774*4882a593Smuzhiyun 	/* wait for diseqc message to finish sending */
775*4882a593Smuzhiyun 	cx24123_wait_for_diseqc(state);
776*4882a593Smuzhiyun 
777*4882a593Smuzhiyun 	/* restart continuous tone if enabled */
778*4882a593Smuzhiyun 	if (tone & 0x10)
779*4882a593Smuzhiyun 		cx24123_writereg(state, 0x29, tone & ~0x40);
780*4882a593Smuzhiyun 
781*4882a593Smuzhiyun 	return 0;
782*4882a593Smuzhiyun }
783*4882a593Smuzhiyun 
cx24123_diseqc_send_burst(struct dvb_frontend * fe,enum fe_sec_mini_cmd burst)784*4882a593Smuzhiyun static int cx24123_diseqc_send_burst(struct dvb_frontend *fe,
785*4882a593Smuzhiyun 				     enum fe_sec_mini_cmd burst)
786*4882a593Smuzhiyun {
787*4882a593Smuzhiyun 	struct cx24123_state *state = fe->demodulator_priv;
788*4882a593Smuzhiyun 	int val, tone;
789*4882a593Smuzhiyun 
790*4882a593Smuzhiyun 	dprintk("\n");
791*4882a593Smuzhiyun 
792*4882a593Smuzhiyun 	/* stop continuous tone if enabled */
793*4882a593Smuzhiyun 	tone = cx24123_readreg(state, 0x29);
794*4882a593Smuzhiyun 	if (tone & 0x10)
795*4882a593Smuzhiyun 		cx24123_writereg(state, 0x29, tone & ~0x50);
796*4882a593Smuzhiyun 
797*4882a593Smuzhiyun 	/* wait for diseqc queue ready */
798*4882a593Smuzhiyun 	cx24123_wait_for_diseqc(state);
799*4882a593Smuzhiyun 
800*4882a593Smuzhiyun 	/* select tone mode */
801*4882a593Smuzhiyun 	cx24123_writereg(state, 0x2a, cx24123_readreg(state, 0x2a) | 0x4);
802*4882a593Smuzhiyun 	msleep(30);
803*4882a593Smuzhiyun 	val = cx24123_readreg(state, 0x29);
804*4882a593Smuzhiyun 	if (burst == SEC_MINI_A)
805*4882a593Smuzhiyun 		cx24123_writereg(state, 0x29, ((val & 0x90) | 0x40 | 0x00));
806*4882a593Smuzhiyun 	else if (burst == SEC_MINI_B)
807*4882a593Smuzhiyun 		cx24123_writereg(state, 0x29, ((val & 0x90) | 0x40 | 0x08));
808*4882a593Smuzhiyun 	else
809*4882a593Smuzhiyun 		return -EINVAL;
810*4882a593Smuzhiyun 
811*4882a593Smuzhiyun 	cx24123_wait_for_diseqc(state);
812*4882a593Smuzhiyun 	cx24123_writereg(state, 0x2a, cx24123_readreg(state, 0x2a) & 0xfb);
813*4882a593Smuzhiyun 
814*4882a593Smuzhiyun 	/* restart continuous tone if enabled */
815*4882a593Smuzhiyun 	if (tone & 0x10)
816*4882a593Smuzhiyun 		cx24123_writereg(state, 0x29, tone & ~0x40);
817*4882a593Smuzhiyun 
818*4882a593Smuzhiyun 	return 0;
819*4882a593Smuzhiyun }
820*4882a593Smuzhiyun 
cx24123_read_status(struct dvb_frontend * fe,enum fe_status * status)821*4882a593Smuzhiyun static int cx24123_read_status(struct dvb_frontend *fe, enum fe_status *status)
822*4882a593Smuzhiyun {
823*4882a593Smuzhiyun 	struct cx24123_state *state = fe->demodulator_priv;
824*4882a593Smuzhiyun 	int sync = cx24123_readreg(state, 0x14);
825*4882a593Smuzhiyun 
826*4882a593Smuzhiyun 	*status = 0;
827*4882a593Smuzhiyun 	if (state->config->dont_use_pll) {
828*4882a593Smuzhiyun 		u32 tun_status = 0;
829*4882a593Smuzhiyun 		if (fe->ops.tuner_ops.get_status)
830*4882a593Smuzhiyun 			fe->ops.tuner_ops.get_status(fe, &tun_status);
831*4882a593Smuzhiyun 		if (tun_status & TUNER_STATUS_LOCKED)
832*4882a593Smuzhiyun 			*status |= FE_HAS_SIGNAL;
833*4882a593Smuzhiyun 	} else {
834*4882a593Smuzhiyun 		int lock = cx24123_readreg(state, 0x20);
835*4882a593Smuzhiyun 		if (lock & 0x01)
836*4882a593Smuzhiyun 			*status |= FE_HAS_SIGNAL;
837*4882a593Smuzhiyun 	}
838*4882a593Smuzhiyun 
839*4882a593Smuzhiyun 	if (sync & 0x02)
840*4882a593Smuzhiyun 		*status |= FE_HAS_CARRIER;	/* Phase locked */
841*4882a593Smuzhiyun 	if (sync & 0x04)
842*4882a593Smuzhiyun 		*status |= FE_HAS_VITERBI;
843*4882a593Smuzhiyun 
844*4882a593Smuzhiyun 	/* Reed-Solomon Status */
845*4882a593Smuzhiyun 	if (sync & 0x08)
846*4882a593Smuzhiyun 		*status |= FE_HAS_SYNC;
847*4882a593Smuzhiyun 	if (sync & 0x80)
848*4882a593Smuzhiyun 		*status |= FE_HAS_LOCK;		/*Full Sync */
849*4882a593Smuzhiyun 
850*4882a593Smuzhiyun 	return 0;
851*4882a593Smuzhiyun }
852*4882a593Smuzhiyun 
853*4882a593Smuzhiyun /*
854*4882a593Smuzhiyun  * Configured to return the measurement of errors in blocks,
855*4882a593Smuzhiyun  * because no UCBLOCKS value is available, so this value doubles up
856*4882a593Smuzhiyun  * to satisfy both measurements.
857*4882a593Smuzhiyun  */
cx24123_read_ber(struct dvb_frontend * fe,u32 * ber)858*4882a593Smuzhiyun static int cx24123_read_ber(struct dvb_frontend *fe, u32 *ber)
859*4882a593Smuzhiyun {
860*4882a593Smuzhiyun 	struct cx24123_state *state = fe->demodulator_priv;
861*4882a593Smuzhiyun 
862*4882a593Smuzhiyun 	/* The true bit error rate is this value divided by
863*4882a593Smuzhiyun 	   the window size (set as 256 * 255) */
864*4882a593Smuzhiyun 	*ber = ((cx24123_readreg(state, 0x1c) & 0x3f) << 16) |
865*4882a593Smuzhiyun 		(cx24123_readreg(state, 0x1d) << 8 |
866*4882a593Smuzhiyun 		 cx24123_readreg(state, 0x1e));
867*4882a593Smuzhiyun 
868*4882a593Smuzhiyun 	dprintk("BER = %d\n", *ber);
869*4882a593Smuzhiyun 
870*4882a593Smuzhiyun 	return 0;
871*4882a593Smuzhiyun }
872*4882a593Smuzhiyun 
cx24123_read_signal_strength(struct dvb_frontend * fe,u16 * signal_strength)873*4882a593Smuzhiyun static int cx24123_read_signal_strength(struct dvb_frontend *fe,
874*4882a593Smuzhiyun 	u16 *signal_strength)
875*4882a593Smuzhiyun {
876*4882a593Smuzhiyun 	struct cx24123_state *state = fe->demodulator_priv;
877*4882a593Smuzhiyun 
878*4882a593Smuzhiyun 	/* larger = better */
879*4882a593Smuzhiyun 	*signal_strength = cx24123_readreg(state, 0x3b) << 8;
880*4882a593Smuzhiyun 
881*4882a593Smuzhiyun 	dprintk("Signal strength = %d\n", *signal_strength);
882*4882a593Smuzhiyun 
883*4882a593Smuzhiyun 	return 0;
884*4882a593Smuzhiyun }
885*4882a593Smuzhiyun 
cx24123_read_snr(struct dvb_frontend * fe,u16 * snr)886*4882a593Smuzhiyun static int cx24123_read_snr(struct dvb_frontend *fe, u16 *snr)
887*4882a593Smuzhiyun {
888*4882a593Smuzhiyun 	struct cx24123_state *state = fe->demodulator_priv;
889*4882a593Smuzhiyun 
890*4882a593Smuzhiyun 	/* Inverted raw Es/N0 count, totally bogus but better than the
891*4882a593Smuzhiyun 	   BER threshold. */
892*4882a593Smuzhiyun 	*snr = 65535 - (((u16)cx24123_readreg(state, 0x18) << 8) |
893*4882a593Smuzhiyun 			 (u16)cx24123_readreg(state, 0x19));
894*4882a593Smuzhiyun 
895*4882a593Smuzhiyun 	dprintk("read S/N index = %d\n", *snr);
896*4882a593Smuzhiyun 
897*4882a593Smuzhiyun 	return 0;
898*4882a593Smuzhiyun }
899*4882a593Smuzhiyun 
cx24123_set_frontend(struct dvb_frontend * fe)900*4882a593Smuzhiyun static int cx24123_set_frontend(struct dvb_frontend *fe)
901*4882a593Smuzhiyun {
902*4882a593Smuzhiyun 	struct cx24123_state *state = fe->demodulator_priv;
903*4882a593Smuzhiyun 	struct dtv_frontend_properties *p = &fe->dtv_property_cache;
904*4882a593Smuzhiyun 
905*4882a593Smuzhiyun 	dprintk("\n");
906*4882a593Smuzhiyun 
907*4882a593Smuzhiyun 	if (state->config->set_ts_params)
908*4882a593Smuzhiyun 		state->config->set_ts_params(fe, 0);
909*4882a593Smuzhiyun 
910*4882a593Smuzhiyun 	state->currentfreq = p->frequency;
911*4882a593Smuzhiyun 	state->currentsymbolrate = p->symbol_rate;
912*4882a593Smuzhiyun 
913*4882a593Smuzhiyun 	cx24123_set_inversion(state, p->inversion);
914*4882a593Smuzhiyun 	cx24123_set_fec(state, p->fec_inner);
915*4882a593Smuzhiyun 	cx24123_set_symbolrate(state, p->symbol_rate);
916*4882a593Smuzhiyun 
917*4882a593Smuzhiyun 	if (!state->config->dont_use_pll)
918*4882a593Smuzhiyun 		cx24123_pll_tune(fe);
919*4882a593Smuzhiyun 	else if (fe->ops.tuner_ops.set_params)
920*4882a593Smuzhiyun 		fe->ops.tuner_ops.set_params(fe);
921*4882a593Smuzhiyun 	else
922*4882a593Smuzhiyun 		err("it seems I don't have a tuner...");
923*4882a593Smuzhiyun 
924*4882a593Smuzhiyun 	/* Enable automatic acquisition and reset cycle */
925*4882a593Smuzhiyun 	cx24123_writereg(state, 0x03, (cx24123_readreg(state, 0x03) | 0x07));
926*4882a593Smuzhiyun 	cx24123_writereg(state, 0x00, 0x10);
927*4882a593Smuzhiyun 	cx24123_writereg(state, 0x00, 0);
928*4882a593Smuzhiyun 
929*4882a593Smuzhiyun 	if (state->config->agc_callback)
930*4882a593Smuzhiyun 		state->config->agc_callback(fe);
931*4882a593Smuzhiyun 
932*4882a593Smuzhiyun 	return 0;
933*4882a593Smuzhiyun }
934*4882a593Smuzhiyun 
cx24123_get_frontend(struct dvb_frontend * fe,struct dtv_frontend_properties * p)935*4882a593Smuzhiyun static int cx24123_get_frontend(struct dvb_frontend *fe,
936*4882a593Smuzhiyun 				struct dtv_frontend_properties *p)
937*4882a593Smuzhiyun {
938*4882a593Smuzhiyun 	struct cx24123_state *state = fe->demodulator_priv;
939*4882a593Smuzhiyun 
940*4882a593Smuzhiyun 	dprintk("\n");
941*4882a593Smuzhiyun 
942*4882a593Smuzhiyun 	if (cx24123_get_inversion(state, &p->inversion) != 0) {
943*4882a593Smuzhiyun 		err("%s: Failed to get inversion status\n", __func__);
944*4882a593Smuzhiyun 		return -EREMOTEIO;
945*4882a593Smuzhiyun 	}
946*4882a593Smuzhiyun 	if (cx24123_get_fec(state, &p->fec_inner) != 0) {
947*4882a593Smuzhiyun 		err("%s: Failed to get fec status\n", __func__);
948*4882a593Smuzhiyun 		return -EREMOTEIO;
949*4882a593Smuzhiyun 	}
950*4882a593Smuzhiyun 	p->frequency = state->currentfreq;
951*4882a593Smuzhiyun 	p->symbol_rate = state->currentsymbolrate;
952*4882a593Smuzhiyun 
953*4882a593Smuzhiyun 	return 0;
954*4882a593Smuzhiyun }
955*4882a593Smuzhiyun 
cx24123_set_tone(struct dvb_frontend * fe,enum fe_sec_tone_mode tone)956*4882a593Smuzhiyun static int cx24123_set_tone(struct dvb_frontend *fe, enum fe_sec_tone_mode tone)
957*4882a593Smuzhiyun {
958*4882a593Smuzhiyun 	struct cx24123_state *state = fe->demodulator_priv;
959*4882a593Smuzhiyun 	u8 val;
960*4882a593Smuzhiyun 
961*4882a593Smuzhiyun 	/* wait for diseqc queue ready */
962*4882a593Smuzhiyun 	cx24123_wait_for_diseqc(state);
963*4882a593Smuzhiyun 
964*4882a593Smuzhiyun 	val = cx24123_readreg(state, 0x29) & ~0x40;
965*4882a593Smuzhiyun 
966*4882a593Smuzhiyun 	switch (tone) {
967*4882a593Smuzhiyun 	case SEC_TONE_ON:
968*4882a593Smuzhiyun 		dprintk("setting tone on\n");
969*4882a593Smuzhiyun 		return cx24123_writereg(state, 0x29, val | 0x10);
970*4882a593Smuzhiyun 	case SEC_TONE_OFF:
971*4882a593Smuzhiyun 		dprintk("setting tone off\n");
972*4882a593Smuzhiyun 		return cx24123_writereg(state, 0x29, val & 0xef);
973*4882a593Smuzhiyun 	default:
974*4882a593Smuzhiyun 		err("CASE reached default with tone=%d\n", tone);
975*4882a593Smuzhiyun 		return -EINVAL;
976*4882a593Smuzhiyun 	}
977*4882a593Smuzhiyun 
978*4882a593Smuzhiyun 	return 0;
979*4882a593Smuzhiyun }
980*4882a593Smuzhiyun 
cx24123_tune(struct dvb_frontend * fe,bool re_tune,unsigned int mode_flags,unsigned int * delay,enum fe_status * status)981*4882a593Smuzhiyun static int cx24123_tune(struct dvb_frontend *fe,
982*4882a593Smuzhiyun 			bool re_tune,
983*4882a593Smuzhiyun 			unsigned int mode_flags,
984*4882a593Smuzhiyun 			unsigned int *delay,
985*4882a593Smuzhiyun 			enum fe_status *status)
986*4882a593Smuzhiyun {
987*4882a593Smuzhiyun 	int retval = 0;
988*4882a593Smuzhiyun 
989*4882a593Smuzhiyun 	if (re_tune)
990*4882a593Smuzhiyun 		retval = cx24123_set_frontend(fe);
991*4882a593Smuzhiyun 
992*4882a593Smuzhiyun 	if (!(mode_flags & FE_TUNE_MODE_ONESHOT))
993*4882a593Smuzhiyun 		cx24123_read_status(fe, status);
994*4882a593Smuzhiyun 	*delay = HZ/10;
995*4882a593Smuzhiyun 
996*4882a593Smuzhiyun 	return retval;
997*4882a593Smuzhiyun }
998*4882a593Smuzhiyun 
cx24123_get_algo(struct dvb_frontend * fe)999*4882a593Smuzhiyun static enum dvbfe_algo cx24123_get_algo(struct dvb_frontend *fe)
1000*4882a593Smuzhiyun {
1001*4882a593Smuzhiyun 	return DVBFE_ALGO_HW;
1002*4882a593Smuzhiyun }
1003*4882a593Smuzhiyun 
cx24123_release(struct dvb_frontend * fe)1004*4882a593Smuzhiyun static void cx24123_release(struct dvb_frontend *fe)
1005*4882a593Smuzhiyun {
1006*4882a593Smuzhiyun 	struct cx24123_state *state = fe->demodulator_priv;
1007*4882a593Smuzhiyun 	dprintk("\n");
1008*4882a593Smuzhiyun 	i2c_del_adapter(&state->tuner_i2c_adapter);
1009*4882a593Smuzhiyun 	kfree(state);
1010*4882a593Smuzhiyun }
1011*4882a593Smuzhiyun 
cx24123_tuner_i2c_tuner_xfer(struct i2c_adapter * i2c_adap,struct i2c_msg msg[],int num)1012*4882a593Smuzhiyun static int cx24123_tuner_i2c_tuner_xfer(struct i2c_adapter *i2c_adap,
1013*4882a593Smuzhiyun 	struct i2c_msg msg[], int num)
1014*4882a593Smuzhiyun {
1015*4882a593Smuzhiyun 	struct cx24123_state *state = i2c_get_adapdata(i2c_adap);
1016*4882a593Smuzhiyun 	/* this repeater closes after the first stop */
1017*4882a593Smuzhiyun 	cx24123_repeater_mode(state, 1, 1);
1018*4882a593Smuzhiyun 	return i2c_transfer(state->i2c, msg, num);
1019*4882a593Smuzhiyun }
1020*4882a593Smuzhiyun 
cx24123_tuner_i2c_func(struct i2c_adapter * adapter)1021*4882a593Smuzhiyun static u32 cx24123_tuner_i2c_func(struct i2c_adapter *adapter)
1022*4882a593Smuzhiyun {
1023*4882a593Smuzhiyun 	return I2C_FUNC_I2C;
1024*4882a593Smuzhiyun }
1025*4882a593Smuzhiyun 
1026*4882a593Smuzhiyun static const struct i2c_algorithm cx24123_tuner_i2c_algo = {
1027*4882a593Smuzhiyun 	.master_xfer   = cx24123_tuner_i2c_tuner_xfer,
1028*4882a593Smuzhiyun 	.functionality = cx24123_tuner_i2c_func,
1029*4882a593Smuzhiyun };
1030*4882a593Smuzhiyun 
1031*4882a593Smuzhiyun struct i2c_adapter *
cx24123_get_tuner_i2c_adapter(struct dvb_frontend * fe)1032*4882a593Smuzhiyun 	cx24123_get_tuner_i2c_adapter(struct dvb_frontend *fe)
1033*4882a593Smuzhiyun {
1034*4882a593Smuzhiyun 	struct cx24123_state *state = fe->demodulator_priv;
1035*4882a593Smuzhiyun 	return &state->tuner_i2c_adapter;
1036*4882a593Smuzhiyun }
1037*4882a593Smuzhiyun EXPORT_SYMBOL(cx24123_get_tuner_i2c_adapter);
1038*4882a593Smuzhiyun 
1039*4882a593Smuzhiyun static const struct dvb_frontend_ops cx24123_ops;
1040*4882a593Smuzhiyun 
cx24123_attach(const struct cx24123_config * config,struct i2c_adapter * i2c)1041*4882a593Smuzhiyun struct dvb_frontend *cx24123_attach(const struct cx24123_config *config,
1042*4882a593Smuzhiyun 				    struct i2c_adapter *i2c)
1043*4882a593Smuzhiyun {
1044*4882a593Smuzhiyun 	/* allocate memory for the internal state */
1045*4882a593Smuzhiyun 	struct cx24123_state *state =
1046*4882a593Smuzhiyun 		kzalloc(sizeof(struct cx24123_state), GFP_KERNEL);
1047*4882a593Smuzhiyun 
1048*4882a593Smuzhiyun 	dprintk("\n");
1049*4882a593Smuzhiyun 	if (state == NULL) {
1050*4882a593Smuzhiyun 		err("Unable to kzalloc\n");
1051*4882a593Smuzhiyun 		goto error;
1052*4882a593Smuzhiyun 	}
1053*4882a593Smuzhiyun 
1054*4882a593Smuzhiyun 	/* setup the state */
1055*4882a593Smuzhiyun 	state->config = config;
1056*4882a593Smuzhiyun 	state->i2c = i2c;
1057*4882a593Smuzhiyun 
1058*4882a593Smuzhiyun 	/* check if the demod is there */
1059*4882a593Smuzhiyun 	state->demod_rev = cx24123_readreg(state, 0x00);
1060*4882a593Smuzhiyun 	switch (state->demod_rev) {
1061*4882a593Smuzhiyun 	case 0xe1:
1062*4882a593Smuzhiyun 		info("detected CX24123C\n");
1063*4882a593Smuzhiyun 		break;
1064*4882a593Smuzhiyun 	case 0xd1:
1065*4882a593Smuzhiyun 		info("detected CX24123\n");
1066*4882a593Smuzhiyun 		break;
1067*4882a593Smuzhiyun 	default:
1068*4882a593Smuzhiyun 		err("wrong demod revision: %x\n", state->demod_rev);
1069*4882a593Smuzhiyun 		goto error;
1070*4882a593Smuzhiyun 	}
1071*4882a593Smuzhiyun 
1072*4882a593Smuzhiyun 	/* create dvb_frontend */
1073*4882a593Smuzhiyun 	memcpy(&state->frontend.ops, &cx24123_ops,
1074*4882a593Smuzhiyun 		sizeof(struct dvb_frontend_ops));
1075*4882a593Smuzhiyun 	state->frontend.demodulator_priv = state;
1076*4882a593Smuzhiyun 
1077*4882a593Smuzhiyun 	/* create tuner i2c adapter */
1078*4882a593Smuzhiyun 	if (config->dont_use_pll)
1079*4882a593Smuzhiyun 		cx24123_repeater_mode(state, 1, 0);
1080*4882a593Smuzhiyun 
1081*4882a593Smuzhiyun 	strscpy(state->tuner_i2c_adapter.name, "CX24123 tuner I2C bus",
1082*4882a593Smuzhiyun 		sizeof(state->tuner_i2c_adapter.name));
1083*4882a593Smuzhiyun 	state->tuner_i2c_adapter.algo      = &cx24123_tuner_i2c_algo;
1084*4882a593Smuzhiyun 	state->tuner_i2c_adapter.algo_data = NULL;
1085*4882a593Smuzhiyun 	state->tuner_i2c_adapter.dev.parent = i2c->dev.parent;
1086*4882a593Smuzhiyun 	i2c_set_adapdata(&state->tuner_i2c_adapter, state);
1087*4882a593Smuzhiyun 	if (i2c_add_adapter(&state->tuner_i2c_adapter) < 0) {
1088*4882a593Smuzhiyun 		err("tuner i2c bus could not be initialized\n");
1089*4882a593Smuzhiyun 		goto error;
1090*4882a593Smuzhiyun 	}
1091*4882a593Smuzhiyun 
1092*4882a593Smuzhiyun 	return &state->frontend;
1093*4882a593Smuzhiyun 
1094*4882a593Smuzhiyun error:
1095*4882a593Smuzhiyun 	kfree(state);
1096*4882a593Smuzhiyun 
1097*4882a593Smuzhiyun 	return NULL;
1098*4882a593Smuzhiyun }
1099*4882a593Smuzhiyun EXPORT_SYMBOL(cx24123_attach);
1100*4882a593Smuzhiyun 
1101*4882a593Smuzhiyun static const struct dvb_frontend_ops cx24123_ops = {
1102*4882a593Smuzhiyun 	.delsys = { SYS_DVBS },
1103*4882a593Smuzhiyun 	.info = {
1104*4882a593Smuzhiyun 		.name = "Conexant CX24123/CX24109",
1105*4882a593Smuzhiyun 		.frequency_min_hz =  950 * MHz,
1106*4882a593Smuzhiyun 		.frequency_max_hz = 2150 * MHz,
1107*4882a593Smuzhiyun 		.frequency_stepsize_hz = 1011 * kHz,
1108*4882a593Smuzhiyun 		.frequency_tolerance_hz = 5 * MHz,
1109*4882a593Smuzhiyun 		.symbol_rate_min = 1000000,
1110*4882a593Smuzhiyun 		.symbol_rate_max = 45000000,
1111*4882a593Smuzhiyun 		.caps = FE_CAN_INVERSION_AUTO |
1112*4882a593Smuzhiyun 			FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
1113*4882a593Smuzhiyun 			FE_CAN_FEC_4_5 | FE_CAN_FEC_5_6 | FE_CAN_FEC_6_7 |
1114*4882a593Smuzhiyun 			FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
1115*4882a593Smuzhiyun 			FE_CAN_QPSK | FE_CAN_RECOVER
1116*4882a593Smuzhiyun 	},
1117*4882a593Smuzhiyun 
1118*4882a593Smuzhiyun 	.release = cx24123_release,
1119*4882a593Smuzhiyun 
1120*4882a593Smuzhiyun 	.init = cx24123_initfe,
1121*4882a593Smuzhiyun 	.set_frontend = cx24123_set_frontend,
1122*4882a593Smuzhiyun 	.get_frontend = cx24123_get_frontend,
1123*4882a593Smuzhiyun 	.read_status = cx24123_read_status,
1124*4882a593Smuzhiyun 	.read_ber = cx24123_read_ber,
1125*4882a593Smuzhiyun 	.read_signal_strength = cx24123_read_signal_strength,
1126*4882a593Smuzhiyun 	.read_snr = cx24123_read_snr,
1127*4882a593Smuzhiyun 	.diseqc_send_master_cmd = cx24123_send_diseqc_msg,
1128*4882a593Smuzhiyun 	.diseqc_send_burst = cx24123_diseqc_send_burst,
1129*4882a593Smuzhiyun 	.set_tone = cx24123_set_tone,
1130*4882a593Smuzhiyun 	.set_voltage = cx24123_set_voltage,
1131*4882a593Smuzhiyun 	.tune = cx24123_tune,
1132*4882a593Smuzhiyun 	.get_frontend_algo = cx24123_get_algo,
1133*4882a593Smuzhiyun };
1134*4882a593Smuzhiyun 
1135*4882a593Smuzhiyun MODULE_DESCRIPTION("DVB Frontend module for Conexant " \
1136*4882a593Smuzhiyun 	"CX24123/CX24109/CX24113 hardware");
1137*4882a593Smuzhiyun MODULE_AUTHOR("Steven Toth");
1138*4882a593Smuzhiyun MODULE_LICENSE("GPL");
1139*4882a593Smuzhiyun 
1140