1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun Conexant cx24120/cx24118 - DVBS/S2 Satellite demod/tuner driver
4*4882a593Smuzhiyun
5*4882a593Smuzhiyun Copyright (C) 2008 Patrick Boettcher <pb@linuxtv.org>
6*4882a593Smuzhiyun Copyright (C) 2009 Sergey Tyurin <forum.free-x.de>
7*4882a593Smuzhiyun Updated 2012 by Jannis Achstetter <jannis_achstetter@web.de>
8*4882a593Smuzhiyun Copyright (C) 2015 Jemma Denson <jdenson@gmail.com>
9*4882a593Smuzhiyun April 2015
10*4882a593Smuzhiyun Refactored & simplified driver
11*4882a593Smuzhiyun Updated to work with delivery system supplied by DVBv5
12*4882a593Smuzhiyun Add frequency, fec & pilot to get_frontend
13*4882a593Smuzhiyun
14*4882a593Smuzhiyun Cards supported: Technisat Skystar S2
15*4882a593Smuzhiyun
16*4882a593Smuzhiyun */
17*4882a593Smuzhiyun
18*4882a593Smuzhiyun #include <linux/slab.h>
19*4882a593Smuzhiyun #include <linux/kernel.h>
20*4882a593Smuzhiyun #include <linux/module.h>
21*4882a593Smuzhiyun #include <linux/moduleparam.h>
22*4882a593Smuzhiyun #include <linux/init.h>
23*4882a593Smuzhiyun #include <linux/firmware.h>
24*4882a593Smuzhiyun #include <media/dvb_frontend.h>
25*4882a593Smuzhiyun #include "cx24120.h"
26*4882a593Smuzhiyun
27*4882a593Smuzhiyun #define CX24120_SEARCH_RANGE_KHZ 5000
28*4882a593Smuzhiyun #define CX24120_FIRMWARE "dvb-fe-cx24120-1.20.58.2.fw"
29*4882a593Smuzhiyun
30*4882a593Smuzhiyun /* cx24120 i2c registers */
31*4882a593Smuzhiyun #define CX24120_REG_CMD_START 0x00 /* write cmd_id */
32*4882a593Smuzhiyun #define CX24120_REG_CMD_ARGS 0x01 /* write command arguments */
33*4882a593Smuzhiyun #define CX24120_REG_CMD_END 0x1f /* write 0x01 for end */
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun #define CX24120_REG_MAILBOX 0x33
36*4882a593Smuzhiyun #define CX24120_REG_FREQ3 0x34 /* frequency */
37*4882a593Smuzhiyun #define CX24120_REG_FREQ2 0x35
38*4882a593Smuzhiyun #define CX24120_REG_FREQ1 0x36
39*4882a593Smuzhiyun
40*4882a593Smuzhiyun #define CX24120_REG_FECMODE 0x39 /* FEC status */
41*4882a593Smuzhiyun #define CX24120_REG_STATUS 0x3a /* Tuner status */
42*4882a593Smuzhiyun #define CX24120_REG_SIGSTR_H 0x3a /* Signal strength high */
43*4882a593Smuzhiyun #define CX24120_REG_SIGSTR_L 0x3b /* Signal strength low byte */
44*4882a593Smuzhiyun #define CX24120_REG_QUALITY_H 0x40 /* SNR high byte */
45*4882a593Smuzhiyun #define CX24120_REG_QUALITY_L 0x41 /* SNR low byte */
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun #define CX24120_REG_BER_HH 0x47 /* BER high byte of high word */
48*4882a593Smuzhiyun #define CX24120_REG_BER_HL 0x48 /* BER low byte of high word */
49*4882a593Smuzhiyun #define CX24120_REG_BER_LH 0x49 /* BER high byte of low word */
50*4882a593Smuzhiyun #define CX24120_REG_BER_LL 0x4a /* BER low byte of low word */
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun #define CX24120_REG_UCB_H 0x50 /* UCB high byte */
53*4882a593Smuzhiyun #define CX24120_REG_UCB_L 0x51 /* UCB low byte */
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun #define CX24120_REG_CLKDIV 0xe6
56*4882a593Smuzhiyun #define CX24120_REG_RATEDIV 0xf0
57*4882a593Smuzhiyun
58*4882a593Smuzhiyun #define CX24120_REG_REVISION 0xff /* Chip revision (ro) */
59*4882a593Smuzhiyun
60*4882a593Smuzhiyun /* Command messages */
61*4882a593Smuzhiyun enum command_message_id {
62*4882a593Smuzhiyun CMD_VCO_SET = 0x10, /* cmd.len = 12; */
63*4882a593Smuzhiyun CMD_TUNEREQUEST = 0x11, /* cmd.len = 15; */
64*4882a593Smuzhiyun
65*4882a593Smuzhiyun CMD_MPEG_ONOFF = 0x13, /* cmd.len = 4; */
66*4882a593Smuzhiyun CMD_MPEG_INIT = 0x14, /* cmd.len = 7; */
67*4882a593Smuzhiyun CMD_BANDWIDTH = 0x15, /* cmd.len = 12; */
68*4882a593Smuzhiyun CMD_CLOCK_READ = 0x16, /* read clock */
69*4882a593Smuzhiyun CMD_CLOCK_SET = 0x17, /* cmd.len = 10; */
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun CMD_DISEQC_MSG1 = 0x20, /* cmd.len = 11; */
72*4882a593Smuzhiyun CMD_DISEQC_MSG2 = 0x21, /* cmd.len = d->msg_len + 6; */
73*4882a593Smuzhiyun CMD_SETVOLTAGE = 0x22, /* cmd.len = 2; */
74*4882a593Smuzhiyun CMD_SETTONE = 0x23, /* cmd.len = 4; */
75*4882a593Smuzhiyun CMD_DISEQC_BURST = 0x24, /* cmd.len not used !!! */
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun CMD_READ_SNR = 0x1a, /* Read signal strength */
78*4882a593Smuzhiyun CMD_START_TUNER = 0x1b, /* ??? */
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun CMD_FWVERSION = 0x35,
81*4882a593Smuzhiyun
82*4882a593Smuzhiyun CMD_BER_CTRL = 0x3c, /* cmd.len = 0x03; */
83*4882a593Smuzhiyun };
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun #define CX24120_MAX_CMD_LEN 30
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun /* pilot mask */
88*4882a593Smuzhiyun #define CX24120_PILOT_OFF 0x00
89*4882a593Smuzhiyun #define CX24120_PILOT_ON 0x40
90*4882a593Smuzhiyun #define CX24120_PILOT_AUTO 0x80
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun /* signal status */
93*4882a593Smuzhiyun #define CX24120_HAS_SIGNAL 0x01
94*4882a593Smuzhiyun #define CX24120_HAS_CARRIER 0x02
95*4882a593Smuzhiyun #define CX24120_HAS_VITERBI 0x04
96*4882a593Smuzhiyun #define CX24120_HAS_LOCK 0x08
97*4882a593Smuzhiyun #define CX24120_HAS_UNK1 0x10
98*4882a593Smuzhiyun #define CX24120_HAS_UNK2 0x20
99*4882a593Smuzhiyun #define CX24120_STATUS_MASK 0x0f
100*4882a593Smuzhiyun #define CX24120_SIGNAL_MASK 0xc0
101*4882a593Smuzhiyun
102*4882a593Smuzhiyun /* ber window */
103*4882a593Smuzhiyun #define CX24120_BER_WINDOW 16
104*4882a593Smuzhiyun #define CX24120_BER_WSIZE ((1 << CX24120_BER_WINDOW) * 208 * 8)
105*4882a593Smuzhiyun
106*4882a593Smuzhiyun #define info(args...) pr_info("cx24120: " args)
107*4882a593Smuzhiyun #define err(args...) pr_err("cx24120: ### ERROR: " args)
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun /* The Demod/Tuner can't easily provide these, we cache them */
110*4882a593Smuzhiyun struct cx24120_tuning {
111*4882a593Smuzhiyun u32 frequency;
112*4882a593Smuzhiyun u32 symbol_rate;
113*4882a593Smuzhiyun enum fe_spectral_inversion inversion;
114*4882a593Smuzhiyun enum fe_code_rate fec;
115*4882a593Smuzhiyun
116*4882a593Smuzhiyun enum fe_delivery_system delsys;
117*4882a593Smuzhiyun enum fe_modulation modulation;
118*4882a593Smuzhiyun enum fe_pilot pilot;
119*4882a593Smuzhiyun
120*4882a593Smuzhiyun /* Demod values */
121*4882a593Smuzhiyun u8 fec_val;
122*4882a593Smuzhiyun u8 fec_mask;
123*4882a593Smuzhiyun u8 clkdiv;
124*4882a593Smuzhiyun u8 ratediv;
125*4882a593Smuzhiyun u8 inversion_val;
126*4882a593Smuzhiyun u8 pilot_val;
127*4882a593Smuzhiyun };
128*4882a593Smuzhiyun
129*4882a593Smuzhiyun /* Private state */
130*4882a593Smuzhiyun struct cx24120_state {
131*4882a593Smuzhiyun struct i2c_adapter *i2c;
132*4882a593Smuzhiyun const struct cx24120_config *config;
133*4882a593Smuzhiyun struct dvb_frontend frontend;
134*4882a593Smuzhiyun
135*4882a593Smuzhiyun u8 cold_init;
136*4882a593Smuzhiyun u8 mpeg_enabled;
137*4882a593Smuzhiyun u8 need_clock_set;
138*4882a593Smuzhiyun
139*4882a593Smuzhiyun /* current and next tuning parameters */
140*4882a593Smuzhiyun struct cx24120_tuning dcur;
141*4882a593Smuzhiyun struct cx24120_tuning dnxt;
142*4882a593Smuzhiyun
143*4882a593Smuzhiyun enum fe_status fe_status;
144*4882a593Smuzhiyun
145*4882a593Smuzhiyun /* dvbv5 stats calculations */
146*4882a593Smuzhiyun u32 bitrate;
147*4882a593Smuzhiyun u32 berw_usecs;
148*4882a593Smuzhiyun u32 ber_prev;
149*4882a593Smuzhiyun u32 ucb_offset;
150*4882a593Smuzhiyun unsigned long ber_jiffies_stats;
151*4882a593Smuzhiyun unsigned long per_jiffies_stats;
152*4882a593Smuzhiyun };
153*4882a593Smuzhiyun
154*4882a593Smuzhiyun /* Command message to firmware */
155*4882a593Smuzhiyun struct cx24120_cmd {
156*4882a593Smuzhiyun u8 id;
157*4882a593Smuzhiyun u8 len;
158*4882a593Smuzhiyun u8 arg[CX24120_MAX_CMD_LEN];
159*4882a593Smuzhiyun };
160*4882a593Smuzhiyun
161*4882a593Smuzhiyun /* Read single register */
cx24120_readreg(struct cx24120_state * state,u8 reg)162*4882a593Smuzhiyun static int cx24120_readreg(struct cx24120_state *state, u8 reg)
163*4882a593Smuzhiyun {
164*4882a593Smuzhiyun int ret;
165*4882a593Smuzhiyun u8 buf = 0;
166*4882a593Smuzhiyun struct i2c_msg msg[] = {
167*4882a593Smuzhiyun {
168*4882a593Smuzhiyun .addr = state->config->i2c_addr,
169*4882a593Smuzhiyun .flags = 0,
170*4882a593Smuzhiyun .len = 1,
171*4882a593Smuzhiyun .buf = ®
172*4882a593Smuzhiyun }, {
173*4882a593Smuzhiyun .addr = state->config->i2c_addr,
174*4882a593Smuzhiyun .flags = I2C_M_RD,
175*4882a593Smuzhiyun .len = 1,
176*4882a593Smuzhiyun .buf = &buf
177*4882a593Smuzhiyun }
178*4882a593Smuzhiyun };
179*4882a593Smuzhiyun
180*4882a593Smuzhiyun ret = i2c_transfer(state->i2c, msg, 2);
181*4882a593Smuzhiyun if (ret != 2) {
182*4882a593Smuzhiyun err("Read error: reg=0x%02x, ret=%i)\n", reg, ret);
183*4882a593Smuzhiyun return ret;
184*4882a593Smuzhiyun }
185*4882a593Smuzhiyun
186*4882a593Smuzhiyun dev_dbg(&state->i2c->dev, "reg=0x%02x; data=0x%02x\n", reg, buf);
187*4882a593Smuzhiyun
188*4882a593Smuzhiyun return buf;
189*4882a593Smuzhiyun }
190*4882a593Smuzhiyun
191*4882a593Smuzhiyun /* Write single register */
cx24120_writereg(struct cx24120_state * state,u8 reg,u8 data)192*4882a593Smuzhiyun static int cx24120_writereg(struct cx24120_state *state, u8 reg, u8 data)
193*4882a593Smuzhiyun {
194*4882a593Smuzhiyun u8 buf[] = { reg, data };
195*4882a593Smuzhiyun struct i2c_msg msg = {
196*4882a593Smuzhiyun .addr = state->config->i2c_addr,
197*4882a593Smuzhiyun .flags = 0,
198*4882a593Smuzhiyun .buf = buf,
199*4882a593Smuzhiyun .len = 2
200*4882a593Smuzhiyun };
201*4882a593Smuzhiyun int ret;
202*4882a593Smuzhiyun
203*4882a593Smuzhiyun ret = i2c_transfer(state->i2c, &msg, 1);
204*4882a593Smuzhiyun if (ret != 1) {
205*4882a593Smuzhiyun err("Write error: i2c_write error(err == %i, 0x%02x: 0x%02x)\n",
206*4882a593Smuzhiyun ret, reg, data);
207*4882a593Smuzhiyun return ret;
208*4882a593Smuzhiyun }
209*4882a593Smuzhiyun
210*4882a593Smuzhiyun dev_dbg(&state->i2c->dev, "reg=0x%02x; data=0x%02x\n", reg, data);
211*4882a593Smuzhiyun
212*4882a593Smuzhiyun return 0;
213*4882a593Smuzhiyun }
214*4882a593Smuzhiyun
215*4882a593Smuzhiyun /* Write multiple registers in chunks of i2c_wr_max-sized buffers */
cx24120_writeregs(struct cx24120_state * state,u8 reg,const u8 * values,u16 len,u8 incr)216*4882a593Smuzhiyun static int cx24120_writeregs(struct cx24120_state *state,
217*4882a593Smuzhiyun u8 reg, const u8 *values, u16 len, u8 incr)
218*4882a593Smuzhiyun {
219*4882a593Smuzhiyun int ret;
220*4882a593Smuzhiyun u16 max = state->config->i2c_wr_max > 0 ?
221*4882a593Smuzhiyun state->config->i2c_wr_max :
222*4882a593Smuzhiyun len;
223*4882a593Smuzhiyun
224*4882a593Smuzhiyun struct i2c_msg msg = {
225*4882a593Smuzhiyun .addr = state->config->i2c_addr,
226*4882a593Smuzhiyun .flags = 0,
227*4882a593Smuzhiyun };
228*4882a593Smuzhiyun
229*4882a593Smuzhiyun msg.buf = kmalloc(max + 1, GFP_KERNEL);
230*4882a593Smuzhiyun if (!msg.buf)
231*4882a593Smuzhiyun return -ENOMEM;
232*4882a593Smuzhiyun
233*4882a593Smuzhiyun while (len) {
234*4882a593Smuzhiyun msg.buf[0] = reg;
235*4882a593Smuzhiyun msg.len = len > max ? max : len;
236*4882a593Smuzhiyun memcpy(&msg.buf[1], values, msg.len);
237*4882a593Smuzhiyun
238*4882a593Smuzhiyun len -= msg.len; /* data length revers counter */
239*4882a593Smuzhiyun values += msg.len; /* incr data pointer */
240*4882a593Smuzhiyun
241*4882a593Smuzhiyun if (incr)
242*4882a593Smuzhiyun reg += msg.len;
243*4882a593Smuzhiyun msg.len++; /* don't forget the addr byte */
244*4882a593Smuzhiyun
245*4882a593Smuzhiyun ret = i2c_transfer(state->i2c, &msg, 1);
246*4882a593Smuzhiyun if (ret != 1) {
247*4882a593Smuzhiyun err("i2c_write error(err == %i, 0x%02x)\n", ret, reg);
248*4882a593Smuzhiyun goto out;
249*4882a593Smuzhiyun }
250*4882a593Smuzhiyun
251*4882a593Smuzhiyun dev_dbg(&state->i2c->dev, "reg=0x%02x; data=%*ph\n",
252*4882a593Smuzhiyun reg, msg.len - 1, msg.buf + 1);
253*4882a593Smuzhiyun }
254*4882a593Smuzhiyun
255*4882a593Smuzhiyun ret = 0;
256*4882a593Smuzhiyun
257*4882a593Smuzhiyun out:
258*4882a593Smuzhiyun kfree(msg.buf);
259*4882a593Smuzhiyun return ret;
260*4882a593Smuzhiyun }
261*4882a593Smuzhiyun
262*4882a593Smuzhiyun static const struct dvb_frontend_ops cx24120_ops;
263*4882a593Smuzhiyun
cx24120_attach(const struct cx24120_config * config,struct i2c_adapter * i2c)264*4882a593Smuzhiyun struct dvb_frontend *cx24120_attach(const struct cx24120_config *config,
265*4882a593Smuzhiyun struct i2c_adapter *i2c)
266*4882a593Smuzhiyun {
267*4882a593Smuzhiyun struct cx24120_state *state;
268*4882a593Smuzhiyun int demod_rev;
269*4882a593Smuzhiyun
270*4882a593Smuzhiyun info("Conexant cx24120/cx24118 - DVBS/S2 Satellite demod/tuner\n");
271*4882a593Smuzhiyun state = kzalloc(sizeof(*state), GFP_KERNEL);
272*4882a593Smuzhiyun if (!state) {
273*4882a593Smuzhiyun err("Unable to allocate memory for cx24120_state\n");
274*4882a593Smuzhiyun goto error;
275*4882a593Smuzhiyun }
276*4882a593Smuzhiyun
277*4882a593Smuzhiyun /* setup the state */
278*4882a593Smuzhiyun state->config = config;
279*4882a593Smuzhiyun state->i2c = i2c;
280*4882a593Smuzhiyun
281*4882a593Smuzhiyun /* check if the demod is present and has proper type */
282*4882a593Smuzhiyun demod_rev = cx24120_readreg(state, CX24120_REG_REVISION);
283*4882a593Smuzhiyun switch (demod_rev) {
284*4882a593Smuzhiyun case 0x07:
285*4882a593Smuzhiyun info("Demod cx24120 rev. 0x07 detected.\n");
286*4882a593Smuzhiyun break;
287*4882a593Smuzhiyun case 0x05:
288*4882a593Smuzhiyun info("Demod cx24120 rev. 0x05 detected.\n");
289*4882a593Smuzhiyun break;
290*4882a593Smuzhiyun default:
291*4882a593Smuzhiyun err("Unsupported demod revision: 0x%x detected.\n", demod_rev);
292*4882a593Smuzhiyun goto error;
293*4882a593Smuzhiyun }
294*4882a593Smuzhiyun
295*4882a593Smuzhiyun /* create dvb_frontend */
296*4882a593Smuzhiyun state->cold_init = 0;
297*4882a593Smuzhiyun memcpy(&state->frontend.ops, &cx24120_ops,
298*4882a593Smuzhiyun sizeof(struct dvb_frontend_ops));
299*4882a593Smuzhiyun state->frontend.demodulator_priv = state;
300*4882a593Smuzhiyun
301*4882a593Smuzhiyun info("Conexant cx24120/cx24118 attached.\n");
302*4882a593Smuzhiyun return &state->frontend;
303*4882a593Smuzhiyun
304*4882a593Smuzhiyun error:
305*4882a593Smuzhiyun kfree(state);
306*4882a593Smuzhiyun return NULL;
307*4882a593Smuzhiyun }
308*4882a593Smuzhiyun EXPORT_SYMBOL(cx24120_attach);
309*4882a593Smuzhiyun
cx24120_test_rom(struct cx24120_state * state)310*4882a593Smuzhiyun static int cx24120_test_rom(struct cx24120_state *state)
311*4882a593Smuzhiyun {
312*4882a593Smuzhiyun int err, ret;
313*4882a593Smuzhiyun
314*4882a593Smuzhiyun err = cx24120_readreg(state, 0xfd);
315*4882a593Smuzhiyun if (err & 4) {
316*4882a593Smuzhiyun ret = cx24120_readreg(state, 0xdf) & 0xfe;
317*4882a593Smuzhiyun err = cx24120_writereg(state, 0xdf, ret);
318*4882a593Smuzhiyun }
319*4882a593Smuzhiyun return err;
320*4882a593Smuzhiyun }
321*4882a593Smuzhiyun
cx24120_read_snr(struct dvb_frontend * fe,u16 * snr)322*4882a593Smuzhiyun static int cx24120_read_snr(struct dvb_frontend *fe, u16 *snr)
323*4882a593Smuzhiyun {
324*4882a593Smuzhiyun struct dtv_frontend_properties *c = &fe->dtv_property_cache;
325*4882a593Smuzhiyun
326*4882a593Smuzhiyun if (c->cnr.stat[0].scale != FE_SCALE_DECIBEL)
327*4882a593Smuzhiyun *snr = 0;
328*4882a593Smuzhiyun else
329*4882a593Smuzhiyun *snr = div_s64(c->cnr.stat[0].svalue, 100);
330*4882a593Smuzhiyun
331*4882a593Smuzhiyun return 0;
332*4882a593Smuzhiyun }
333*4882a593Smuzhiyun
cx24120_read_ber(struct dvb_frontend * fe,u32 * ber)334*4882a593Smuzhiyun static int cx24120_read_ber(struct dvb_frontend *fe, u32 *ber)
335*4882a593Smuzhiyun {
336*4882a593Smuzhiyun struct cx24120_state *state = fe->demodulator_priv;
337*4882a593Smuzhiyun struct dtv_frontend_properties *c = &fe->dtv_property_cache;
338*4882a593Smuzhiyun
339*4882a593Smuzhiyun if (c->post_bit_error.stat[0].scale != FE_SCALE_COUNTER) {
340*4882a593Smuzhiyun *ber = 0;
341*4882a593Smuzhiyun return 0;
342*4882a593Smuzhiyun }
343*4882a593Smuzhiyun
344*4882a593Smuzhiyun *ber = c->post_bit_error.stat[0].uvalue - state->ber_prev;
345*4882a593Smuzhiyun state->ber_prev = c->post_bit_error.stat[0].uvalue;
346*4882a593Smuzhiyun
347*4882a593Smuzhiyun return 0;
348*4882a593Smuzhiyun }
349*4882a593Smuzhiyun
350*4882a593Smuzhiyun static int cx24120_msg_mpeg_output_global_config(struct cx24120_state *state,
351*4882a593Smuzhiyun u8 flag);
352*4882a593Smuzhiyun
353*4882a593Smuzhiyun /* Check if we're running a command that needs to disable mpeg out */
cx24120_check_cmd(struct cx24120_state * state,u8 id)354*4882a593Smuzhiyun static void cx24120_check_cmd(struct cx24120_state *state, u8 id)
355*4882a593Smuzhiyun {
356*4882a593Smuzhiyun switch (id) {
357*4882a593Smuzhiyun case CMD_TUNEREQUEST:
358*4882a593Smuzhiyun case CMD_CLOCK_READ:
359*4882a593Smuzhiyun case CMD_DISEQC_MSG1:
360*4882a593Smuzhiyun case CMD_DISEQC_MSG2:
361*4882a593Smuzhiyun case CMD_SETVOLTAGE:
362*4882a593Smuzhiyun case CMD_SETTONE:
363*4882a593Smuzhiyun case CMD_DISEQC_BURST:
364*4882a593Smuzhiyun cx24120_msg_mpeg_output_global_config(state, 0);
365*4882a593Smuzhiyun /* Old driver would do a msleep(100) here */
366*4882a593Smuzhiyun default:
367*4882a593Smuzhiyun return;
368*4882a593Smuzhiyun }
369*4882a593Smuzhiyun }
370*4882a593Smuzhiyun
371*4882a593Smuzhiyun /* Send a message to the firmware */
cx24120_message_send(struct cx24120_state * state,struct cx24120_cmd * cmd)372*4882a593Smuzhiyun static int cx24120_message_send(struct cx24120_state *state,
373*4882a593Smuzhiyun struct cx24120_cmd *cmd)
374*4882a593Smuzhiyun {
375*4882a593Smuzhiyun int ficus;
376*4882a593Smuzhiyun
377*4882a593Smuzhiyun if (state->mpeg_enabled) {
378*4882a593Smuzhiyun /* Disable mpeg out on certain commands */
379*4882a593Smuzhiyun cx24120_check_cmd(state, cmd->id);
380*4882a593Smuzhiyun }
381*4882a593Smuzhiyun
382*4882a593Smuzhiyun cx24120_writereg(state, CX24120_REG_CMD_START, cmd->id);
383*4882a593Smuzhiyun cx24120_writeregs(state, CX24120_REG_CMD_ARGS, &cmd->arg[0],
384*4882a593Smuzhiyun cmd->len, 1);
385*4882a593Smuzhiyun cx24120_writereg(state, CX24120_REG_CMD_END, 0x01);
386*4882a593Smuzhiyun
387*4882a593Smuzhiyun ficus = 1000;
388*4882a593Smuzhiyun while (cx24120_readreg(state, CX24120_REG_CMD_END)) {
389*4882a593Smuzhiyun msleep(20);
390*4882a593Smuzhiyun ficus -= 20;
391*4882a593Smuzhiyun if (ficus == 0) {
392*4882a593Smuzhiyun err("Error sending message to firmware\n");
393*4882a593Smuzhiyun return -EREMOTEIO;
394*4882a593Smuzhiyun }
395*4882a593Smuzhiyun }
396*4882a593Smuzhiyun dev_dbg(&state->i2c->dev, "sent message 0x%02x\n", cmd->id);
397*4882a593Smuzhiyun
398*4882a593Smuzhiyun return 0;
399*4882a593Smuzhiyun }
400*4882a593Smuzhiyun
401*4882a593Smuzhiyun /* Send a message and fill arg[] with the results */
cx24120_message_sendrcv(struct cx24120_state * state,struct cx24120_cmd * cmd,u8 numreg)402*4882a593Smuzhiyun static int cx24120_message_sendrcv(struct cx24120_state *state,
403*4882a593Smuzhiyun struct cx24120_cmd *cmd, u8 numreg)
404*4882a593Smuzhiyun {
405*4882a593Smuzhiyun int ret, i;
406*4882a593Smuzhiyun
407*4882a593Smuzhiyun if (numreg > CX24120_MAX_CMD_LEN) {
408*4882a593Smuzhiyun err("Too many registers to read. cmd->reg = %d", numreg);
409*4882a593Smuzhiyun return -EREMOTEIO;
410*4882a593Smuzhiyun }
411*4882a593Smuzhiyun
412*4882a593Smuzhiyun ret = cx24120_message_send(state, cmd);
413*4882a593Smuzhiyun if (ret != 0)
414*4882a593Smuzhiyun return ret;
415*4882a593Smuzhiyun
416*4882a593Smuzhiyun if (!numreg)
417*4882a593Smuzhiyun return 0;
418*4882a593Smuzhiyun
419*4882a593Smuzhiyun /* Read numreg registers starting from register cmd->len */
420*4882a593Smuzhiyun for (i = 0; i < numreg; i++)
421*4882a593Smuzhiyun cmd->arg[i] = cx24120_readreg(state, (cmd->len + i + 1));
422*4882a593Smuzhiyun
423*4882a593Smuzhiyun return 0;
424*4882a593Smuzhiyun }
425*4882a593Smuzhiyun
cx24120_read_signal_strength(struct dvb_frontend * fe,u16 * signal_strength)426*4882a593Smuzhiyun static int cx24120_read_signal_strength(struct dvb_frontend *fe,
427*4882a593Smuzhiyun u16 *signal_strength)
428*4882a593Smuzhiyun {
429*4882a593Smuzhiyun struct dtv_frontend_properties *c = &fe->dtv_property_cache;
430*4882a593Smuzhiyun
431*4882a593Smuzhiyun if (c->strength.stat[0].scale != FE_SCALE_RELATIVE)
432*4882a593Smuzhiyun *signal_strength = 0;
433*4882a593Smuzhiyun else
434*4882a593Smuzhiyun *signal_strength = c->strength.stat[0].uvalue;
435*4882a593Smuzhiyun
436*4882a593Smuzhiyun return 0;
437*4882a593Smuzhiyun }
438*4882a593Smuzhiyun
cx24120_msg_mpeg_output_global_config(struct cx24120_state * state,u8 enable)439*4882a593Smuzhiyun static int cx24120_msg_mpeg_output_global_config(struct cx24120_state *state,
440*4882a593Smuzhiyun u8 enable)
441*4882a593Smuzhiyun {
442*4882a593Smuzhiyun struct cx24120_cmd cmd;
443*4882a593Smuzhiyun int ret;
444*4882a593Smuzhiyun
445*4882a593Smuzhiyun cmd.id = CMD_MPEG_ONOFF;
446*4882a593Smuzhiyun cmd.len = 4;
447*4882a593Smuzhiyun cmd.arg[0] = 0x01;
448*4882a593Smuzhiyun cmd.arg[1] = 0x00;
449*4882a593Smuzhiyun cmd.arg[2] = enable ? 0 : (u8)(-1);
450*4882a593Smuzhiyun cmd.arg[3] = 0x01;
451*4882a593Smuzhiyun
452*4882a593Smuzhiyun ret = cx24120_message_send(state, &cmd);
453*4882a593Smuzhiyun if (ret != 0) {
454*4882a593Smuzhiyun dev_dbg(&state->i2c->dev, "failed to %s MPEG output\n",
455*4882a593Smuzhiyun enable ? "enable" : "disable");
456*4882a593Smuzhiyun return ret;
457*4882a593Smuzhiyun }
458*4882a593Smuzhiyun
459*4882a593Smuzhiyun state->mpeg_enabled = enable;
460*4882a593Smuzhiyun dev_dbg(&state->i2c->dev, "MPEG output %s\n",
461*4882a593Smuzhiyun enable ? "enabled" : "disabled");
462*4882a593Smuzhiyun
463*4882a593Smuzhiyun return 0;
464*4882a593Smuzhiyun }
465*4882a593Smuzhiyun
cx24120_msg_mpeg_output_config(struct cx24120_state * state,u8 seq)466*4882a593Smuzhiyun static int cx24120_msg_mpeg_output_config(struct cx24120_state *state, u8 seq)
467*4882a593Smuzhiyun {
468*4882a593Smuzhiyun struct cx24120_cmd cmd;
469*4882a593Smuzhiyun struct cx24120_initial_mpeg_config i =
470*4882a593Smuzhiyun state->config->initial_mpeg_config;
471*4882a593Smuzhiyun
472*4882a593Smuzhiyun cmd.id = CMD_MPEG_INIT;
473*4882a593Smuzhiyun cmd.len = 7;
474*4882a593Smuzhiyun cmd.arg[0] = seq; /* sequental number - can be 0,1,2 */
475*4882a593Smuzhiyun cmd.arg[1] = ((i.x1 & 0x01) << 1) | ((i.x1 >> 1) & 0x01);
476*4882a593Smuzhiyun cmd.arg[2] = 0x05;
477*4882a593Smuzhiyun cmd.arg[3] = 0x02;
478*4882a593Smuzhiyun cmd.arg[4] = ((i.x2 >> 1) & 0x01);
479*4882a593Smuzhiyun cmd.arg[5] = (i.x2 & 0xf0) | (i.x3 & 0x0f);
480*4882a593Smuzhiyun cmd.arg[6] = 0x10;
481*4882a593Smuzhiyun
482*4882a593Smuzhiyun return cx24120_message_send(state, &cmd);
483*4882a593Smuzhiyun }
484*4882a593Smuzhiyun
cx24120_diseqc_send_burst(struct dvb_frontend * fe,enum fe_sec_mini_cmd burst)485*4882a593Smuzhiyun static int cx24120_diseqc_send_burst(struct dvb_frontend *fe,
486*4882a593Smuzhiyun enum fe_sec_mini_cmd burst)
487*4882a593Smuzhiyun {
488*4882a593Smuzhiyun struct cx24120_state *state = fe->demodulator_priv;
489*4882a593Smuzhiyun struct cx24120_cmd cmd;
490*4882a593Smuzhiyun
491*4882a593Smuzhiyun dev_dbg(&state->i2c->dev, "\n");
492*4882a593Smuzhiyun
493*4882a593Smuzhiyun /*
494*4882a593Smuzhiyun * Yes, cmd.len is set to zero. The old driver
495*4882a593Smuzhiyun * didn't specify any len, but also had a
496*4882a593Smuzhiyun * memset 0 before every use of the cmd struct
497*4882a593Smuzhiyun * which would have set it to zero.
498*4882a593Smuzhiyun * This quite probably needs looking into.
499*4882a593Smuzhiyun */
500*4882a593Smuzhiyun cmd.id = CMD_DISEQC_BURST;
501*4882a593Smuzhiyun cmd.len = 0;
502*4882a593Smuzhiyun cmd.arg[0] = 0x00;
503*4882a593Smuzhiyun cmd.arg[1] = (burst == SEC_MINI_B) ? 0x01 : 0x00;
504*4882a593Smuzhiyun
505*4882a593Smuzhiyun return cx24120_message_send(state, &cmd);
506*4882a593Smuzhiyun }
507*4882a593Smuzhiyun
cx24120_set_tone(struct dvb_frontend * fe,enum fe_sec_tone_mode tone)508*4882a593Smuzhiyun static int cx24120_set_tone(struct dvb_frontend *fe, enum fe_sec_tone_mode tone)
509*4882a593Smuzhiyun {
510*4882a593Smuzhiyun struct cx24120_state *state = fe->demodulator_priv;
511*4882a593Smuzhiyun struct cx24120_cmd cmd;
512*4882a593Smuzhiyun
513*4882a593Smuzhiyun dev_dbg(&state->i2c->dev, "(%d)\n", tone);
514*4882a593Smuzhiyun
515*4882a593Smuzhiyun if ((tone != SEC_TONE_ON) && (tone != SEC_TONE_OFF)) {
516*4882a593Smuzhiyun err("Invalid tone=%d\n", tone);
517*4882a593Smuzhiyun return -EINVAL;
518*4882a593Smuzhiyun }
519*4882a593Smuzhiyun
520*4882a593Smuzhiyun cmd.id = CMD_SETTONE;
521*4882a593Smuzhiyun cmd.len = 4;
522*4882a593Smuzhiyun cmd.arg[0] = 0x00;
523*4882a593Smuzhiyun cmd.arg[1] = 0x00;
524*4882a593Smuzhiyun cmd.arg[2] = 0x00;
525*4882a593Smuzhiyun cmd.arg[3] = (tone == SEC_TONE_ON) ? 0x01 : 0x00;
526*4882a593Smuzhiyun
527*4882a593Smuzhiyun return cx24120_message_send(state, &cmd);
528*4882a593Smuzhiyun }
529*4882a593Smuzhiyun
cx24120_set_voltage(struct dvb_frontend * fe,enum fe_sec_voltage voltage)530*4882a593Smuzhiyun static int cx24120_set_voltage(struct dvb_frontend *fe,
531*4882a593Smuzhiyun enum fe_sec_voltage voltage)
532*4882a593Smuzhiyun {
533*4882a593Smuzhiyun struct cx24120_state *state = fe->demodulator_priv;
534*4882a593Smuzhiyun struct cx24120_cmd cmd;
535*4882a593Smuzhiyun
536*4882a593Smuzhiyun dev_dbg(&state->i2c->dev, "(%d)\n", voltage);
537*4882a593Smuzhiyun
538*4882a593Smuzhiyun cmd.id = CMD_SETVOLTAGE;
539*4882a593Smuzhiyun cmd.len = 2;
540*4882a593Smuzhiyun cmd.arg[0] = 0x00;
541*4882a593Smuzhiyun cmd.arg[1] = (voltage == SEC_VOLTAGE_18) ? 0x01 : 0x00;
542*4882a593Smuzhiyun
543*4882a593Smuzhiyun return cx24120_message_send(state, &cmd);
544*4882a593Smuzhiyun }
545*4882a593Smuzhiyun
cx24120_send_diseqc_msg(struct dvb_frontend * fe,struct dvb_diseqc_master_cmd * d)546*4882a593Smuzhiyun static int cx24120_send_diseqc_msg(struct dvb_frontend *fe,
547*4882a593Smuzhiyun struct dvb_diseqc_master_cmd *d)
548*4882a593Smuzhiyun {
549*4882a593Smuzhiyun struct cx24120_state *state = fe->demodulator_priv;
550*4882a593Smuzhiyun struct cx24120_cmd cmd;
551*4882a593Smuzhiyun int back_count;
552*4882a593Smuzhiyun
553*4882a593Smuzhiyun dev_dbg(&state->i2c->dev, "\n");
554*4882a593Smuzhiyun
555*4882a593Smuzhiyun cmd.id = CMD_DISEQC_MSG1;
556*4882a593Smuzhiyun cmd.len = 11;
557*4882a593Smuzhiyun cmd.arg[0] = 0x00;
558*4882a593Smuzhiyun cmd.arg[1] = 0x00;
559*4882a593Smuzhiyun cmd.arg[2] = 0x03;
560*4882a593Smuzhiyun cmd.arg[3] = 0x16;
561*4882a593Smuzhiyun cmd.arg[4] = 0x28;
562*4882a593Smuzhiyun cmd.arg[5] = 0x01;
563*4882a593Smuzhiyun cmd.arg[6] = 0x01;
564*4882a593Smuzhiyun cmd.arg[7] = 0x14;
565*4882a593Smuzhiyun cmd.arg[8] = 0x19;
566*4882a593Smuzhiyun cmd.arg[9] = 0x14;
567*4882a593Smuzhiyun cmd.arg[10] = 0x1e;
568*4882a593Smuzhiyun
569*4882a593Smuzhiyun if (cx24120_message_send(state, &cmd)) {
570*4882a593Smuzhiyun err("send 1st message(0x%x) failed\n", cmd.id);
571*4882a593Smuzhiyun return -EREMOTEIO;
572*4882a593Smuzhiyun }
573*4882a593Smuzhiyun
574*4882a593Smuzhiyun cmd.id = CMD_DISEQC_MSG2;
575*4882a593Smuzhiyun cmd.len = d->msg_len + 6;
576*4882a593Smuzhiyun cmd.arg[0] = 0x00;
577*4882a593Smuzhiyun cmd.arg[1] = 0x01;
578*4882a593Smuzhiyun cmd.arg[2] = 0x02;
579*4882a593Smuzhiyun cmd.arg[3] = 0x00;
580*4882a593Smuzhiyun cmd.arg[4] = 0x00;
581*4882a593Smuzhiyun cmd.arg[5] = d->msg_len;
582*4882a593Smuzhiyun
583*4882a593Smuzhiyun memcpy(&cmd.arg[6], &d->msg, d->msg_len);
584*4882a593Smuzhiyun
585*4882a593Smuzhiyun if (cx24120_message_send(state, &cmd)) {
586*4882a593Smuzhiyun err("send 2nd message(0x%x) failed\n", cmd.id);
587*4882a593Smuzhiyun return -EREMOTEIO;
588*4882a593Smuzhiyun }
589*4882a593Smuzhiyun
590*4882a593Smuzhiyun back_count = 500;
591*4882a593Smuzhiyun do {
592*4882a593Smuzhiyun if (!(cx24120_readreg(state, 0x93) & 0x01)) {
593*4882a593Smuzhiyun dev_dbg(&state->i2c->dev, "diseqc sequence sent\n");
594*4882a593Smuzhiyun return 0;
595*4882a593Smuzhiyun }
596*4882a593Smuzhiyun msleep(20);
597*4882a593Smuzhiyun back_count -= 20;
598*4882a593Smuzhiyun } while (back_count);
599*4882a593Smuzhiyun
600*4882a593Smuzhiyun err("Too long waiting for diseqc.\n");
601*4882a593Smuzhiyun return -ETIMEDOUT;
602*4882a593Smuzhiyun }
603*4882a593Smuzhiyun
cx24120_get_stats(struct cx24120_state * state)604*4882a593Smuzhiyun static void cx24120_get_stats(struct cx24120_state *state)
605*4882a593Smuzhiyun {
606*4882a593Smuzhiyun struct dvb_frontend *fe = &state->frontend;
607*4882a593Smuzhiyun struct dtv_frontend_properties *c = &fe->dtv_property_cache;
608*4882a593Smuzhiyun struct cx24120_cmd cmd;
609*4882a593Smuzhiyun int ret, cnr, msecs;
610*4882a593Smuzhiyun u16 sig, ucb;
611*4882a593Smuzhiyun u32 ber;
612*4882a593Smuzhiyun
613*4882a593Smuzhiyun dev_dbg(&state->i2c->dev, "\n");
614*4882a593Smuzhiyun
615*4882a593Smuzhiyun /* signal strength */
616*4882a593Smuzhiyun if (state->fe_status & FE_HAS_SIGNAL) {
617*4882a593Smuzhiyun cmd.id = CMD_READ_SNR;
618*4882a593Smuzhiyun cmd.len = 1;
619*4882a593Smuzhiyun cmd.arg[0] = 0x00;
620*4882a593Smuzhiyun
621*4882a593Smuzhiyun ret = cx24120_message_send(state, &cmd);
622*4882a593Smuzhiyun if (ret != 0) {
623*4882a593Smuzhiyun err("error reading signal strength\n");
624*4882a593Smuzhiyun return;
625*4882a593Smuzhiyun }
626*4882a593Smuzhiyun
627*4882a593Smuzhiyun /* raw */
628*4882a593Smuzhiyun sig = cx24120_readreg(state, CX24120_REG_SIGSTR_H) >> 6;
629*4882a593Smuzhiyun sig = sig << 8;
630*4882a593Smuzhiyun sig |= cx24120_readreg(state, CX24120_REG_SIGSTR_L);
631*4882a593Smuzhiyun dev_dbg(&state->i2c->dev,
632*4882a593Smuzhiyun "signal strength from firmware = 0x%x\n", sig);
633*4882a593Smuzhiyun
634*4882a593Smuzhiyun /* cooked */
635*4882a593Smuzhiyun sig = -100 * sig + 94324;
636*4882a593Smuzhiyun
637*4882a593Smuzhiyun c->strength.stat[0].scale = FE_SCALE_RELATIVE;
638*4882a593Smuzhiyun c->strength.stat[0].uvalue = sig;
639*4882a593Smuzhiyun } else {
640*4882a593Smuzhiyun c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
641*4882a593Smuzhiyun }
642*4882a593Smuzhiyun
643*4882a593Smuzhiyun /* CNR */
644*4882a593Smuzhiyun if (state->fe_status & FE_HAS_VITERBI) {
645*4882a593Smuzhiyun cnr = cx24120_readreg(state, CX24120_REG_QUALITY_H) << 8;
646*4882a593Smuzhiyun cnr |= cx24120_readreg(state, CX24120_REG_QUALITY_L);
647*4882a593Smuzhiyun dev_dbg(&state->i2c->dev, "read SNR index = %d\n", cnr);
648*4882a593Smuzhiyun
649*4882a593Smuzhiyun /* guessed - seems about right */
650*4882a593Smuzhiyun cnr = cnr * 100;
651*4882a593Smuzhiyun
652*4882a593Smuzhiyun c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
653*4882a593Smuzhiyun c->cnr.stat[0].svalue = cnr;
654*4882a593Smuzhiyun } else {
655*4882a593Smuzhiyun c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
656*4882a593Smuzhiyun }
657*4882a593Smuzhiyun
658*4882a593Smuzhiyun /* BER & UCB require lock */
659*4882a593Smuzhiyun if (!(state->fe_status & FE_HAS_LOCK)) {
660*4882a593Smuzhiyun c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
661*4882a593Smuzhiyun c->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
662*4882a593Smuzhiyun c->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
663*4882a593Smuzhiyun c->block_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
664*4882a593Smuzhiyun return;
665*4882a593Smuzhiyun }
666*4882a593Smuzhiyun
667*4882a593Smuzhiyun /* BER */
668*4882a593Smuzhiyun if (time_after(jiffies, state->ber_jiffies_stats)) {
669*4882a593Smuzhiyun msecs = (state->berw_usecs + 500) / 1000;
670*4882a593Smuzhiyun state->ber_jiffies_stats = jiffies + msecs_to_jiffies(msecs);
671*4882a593Smuzhiyun
672*4882a593Smuzhiyun ber = cx24120_readreg(state, CX24120_REG_BER_HH) << 24;
673*4882a593Smuzhiyun ber |= cx24120_readreg(state, CX24120_REG_BER_HL) << 16;
674*4882a593Smuzhiyun ber |= cx24120_readreg(state, CX24120_REG_BER_LH) << 8;
675*4882a593Smuzhiyun ber |= cx24120_readreg(state, CX24120_REG_BER_LL);
676*4882a593Smuzhiyun dev_dbg(&state->i2c->dev, "read BER index = %d\n", ber);
677*4882a593Smuzhiyun
678*4882a593Smuzhiyun c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
679*4882a593Smuzhiyun c->post_bit_error.stat[0].uvalue += ber;
680*4882a593Smuzhiyun
681*4882a593Smuzhiyun c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
682*4882a593Smuzhiyun c->post_bit_count.stat[0].uvalue += CX24120_BER_WSIZE;
683*4882a593Smuzhiyun }
684*4882a593Smuzhiyun
685*4882a593Smuzhiyun /* UCB */
686*4882a593Smuzhiyun if (time_after(jiffies, state->per_jiffies_stats)) {
687*4882a593Smuzhiyun state->per_jiffies_stats = jiffies + msecs_to_jiffies(1000);
688*4882a593Smuzhiyun
689*4882a593Smuzhiyun ucb = cx24120_readreg(state, CX24120_REG_UCB_H) << 8;
690*4882a593Smuzhiyun ucb |= cx24120_readreg(state, CX24120_REG_UCB_L);
691*4882a593Smuzhiyun dev_dbg(&state->i2c->dev, "ucblocks = %d\n", ucb);
692*4882a593Smuzhiyun
693*4882a593Smuzhiyun /* handle reset */
694*4882a593Smuzhiyun if (ucb < state->ucb_offset)
695*4882a593Smuzhiyun state->ucb_offset = c->block_error.stat[0].uvalue;
696*4882a593Smuzhiyun
697*4882a593Smuzhiyun c->block_error.stat[0].scale = FE_SCALE_COUNTER;
698*4882a593Smuzhiyun c->block_error.stat[0].uvalue = ucb + state->ucb_offset;
699*4882a593Smuzhiyun
700*4882a593Smuzhiyun c->block_count.stat[0].scale = FE_SCALE_COUNTER;
701*4882a593Smuzhiyun c->block_count.stat[0].uvalue += state->bitrate / 8 / 208;
702*4882a593Smuzhiyun }
703*4882a593Smuzhiyun }
704*4882a593Smuzhiyun
705*4882a593Smuzhiyun static void cx24120_set_clock_ratios(struct dvb_frontend *fe);
706*4882a593Smuzhiyun
707*4882a593Smuzhiyun /* Read current tuning status */
cx24120_read_status(struct dvb_frontend * fe,enum fe_status * status)708*4882a593Smuzhiyun static int cx24120_read_status(struct dvb_frontend *fe, enum fe_status *status)
709*4882a593Smuzhiyun {
710*4882a593Smuzhiyun struct cx24120_state *state = fe->demodulator_priv;
711*4882a593Smuzhiyun int lock;
712*4882a593Smuzhiyun
713*4882a593Smuzhiyun lock = cx24120_readreg(state, CX24120_REG_STATUS);
714*4882a593Smuzhiyun
715*4882a593Smuzhiyun dev_dbg(&state->i2c->dev, "status = 0x%02x\n", lock);
716*4882a593Smuzhiyun
717*4882a593Smuzhiyun *status = 0;
718*4882a593Smuzhiyun
719*4882a593Smuzhiyun if (lock & CX24120_HAS_SIGNAL)
720*4882a593Smuzhiyun *status = FE_HAS_SIGNAL;
721*4882a593Smuzhiyun if (lock & CX24120_HAS_CARRIER)
722*4882a593Smuzhiyun *status |= FE_HAS_CARRIER;
723*4882a593Smuzhiyun if (lock & CX24120_HAS_VITERBI)
724*4882a593Smuzhiyun *status |= FE_HAS_VITERBI | FE_HAS_SYNC;
725*4882a593Smuzhiyun if (lock & CX24120_HAS_LOCK)
726*4882a593Smuzhiyun *status |= FE_HAS_LOCK;
727*4882a593Smuzhiyun
728*4882a593Smuzhiyun /*
729*4882a593Smuzhiyun * TODO: is FE_HAS_SYNC in the right place?
730*4882a593Smuzhiyun * Other cx241xx drivers have this slightly
731*4882a593Smuzhiyun * different
732*4882a593Smuzhiyun */
733*4882a593Smuzhiyun
734*4882a593Smuzhiyun state->fe_status = *status;
735*4882a593Smuzhiyun cx24120_get_stats(state);
736*4882a593Smuzhiyun
737*4882a593Smuzhiyun /* Set the clock once tuned in */
738*4882a593Smuzhiyun if (state->need_clock_set && *status & FE_HAS_LOCK) {
739*4882a593Smuzhiyun /* Set clock ratios */
740*4882a593Smuzhiyun cx24120_set_clock_ratios(fe);
741*4882a593Smuzhiyun
742*4882a593Smuzhiyun /* Old driver would do a msleep(200) here */
743*4882a593Smuzhiyun
744*4882a593Smuzhiyun /* Renable mpeg output */
745*4882a593Smuzhiyun if (!state->mpeg_enabled)
746*4882a593Smuzhiyun cx24120_msg_mpeg_output_global_config(state, 1);
747*4882a593Smuzhiyun
748*4882a593Smuzhiyun state->need_clock_set = 0;
749*4882a593Smuzhiyun }
750*4882a593Smuzhiyun
751*4882a593Smuzhiyun return 0;
752*4882a593Smuzhiyun }
753*4882a593Smuzhiyun
754*4882a593Smuzhiyun /*
755*4882a593Smuzhiyun * FEC & modulation lookup table
756*4882a593Smuzhiyun * Used for decoding the REG_FECMODE register
757*4882a593Smuzhiyun * once tuned in.
758*4882a593Smuzhiyun */
759*4882a593Smuzhiyun struct cx24120_modfec {
760*4882a593Smuzhiyun enum fe_delivery_system delsys;
761*4882a593Smuzhiyun enum fe_modulation mod;
762*4882a593Smuzhiyun enum fe_code_rate fec;
763*4882a593Smuzhiyun u8 val;
764*4882a593Smuzhiyun };
765*4882a593Smuzhiyun
766*4882a593Smuzhiyun static const struct cx24120_modfec modfec_lookup_table[] = {
767*4882a593Smuzhiyun /*delsys mod fec val */
768*4882a593Smuzhiyun { SYS_DVBS, QPSK, FEC_1_2, 0x01 },
769*4882a593Smuzhiyun { SYS_DVBS, QPSK, FEC_2_3, 0x02 },
770*4882a593Smuzhiyun { SYS_DVBS, QPSK, FEC_3_4, 0x03 },
771*4882a593Smuzhiyun { SYS_DVBS, QPSK, FEC_4_5, 0x04 },
772*4882a593Smuzhiyun { SYS_DVBS, QPSK, FEC_5_6, 0x05 },
773*4882a593Smuzhiyun { SYS_DVBS, QPSK, FEC_6_7, 0x06 },
774*4882a593Smuzhiyun { SYS_DVBS, QPSK, FEC_7_8, 0x07 },
775*4882a593Smuzhiyun
776*4882a593Smuzhiyun { SYS_DVBS2, QPSK, FEC_1_2, 0x04 },
777*4882a593Smuzhiyun { SYS_DVBS2, QPSK, FEC_3_5, 0x05 },
778*4882a593Smuzhiyun { SYS_DVBS2, QPSK, FEC_2_3, 0x06 },
779*4882a593Smuzhiyun { SYS_DVBS2, QPSK, FEC_3_4, 0x07 },
780*4882a593Smuzhiyun { SYS_DVBS2, QPSK, FEC_4_5, 0x08 },
781*4882a593Smuzhiyun { SYS_DVBS2, QPSK, FEC_5_6, 0x09 },
782*4882a593Smuzhiyun { SYS_DVBS2, QPSK, FEC_8_9, 0x0a },
783*4882a593Smuzhiyun { SYS_DVBS2, QPSK, FEC_9_10, 0x0b },
784*4882a593Smuzhiyun
785*4882a593Smuzhiyun { SYS_DVBS2, PSK_8, FEC_3_5, 0x0c },
786*4882a593Smuzhiyun { SYS_DVBS2, PSK_8, FEC_2_3, 0x0d },
787*4882a593Smuzhiyun { SYS_DVBS2, PSK_8, FEC_3_4, 0x0e },
788*4882a593Smuzhiyun { SYS_DVBS2, PSK_8, FEC_5_6, 0x0f },
789*4882a593Smuzhiyun { SYS_DVBS2, PSK_8, FEC_8_9, 0x10 },
790*4882a593Smuzhiyun { SYS_DVBS2, PSK_8, FEC_9_10, 0x11 },
791*4882a593Smuzhiyun };
792*4882a593Smuzhiyun
793*4882a593Smuzhiyun /* Retrieve current fec, modulation & pilot values */
cx24120_get_fec(struct dvb_frontend * fe)794*4882a593Smuzhiyun static int cx24120_get_fec(struct dvb_frontend *fe)
795*4882a593Smuzhiyun {
796*4882a593Smuzhiyun struct dtv_frontend_properties *c = &fe->dtv_property_cache;
797*4882a593Smuzhiyun struct cx24120_state *state = fe->demodulator_priv;
798*4882a593Smuzhiyun int idx;
799*4882a593Smuzhiyun int ret;
800*4882a593Smuzhiyun int fec;
801*4882a593Smuzhiyun
802*4882a593Smuzhiyun ret = cx24120_readreg(state, CX24120_REG_FECMODE);
803*4882a593Smuzhiyun fec = ret & 0x3f; /* Lower 6 bits */
804*4882a593Smuzhiyun
805*4882a593Smuzhiyun dev_dbg(&state->i2c->dev, "raw fec = %d\n", fec);
806*4882a593Smuzhiyun
807*4882a593Smuzhiyun for (idx = 0; idx < ARRAY_SIZE(modfec_lookup_table); idx++) {
808*4882a593Smuzhiyun if (modfec_lookup_table[idx].delsys != state->dcur.delsys)
809*4882a593Smuzhiyun continue;
810*4882a593Smuzhiyun if (modfec_lookup_table[idx].val != fec)
811*4882a593Smuzhiyun continue;
812*4882a593Smuzhiyun
813*4882a593Smuzhiyun break; /* found */
814*4882a593Smuzhiyun }
815*4882a593Smuzhiyun
816*4882a593Smuzhiyun if (idx >= ARRAY_SIZE(modfec_lookup_table)) {
817*4882a593Smuzhiyun dev_dbg(&state->i2c->dev, "couldn't find fec!\n");
818*4882a593Smuzhiyun return -EINVAL;
819*4882a593Smuzhiyun }
820*4882a593Smuzhiyun
821*4882a593Smuzhiyun /* save values back to cache */
822*4882a593Smuzhiyun c->modulation = modfec_lookup_table[idx].mod;
823*4882a593Smuzhiyun c->fec_inner = modfec_lookup_table[idx].fec;
824*4882a593Smuzhiyun c->pilot = (ret & 0x80) ? PILOT_ON : PILOT_OFF;
825*4882a593Smuzhiyun
826*4882a593Smuzhiyun dev_dbg(&state->i2c->dev, "mod(%d), fec(%d), pilot(%d)\n",
827*4882a593Smuzhiyun c->modulation, c->fec_inner, c->pilot);
828*4882a593Smuzhiyun
829*4882a593Smuzhiyun return 0;
830*4882a593Smuzhiyun }
831*4882a593Smuzhiyun
832*4882a593Smuzhiyun /* Calculate ber window time */
cx24120_calculate_ber_window(struct cx24120_state * state,u32 rate)833*4882a593Smuzhiyun static void cx24120_calculate_ber_window(struct cx24120_state *state, u32 rate)
834*4882a593Smuzhiyun {
835*4882a593Smuzhiyun struct dvb_frontend *fe = &state->frontend;
836*4882a593Smuzhiyun struct dtv_frontend_properties *c = &fe->dtv_property_cache;
837*4882a593Smuzhiyun u64 tmp;
838*4882a593Smuzhiyun
839*4882a593Smuzhiyun /*
840*4882a593Smuzhiyun * Calculate bitrate from rate in the clock ratios table.
841*4882a593Smuzhiyun * This isn't *exactly* right but close enough.
842*4882a593Smuzhiyun */
843*4882a593Smuzhiyun tmp = (u64)c->symbol_rate * rate;
844*4882a593Smuzhiyun do_div(tmp, 256);
845*4882a593Smuzhiyun state->bitrate = tmp;
846*4882a593Smuzhiyun
847*4882a593Smuzhiyun /* usecs per ber window */
848*4882a593Smuzhiyun tmp = 1000000ULL * CX24120_BER_WSIZE;
849*4882a593Smuzhiyun do_div(tmp, state->bitrate);
850*4882a593Smuzhiyun state->berw_usecs = tmp;
851*4882a593Smuzhiyun
852*4882a593Smuzhiyun dev_dbg(&state->i2c->dev, "bitrate: %u, berw_usecs: %u\n",
853*4882a593Smuzhiyun state->bitrate, state->berw_usecs);
854*4882a593Smuzhiyun }
855*4882a593Smuzhiyun
856*4882a593Smuzhiyun /*
857*4882a593Smuzhiyun * Clock ratios lookup table
858*4882a593Smuzhiyun *
859*4882a593Smuzhiyun * Values obtained from much larger table in old driver
860*4882a593Smuzhiyun * which had numerous entries which would never match.
861*4882a593Smuzhiyun *
862*4882a593Smuzhiyun * There's probably some way of calculating these but I
863*4882a593Smuzhiyun * can't determine the pattern
864*4882a593Smuzhiyun */
865*4882a593Smuzhiyun struct cx24120_clock_ratios_table {
866*4882a593Smuzhiyun enum fe_delivery_system delsys;
867*4882a593Smuzhiyun enum fe_pilot pilot;
868*4882a593Smuzhiyun enum fe_modulation mod;
869*4882a593Smuzhiyun enum fe_code_rate fec;
870*4882a593Smuzhiyun u32 m_rat;
871*4882a593Smuzhiyun u32 n_rat;
872*4882a593Smuzhiyun u32 rate;
873*4882a593Smuzhiyun };
874*4882a593Smuzhiyun
875*4882a593Smuzhiyun static const struct cx24120_clock_ratios_table clock_ratios_table[] = {
876*4882a593Smuzhiyun /*delsys pilot mod fec m_rat n_rat rate */
877*4882a593Smuzhiyun { SYS_DVBS2, PILOT_OFF, QPSK, FEC_1_2, 273088, 254505, 274 },
878*4882a593Smuzhiyun { SYS_DVBS2, PILOT_OFF, QPSK, FEC_3_5, 17272, 13395, 330 },
879*4882a593Smuzhiyun { SYS_DVBS2, PILOT_OFF, QPSK, FEC_2_3, 24344, 16967, 367 },
880*4882a593Smuzhiyun { SYS_DVBS2, PILOT_OFF, QPSK, FEC_3_4, 410788, 254505, 413 },
881*4882a593Smuzhiyun { SYS_DVBS2, PILOT_OFF, QPSK, FEC_4_5, 438328, 254505, 440 },
882*4882a593Smuzhiyun { SYS_DVBS2, PILOT_OFF, QPSK, FEC_5_6, 30464, 16967, 459 },
883*4882a593Smuzhiyun { SYS_DVBS2, PILOT_OFF, QPSK, FEC_8_9, 487832, 254505, 490 },
884*4882a593Smuzhiyun { SYS_DVBS2, PILOT_OFF, QPSK, FEC_9_10, 493952, 254505, 496 },
885*4882a593Smuzhiyun { SYS_DVBS2, PILOT_OFF, PSK_8, FEC_3_5, 328168, 169905, 494 },
886*4882a593Smuzhiyun { SYS_DVBS2, PILOT_OFF, PSK_8, FEC_2_3, 24344, 11327, 550 },
887*4882a593Smuzhiyun { SYS_DVBS2, PILOT_OFF, PSK_8, FEC_3_4, 410788, 169905, 618 },
888*4882a593Smuzhiyun { SYS_DVBS2, PILOT_OFF, PSK_8, FEC_5_6, 30464, 11327, 688 },
889*4882a593Smuzhiyun { SYS_DVBS2, PILOT_OFF, PSK_8, FEC_8_9, 487832, 169905, 735 },
890*4882a593Smuzhiyun { SYS_DVBS2, PILOT_OFF, PSK_8, FEC_9_10, 493952, 169905, 744 },
891*4882a593Smuzhiyun { SYS_DVBS2, PILOT_ON, QPSK, FEC_1_2, 273088, 260709, 268 },
892*4882a593Smuzhiyun { SYS_DVBS2, PILOT_ON, QPSK, FEC_3_5, 328168, 260709, 322 },
893*4882a593Smuzhiyun { SYS_DVBS2, PILOT_ON, QPSK, FEC_2_3, 121720, 86903, 358 },
894*4882a593Smuzhiyun { SYS_DVBS2, PILOT_ON, QPSK, FEC_3_4, 410788, 260709, 403 },
895*4882a593Smuzhiyun { SYS_DVBS2, PILOT_ON, QPSK, FEC_4_5, 438328, 260709, 430 },
896*4882a593Smuzhiyun { SYS_DVBS2, PILOT_ON, QPSK, FEC_5_6, 152320, 86903, 448 },
897*4882a593Smuzhiyun { SYS_DVBS2, PILOT_ON, QPSK, FEC_8_9, 487832, 260709, 479 },
898*4882a593Smuzhiyun { SYS_DVBS2, PILOT_ON, QPSK, FEC_9_10, 493952, 260709, 485 },
899*4882a593Smuzhiyun { SYS_DVBS2, PILOT_ON, PSK_8, FEC_3_5, 328168, 173853, 483 },
900*4882a593Smuzhiyun { SYS_DVBS2, PILOT_ON, PSK_8, FEC_2_3, 121720, 57951, 537 },
901*4882a593Smuzhiyun { SYS_DVBS2, PILOT_ON, PSK_8, FEC_3_4, 410788, 173853, 604 },
902*4882a593Smuzhiyun { SYS_DVBS2, PILOT_ON, PSK_8, FEC_5_6, 152320, 57951, 672 },
903*4882a593Smuzhiyun { SYS_DVBS2, PILOT_ON, PSK_8, FEC_8_9, 487832, 173853, 718 },
904*4882a593Smuzhiyun { SYS_DVBS2, PILOT_ON, PSK_8, FEC_9_10, 493952, 173853, 727 },
905*4882a593Smuzhiyun { SYS_DVBS, PILOT_OFF, QPSK, FEC_1_2, 152592, 152592, 256 },
906*4882a593Smuzhiyun { SYS_DVBS, PILOT_OFF, QPSK, FEC_2_3, 305184, 228888, 341 },
907*4882a593Smuzhiyun { SYS_DVBS, PILOT_OFF, QPSK, FEC_3_4, 457776, 305184, 384 },
908*4882a593Smuzhiyun { SYS_DVBS, PILOT_OFF, QPSK, FEC_5_6, 762960, 457776, 427 },
909*4882a593Smuzhiyun { SYS_DVBS, PILOT_OFF, QPSK, FEC_7_8, 1068144, 610368, 448 },
910*4882a593Smuzhiyun };
911*4882a593Smuzhiyun
912*4882a593Smuzhiyun /* Set clock ratio from lookup table */
cx24120_set_clock_ratios(struct dvb_frontend * fe)913*4882a593Smuzhiyun static void cx24120_set_clock_ratios(struct dvb_frontend *fe)
914*4882a593Smuzhiyun {
915*4882a593Smuzhiyun struct dtv_frontend_properties *c = &fe->dtv_property_cache;
916*4882a593Smuzhiyun struct cx24120_state *state = fe->demodulator_priv;
917*4882a593Smuzhiyun struct cx24120_cmd cmd;
918*4882a593Smuzhiyun int ret, idx;
919*4882a593Smuzhiyun
920*4882a593Smuzhiyun /* Find fec, modulation, pilot */
921*4882a593Smuzhiyun ret = cx24120_get_fec(fe);
922*4882a593Smuzhiyun if (ret != 0)
923*4882a593Smuzhiyun return;
924*4882a593Smuzhiyun
925*4882a593Smuzhiyun /* Find the clock ratios in the lookup table */
926*4882a593Smuzhiyun for (idx = 0; idx < ARRAY_SIZE(clock_ratios_table); idx++) {
927*4882a593Smuzhiyun if (clock_ratios_table[idx].delsys != state->dcur.delsys)
928*4882a593Smuzhiyun continue;
929*4882a593Smuzhiyun if (clock_ratios_table[idx].mod != c->modulation)
930*4882a593Smuzhiyun continue;
931*4882a593Smuzhiyun if (clock_ratios_table[idx].fec != c->fec_inner)
932*4882a593Smuzhiyun continue;
933*4882a593Smuzhiyun if (clock_ratios_table[idx].pilot != c->pilot)
934*4882a593Smuzhiyun continue;
935*4882a593Smuzhiyun
936*4882a593Smuzhiyun break; /* found */
937*4882a593Smuzhiyun }
938*4882a593Smuzhiyun
939*4882a593Smuzhiyun if (idx >= ARRAY_SIZE(clock_ratios_table)) {
940*4882a593Smuzhiyun info("Clock ratio not found - data reception in danger\n");
941*4882a593Smuzhiyun return;
942*4882a593Smuzhiyun }
943*4882a593Smuzhiyun
944*4882a593Smuzhiyun /* Read current values? */
945*4882a593Smuzhiyun cmd.id = CMD_CLOCK_READ;
946*4882a593Smuzhiyun cmd.len = 1;
947*4882a593Smuzhiyun cmd.arg[0] = 0x00;
948*4882a593Smuzhiyun ret = cx24120_message_sendrcv(state, &cmd, 6);
949*4882a593Smuzhiyun if (ret != 0)
950*4882a593Smuzhiyun return;
951*4882a593Smuzhiyun /* in cmd[0]-[5] - result */
952*4882a593Smuzhiyun
953*4882a593Smuzhiyun dev_dbg(&state->i2c->dev, "m=%d, n=%d; idx: %d m=%d, n=%d, rate=%d\n",
954*4882a593Smuzhiyun cmd.arg[2] | (cmd.arg[1] << 8) | (cmd.arg[0] << 16),
955*4882a593Smuzhiyun cmd.arg[5] | (cmd.arg[4] << 8) | (cmd.arg[3] << 16),
956*4882a593Smuzhiyun idx,
957*4882a593Smuzhiyun clock_ratios_table[idx].m_rat,
958*4882a593Smuzhiyun clock_ratios_table[idx].n_rat,
959*4882a593Smuzhiyun clock_ratios_table[idx].rate);
960*4882a593Smuzhiyun
961*4882a593Smuzhiyun /* Set the clock */
962*4882a593Smuzhiyun cmd.id = CMD_CLOCK_SET;
963*4882a593Smuzhiyun cmd.len = 10;
964*4882a593Smuzhiyun cmd.arg[0] = 0;
965*4882a593Smuzhiyun cmd.arg[1] = 0x10;
966*4882a593Smuzhiyun cmd.arg[2] = (clock_ratios_table[idx].m_rat >> 16) & 0xff;
967*4882a593Smuzhiyun cmd.arg[3] = (clock_ratios_table[idx].m_rat >> 8) & 0xff;
968*4882a593Smuzhiyun cmd.arg[4] = (clock_ratios_table[idx].m_rat >> 0) & 0xff;
969*4882a593Smuzhiyun cmd.arg[5] = (clock_ratios_table[idx].n_rat >> 16) & 0xff;
970*4882a593Smuzhiyun cmd.arg[6] = (clock_ratios_table[idx].n_rat >> 8) & 0xff;
971*4882a593Smuzhiyun cmd.arg[7] = (clock_ratios_table[idx].n_rat >> 0) & 0xff;
972*4882a593Smuzhiyun cmd.arg[8] = (clock_ratios_table[idx].rate >> 8) & 0xff;
973*4882a593Smuzhiyun cmd.arg[9] = (clock_ratios_table[idx].rate >> 0) & 0xff;
974*4882a593Smuzhiyun
975*4882a593Smuzhiyun cx24120_message_send(state, &cmd);
976*4882a593Smuzhiyun
977*4882a593Smuzhiyun /* Calculate ber window rates for stat work */
978*4882a593Smuzhiyun cx24120_calculate_ber_window(state, clock_ratios_table[idx].rate);
979*4882a593Smuzhiyun }
980*4882a593Smuzhiyun
981*4882a593Smuzhiyun /* Set inversion value */
cx24120_set_inversion(struct cx24120_state * state,enum fe_spectral_inversion inversion)982*4882a593Smuzhiyun static int cx24120_set_inversion(struct cx24120_state *state,
983*4882a593Smuzhiyun enum fe_spectral_inversion inversion)
984*4882a593Smuzhiyun {
985*4882a593Smuzhiyun dev_dbg(&state->i2c->dev, "(%d)\n", inversion);
986*4882a593Smuzhiyun
987*4882a593Smuzhiyun switch (inversion) {
988*4882a593Smuzhiyun case INVERSION_OFF:
989*4882a593Smuzhiyun state->dnxt.inversion_val = 0x00;
990*4882a593Smuzhiyun break;
991*4882a593Smuzhiyun case INVERSION_ON:
992*4882a593Smuzhiyun state->dnxt.inversion_val = 0x04;
993*4882a593Smuzhiyun break;
994*4882a593Smuzhiyun case INVERSION_AUTO:
995*4882a593Smuzhiyun state->dnxt.inversion_val = 0x0c;
996*4882a593Smuzhiyun break;
997*4882a593Smuzhiyun default:
998*4882a593Smuzhiyun return -EINVAL;
999*4882a593Smuzhiyun }
1000*4882a593Smuzhiyun
1001*4882a593Smuzhiyun state->dnxt.inversion = inversion;
1002*4882a593Smuzhiyun
1003*4882a593Smuzhiyun return 0;
1004*4882a593Smuzhiyun }
1005*4882a593Smuzhiyun
1006*4882a593Smuzhiyun /* FEC lookup table for tuning */
1007*4882a593Smuzhiyun struct cx24120_modfec_table {
1008*4882a593Smuzhiyun enum fe_delivery_system delsys;
1009*4882a593Smuzhiyun enum fe_modulation mod;
1010*4882a593Smuzhiyun enum fe_code_rate fec;
1011*4882a593Smuzhiyun u8 val;
1012*4882a593Smuzhiyun };
1013*4882a593Smuzhiyun
1014*4882a593Smuzhiyun static const struct cx24120_modfec_table modfec_table[] = {
1015*4882a593Smuzhiyun /*delsys mod fec val */
1016*4882a593Smuzhiyun { SYS_DVBS, QPSK, FEC_1_2, 0x2e },
1017*4882a593Smuzhiyun { SYS_DVBS, QPSK, FEC_2_3, 0x2f },
1018*4882a593Smuzhiyun { SYS_DVBS, QPSK, FEC_3_4, 0x30 },
1019*4882a593Smuzhiyun { SYS_DVBS, QPSK, FEC_5_6, 0x31 },
1020*4882a593Smuzhiyun { SYS_DVBS, QPSK, FEC_6_7, 0x32 },
1021*4882a593Smuzhiyun { SYS_DVBS, QPSK, FEC_7_8, 0x33 },
1022*4882a593Smuzhiyun
1023*4882a593Smuzhiyun { SYS_DVBS2, QPSK, FEC_1_2, 0x04 },
1024*4882a593Smuzhiyun { SYS_DVBS2, QPSK, FEC_3_5, 0x05 },
1025*4882a593Smuzhiyun { SYS_DVBS2, QPSK, FEC_2_3, 0x06 },
1026*4882a593Smuzhiyun { SYS_DVBS2, QPSK, FEC_3_4, 0x07 },
1027*4882a593Smuzhiyun { SYS_DVBS2, QPSK, FEC_4_5, 0x08 },
1028*4882a593Smuzhiyun { SYS_DVBS2, QPSK, FEC_5_6, 0x09 },
1029*4882a593Smuzhiyun { SYS_DVBS2, QPSK, FEC_8_9, 0x0a },
1030*4882a593Smuzhiyun { SYS_DVBS2, QPSK, FEC_9_10, 0x0b },
1031*4882a593Smuzhiyun
1032*4882a593Smuzhiyun { SYS_DVBS2, PSK_8, FEC_3_5, 0x0c },
1033*4882a593Smuzhiyun { SYS_DVBS2, PSK_8, FEC_2_3, 0x0d },
1034*4882a593Smuzhiyun { SYS_DVBS2, PSK_8, FEC_3_4, 0x0e },
1035*4882a593Smuzhiyun { SYS_DVBS2, PSK_8, FEC_5_6, 0x0f },
1036*4882a593Smuzhiyun { SYS_DVBS2, PSK_8, FEC_8_9, 0x10 },
1037*4882a593Smuzhiyun { SYS_DVBS2, PSK_8, FEC_9_10, 0x11 },
1038*4882a593Smuzhiyun };
1039*4882a593Smuzhiyun
1040*4882a593Smuzhiyun /* Set fec_val & fec_mask values from delsys, modulation & fec */
cx24120_set_fec(struct cx24120_state * state,enum fe_modulation mod,enum fe_code_rate fec)1041*4882a593Smuzhiyun static int cx24120_set_fec(struct cx24120_state *state, enum fe_modulation mod,
1042*4882a593Smuzhiyun enum fe_code_rate fec)
1043*4882a593Smuzhiyun {
1044*4882a593Smuzhiyun int idx;
1045*4882a593Smuzhiyun
1046*4882a593Smuzhiyun dev_dbg(&state->i2c->dev, "(0x%02x,0x%02x)\n", mod, fec);
1047*4882a593Smuzhiyun
1048*4882a593Smuzhiyun state->dnxt.fec = fec;
1049*4882a593Smuzhiyun
1050*4882a593Smuzhiyun /* Lookup fec_val from modfec table */
1051*4882a593Smuzhiyun for (idx = 0; idx < ARRAY_SIZE(modfec_table); idx++) {
1052*4882a593Smuzhiyun if (modfec_table[idx].delsys != state->dnxt.delsys)
1053*4882a593Smuzhiyun continue;
1054*4882a593Smuzhiyun if (modfec_table[idx].mod != mod)
1055*4882a593Smuzhiyun continue;
1056*4882a593Smuzhiyun if (modfec_table[idx].fec != fec)
1057*4882a593Smuzhiyun continue;
1058*4882a593Smuzhiyun
1059*4882a593Smuzhiyun /* found */
1060*4882a593Smuzhiyun state->dnxt.fec_mask = 0x00;
1061*4882a593Smuzhiyun state->dnxt.fec_val = modfec_table[idx].val;
1062*4882a593Smuzhiyun return 0;
1063*4882a593Smuzhiyun }
1064*4882a593Smuzhiyun
1065*4882a593Smuzhiyun if (state->dnxt.delsys == SYS_DVBS2) {
1066*4882a593Smuzhiyun /* DVBS2 auto is 0x00/0x00 */
1067*4882a593Smuzhiyun state->dnxt.fec_mask = 0x00;
1068*4882a593Smuzhiyun state->dnxt.fec_val = 0x00;
1069*4882a593Smuzhiyun } else {
1070*4882a593Smuzhiyun /* Set DVB-S to auto */
1071*4882a593Smuzhiyun state->dnxt.fec_val = 0x2e;
1072*4882a593Smuzhiyun state->dnxt.fec_mask = 0xac;
1073*4882a593Smuzhiyun }
1074*4882a593Smuzhiyun
1075*4882a593Smuzhiyun return 0;
1076*4882a593Smuzhiyun }
1077*4882a593Smuzhiyun
1078*4882a593Smuzhiyun /* Set pilot */
cx24120_set_pilot(struct cx24120_state * state,enum fe_pilot pilot)1079*4882a593Smuzhiyun static int cx24120_set_pilot(struct cx24120_state *state, enum fe_pilot pilot)
1080*4882a593Smuzhiyun {
1081*4882a593Smuzhiyun dev_dbg(&state->i2c->dev, "(%d)\n", pilot);
1082*4882a593Smuzhiyun
1083*4882a593Smuzhiyun /* Pilot only valid in DVBS2 */
1084*4882a593Smuzhiyun if (state->dnxt.delsys != SYS_DVBS2) {
1085*4882a593Smuzhiyun state->dnxt.pilot_val = CX24120_PILOT_OFF;
1086*4882a593Smuzhiyun return 0;
1087*4882a593Smuzhiyun }
1088*4882a593Smuzhiyun
1089*4882a593Smuzhiyun switch (pilot) {
1090*4882a593Smuzhiyun case PILOT_OFF:
1091*4882a593Smuzhiyun state->dnxt.pilot_val = CX24120_PILOT_OFF;
1092*4882a593Smuzhiyun break;
1093*4882a593Smuzhiyun case PILOT_ON:
1094*4882a593Smuzhiyun state->dnxt.pilot_val = CX24120_PILOT_ON;
1095*4882a593Smuzhiyun break;
1096*4882a593Smuzhiyun case PILOT_AUTO:
1097*4882a593Smuzhiyun default:
1098*4882a593Smuzhiyun state->dnxt.pilot_val = CX24120_PILOT_AUTO;
1099*4882a593Smuzhiyun }
1100*4882a593Smuzhiyun
1101*4882a593Smuzhiyun return 0;
1102*4882a593Smuzhiyun }
1103*4882a593Smuzhiyun
1104*4882a593Smuzhiyun /* Set symbol rate */
cx24120_set_symbolrate(struct cx24120_state * state,u32 rate)1105*4882a593Smuzhiyun static int cx24120_set_symbolrate(struct cx24120_state *state, u32 rate)
1106*4882a593Smuzhiyun {
1107*4882a593Smuzhiyun dev_dbg(&state->i2c->dev, "(%d)\n", rate);
1108*4882a593Smuzhiyun
1109*4882a593Smuzhiyun state->dnxt.symbol_rate = rate;
1110*4882a593Smuzhiyun
1111*4882a593Smuzhiyun /* Check symbol rate */
1112*4882a593Smuzhiyun if (rate > 31000000) {
1113*4882a593Smuzhiyun state->dnxt.clkdiv = (-(rate < 31000001) & 3) + 2;
1114*4882a593Smuzhiyun state->dnxt.ratediv = (-(rate < 31000001) & 6) + 4;
1115*4882a593Smuzhiyun } else {
1116*4882a593Smuzhiyun state->dnxt.clkdiv = 3;
1117*4882a593Smuzhiyun state->dnxt.ratediv = 6;
1118*4882a593Smuzhiyun }
1119*4882a593Smuzhiyun
1120*4882a593Smuzhiyun return 0;
1121*4882a593Smuzhiyun }
1122*4882a593Smuzhiyun
1123*4882a593Smuzhiyun /* Overwrite the current tuning params, we are about to tune */
cx24120_clone_params(struct dvb_frontend * fe)1124*4882a593Smuzhiyun static void cx24120_clone_params(struct dvb_frontend *fe)
1125*4882a593Smuzhiyun {
1126*4882a593Smuzhiyun struct cx24120_state *state = fe->demodulator_priv;
1127*4882a593Smuzhiyun
1128*4882a593Smuzhiyun state->dcur = state->dnxt;
1129*4882a593Smuzhiyun }
1130*4882a593Smuzhiyun
cx24120_set_frontend(struct dvb_frontend * fe)1131*4882a593Smuzhiyun static int cx24120_set_frontend(struct dvb_frontend *fe)
1132*4882a593Smuzhiyun {
1133*4882a593Smuzhiyun struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1134*4882a593Smuzhiyun struct cx24120_state *state = fe->demodulator_priv;
1135*4882a593Smuzhiyun struct cx24120_cmd cmd;
1136*4882a593Smuzhiyun int ret;
1137*4882a593Smuzhiyun
1138*4882a593Smuzhiyun switch (c->delivery_system) {
1139*4882a593Smuzhiyun case SYS_DVBS2:
1140*4882a593Smuzhiyun dev_dbg(&state->i2c->dev, "DVB-S2\n");
1141*4882a593Smuzhiyun break;
1142*4882a593Smuzhiyun case SYS_DVBS:
1143*4882a593Smuzhiyun dev_dbg(&state->i2c->dev, "DVB-S\n");
1144*4882a593Smuzhiyun break;
1145*4882a593Smuzhiyun default:
1146*4882a593Smuzhiyun dev_dbg(&state->i2c->dev,
1147*4882a593Smuzhiyun "delivery system(%d) not supported\n",
1148*4882a593Smuzhiyun c->delivery_system);
1149*4882a593Smuzhiyun return -EINVAL;
1150*4882a593Smuzhiyun }
1151*4882a593Smuzhiyun
1152*4882a593Smuzhiyun state->dnxt.delsys = c->delivery_system;
1153*4882a593Smuzhiyun state->dnxt.modulation = c->modulation;
1154*4882a593Smuzhiyun state->dnxt.frequency = c->frequency;
1155*4882a593Smuzhiyun state->dnxt.pilot = c->pilot;
1156*4882a593Smuzhiyun
1157*4882a593Smuzhiyun ret = cx24120_set_inversion(state, c->inversion);
1158*4882a593Smuzhiyun if (ret != 0)
1159*4882a593Smuzhiyun return ret;
1160*4882a593Smuzhiyun
1161*4882a593Smuzhiyun ret = cx24120_set_fec(state, c->modulation, c->fec_inner);
1162*4882a593Smuzhiyun if (ret != 0)
1163*4882a593Smuzhiyun return ret;
1164*4882a593Smuzhiyun
1165*4882a593Smuzhiyun ret = cx24120_set_pilot(state, c->pilot);
1166*4882a593Smuzhiyun if (ret != 0)
1167*4882a593Smuzhiyun return ret;
1168*4882a593Smuzhiyun
1169*4882a593Smuzhiyun ret = cx24120_set_symbolrate(state, c->symbol_rate);
1170*4882a593Smuzhiyun if (ret != 0)
1171*4882a593Smuzhiyun return ret;
1172*4882a593Smuzhiyun
1173*4882a593Smuzhiyun /* discard the 'current' tuning parameters and prepare to tune */
1174*4882a593Smuzhiyun cx24120_clone_params(fe);
1175*4882a593Smuzhiyun
1176*4882a593Smuzhiyun dev_dbg(&state->i2c->dev,
1177*4882a593Smuzhiyun "delsys = %d\n", state->dcur.delsys);
1178*4882a593Smuzhiyun dev_dbg(&state->i2c->dev,
1179*4882a593Smuzhiyun "modulation = %d\n", state->dcur.modulation);
1180*4882a593Smuzhiyun dev_dbg(&state->i2c->dev,
1181*4882a593Smuzhiyun "frequency = %d\n", state->dcur.frequency);
1182*4882a593Smuzhiyun dev_dbg(&state->i2c->dev,
1183*4882a593Smuzhiyun "pilot = %d (val = 0x%02x)\n",
1184*4882a593Smuzhiyun state->dcur.pilot, state->dcur.pilot_val);
1185*4882a593Smuzhiyun dev_dbg(&state->i2c->dev,
1186*4882a593Smuzhiyun "symbol_rate = %d (clkdiv/ratediv = 0x%02x/0x%02x)\n",
1187*4882a593Smuzhiyun state->dcur.symbol_rate,
1188*4882a593Smuzhiyun state->dcur.clkdiv, state->dcur.ratediv);
1189*4882a593Smuzhiyun dev_dbg(&state->i2c->dev,
1190*4882a593Smuzhiyun "FEC = %d (mask/val = 0x%02x/0x%02x)\n",
1191*4882a593Smuzhiyun state->dcur.fec, state->dcur.fec_mask, state->dcur.fec_val);
1192*4882a593Smuzhiyun dev_dbg(&state->i2c->dev,
1193*4882a593Smuzhiyun "Inversion = %d (val = 0x%02x)\n",
1194*4882a593Smuzhiyun state->dcur.inversion, state->dcur.inversion_val);
1195*4882a593Smuzhiyun
1196*4882a593Smuzhiyun /* Flag that clock needs to be set after tune */
1197*4882a593Smuzhiyun state->need_clock_set = 1;
1198*4882a593Smuzhiyun
1199*4882a593Smuzhiyun /* Tune in */
1200*4882a593Smuzhiyun cmd.id = CMD_TUNEREQUEST;
1201*4882a593Smuzhiyun cmd.len = 15;
1202*4882a593Smuzhiyun cmd.arg[0] = 0;
1203*4882a593Smuzhiyun cmd.arg[1] = (state->dcur.frequency & 0xff0000) >> 16;
1204*4882a593Smuzhiyun cmd.arg[2] = (state->dcur.frequency & 0x00ff00) >> 8;
1205*4882a593Smuzhiyun cmd.arg[3] = (state->dcur.frequency & 0x0000ff);
1206*4882a593Smuzhiyun cmd.arg[4] = ((state->dcur.symbol_rate / 1000) & 0xff00) >> 8;
1207*4882a593Smuzhiyun cmd.arg[5] = ((state->dcur.symbol_rate / 1000) & 0x00ff);
1208*4882a593Smuzhiyun cmd.arg[6] = state->dcur.inversion;
1209*4882a593Smuzhiyun cmd.arg[7] = state->dcur.fec_val | state->dcur.pilot_val;
1210*4882a593Smuzhiyun cmd.arg[8] = CX24120_SEARCH_RANGE_KHZ >> 8;
1211*4882a593Smuzhiyun cmd.arg[9] = CX24120_SEARCH_RANGE_KHZ & 0xff;
1212*4882a593Smuzhiyun cmd.arg[10] = 0; /* maybe rolloff? */
1213*4882a593Smuzhiyun cmd.arg[11] = state->dcur.fec_mask;
1214*4882a593Smuzhiyun cmd.arg[12] = state->dcur.ratediv;
1215*4882a593Smuzhiyun cmd.arg[13] = state->dcur.clkdiv;
1216*4882a593Smuzhiyun cmd.arg[14] = 0;
1217*4882a593Smuzhiyun
1218*4882a593Smuzhiyun /* Send tune command */
1219*4882a593Smuzhiyun ret = cx24120_message_send(state, &cmd);
1220*4882a593Smuzhiyun if (ret != 0)
1221*4882a593Smuzhiyun return ret;
1222*4882a593Smuzhiyun
1223*4882a593Smuzhiyun /* Write symbol rate values */
1224*4882a593Smuzhiyun ret = cx24120_writereg(state, CX24120_REG_CLKDIV, state->dcur.clkdiv);
1225*4882a593Smuzhiyun ret = cx24120_readreg(state, CX24120_REG_RATEDIV);
1226*4882a593Smuzhiyun ret &= 0xfffffff0;
1227*4882a593Smuzhiyun ret |= state->dcur.ratediv;
1228*4882a593Smuzhiyun ret = cx24120_writereg(state, CX24120_REG_RATEDIV, ret);
1229*4882a593Smuzhiyun
1230*4882a593Smuzhiyun return 0;
1231*4882a593Smuzhiyun }
1232*4882a593Smuzhiyun
1233*4882a593Smuzhiyun /* Set vco from config */
cx24120_set_vco(struct cx24120_state * state)1234*4882a593Smuzhiyun static int cx24120_set_vco(struct cx24120_state *state)
1235*4882a593Smuzhiyun {
1236*4882a593Smuzhiyun struct cx24120_cmd cmd;
1237*4882a593Smuzhiyun u32 nxtal_khz, vco;
1238*4882a593Smuzhiyun u64 inv_vco;
1239*4882a593Smuzhiyun u32 xtal_khz = state->config->xtal_khz;
1240*4882a593Smuzhiyun
1241*4882a593Smuzhiyun nxtal_khz = xtal_khz * 4;
1242*4882a593Smuzhiyun vco = nxtal_khz * 10;
1243*4882a593Smuzhiyun inv_vco = DIV_ROUND_CLOSEST_ULL(0x400000000ULL, vco);
1244*4882a593Smuzhiyun
1245*4882a593Smuzhiyun dev_dbg(&state->i2c->dev, "xtal=%d, vco=%d, inv_vco=%lld\n",
1246*4882a593Smuzhiyun xtal_khz, vco, inv_vco);
1247*4882a593Smuzhiyun
1248*4882a593Smuzhiyun cmd.id = CMD_VCO_SET;
1249*4882a593Smuzhiyun cmd.len = 12;
1250*4882a593Smuzhiyun cmd.arg[0] = (vco >> 16) & 0xff;
1251*4882a593Smuzhiyun cmd.arg[1] = (vco >> 8) & 0xff;
1252*4882a593Smuzhiyun cmd.arg[2] = vco & 0xff;
1253*4882a593Smuzhiyun cmd.arg[3] = (inv_vco >> 8) & 0xff;
1254*4882a593Smuzhiyun cmd.arg[4] = (inv_vco) & 0xff;
1255*4882a593Smuzhiyun cmd.arg[5] = 0x03;
1256*4882a593Smuzhiyun cmd.arg[6] = (nxtal_khz >> 8) & 0xff;
1257*4882a593Smuzhiyun cmd.arg[7] = nxtal_khz & 0xff;
1258*4882a593Smuzhiyun cmd.arg[8] = 0x06;
1259*4882a593Smuzhiyun cmd.arg[9] = 0x03;
1260*4882a593Smuzhiyun cmd.arg[10] = (xtal_khz >> 16) & 0xff;
1261*4882a593Smuzhiyun cmd.arg[11] = xtal_khz & 0xff;
1262*4882a593Smuzhiyun
1263*4882a593Smuzhiyun return cx24120_message_send(state, &cmd);
1264*4882a593Smuzhiyun }
1265*4882a593Smuzhiyun
cx24120_init(struct dvb_frontend * fe)1266*4882a593Smuzhiyun static int cx24120_init(struct dvb_frontend *fe)
1267*4882a593Smuzhiyun {
1268*4882a593Smuzhiyun const struct firmware *fw;
1269*4882a593Smuzhiyun struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1270*4882a593Smuzhiyun struct cx24120_state *state = fe->demodulator_priv;
1271*4882a593Smuzhiyun struct cx24120_cmd cmd;
1272*4882a593Smuzhiyun u8 reg;
1273*4882a593Smuzhiyun int ret, i;
1274*4882a593Smuzhiyun unsigned char vers[4];
1275*4882a593Smuzhiyun
1276*4882a593Smuzhiyun if (state->cold_init)
1277*4882a593Smuzhiyun return 0;
1278*4882a593Smuzhiyun
1279*4882a593Smuzhiyun /* ???? */
1280*4882a593Smuzhiyun cx24120_writereg(state, 0xea, 0x00);
1281*4882a593Smuzhiyun cx24120_test_rom(state);
1282*4882a593Smuzhiyun reg = cx24120_readreg(state, 0xfb) & 0xfe;
1283*4882a593Smuzhiyun cx24120_writereg(state, 0xfb, reg);
1284*4882a593Smuzhiyun reg = cx24120_readreg(state, 0xfc) & 0xfe;
1285*4882a593Smuzhiyun cx24120_writereg(state, 0xfc, reg);
1286*4882a593Smuzhiyun cx24120_writereg(state, 0xc3, 0x04);
1287*4882a593Smuzhiyun cx24120_writereg(state, 0xc4, 0x04);
1288*4882a593Smuzhiyun cx24120_writereg(state, 0xce, 0x00);
1289*4882a593Smuzhiyun cx24120_writereg(state, 0xcf, 0x00);
1290*4882a593Smuzhiyun reg = cx24120_readreg(state, 0xea) & 0xfe;
1291*4882a593Smuzhiyun cx24120_writereg(state, 0xea, reg);
1292*4882a593Smuzhiyun cx24120_writereg(state, 0xeb, 0x0c);
1293*4882a593Smuzhiyun cx24120_writereg(state, 0xec, 0x06);
1294*4882a593Smuzhiyun cx24120_writereg(state, 0xed, 0x05);
1295*4882a593Smuzhiyun cx24120_writereg(state, 0xee, 0x03);
1296*4882a593Smuzhiyun cx24120_writereg(state, 0xef, 0x05);
1297*4882a593Smuzhiyun cx24120_writereg(state, 0xf3, 0x03);
1298*4882a593Smuzhiyun cx24120_writereg(state, 0xf4, 0x44);
1299*4882a593Smuzhiyun
1300*4882a593Smuzhiyun for (i = 0; i < 3; i++) {
1301*4882a593Smuzhiyun cx24120_writereg(state, 0xf0 + i, 0x04);
1302*4882a593Smuzhiyun cx24120_writereg(state, 0xe6 + i, 0x02);
1303*4882a593Smuzhiyun }
1304*4882a593Smuzhiyun
1305*4882a593Smuzhiyun cx24120_writereg(state, 0xea, (reg | 0x01));
1306*4882a593Smuzhiyun for (i = 0; i < 6; i += 2) {
1307*4882a593Smuzhiyun cx24120_writereg(state, 0xc5 + i, 0x00);
1308*4882a593Smuzhiyun cx24120_writereg(state, 0xc6 + i, 0x00);
1309*4882a593Smuzhiyun }
1310*4882a593Smuzhiyun
1311*4882a593Smuzhiyun cx24120_writereg(state, 0xe4, 0x03);
1312*4882a593Smuzhiyun cx24120_writereg(state, 0xeb, 0x0a);
1313*4882a593Smuzhiyun
1314*4882a593Smuzhiyun dev_dbg(&state->i2c->dev, "requesting firmware (%s) to download...\n",
1315*4882a593Smuzhiyun CX24120_FIRMWARE);
1316*4882a593Smuzhiyun
1317*4882a593Smuzhiyun ret = state->config->request_firmware(fe, &fw, CX24120_FIRMWARE);
1318*4882a593Smuzhiyun if (ret) {
1319*4882a593Smuzhiyun err("Could not load firmware (%s): %d\n", CX24120_FIRMWARE,
1320*4882a593Smuzhiyun ret);
1321*4882a593Smuzhiyun return ret;
1322*4882a593Smuzhiyun }
1323*4882a593Smuzhiyun
1324*4882a593Smuzhiyun dev_dbg(&state->i2c->dev,
1325*4882a593Smuzhiyun "Firmware found, size %d bytes (%02x %02x .. %02x %02x)\n",
1326*4882a593Smuzhiyun (int)fw->size, /* firmware_size in bytes */
1327*4882a593Smuzhiyun fw->data[0], /* fw 1st byte */
1328*4882a593Smuzhiyun fw->data[1], /* fw 2d byte */
1329*4882a593Smuzhiyun fw->data[fw->size - 2], /* fw before last byte */
1330*4882a593Smuzhiyun fw->data[fw->size - 1]); /* fw last byte */
1331*4882a593Smuzhiyun
1332*4882a593Smuzhiyun cx24120_test_rom(state);
1333*4882a593Smuzhiyun reg = cx24120_readreg(state, 0xfb) & 0xfe;
1334*4882a593Smuzhiyun cx24120_writereg(state, 0xfb, reg);
1335*4882a593Smuzhiyun cx24120_writereg(state, 0xe0, 0x76);
1336*4882a593Smuzhiyun cx24120_writereg(state, 0xf7, 0x81);
1337*4882a593Smuzhiyun cx24120_writereg(state, 0xf8, 0x00);
1338*4882a593Smuzhiyun cx24120_writereg(state, 0xf9, 0x00);
1339*4882a593Smuzhiyun cx24120_writeregs(state, 0xfa, fw->data, (fw->size - 1), 0x00);
1340*4882a593Smuzhiyun cx24120_writereg(state, 0xf7, 0xc0);
1341*4882a593Smuzhiyun cx24120_writereg(state, 0xe0, 0x00);
1342*4882a593Smuzhiyun reg = (fw->size - 2) & 0x00ff;
1343*4882a593Smuzhiyun cx24120_writereg(state, 0xf8, reg);
1344*4882a593Smuzhiyun reg = ((fw->size - 2) >> 8) & 0x00ff;
1345*4882a593Smuzhiyun cx24120_writereg(state, 0xf9, reg);
1346*4882a593Smuzhiyun cx24120_writereg(state, 0xf7, 0x00);
1347*4882a593Smuzhiyun cx24120_writereg(state, 0xdc, 0x00);
1348*4882a593Smuzhiyun cx24120_writereg(state, 0xdc, 0x07);
1349*4882a593Smuzhiyun msleep(500);
1350*4882a593Smuzhiyun
1351*4882a593Smuzhiyun /* Check final byte matches final byte of firmware */
1352*4882a593Smuzhiyun reg = cx24120_readreg(state, 0xe1);
1353*4882a593Smuzhiyun if (reg == fw->data[fw->size - 1]) {
1354*4882a593Smuzhiyun dev_dbg(&state->i2c->dev, "Firmware uploaded successfully\n");
1355*4882a593Smuzhiyun ret = 0;
1356*4882a593Smuzhiyun } else {
1357*4882a593Smuzhiyun err("Firmware upload failed. Last byte returned=0x%x\n", ret);
1358*4882a593Smuzhiyun ret = -EREMOTEIO;
1359*4882a593Smuzhiyun }
1360*4882a593Smuzhiyun cx24120_writereg(state, 0xdc, 0x00);
1361*4882a593Smuzhiyun release_firmware(fw);
1362*4882a593Smuzhiyun if (ret != 0)
1363*4882a593Smuzhiyun return ret;
1364*4882a593Smuzhiyun
1365*4882a593Smuzhiyun /* Start tuner */
1366*4882a593Smuzhiyun cmd.id = CMD_START_TUNER;
1367*4882a593Smuzhiyun cmd.len = 3;
1368*4882a593Smuzhiyun cmd.arg[0] = 0x00;
1369*4882a593Smuzhiyun cmd.arg[1] = 0x00;
1370*4882a593Smuzhiyun cmd.arg[2] = 0x00;
1371*4882a593Smuzhiyun
1372*4882a593Smuzhiyun if (cx24120_message_send(state, &cmd) != 0) {
1373*4882a593Smuzhiyun err("Error tuner start! :(\n");
1374*4882a593Smuzhiyun return -EREMOTEIO;
1375*4882a593Smuzhiyun }
1376*4882a593Smuzhiyun
1377*4882a593Smuzhiyun /* Set VCO */
1378*4882a593Smuzhiyun ret = cx24120_set_vco(state);
1379*4882a593Smuzhiyun if (ret != 0) {
1380*4882a593Smuzhiyun err("Error set VCO! :(\n");
1381*4882a593Smuzhiyun return ret;
1382*4882a593Smuzhiyun }
1383*4882a593Smuzhiyun
1384*4882a593Smuzhiyun /* set bandwidth */
1385*4882a593Smuzhiyun cmd.id = CMD_BANDWIDTH;
1386*4882a593Smuzhiyun cmd.len = 12;
1387*4882a593Smuzhiyun cmd.arg[0] = 0x00;
1388*4882a593Smuzhiyun cmd.arg[1] = 0x00;
1389*4882a593Smuzhiyun cmd.arg[2] = 0x00;
1390*4882a593Smuzhiyun cmd.arg[3] = 0x00;
1391*4882a593Smuzhiyun cmd.arg[4] = 0x05;
1392*4882a593Smuzhiyun cmd.arg[5] = 0x02;
1393*4882a593Smuzhiyun cmd.arg[6] = 0x02;
1394*4882a593Smuzhiyun cmd.arg[7] = 0x00;
1395*4882a593Smuzhiyun cmd.arg[8] = 0x05;
1396*4882a593Smuzhiyun cmd.arg[9] = 0x02;
1397*4882a593Smuzhiyun cmd.arg[10] = 0x02;
1398*4882a593Smuzhiyun cmd.arg[11] = 0x00;
1399*4882a593Smuzhiyun
1400*4882a593Smuzhiyun if (cx24120_message_send(state, &cmd)) {
1401*4882a593Smuzhiyun err("Error set bandwidth!\n");
1402*4882a593Smuzhiyun return -EREMOTEIO;
1403*4882a593Smuzhiyun }
1404*4882a593Smuzhiyun
1405*4882a593Smuzhiyun reg = cx24120_readreg(state, 0xba);
1406*4882a593Smuzhiyun if (reg > 3) {
1407*4882a593Smuzhiyun dev_dbg(&state->i2c->dev, "Reset-readreg 0xba: %x\n", ret);
1408*4882a593Smuzhiyun err("Error initialising tuner!\n");
1409*4882a593Smuzhiyun return -EREMOTEIO;
1410*4882a593Smuzhiyun }
1411*4882a593Smuzhiyun
1412*4882a593Smuzhiyun dev_dbg(&state->i2c->dev, "Tuner initialised correctly.\n");
1413*4882a593Smuzhiyun
1414*4882a593Smuzhiyun /* Initialise mpeg outputs */
1415*4882a593Smuzhiyun cx24120_writereg(state, 0xeb, 0x0a);
1416*4882a593Smuzhiyun if (cx24120_msg_mpeg_output_global_config(state, 0) ||
1417*4882a593Smuzhiyun cx24120_msg_mpeg_output_config(state, 0) ||
1418*4882a593Smuzhiyun cx24120_msg_mpeg_output_config(state, 1) ||
1419*4882a593Smuzhiyun cx24120_msg_mpeg_output_config(state, 2)) {
1420*4882a593Smuzhiyun err("Error initialising mpeg output. :(\n");
1421*4882a593Smuzhiyun return -EREMOTEIO;
1422*4882a593Smuzhiyun }
1423*4882a593Smuzhiyun
1424*4882a593Smuzhiyun /* Set size of BER window */
1425*4882a593Smuzhiyun cmd.id = CMD_BER_CTRL;
1426*4882a593Smuzhiyun cmd.len = 3;
1427*4882a593Smuzhiyun cmd.arg[0] = 0x00;
1428*4882a593Smuzhiyun cmd.arg[1] = CX24120_BER_WINDOW;
1429*4882a593Smuzhiyun cmd.arg[2] = CX24120_BER_WINDOW;
1430*4882a593Smuzhiyun if (cx24120_message_send(state, &cmd)) {
1431*4882a593Smuzhiyun err("Error setting ber window\n");
1432*4882a593Smuzhiyun return -EREMOTEIO;
1433*4882a593Smuzhiyun }
1434*4882a593Smuzhiyun
1435*4882a593Smuzhiyun /* Firmware CMD 35: Get firmware version */
1436*4882a593Smuzhiyun cmd.id = CMD_FWVERSION;
1437*4882a593Smuzhiyun cmd.len = 1;
1438*4882a593Smuzhiyun for (i = 0; i < 4; i++) {
1439*4882a593Smuzhiyun cmd.arg[0] = i;
1440*4882a593Smuzhiyun ret = cx24120_message_send(state, &cmd);
1441*4882a593Smuzhiyun if (ret != 0)
1442*4882a593Smuzhiyun return ret;
1443*4882a593Smuzhiyun vers[i] = cx24120_readreg(state, CX24120_REG_MAILBOX);
1444*4882a593Smuzhiyun }
1445*4882a593Smuzhiyun info("FW version %i.%i.%i.%i\n", vers[0], vers[1], vers[2], vers[3]);
1446*4882a593Smuzhiyun
1447*4882a593Smuzhiyun /* init stats here in order signal app which stats are supported */
1448*4882a593Smuzhiyun c->strength.len = 1;
1449*4882a593Smuzhiyun c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
1450*4882a593Smuzhiyun c->cnr.len = 1;
1451*4882a593Smuzhiyun c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
1452*4882a593Smuzhiyun c->post_bit_error.len = 1;
1453*4882a593Smuzhiyun c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
1454*4882a593Smuzhiyun c->post_bit_count.len = 1;
1455*4882a593Smuzhiyun c->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
1456*4882a593Smuzhiyun c->block_error.len = 1;
1457*4882a593Smuzhiyun c->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
1458*4882a593Smuzhiyun c->block_count.len = 1;
1459*4882a593Smuzhiyun c->block_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
1460*4882a593Smuzhiyun
1461*4882a593Smuzhiyun state->cold_init = 1;
1462*4882a593Smuzhiyun
1463*4882a593Smuzhiyun return 0;
1464*4882a593Smuzhiyun }
1465*4882a593Smuzhiyun
cx24120_tune(struct dvb_frontend * fe,bool re_tune,unsigned int mode_flags,unsigned int * delay,enum fe_status * status)1466*4882a593Smuzhiyun static int cx24120_tune(struct dvb_frontend *fe, bool re_tune,
1467*4882a593Smuzhiyun unsigned int mode_flags, unsigned int *delay,
1468*4882a593Smuzhiyun enum fe_status *status)
1469*4882a593Smuzhiyun {
1470*4882a593Smuzhiyun struct cx24120_state *state = fe->demodulator_priv;
1471*4882a593Smuzhiyun int ret;
1472*4882a593Smuzhiyun
1473*4882a593Smuzhiyun dev_dbg(&state->i2c->dev, "(%d)\n", re_tune);
1474*4882a593Smuzhiyun
1475*4882a593Smuzhiyun /* TODO: Do we need to set delay? */
1476*4882a593Smuzhiyun
1477*4882a593Smuzhiyun if (re_tune) {
1478*4882a593Smuzhiyun ret = cx24120_set_frontend(fe);
1479*4882a593Smuzhiyun if (ret)
1480*4882a593Smuzhiyun return ret;
1481*4882a593Smuzhiyun }
1482*4882a593Smuzhiyun
1483*4882a593Smuzhiyun return cx24120_read_status(fe, status);
1484*4882a593Smuzhiyun }
1485*4882a593Smuzhiyun
cx24120_get_algo(struct dvb_frontend * fe)1486*4882a593Smuzhiyun static enum dvbfe_algo cx24120_get_algo(struct dvb_frontend *fe)
1487*4882a593Smuzhiyun {
1488*4882a593Smuzhiyun return DVBFE_ALGO_HW;
1489*4882a593Smuzhiyun }
1490*4882a593Smuzhiyun
cx24120_sleep(struct dvb_frontend * fe)1491*4882a593Smuzhiyun static int cx24120_sleep(struct dvb_frontend *fe)
1492*4882a593Smuzhiyun {
1493*4882a593Smuzhiyun return 0;
1494*4882a593Smuzhiyun }
1495*4882a593Smuzhiyun
cx24120_get_frontend(struct dvb_frontend * fe,struct dtv_frontend_properties * c)1496*4882a593Smuzhiyun static int cx24120_get_frontend(struct dvb_frontend *fe,
1497*4882a593Smuzhiyun struct dtv_frontend_properties *c)
1498*4882a593Smuzhiyun {
1499*4882a593Smuzhiyun struct cx24120_state *state = fe->demodulator_priv;
1500*4882a593Smuzhiyun u8 freq1, freq2, freq3;
1501*4882a593Smuzhiyun int status;
1502*4882a593Smuzhiyun
1503*4882a593Smuzhiyun dev_dbg(&state->i2c->dev, "\n");
1504*4882a593Smuzhiyun
1505*4882a593Smuzhiyun /* don't return empty data if we're not tuned in */
1506*4882a593Smuzhiyun status = cx24120_readreg(state, CX24120_REG_STATUS);
1507*4882a593Smuzhiyun if (!(status & CX24120_HAS_LOCK))
1508*4882a593Smuzhiyun return 0;
1509*4882a593Smuzhiyun
1510*4882a593Smuzhiyun /* Get frequency */
1511*4882a593Smuzhiyun freq1 = cx24120_readreg(state, CX24120_REG_FREQ1);
1512*4882a593Smuzhiyun freq2 = cx24120_readreg(state, CX24120_REG_FREQ2);
1513*4882a593Smuzhiyun freq3 = cx24120_readreg(state, CX24120_REG_FREQ3);
1514*4882a593Smuzhiyun c->frequency = (freq3 << 16) | (freq2 << 8) | freq1;
1515*4882a593Smuzhiyun dev_dbg(&state->i2c->dev, "frequency = %d\n", c->frequency);
1516*4882a593Smuzhiyun
1517*4882a593Smuzhiyun /* Get modulation, fec, pilot */
1518*4882a593Smuzhiyun cx24120_get_fec(fe);
1519*4882a593Smuzhiyun
1520*4882a593Smuzhiyun return 0;
1521*4882a593Smuzhiyun }
1522*4882a593Smuzhiyun
cx24120_release(struct dvb_frontend * fe)1523*4882a593Smuzhiyun static void cx24120_release(struct dvb_frontend *fe)
1524*4882a593Smuzhiyun {
1525*4882a593Smuzhiyun struct cx24120_state *state = fe->demodulator_priv;
1526*4882a593Smuzhiyun
1527*4882a593Smuzhiyun dev_dbg(&state->i2c->dev, "Clear state structure\n");
1528*4882a593Smuzhiyun kfree(state);
1529*4882a593Smuzhiyun }
1530*4882a593Smuzhiyun
cx24120_read_ucblocks(struct dvb_frontend * fe,u32 * ucblocks)1531*4882a593Smuzhiyun static int cx24120_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
1532*4882a593Smuzhiyun {
1533*4882a593Smuzhiyun struct cx24120_state *state = fe->demodulator_priv;
1534*4882a593Smuzhiyun struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1535*4882a593Smuzhiyun
1536*4882a593Smuzhiyun if (c->block_error.stat[0].scale != FE_SCALE_COUNTER) {
1537*4882a593Smuzhiyun *ucblocks = 0;
1538*4882a593Smuzhiyun return 0;
1539*4882a593Smuzhiyun }
1540*4882a593Smuzhiyun
1541*4882a593Smuzhiyun *ucblocks = c->block_error.stat[0].uvalue - state->ucb_offset;
1542*4882a593Smuzhiyun
1543*4882a593Smuzhiyun return 0;
1544*4882a593Smuzhiyun }
1545*4882a593Smuzhiyun
1546*4882a593Smuzhiyun static const struct dvb_frontend_ops cx24120_ops = {
1547*4882a593Smuzhiyun .delsys = { SYS_DVBS, SYS_DVBS2 },
1548*4882a593Smuzhiyun .info = {
1549*4882a593Smuzhiyun .name = "Conexant CX24120/CX24118",
1550*4882a593Smuzhiyun .frequency_min_hz = 950 * MHz,
1551*4882a593Smuzhiyun .frequency_max_hz = 2150 * MHz,
1552*4882a593Smuzhiyun .frequency_stepsize_hz = 1011 * kHz,
1553*4882a593Smuzhiyun .frequency_tolerance_hz = 5 * MHz,
1554*4882a593Smuzhiyun .symbol_rate_min = 1000000,
1555*4882a593Smuzhiyun .symbol_rate_max = 45000000,
1556*4882a593Smuzhiyun .caps = FE_CAN_INVERSION_AUTO |
1557*4882a593Smuzhiyun FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
1558*4882a593Smuzhiyun FE_CAN_FEC_4_5 | FE_CAN_FEC_5_6 | FE_CAN_FEC_6_7 |
1559*4882a593Smuzhiyun FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
1560*4882a593Smuzhiyun FE_CAN_2G_MODULATION |
1561*4882a593Smuzhiyun FE_CAN_QPSK | FE_CAN_RECOVER
1562*4882a593Smuzhiyun },
1563*4882a593Smuzhiyun .release = cx24120_release,
1564*4882a593Smuzhiyun
1565*4882a593Smuzhiyun .init = cx24120_init,
1566*4882a593Smuzhiyun .sleep = cx24120_sleep,
1567*4882a593Smuzhiyun
1568*4882a593Smuzhiyun .tune = cx24120_tune,
1569*4882a593Smuzhiyun .get_frontend_algo = cx24120_get_algo,
1570*4882a593Smuzhiyun .set_frontend = cx24120_set_frontend,
1571*4882a593Smuzhiyun
1572*4882a593Smuzhiyun .get_frontend = cx24120_get_frontend,
1573*4882a593Smuzhiyun .read_status = cx24120_read_status,
1574*4882a593Smuzhiyun .read_ber = cx24120_read_ber,
1575*4882a593Smuzhiyun .read_signal_strength = cx24120_read_signal_strength,
1576*4882a593Smuzhiyun .read_snr = cx24120_read_snr,
1577*4882a593Smuzhiyun .read_ucblocks = cx24120_read_ucblocks,
1578*4882a593Smuzhiyun
1579*4882a593Smuzhiyun .diseqc_send_master_cmd = cx24120_send_diseqc_msg,
1580*4882a593Smuzhiyun
1581*4882a593Smuzhiyun .diseqc_send_burst = cx24120_diseqc_send_burst,
1582*4882a593Smuzhiyun .set_tone = cx24120_set_tone,
1583*4882a593Smuzhiyun .set_voltage = cx24120_set_voltage,
1584*4882a593Smuzhiyun };
1585*4882a593Smuzhiyun
1586*4882a593Smuzhiyun MODULE_DESCRIPTION("DVB Frontend module for Conexant CX24120/CX24118 hardware");
1587*4882a593Smuzhiyun MODULE_AUTHOR("Jemma Denson");
1588*4882a593Smuzhiyun MODULE_LICENSE("GPL");
1589