blob: 654d7dc879d9fbb7e7d7fd9acdf43620215270e4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 cx24110 - Single Chip Satellite Channel Receiver driver module
3
Johannes Stezenbacha8d995c2005-09-09 13:02:19 -07004 Copyright (C) 2002 Peter Hettkamp <peter.hettkamp@htp-tel.de> based on
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 work
6 Copyright (C) 1999 Convergence Integrated Media GmbH <ralph@convergence.de>
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22
23*/
24
25#include <linux/slab.h>
26#include <linux/kernel.h>
27#include <linux/module.h>
28#include <linux/moduleparam.h>
29#include <linux/init.h>
Tim Schmielau8c65b4a2005-11-07 00:59:43 -080030#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32#include "dvb_frontend.h"
33#include "cx24110.h"
34
35
36struct cx24110_state {
37
38 struct i2c_adapter* i2c;
39
40 struct dvb_frontend_ops ops;
41
42 const struct cx24110_config* config;
43
44 struct dvb_frontend frontend;
45
46 u32 lastber;
47 u32 lastbler;
48 u32 lastesn0;
49};
50
51static int debug;
52#define dprintk(args...) \
53 do { \
54 if (debug) printk(KERN_DEBUG "cx24110: " args); \
55 } while (0)
56
57static struct {u8 reg; u8 data;} cx24110_regdata[]=
58 /* Comments beginning with @ denote this value should
59 be the default */
60 {{0x09,0x01}, /* SoftResetAll */
61 {0x09,0x00}, /* release reset */
62 {0x01,0xe8}, /* MSB of code rate 27.5MS/s */
63 {0x02,0x17}, /* middle byte " */
64 {0x03,0x29}, /* LSB " */
65 {0x05,0x03}, /* @ DVB mode, standard code rate 3/4 */
66 {0x06,0xa5}, /* @ PLL 60MHz */
67 {0x07,0x01}, /* @ Fclk, i.e. sampling clock, 60MHz */
68 {0x0a,0x00}, /* @ partial chip disables, do not set */
69 {0x0b,0x01}, /* set output clock in gapped mode, start signal low
70 active for first byte */
71 {0x0c,0x11}, /* no parity bytes, large hold time, serial data out */
72 {0x0d,0x6f}, /* @ RS Sync/Unsync thresholds */
73 {0x10,0x40}, /* chip doc is misleading here: write bit 6 as 1
74 to avoid starting the BER counter. Reset the
75 CRC test bit. Finite counting selected */
76 {0x15,0xff}, /* @ size of the limited time window for RS BER
77 estimation. It is <value>*256 RS blocks, this
78 gives approx. 2.6 sec at 27.5MS/s, rate 3/4 */
79 {0x16,0x00}, /* @ enable all RS output ports */
80 {0x17,0x04}, /* @ time window allowed for the RS to sync */
81 {0x18,0xae}, /* @ allow all standard DVB code rates to be scanned
82 for automatically */
83 /* leave the current code rate and normalization
84 registers as they are after reset... */
85 {0x21,0x10}, /* @ during AutoAcq, search each viterbi setting
86 only once */
87 {0x23,0x18}, /* @ size of the limited time window for Viterbi BER
88 estimation. It is <value>*65536 channel bits, i.e.
89 approx. 38ms at 27.5MS/s, rate 3/4 */
90 {0x24,0x24}, /* do not trigger Viterbi CRC test. Finite count window */
91 /* leave front-end AGC parameters at default values */
92 /* leave decimation AGC parameters at default values */
93 {0x35,0x40}, /* disable all interrupts. They are not connected anyway */
94 {0x36,0xff}, /* clear all interrupt pending flags */
95 {0x37,0x00}, /* @ fully enable AutoAcqq state machine */
96 {0x38,0x07}, /* @ enable fade recovery, but not autostart AutoAcq */
97 /* leave the equalizer parameters on their default values */
98 /* leave the final AGC parameters on their default values */
99 {0x41,0x00}, /* @ MSB of front-end derotator frequency */
100 {0x42,0x00}, /* @ middle bytes " */
101 {0x43,0x00}, /* @ LSB " */
102 /* leave the carrier tracking loop parameters on default */
103 /* leave the bit timing loop parameters at gefault */
104 {0x56,0x4d}, /* set the filtune voltage to 2.7V, as recommended by */
105 /* the cx24108 data sheet for symbol rates above 15MS/s */
106 {0x57,0x00}, /* @ Filter sigma delta enabled, positive */
107 {0x61,0x95}, /* GPIO pins 1-4 have special function */
108 {0x62,0x05}, /* GPIO pin 5 has special function, pin 6 is GPIO */
109 {0x63,0x00}, /* All GPIO pins use CMOS output characteristics */
110 {0x64,0x20}, /* GPIO 6 is input, all others are outputs */
111 {0x6d,0x30}, /* tuner auto mode clock freq 62kHz */
112 {0x70,0x15}, /* use auto mode, tuner word is 21 bits long */
113 {0x73,0x00}, /* @ disable several demod bypasses */
114 {0x74,0x00}, /* @ " */
115 {0x75,0x00} /* @ " */
116 /* the remaining registers are for SEC */
117 };
118
119
120static int cx24110_writereg (struct cx24110_state* state, int reg, int data)
121{
122 u8 buf [] = { reg, data };
123 struct i2c_msg msg = { .addr = state->config->demod_address, .flags = 0, .buf = buf, .len = 2 };
124 int err;
125
126 if ((err = i2c_transfer(state->i2c, &msg, 1)) != 1) {
127 dprintk ("%s: writereg error (err == %i, reg == 0x%02x,"
128 " data == 0x%02x)\n", __FUNCTION__, err, reg, data);
129 return -EREMOTEIO;
130 }
131
132 return 0;
133}
134
135static int cx24110_readreg (struct cx24110_state* state, u8 reg)
136{
137 int ret;
138 u8 b0 [] = { reg };
139 u8 b1 [] = { 0 };
140 struct i2c_msg msg [] = { { .addr = state->config->demod_address, .flags = 0, .buf = b0, .len = 1 },
141 { .addr = state->config->demod_address, .flags = I2C_M_RD, .buf = b1, .len = 1 } };
142
143 ret = i2c_transfer(state->i2c, msg, 2);
144
145 if (ret != 2) return ret;
146
147 return b1[0];
148}
149
150static int cx24110_set_inversion (struct cx24110_state* state, fe_spectral_inversion_t inversion)
151{
152/* fixme (low): error handling */
153
154 switch (inversion) {
155 case INVERSION_OFF:
156 cx24110_writereg(state,0x37,cx24110_readreg(state,0x37)|0x1);
157 /* AcqSpectrInvDis on. No idea why someone should want this */
158 cx24110_writereg(state,0x5,cx24110_readreg(state,0x5)&0xf7);
159 /* Initial value 0 at start of acq */
160 cx24110_writereg(state,0x22,cx24110_readreg(state,0x22)&0xef);
161 /* current value 0 */
162 /* The cx24110 manual tells us this reg is read-only.
163 But what the heck... set it ayways */
164 break;
165 case INVERSION_ON:
166 cx24110_writereg(state,0x37,cx24110_readreg(state,0x37)|0x1);
167 /* AcqSpectrInvDis on. No idea why someone should want this */
168 cx24110_writereg(state,0x5,cx24110_readreg(state,0x5)|0x08);
169 /* Initial value 1 at start of acq */
170 cx24110_writereg(state,0x22,cx24110_readreg(state,0x22)|0x10);
171 /* current value 1 */
172 break;
173 case INVERSION_AUTO:
174 cx24110_writereg(state,0x37,cx24110_readreg(state,0x37)&0xfe);
175 /* AcqSpectrInvDis off. Leave initial & current states as is */
176 break;
177 default:
178 return -EINVAL;
179 }
180
181 return 0;
182}
183
184static int cx24110_set_fec (struct cx24110_state* state, fe_code_rate_t fec)
185{
186/* fixme (low): error handling */
187
188 static const int rate[]={-1,1,2,3,5,7,-1};
189 static const int g1[]={-1,0x01,0x02,0x05,0x15,0x45,-1};
190 static const int g2[]={-1,0x01,0x03,0x06,0x1a,0x7a,-1};
191
192 /* Well, the AutoAcq engine of the cx24106 and 24110 automatically
193 searches all enabled viterbi rates, and can handle non-standard
194 rates as well. */
195
196 if (fec>FEC_AUTO)
197 fec=FEC_AUTO;
198
199 if (fec==FEC_AUTO) { /* (re-)establish AutoAcq behaviour */
200 cx24110_writereg(state,0x37,cx24110_readreg(state,0x37)&0xdf);
201 /* clear AcqVitDis bit */
202 cx24110_writereg(state,0x18,0xae);
203 /* allow all DVB standard code rates */
204 cx24110_writereg(state,0x05,(cx24110_readreg(state,0x05)&0xf0)|0x3);
205 /* set nominal Viterbi rate 3/4 */
206 cx24110_writereg(state,0x22,(cx24110_readreg(state,0x22)&0xf0)|0x3);
207 /* set current Viterbi rate 3/4 */
208 cx24110_writereg(state,0x1a,0x05); cx24110_writereg(state,0x1b,0x06);
209 /* set the puncture registers for code rate 3/4 */
210 return 0;
211 } else {
212 cx24110_writereg(state,0x37,cx24110_readreg(state,0x37)|0x20);
213 /* set AcqVitDis bit */
214 if(rate[fec]>0) {
215 cx24110_writereg(state,0x05,(cx24110_readreg(state,0x05)&0xf0)|rate[fec]);
216 /* set nominal Viterbi rate */
217 cx24110_writereg(state,0x22,(cx24110_readreg(state,0x22)&0xf0)|rate[fec]);
218 /* set current Viterbi rate */
219 cx24110_writereg(state,0x1a,g1[fec]);
220 cx24110_writereg(state,0x1b,g2[fec]);
221 /* not sure if this is the right way: I always used AutoAcq mode */
222 } else
223 return -EOPNOTSUPP;
224/* fixme (low): which is the correct return code? */
225 };
226 return 0;
227}
228
229static fe_code_rate_t cx24110_get_fec (struct cx24110_state* state)
230{
231 int i;
232
233 i=cx24110_readreg(state,0x22)&0x0f;
234 if(!(i&0x08)) {
235 return FEC_1_2 + i - 1;
236 } else {
237/* fixme (low): a special code rate has been selected. In theory, we need to
238 return a denominator value, a numerator value, and a pair of puncture
239 maps to correctly describe this mode. But this should never happen in
240 practice, because it cannot be set by cx24110_get_fec. */
241 return FEC_NONE;
242 }
243}
244
245static int cx24110_set_symbolrate (struct cx24110_state* state, u32 srate)
246{
247/* fixme (low): add error handling */
248 u32 ratio;
249 u32 tmp, fclk, BDRI;
250
251 static const u32 bands[]={5000000UL,15000000UL,90999000UL/2};
252 int i;
253
254dprintk("cx24110 debug: entering %s(%d)\n",__FUNCTION__,srate);
255 if (srate>90999000UL/2)
256 srate=90999000UL/2;
257 if (srate<500000)
258 srate=500000;
259
260 for(i=0;(i<sizeof(bands)/sizeof(bands[0]))&&(srate>bands[i]);i++)
261 ;
262 /* first, check which sample rate is appropriate: 45, 60 80 or 90 MHz,
263 and set the PLL accordingly (R07[1:0] Fclk, R06[7:4] PLLmult,
264 R06[3:0] PLLphaseDetGain */
265 tmp=cx24110_readreg(state,0x07)&0xfc;
266 if(srate<90999000UL/4) { /* sample rate 45MHz*/
267 cx24110_writereg(state,0x07,tmp);
268 cx24110_writereg(state,0x06,0x78);
269 fclk=90999000UL/2;
270 } else if(srate<60666000UL/2) { /* sample rate 60MHz */
271 cx24110_writereg(state,0x07,tmp|0x1);
272 cx24110_writereg(state,0x06,0xa5);
273 fclk=60666000UL;
274 } else if(srate<80888000UL/2) { /* sample rate 80MHz */
275 cx24110_writereg(state,0x07,tmp|0x2);
276 cx24110_writereg(state,0x06,0x87);
277 fclk=80888000UL;
278 } else { /* sample rate 90MHz */
279 cx24110_writereg(state,0x07,tmp|0x3);
280 cx24110_writereg(state,0x06,0x78);
281 fclk=90999000UL;
282 };
283 dprintk("cx24110 debug: fclk %d Hz\n",fclk);
284 /* we need to divide two integers with approx. 27 bits in 32 bit
285 arithmetic giving a 25 bit result */
286 /* the maximum dividend is 90999000/2, 0x02b6446c, this number is
287 also the most complex divisor. Hence, the dividend has,
288 assuming 32bit unsigned arithmetic, 6 clear bits on top, the
289 divisor 2 unused bits at the bottom. Also, the quotient is
290 always less than 1/2. Borrowed from VES1893.c, of course */
291
292 tmp=srate<<6;
293 BDRI=fclk>>2;
294 ratio=(tmp/BDRI);
295
296 tmp=(tmp%BDRI)<<8;
297 ratio=(ratio<<8)+(tmp/BDRI);
298
299 tmp=(tmp%BDRI)<<8;
300 ratio=(ratio<<8)+(tmp/BDRI);
301
302 tmp=(tmp%BDRI)<<1;
303 ratio=(ratio<<1)+(tmp/BDRI);
304
305 dprintk("srate= %d (range %d, up to %d)\n", srate,i,bands[i]);
306 dprintk("fclk = %d\n", fclk);
307 dprintk("ratio= %08x\n", ratio);
308
309 cx24110_writereg(state, 0x1, (ratio>>16)&0xff);
310 cx24110_writereg(state, 0x2, (ratio>>8)&0xff);
311 cx24110_writereg(state, 0x3, (ratio)&0xff);
312
313 return 0;
314
315}
316
317int cx24110_pll_write (struct dvb_frontend* fe, u32 data)
318{
Johannes Stezenbachb8742702005-05-16 21:54:31 -0700319 struct cx24110_state *state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
321/* tuner data is 21 bits long, must be left-aligned in data */
322/* tuner cx24108 is written through a dedicated 3wire interface on the demod chip */
323/* FIXME (low): add error handling, avoid infinite loops if HW fails... */
324
325 dprintk("cx24110 debug: cx24108_write(%8.8x)\n",data);
326
327 cx24110_writereg(state,0x6d,0x30); /* auto mode at 62kHz */
328 cx24110_writereg(state,0x70,0x15); /* auto mode 21 bits */
329
330 /* if the auto tuner writer is still busy, clear it out */
331 while (cx24110_readreg(state,0x6d)&0x80)
332 cx24110_writereg(state,0x72,0);
333
334 /* write the topmost 8 bits */
335 cx24110_writereg(state,0x72,(data>>24)&0xff);
336
337 /* wait for the send to be completed */
338 while ((cx24110_readreg(state,0x6d)&0xc0)==0x80)
339 ;
340
341 /* send another 8 bytes */
342 cx24110_writereg(state,0x72,(data>>16)&0xff);
343 while ((cx24110_readreg(state,0x6d)&0xc0)==0x80)
344 ;
345
346 /* and the topmost 5 bits of this byte */
347 cx24110_writereg(state,0x72,(data>>8)&0xff);
348 while ((cx24110_readreg(state,0x6d)&0xc0)==0x80)
349 ;
350
351 /* now strobe the enable line once */
352 cx24110_writereg(state,0x6d,0x32);
353 cx24110_writereg(state,0x6d,0x30);
354
355 return 0;
356}
357
358static int cx24110_initfe(struct dvb_frontend* fe)
359{
Johannes Stezenbachb8742702005-05-16 21:54:31 -0700360 struct cx24110_state *state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361/* fixme (low): error handling */
362 int i;
363
364 dprintk("%s: init chip\n", __FUNCTION__);
365
366 for(i=0;i<sizeof(cx24110_regdata)/sizeof(cx24110_regdata[0]);i++) {
367 cx24110_writereg(state, cx24110_regdata[i].reg, cx24110_regdata[i].data);
368 };
369
370 if (state->config->pll_init) state->config->pll_init(fe);
371
372 return 0;
373}
374
375static int cx24110_set_voltage (struct dvb_frontend* fe, fe_sec_voltage_t voltage)
376{
Johannes Stezenbachb8742702005-05-16 21:54:31 -0700377 struct cx24110_state *state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
379 switch (voltage) {
380 case SEC_VOLTAGE_13:
381 return cx24110_writereg(state,0x76,(cx24110_readreg(state,0x76)&0x3b)|0xc0);
382 case SEC_VOLTAGE_18:
383 return cx24110_writereg(state,0x76,(cx24110_readreg(state,0x76)&0x3b)|0x40);
384 default:
385 return -EINVAL;
386 };
387}
388
Johannes Stezenbachb8742702005-05-16 21:54:31 -0700389static int cx24110_diseqc_send_burst(struct dvb_frontend* fe, fe_sec_mini_cmd_t burst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390{
Johannes Stezenbachc589ebf2005-09-09 13:02:37 -0700391 int rv, bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 struct cx24110_state *state = fe->demodulator_priv;
Johannes Stezenbachc589ebf2005-09-09 13:02:37 -0700393 unsigned long timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395 if (burst == SEC_MINI_A)
396 bit = 0x00;
397 else if (burst == SEC_MINI_B)
398 bit = 0x08;
399 else
400 return -EINVAL;
401
402 rv = cx24110_readreg(state, 0x77);
Adam Szalkowski296c7862005-09-09 13:02:35 -0700403 if (!(rv & 0x04))
404 cx24110_writereg(state, 0x77, rv | 0x04);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
406 rv = cx24110_readreg(state, 0x76);
407 cx24110_writereg(state, 0x76, ((rv & 0x90) | 0x40 | bit));
Johannes Stezenbachc589ebf2005-09-09 13:02:37 -0700408 timeout = jiffies + msecs_to_jiffies(100);
409 while (!time_after(jiffies, timeout) && !(cx24110_readreg(state, 0x76) & 0x40))
410 ; /* wait for LNB ready */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
412 return 0;
413}
414
415static int cx24110_send_diseqc_msg(struct dvb_frontend* fe,
416 struct dvb_diseqc_master_cmd *cmd)
417{
418 int i, rv;
Johannes Stezenbachb8742702005-05-16 21:54:31 -0700419 struct cx24110_state *state = fe->demodulator_priv;
Johannes Stezenbachc589ebf2005-09-09 13:02:37 -0700420 unsigned long timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
422 for (i = 0; i < cmd->msg_len; i++)
423 cx24110_writereg(state, 0x79 + i, cmd->msg[i]);
424
425 rv = cx24110_readreg(state, 0x77);
Adam Szalkowski296c7862005-09-09 13:02:35 -0700426 if (rv & 0x04) {
427 cx24110_writereg(state, 0x77, rv & ~0x04);
428 msleep(30); /* reportedly fixes switching problems */
429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431 rv = cx24110_readreg(state, 0x76);
432
433 cx24110_writereg(state, 0x76, ((rv & 0x90) | 0x40) | ((cmd->msg_len-3) & 3));
Johannes Stezenbachc589ebf2005-09-09 13:02:37 -0700434 timeout = jiffies + msecs_to_jiffies(100);
435 while (!time_after(jiffies, timeout) && !(cx24110_readreg(state, 0x76) & 0x40))
436 ; /* wait for LNB ready */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438 return 0;
439}
440
441static int cx24110_read_status(struct dvb_frontend* fe, fe_status_t* status)
442{
Johannes Stezenbachb8742702005-05-16 21:54:31 -0700443 struct cx24110_state *state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 int sync = cx24110_readreg (state, 0x55);
446
447 *status = 0;
448
449 if (sync & 0x10)
450 *status |= FE_HAS_SIGNAL;
451
452 if (sync & 0x08)
453 *status |= FE_HAS_CARRIER;
454
455 sync = cx24110_readreg (state, 0x08);
456
457 if (sync & 0x40)
458 *status |= FE_HAS_VITERBI;
459
460 if (sync & 0x20)
461 *status |= FE_HAS_SYNC;
462
463 if ((sync & 0x60) == 0x60)
464 *status |= FE_HAS_LOCK;
465
466 return 0;
467}
468
469static int cx24110_read_ber(struct dvb_frontend* fe, u32* ber)
470{
Johannes Stezenbachb8742702005-05-16 21:54:31 -0700471 struct cx24110_state *state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
473 /* fixme (maybe): value range is 16 bit. Scale? */
474 if(cx24110_readreg(state,0x24)&0x10) {
475 /* the Viterbi error counter has finished one counting window */
476 cx24110_writereg(state,0x24,0x04); /* select the ber reg */
477 state->lastber=cx24110_readreg(state,0x25)|
478 (cx24110_readreg(state,0x26)<<8);
479 cx24110_writereg(state,0x24,0x04); /* start new count window */
480 cx24110_writereg(state,0x24,0x14);
481 }
482 *ber = state->lastber;
483
484 return 0;
485}
486
487static int cx24110_read_signal_strength(struct dvb_frontend* fe, u16* signal_strength)
488{
Johannes Stezenbachb8742702005-05-16 21:54:31 -0700489 struct cx24110_state *state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
491/* no provision in hardware. Read the frontend AGC accumulator. No idea how to scale this, but I know it is 2s complement */
492 u8 signal = cx24110_readreg (state, 0x27)+128;
493 *signal_strength = (signal << 8) | signal;
494
495 return 0;
496}
497
498static int cx24110_read_snr(struct dvb_frontend* fe, u16* snr)
499{
Johannes Stezenbachb8742702005-05-16 21:54:31 -0700500 struct cx24110_state *state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502 /* no provision in hardware. Can be computed from the Es/N0 estimator, but I don't know how. */
503 if(cx24110_readreg(state,0x6a)&0x80) {
504 /* the Es/N0 error counter has finished one counting window */
505 state->lastesn0=cx24110_readreg(state,0x69)|
506 (cx24110_readreg(state,0x68)<<8);
507 cx24110_writereg(state,0x6a,0x84); /* start new count window */
508 }
509 *snr = state->lastesn0;
510
511 return 0;
512}
513
514static int cx24110_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
515{
Johannes Stezenbachb8742702005-05-16 21:54:31 -0700516 struct cx24110_state *state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 u32 lastbyer;
518
519 if(cx24110_readreg(state,0x10)&0x40) {
520 /* the RS error counter has finished one counting window */
521 cx24110_writereg(state,0x10,0x60); /* select the byer reg */
522 lastbyer=cx24110_readreg(state,0x12)|
523 (cx24110_readreg(state,0x13)<<8)|
524 (cx24110_readreg(state,0x14)<<16);
525 cx24110_writereg(state,0x10,0x70); /* select the bler reg */
526 state->lastbler=cx24110_readreg(state,0x12)|
527 (cx24110_readreg(state,0x13)<<8)|
528 (cx24110_readreg(state,0x14)<<16);
529 cx24110_writereg(state,0x10,0x20); /* start new count window */
530 }
531 *ucblocks = state->lastbler;
532
533 return 0;
534}
535
536static int cx24110_set_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters *p)
537{
Johannes Stezenbachb8742702005-05-16 21:54:31 -0700538 struct cx24110_state *state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
540 state->config->pll_set(fe, p);
541 cx24110_set_inversion (state, p->inversion);
542 cx24110_set_fec (state, p->u.qpsk.fec_inner);
543 cx24110_set_symbolrate (state, p->u.qpsk.symbol_rate);
544 cx24110_writereg(state,0x04,0x05); /* start aquisition */
545
546 return 0;
547}
548
549static int cx24110_get_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters *p)
550{
Johannes Stezenbachb8742702005-05-16 21:54:31 -0700551 struct cx24110_state *state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 s32 afc; unsigned sclk;
553
554/* cannot read back tuner settings (freq). Need to have some private storage */
555
556 sclk = cx24110_readreg (state, 0x07) & 0x03;
557/* ok, real AFC (FEDR) freq. is afc/2^24*fsamp, fsamp=45/60/80/90MHz.
558 * Need 64 bit arithmetic. Is thiss possible in the kernel? */
559 if (sclk==0) sclk=90999000L/2L;
560 else if (sclk==1) sclk=60666000L;
561 else if (sclk==2) sclk=80888000L;
562 else sclk=90999000L;
563 sclk>>=8;
564 afc = sclk*(cx24110_readreg (state, 0x44)&0x1f)+
565 ((sclk*cx24110_readreg (state, 0x45))>>8)+
566 ((sclk*cx24110_readreg (state, 0x46))>>16);
567
568 p->frequency += afc;
569 p->inversion = (cx24110_readreg (state, 0x22) & 0x10) ?
570 INVERSION_ON : INVERSION_OFF;
571 p->u.qpsk.fec_inner = cx24110_get_fec (state);
572
573 return 0;
574}
575
576static int cx24110_set_tone(struct dvb_frontend* fe, fe_sec_tone_mode_t tone)
577{
Johannes Stezenbachb8742702005-05-16 21:54:31 -0700578 struct cx24110_state *state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580 return cx24110_writereg(state,0x76,(cx24110_readreg(state,0x76)&~0x10)|(((tone==SEC_TONE_ON))?0x10:0));
581}
582
583static void cx24110_release(struct dvb_frontend* fe)
584{
Johannes Stezenbachb8742702005-05-16 21:54:31 -0700585 struct cx24110_state* state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 kfree(state);
587}
588
589static struct dvb_frontend_ops cx24110_ops;
590
591struct dvb_frontend* cx24110_attach(const struct cx24110_config* config,
592 struct i2c_adapter* i2c)
593{
594 struct cx24110_state* state = NULL;
595 int ret;
596
597 /* allocate memory for the internal state */
Johannes Stezenbachb8742702005-05-16 21:54:31 -0700598 state = kmalloc(sizeof(struct cx24110_state), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 if (state == NULL) goto error;
600
601 /* setup the state */
602 state->config = config;
603 state->i2c = i2c;
604 memcpy(&state->ops, &cx24110_ops, sizeof(struct dvb_frontend_ops));
605 state->lastber = 0;
606 state->lastbler = 0;
607 state->lastesn0 = 0;
608
609 /* check if the demod is there */
610 ret = cx24110_readreg(state, 0x00);
611 if ((ret != 0x5a) && (ret != 0x69)) goto error;
612
613 /* create dvb_frontend */
614 state->frontend.ops = &state->ops;
615 state->frontend.demodulator_priv = state;
616 return &state->frontend;
617
618error:
619 kfree(state);
620 return NULL;
621}
622
623static struct dvb_frontend_ops cx24110_ops = {
624
625 .info = {
626 .name = "Conexant CX24110 DVB-S",
627 .type = FE_QPSK,
628 .frequency_min = 950000,
629 .frequency_max = 2150000,
630 .frequency_stepsize = 1011, /* kHz for QPSK frontends */
631 .frequency_tolerance = 29500,
632 .symbol_rate_min = 1000000,
633 .symbol_rate_max = 45000000,
634 .caps = FE_CAN_INVERSION_AUTO |
635 FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
636 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
637 FE_CAN_QPSK | FE_CAN_RECOVER
638 },
639
640 .release = cx24110_release,
641
642 .init = cx24110_initfe,
643 .set_frontend = cx24110_set_frontend,
644 .get_frontend = cx24110_get_frontend,
645 .read_status = cx24110_read_status,
646 .read_ber = cx24110_read_ber,
647 .read_signal_strength = cx24110_read_signal_strength,
648 .read_snr = cx24110_read_snr,
649 .read_ucblocks = cx24110_read_ucblocks,
650
651 .diseqc_send_master_cmd = cx24110_send_diseqc_msg,
652 .set_tone = cx24110_set_tone,
653 .set_voltage = cx24110_set_voltage,
654 .diseqc_send_burst = cx24110_diseqc_send_burst,
655};
656
657module_param(debug, int, 0644);
658MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
659
660MODULE_DESCRIPTION("Conexant CX24110 DVB-S Demodulator driver");
661MODULE_AUTHOR("Peter Hettkamp");
662MODULE_LICENSE("GPL");
663
664EXPORT_SYMBOL(cx24110_attach);
665EXPORT_SYMBOL(cx24110_pll_write);