]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/media/dvb/frontends/cx24123.c
cfb7d2edd5a92f445abd9edcdd14a33d2129649e
[karo-tx-linux.git] / drivers / media / dvb / frontends / cx24123.c
1 /*
2     Conexant cx24123/cx24109 - DVB QPSK Satellite demod/tuner driver
3
4     Copyright (C) 2005 Steven Toth <stoth@hauppauge.com>
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include <linux/slab.h>
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/moduleparam.h>
25 #include <linux/init.h>
26
27 #include "dvb_frontend.h"
28 #include "cx24123.h"
29
30 static int debug;
31 #define dprintk(args...) \
32         do { \
33                 if (debug) printk (KERN_DEBUG "cx24123: " args); \
34         } while (0)
35
36 struct cx24123_state
37 {
38         struct i2c_adapter* i2c;
39         struct dvb_frontend_ops ops;
40         const struct cx24123_config* config;
41
42         struct dvb_frontend frontend;
43
44         u32 lastber;
45         u16 snr;
46         u8  lnbreg;
47
48         /* Some PLL specifics for tuning */
49         u32 VCAarg;
50         u32 VGAarg;
51         u32 bandselectarg;
52         u32 pllarg;
53
54         /* The Demod/Tuner can't easily provide these, we cache them */
55         u32 currentfreq;
56         u32 currentsymbolrate;
57 };
58
59 /* Various tuner defaults need to be established for a given symbol rate Sps */
60 static struct
61 {
62         u32 symbolrate_low;
63         u32 symbolrate_high;
64         u32 VCAslope;
65         u32 VCAoffset;
66         u32 VGA1offset;
67         u32 VGA2offset;
68         u32 VCAprogdata;
69         u32 VGAprogdata;
70 } cx24123_AGC_vals[] =
71 {
72         {
73                 .symbolrate_low         = 1000000,
74                 .symbolrate_high        = 4999999,
75                 .VCAslope               = 0x07,
76                 .VCAoffset              = 0x0f,
77                 .VGA1offset             = 0x1f8,
78                 .VGA2offset             = 0x1f8,
79                 .VGAprogdata            = (2 << 18) | (0x1f8 << 9) | 0x1f8,
80                 .VCAprogdata            = (4 << 18) | (0x07 << 9) | 0x07,
81         },
82         {
83                 .symbolrate_low         =  5000000,
84                 .symbolrate_high        = 14999999,
85                 .VCAslope               = 0x1f,
86                 .VCAoffset              = 0x1f,
87                 .VGA1offset             = 0x1e0,
88                 .VGA2offset             = 0x180,
89                 .VGAprogdata            = (2 << 18) | (0x180 << 9) | 0x1e0,
90                 .VCAprogdata            = (4 << 18) | (0x07 << 9) | 0x1f,
91         },
92         {
93                 .symbolrate_low         = 15000000,
94                 .symbolrate_high        = 45000000,
95                 .VCAslope               = 0x3f,
96                 .VCAoffset              = 0x3f,
97                 .VGA1offset             = 0x180,
98                 .VGA2offset             = 0x100,
99                 .VGAprogdata            = (2 << 18) | (0x100 << 9) | 0x180,
100                 .VCAprogdata            = (4 << 18) | (0x07 << 9) | 0x3f,
101         },
102 };
103
104 /*
105  * Various tuner defaults need to be established for a given frequency kHz.
106  * fixme: The bounds on the bands do not match the doc in real life.
107  * fixme: Some of them have been moved, other might need adjustment.
108  */
109 static struct
110 {
111         u32 freq_low;
112         u32 freq_high;
113         u32 bandselect;
114         u32 VCOdivider;
115         u32 VCOnumber;
116         u32 progdata;
117 } cx24123_bandselect_vals[] =
118 {
119         {
120                 .freq_low       = 950000,
121                 .freq_high      = 1018999,
122                 .bandselect     = 0x40,
123                 .VCOdivider     = 4,
124                 .VCOnumber      = 7,
125                 .progdata       = (0 << 18) | (0 << 9) | 0x40,
126         },
127         {
128                 .freq_low       = 1019000,
129                 .freq_high      = 1074999,
130                 .bandselect     = 0x80,
131                 .VCOdivider     = 4,
132                 .VCOnumber      = 8,
133                 .progdata       = (0 << 18) | (0 << 9) | 0x80,
134         },
135         {
136                 .freq_low       = 1075000,
137                 .freq_high      = 1227999,
138                 .bandselect     = 0x01,
139                 .VCOdivider     = 2,
140                 .VCOnumber      = 1,
141                 .progdata       = (0 << 18) | (1 << 9) | 0x01,
142         },
143         {
144                 .freq_low       = 1228000,
145                 .freq_high      = 1349999,
146                 .bandselect     = 0x02,
147                 .VCOdivider     = 2,
148                 .VCOnumber      = 2,
149                 .progdata       = (0 << 18) | (1 << 9) | 0x02,
150         },
151         {
152                 .freq_low       = 1350000,
153                 .freq_high      = 1481999,
154                 .bandselect     = 0x04,
155                 .VCOdivider     = 2,
156                 .VCOnumber      = 3,
157                 .progdata       = (0 << 18) | (1 << 9) | 0x04,
158         },
159         {
160                 .freq_low       = 1482000,
161                 .freq_high      = 1595999,
162                 .bandselect     = 0x08,
163                 .VCOdivider     = 2,
164                 .VCOnumber      = 4,
165                 .progdata       = (0 << 18) | (1 << 9) | 0x08,
166         },
167         {
168                 .freq_low       = 1596000,
169                 .freq_high      = 1717999,
170                 .bandselect     = 0x10,
171                 .VCOdivider     = 2,
172                 .VCOnumber      = 5,
173                 .progdata       = (0 << 18) | (1 << 9) | 0x10,
174         },
175         {
176                 .freq_low       = 1718000,
177                 .freq_high      = 1855999,
178                 .bandselect     = 0x20,
179                 .VCOdivider     = 2,
180                 .VCOnumber      = 6,
181                 .progdata       = (0 << 18) | (1 << 9) | 0x20,
182         },
183         {
184                 .freq_low       = 1856000,
185                 .freq_high      = 2035999,
186                 .bandselect     = 0x40,
187                 .VCOdivider     = 2,
188                 .VCOnumber      = 7,
189                 .progdata       = (0 << 18) | (1 << 9) | 0x40,
190         },
191         {
192                 .freq_low       = 2036000,
193                 .freq_high      = 2149999,
194                 .bandselect     = 0x80,
195                 .VCOdivider     = 2,
196                 .VCOnumber      = 8,
197                 .progdata       = (0 << 18) | (1 << 9) | 0x80,
198         },
199 };
200
201 static struct {
202         u8 reg;
203         u8 data;
204 } cx24123_regdata[] =
205 {
206         {0x00, 0x03}, /* Reset system */
207         {0x00, 0x00}, /* Clear reset */
208         {0x01, 0x3b}, /* Apply sensible defaults, from an i2c sniffer */
209         {0x03, 0x07},
210         {0x04, 0x10},
211         {0x05, 0x04},
212         {0x06, 0x31},
213         {0x0d, 0x02},
214         {0x0e, 0x03},
215         {0x0f, 0xfe},
216         {0x10, 0x01},
217         {0x14, 0x01},
218         {0x15, 0x98},
219         {0x16, 0x00},
220         {0x17, 0x01},
221         {0x1b, 0x05},
222         {0x1c, 0x80},
223         {0x1d, 0x00},
224         {0x1e, 0x00},
225         {0x20, 0x41},
226         {0x21, 0x15},
227         {0x27, 0x14},
228         {0x28, 0x46},
229         {0x29, 0x00},
230         {0x2a, 0xb0},
231         {0x2b, 0x73},
232         {0x2c, 0x00},
233         {0x2d, 0x00},
234         {0x2e, 0x00},
235         {0x2f, 0x00},
236         {0x30, 0x00},
237         {0x31, 0x00},
238         {0x32, 0x8c},
239         {0x33, 0x00},
240         {0x34, 0x00},
241         {0x35, 0x03},
242         {0x36, 0x02},
243         {0x37, 0x3a},
244         {0x3a, 0x00},   /* Enable AGC accumulator */
245         {0x44, 0x00},
246         {0x45, 0x00},
247         {0x46, 0x05},
248         {0x56, 0x41},
249         {0x57, 0xff},
250         {0x67, 0x83},
251 };
252
253 static int cx24123_writereg(struct cx24123_state* state, int reg, int data)
254 {
255         u8 buf[] = { reg, data };
256         struct i2c_msg msg = { .addr = state->config->demod_address, .flags = 0, .buf = buf, .len = 2 };
257         int err;
258
259         if ((err = i2c_transfer(state->i2c, &msg, 1)) != 1) {
260                 printk("%s: writereg error(err == %i, reg == 0x%02x,"
261                          " data == 0x%02x)\n", __FUNCTION__, err, reg, data);
262                 return -EREMOTEIO;
263         }
264
265         return 0;
266 }
267
268 static int cx24123_writelnbreg(struct cx24123_state* state, int reg, int data)
269 {
270         u8 buf[] = { reg, data };
271         /* fixme: put the intersil addr int the config */
272         struct i2c_msg msg = { .addr = 0x08, .flags = 0, .buf = buf, .len = 2 };
273         int err;
274
275         if ((err = i2c_transfer(state->i2c, &msg, 1)) != 1) {
276                 printk("%s: writelnbreg error (err == %i, reg == 0x%02x,"
277                          " data == 0x%02x)\n", __FUNCTION__, err, reg, data);
278                 return -EREMOTEIO;
279         }
280
281         /* cache the write, no way to read back */
282         state->lnbreg = data;
283
284         return 0;
285 }
286
287 static int cx24123_readreg(struct cx24123_state* state, u8 reg)
288 {
289         int ret;
290         u8 b0[] = { reg };
291         u8 b1[] = { 0 };
292         struct i2c_msg msg[] = {
293                 { .addr = state->config->demod_address, .flags = 0, .buf = b0, .len = 1 },
294                 { .addr = state->config->demod_address, .flags = I2C_M_RD, .buf = b1, .len = 1 }
295         };
296
297         ret = i2c_transfer(state->i2c, msg, 2);
298
299         if (ret != 2) {
300                 printk("%s: reg=0x%x (error=%d)\n", __FUNCTION__, reg, ret);
301                 return ret;
302         }
303
304         return b1[0];
305 }
306
307 static int cx24123_readlnbreg(struct cx24123_state* state, u8 reg)
308 {
309         return state->lnbreg;
310 }
311
312 static int cx24123_set_inversion(struct cx24123_state* state, fe_spectral_inversion_t inversion)
313 {
314         switch (inversion) {
315         case INVERSION_OFF:
316                 cx24123_writereg(state, 0x0e, cx24123_readreg(state, 0x0e) & 0x7f);
317                 cx24123_writereg(state, 0x10, cx24123_readreg(state, 0x10) | 0x80);
318                 break;
319         case INVERSION_ON:
320                 cx24123_writereg(state, 0x0e, cx24123_readreg(state, 0x0e) | 0x80);
321                 cx24123_writereg(state, 0x10, cx24123_readreg(state, 0x10) | 0x80);
322                 break;
323         case INVERSION_AUTO:
324                 cx24123_writereg(state, 0x10, cx24123_readreg(state, 0x10) & 0x7f);
325                 break;
326         default:
327                 return -EINVAL;
328         }
329
330         return 0;
331 }
332
333 static int cx24123_get_inversion(struct cx24123_state* state, fe_spectral_inversion_t *inversion)
334 {
335         u8 val;
336
337         val = cx24123_readreg(state, 0x1b) >> 7;
338
339         if (val == 0)
340                 *inversion = INVERSION_OFF;
341         else
342                 *inversion = INVERSION_ON;
343
344         return 0;
345 }
346
347 static int cx24123_set_fec(struct cx24123_state* state, fe_code_rate_t fec)
348 {
349         if ( (fec < FEC_NONE) || (fec > FEC_AUTO) )
350                 fec = FEC_AUTO;
351
352         /* Hardware has 5/11 and 3/5 but are never unused */
353         switch (fec) {
354         case FEC_NONE:
355                 return cx24123_writereg(state, 0x0f, 0x01);
356         case FEC_1_2:
357                 return cx24123_writereg(state, 0x0f, 0x02);
358         case FEC_2_3:
359                 return cx24123_writereg(state, 0x0f, 0x04);
360         case FEC_3_4:
361                 return cx24123_writereg(state, 0x0f, 0x08);
362         case FEC_5_6:
363                 return cx24123_writereg(state, 0x0f, 0x20);
364         case FEC_7_8:
365                 return cx24123_writereg(state, 0x0f, 0x80);
366         case FEC_AUTO:
367                 return cx24123_writereg(state, 0x0f, 0xae);
368         default:
369                 return -EOPNOTSUPP;
370         }
371 }
372
373 static int cx24123_get_fec(struct cx24123_state* state, fe_code_rate_t *fec)
374 {
375         int ret;
376         u8 val;
377
378         ret = cx24123_readreg (state, 0x1b);
379         if (ret < 0)
380                 return ret;
381         val = ret & 0x07;
382         switch (val) {
383         case 1:
384                 *fec = FEC_1_2;
385                 break;
386         case 3:
387                 *fec = FEC_2_3;
388                 break;
389         case 4:
390                 *fec = FEC_3_4;
391                 break;
392         case 5:
393                 *fec = FEC_4_5;
394                 break;
395         case 6:
396                 *fec = FEC_5_6;
397                 break;
398         case 7:
399                 *fec = FEC_7_8;
400                 break;
401         case 2: /* *fec = FEC_3_5; break; */
402         case 0: /* *fec = FEC_5_11; break; */
403                 *fec = FEC_AUTO;
404                 break;
405         default:
406                 *fec = FEC_NONE; // can't happen
407         }
408
409         return 0;
410 }
411
412 /* fixme: Symbol rates < 3MSps may not work because of precision loss */
413 static int cx24123_set_symbolrate(struct cx24123_state* state, u32 srate)
414 {
415         u32 val;
416
417         val = (srate / 1185) * 100;
418
419         /* Compensate for scaling up, by removing 17 symbols per 1Msps */
420         val = val - (17 * (srate / 1000000));
421
422         cx24123_writereg(state, 0x08, (val >> 16) & 0xff );
423         cx24123_writereg(state, 0x09, (val >>  8) & 0xff );
424         cx24123_writereg(state, 0x0a, (val      ) & 0xff );
425
426         return 0;
427 }
428
429 /*
430  * Based on the required frequency and symbolrate, the tuner AGC has to be configured
431  * and the correct band selected. Calculate those values
432  */
433 static int cx24123_pll_calculate(struct dvb_frontend* fe, struct dvb_frontend_parameters *p)
434 {
435         struct cx24123_state *state = fe->demodulator_priv;
436         u32 ndiv = 0, adiv = 0, vco_div = 0;
437         int i = 0;
438
439         /* Defaults for low freq, low rate */
440         state->VCAarg = cx24123_AGC_vals[0].VCAprogdata;
441         state->VGAarg = cx24123_AGC_vals[0].VGAprogdata;
442         state->bandselectarg = cx24123_bandselect_vals[0].progdata;
443         vco_div = cx24123_bandselect_vals[0].VCOdivider;
444
445         /* For the given symbolerate, determine the VCA and VGA programming bits */
446         for (i = 0; i < sizeof(cx24123_AGC_vals) / sizeof(cx24123_AGC_vals[0]); i++)
447         {
448                 if ((cx24123_AGC_vals[i].symbolrate_low <= p->u.qpsk.symbol_rate) &&
449                                 (cx24123_AGC_vals[i].symbolrate_high >= p->u.qpsk.symbol_rate) ) {
450                         state->VCAarg = cx24123_AGC_vals[i].VCAprogdata;
451                         state->VGAarg = cx24123_AGC_vals[i].VGAprogdata;
452                 }
453         }
454
455         /* For the given frequency, determine the bandselect programming bits */
456         for (i = 0; i < sizeof(cx24123_bandselect_vals) / sizeof(cx24123_bandselect_vals[0]); i++)
457         {
458                 if ((cx24123_bandselect_vals[i].freq_low <= p->frequency) &&
459                                 (cx24123_bandselect_vals[i].freq_high >= p->frequency) ) {
460                         state->bandselectarg = cx24123_bandselect_vals[i].progdata;
461                         vco_div = cx24123_bandselect_vals[i].VCOdivider;
462                 }
463         }
464
465         /* Determine the N/A dividers for the requested lband freq (in kHz). */
466         /* Note: 10111 (kHz) is the Crystal Freq and divider of 10. */
467         ndiv = ( ((p->frequency * vco_div) / (10111 / 10) / 2) / 32) & 0x1ff;
468         adiv = ( ((p->frequency * vco_div) / (10111 / 10) / 2) % 32) & 0x1f;
469
470         if (adiv == 0)
471                 adiv++;
472
473         /* determine the correct pll frequency values. */
474         /* Command 11, refdiv 11, cpump polarity 1, cpump current 3mA 10. */
475         state->pllarg = (3 << 19) | (3 << 17) | (1 << 16) | (2 << 14);
476         state->pllarg |= (ndiv << 5) | adiv;
477
478         return 0;
479 }
480
481 /*
482  * Tuner data is 21 bits long, must be left-aligned in data.
483  * Tuner cx24109 is written through a dedicated 3wire interface on the demod chip.
484  */
485 static int cx24123_pll_writereg(struct dvb_frontend* fe, struct dvb_frontend_parameters *p, u32 data)
486 {
487         struct cx24123_state *state = fe->demodulator_priv;
488
489         u8 timeout = 0;
490
491         /* align the 21 bytes into to bit23 boundary */
492         data = data << 3;
493
494         /* Reset the demod pll word length to 0x15 bits */
495         cx24123_writereg(state, 0x21, 0x15);
496
497         timeout = 0;
498         /* write the msb 8 bits, wait for the send to be completed */
499         cx24123_writereg(state, 0x22, (data >> 16) & 0xff);
500         while ( ( cx24123_readreg(state, 0x20) & 0x40 ) == 0 )
501         {
502                 /* Safety - No reason why the write should not complete, and we never get here, avoid hang */
503                 if (timeout++ >= 4) {
504                         printk("%s:  demodulator is no longer responding, aborting.\n",__FUNCTION__);
505                         return -EREMOTEIO;
506                 }
507                 msleep(500);
508         }
509
510         timeout = 0;
511         /* send another 8 bytes, wait for the send to be completed */
512         cx24123_writereg(state, 0x22, (data>>8) & 0xff );
513         while ( (cx24123_readreg(state, 0x20) & 0x40 ) == 0 )
514         {
515                 /* Safety - No reason why the write should not complete, and we never get here, avoid hang */
516                 if (timeout++ >= 4) {
517                         printk("%s:  demodulator is not responding, possibly hung, aborting.\n",__FUNCTION__);
518                         return -EREMOTEIO;
519                 }
520                 msleep(500);
521         }
522
523         timeout = 0;
524         /* send the lower 5 bits of this byte, padded with 3 LBB, wait for the send to be completed */
525         cx24123_writereg(state, 0x22, (data) & 0xff );
526         while ((cx24123_readreg(state, 0x20) & 0x80))
527         {
528                 /* Safety - No reason why the write should not complete, and we never get here, avoid hang */
529                 if (timeout++ >= 4) {
530                         printk("%s:  demodulator is not responding, possibly hung, aborting.\n",__FUNCTION__);
531                         return -EREMOTEIO;
532                 }
533                 msleep(500);
534         }
535
536         /* Trigger the demod to configure the tuner */
537         cx24123_writereg(state, 0x20, cx24123_readreg(state, 0x20) | 2);
538         cx24123_writereg(state, 0x20, cx24123_readreg(state, 0x20) & 0xfd);
539
540         return 0;
541 }
542
543 static int cx24123_pll_tune(struct dvb_frontend* fe, struct dvb_frontend_parameters *p)
544 {
545         struct cx24123_state *state = fe->demodulator_priv;
546
547         if (cx24123_pll_calculate(fe, p) != 0) {
548                 printk("%s: cx24123_pll_calcutate failed\n",__FUNCTION__);
549                 return -EINVAL;
550         }
551
552         /* Write the new VCO/VGA */
553         cx24123_pll_writereg(fe, p, state->VCAarg);
554         cx24123_pll_writereg(fe, p, state->VGAarg);
555
556         /* Write the new bandselect and pll args */
557         cx24123_pll_writereg(fe, p, state->bandselectarg);
558         cx24123_pll_writereg(fe, p, state->pllarg);
559
560         return 0;
561 }
562
563 static int cx24123_initfe(struct dvb_frontend* fe)
564 {
565         struct cx24123_state *state = fe->demodulator_priv;
566         int i;
567
568         /* Configure the demod to a good set of defaults */
569         for (i = 0; i < sizeof(cx24123_regdata) / sizeof(cx24123_regdata[0]); i++)
570                 cx24123_writereg(state, cx24123_regdata[i].reg, cx24123_regdata[i].data);
571
572         if (state->config->pll_init)
573                 state->config->pll_init(fe);
574
575         /* Configure the LNB for 14V */
576         cx24123_writelnbreg(state, 0x0, 0x2a);
577
578         return 0;
579 }
580
581 static int cx24123_set_voltage(struct dvb_frontend* fe, fe_sec_voltage_t voltage)
582 {
583         struct cx24123_state *state = fe->demodulator_priv;
584         u8 val;
585
586         val = cx24123_readlnbreg(state, 0x0);
587
588         switch (voltage) {
589         case SEC_VOLTAGE_13:
590                 return cx24123_writelnbreg(state, 0x0, val & 0x32); /* V 13v */
591         case SEC_VOLTAGE_18:
592                 return cx24123_writelnbreg(state, 0x0, val | 0x04); /* H 18v */
593         case SEC_VOLTAGE_OFF:
594                 return cx24123_writelnbreg(state, 0x0, val & 0x30);
595         default:
596                 return -EINVAL;
597         };
598 }
599
600 static int cx24123_send_diseqc_msg(struct dvb_frontend* fe,
601                                    struct dvb_diseqc_master_cmd *cmd)
602 {
603         /* fixme: Implement diseqc */
604         printk("%s: No support yet\n",__FUNCTION__);
605
606         return -ENOTSUPP;
607 }
608
609 static int cx24123_read_status(struct dvb_frontend* fe, fe_status_t* status)
610 {
611         struct cx24123_state *state = fe->demodulator_priv;
612
613         int sync = cx24123_readreg(state, 0x14);
614         int lock = cx24123_readreg(state, 0x20);
615
616         *status = 0;
617         if (lock & 0x01)
618                 *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
619         if (sync & 0x04)
620                 *status |= FE_HAS_VITERBI;
621         if (sync & 0x08)
622                 *status |= FE_HAS_CARRIER;
623         if (sync & 0x80)
624                 *status |= FE_HAS_SYNC | FE_HAS_LOCK;
625
626         return 0;
627 }
628
629 /*
630  * Configured to return the measurement of errors in blocks, because no UCBLOCKS value
631  * is available, so this value doubles up to satisfy both measurements
632  */
633 static int cx24123_read_ber(struct dvb_frontend* fe, u32* ber)
634 {
635         struct cx24123_state *state = fe->demodulator_priv;
636
637         state->lastber =
638                 ((cx24123_readreg(state, 0x1c) & 0x3f) << 16) |
639                 (cx24123_readreg(state, 0x1d) << 8 |
640                 cx24123_readreg(state, 0x1e));
641
642         /* Do the signal quality processing here, it's derived from the BER. */
643         /* Scale the BER from a 24bit to a SNR 16 bit where higher = better */
644         if (state->lastber < 5000)
645                 state->snr = 655*100;
646         else if ( (state->lastber >=   5000) && (state->lastber <  55000) )
647                 state->snr = 655*90;
648         else if ( (state->lastber >=  55000) && (state->lastber < 150000) )
649                 state->snr = 655*80;
650         else if ( (state->lastber >= 150000) && (state->lastber < 250000) )
651                 state->snr = 655*70;
652         else if ( (state->lastber >= 250000) && (state->lastber < 450000) )
653                 state->snr = 655*65;
654         else
655                 state->snr = 0;
656
657         *ber = state->lastber;
658
659         return 0;
660 }
661
662 static int cx24123_read_signal_strength(struct dvb_frontend* fe, u16* signal_strength)
663 {
664         struct cx24123_state *state = fe->demodulator_priv;
665         *signal_strength = cx24123_readreg(state, 0x3b) << 8; /* larger = better */
666
667         return 0;
668 }
669
670 static int cx24123_read_snr(struct dvb_frontend* fe, u16* snr)
671 {
672         struct cx24123_state *state = fe->demodulator_priv;
673         *snr = state->snr;
674
675         return 0;
676 }
677
678 static int cx24123_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
679 {
680         struct cx24123_state *state = fe->demodulator_priv;
681         *ucblocks = state->lastber;
682
683         return 0;
684 }
685
686 static int cx24123_set_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters *p)
687 {
688         struct cx24123_state *state = fe->demodulator_priv;
689
690         if (state->config->set_ts_params)
691                 state->config->set_ts_params(fe, 0);
692
693         state->currentfreq=p->frequency;
694         state->currentsymbolrate = p->u.qpsk.symbol_rate;
695
696         cx24123_set_inversion(state, p->inversion);
697         cx24123_set_fec(state, p->u.qpsk.fec_inner);
698         cx24123_set_symbolrate(state, p->u.qpsk.symbol_rate);
699         cx24123_pll_tune(fe, p);
700
701         /* Enable automatic aquisition and reset cycle */
702         cx24123_writereg(state, 0x03, (cx24123_readreg(state, 0x03) | 0x07));
703         cx24123_writereg(state, 0x00, 0x10);
704         cx24123_writereg(state, 0x00, 0);
705
706         return 0;
707 }
708
709 static int cx24123_get_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters *p)
710 {
711         struct cx24123_state *state = fe->demodulator_priv;
712
713         if (cx24123_get_inversion(state, &p->inversion) != 0) {
714                 printk("%s: Failed to get inversion status\n",__FUNCTION__);
715                 return -EREMOTEIO;
716         }
717         if (cx24123_get_fec(state, &p->u.qpsk.fec_inner) != 0) {
718                 printk("%s: Failed to get fec status\n",__FUNCTION__);
719                 return -EREMOTEIO;
720         }
721         p->frequency = state->currentfreq;
722         p->u.qpsk.symbol_rate = state->currentsymbolrate;
723
724         return 0;
725 }
726
727 static int cx24123_set_tone(struct dvb_frontend* fe, fe_sec_tone_mode_t tone)
728 {
729         struct cx24123_state *state = fe->demodulator_priv;
730         u8 val;
731
732         val = cx24123_readlnbreg(state, 0x0);
733
734         switch (tone) {
735         case SEC_TONE_ON:
736                 return cx24123_writelnbreg(state, 0x0, val | 0x10);
737         case SEC_TONE_OFF:
738                 return cx24123_writelnbreg(state, 0x0, val & 0x2f);
739         default:
740                 printk("%s: CASE reached default with tone=%d\n", __FUNCTION__, tone);
741                 return -EINVAL;
742         }
743 }
744
745 static void cx24123_release(struct dvb_frontend* fe)
746 {
747         struct cx24123_state* state = fe->demodulator_priv;
748         dprintk("%s\n",__FUNCTION__);
749         kfree(state);
750 }
751
752 static struct dvb_frontend_ops cx24123_ops;
753
754 struct dvb_frontend* cx24123_attach(const struct cx24123_config* config,
755                                     struct i2c_adapter* i2c)
756 {
757         struct cx24123_state* state = NULL;
758         int ret;
759
760         dprintk("%s\n",__FUNCTION__);
761
762         /* allocate memory for the internal state */
763         state = kmalloc(sizeof(struct cx24123_state), GFP_KERNEL);
764         if (state == NULL) {
765                 printk("Unable to kmalloc\n");
766                 goto error;
767         }
768
769         /* setup the state */
770         state->config = config;
771         state->i2c = i2c;
772         memcpy(&state->ops, &cx24123_ops, sizeof(struct dvb_frontend_ops));
773         state->lastber = 0;
774         state->snr = 0;
775         state->lnbreg = 0;
776         state->VCAarg = 0;
777         state->VGAarg = 0;
778         state->bandselectarg = 0;
779         state->pllarg = 0;
780         state->currentfreq = 0;
781         state->currentsymbolrate = 0;
782
783         /* check if the demod is there */
784         ret = cx24123_readreg(state, 0x00);
785         if ((ret != 0xd1) && (ret != 0xe1)) {
786                 printk("Version != d1 or e1\n");
787                 goto error;
788         }
789
790         /* create dvb_frontend */
791         state->frontend.ops = &state->ops;
792         state->frontend.demodulator_priv = state;
793         return &state->frontend;
794
795 error:
796         kfree(state);
797
798         return NULL;
799 }
800
801 static struct dvb_frontend_ops cx24123_ops = {
802
803         .info = {
804                 .name = "Conexant CX24123/CX24109",
805                 .type = FE_QPSK,
806                 .frequency_min = 950000,
807                 .frequency_max = 2150000,
808                 .frequency_stepsize = 1011, /* kHz for QPSK frontends */
809                 .frequency_tolerance = 29500,
810                 .symbol_rate_min = 1000000,
811                 .symbol_rate_max = 45000000,
812                 .caps = FE_CAN_INVERSION_AUTO |
813                         FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
814                         FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
815                         FE_CAN_QPSK | FE_CAN_RECOVER
816         },
817
818         .release = cx24123_release,
819
820         .init = cx24123_initfe,
821         .set_frontend = cx24123_set_frontend,
822         .get_frontend = cx24123_get_frontend,
823         .read_status = cx24123_read_status,
824         .read_ber = cx24123_read_ber,
825         .read_signal_strength = cx24123_read_signal_strength,
826         .read_snr = cx24123_read_snr,
827         .read_ucblocks = cx24123_read_ucblocks,
828         .diseqc_send_master_cmd = cx24123_send_diseqc_msg,
829         .set_tone = cx24123_set_tone,
830         .set_voltage = cx24123_set_voltage,
831 };
832
833 module_param(debug, int, 0644);
834 MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
835
836 MODULE_DESCRIPTION("DVB Frontend module for Conexant cx24123/cx24109 hardware");
837 MODULE_AUTHOR("Steven Toth");
838 MODULE_LICENSE("GPL");
839
840 EXPORT_SYMBOL(cx24123_attach);