vserver 1.9.5.x5
[linux-2.6.git] / drivers / media / dvb / frontends / stv0299.c
1 /*
2     Driver for ST STV0299 demodulator
3
4     Copyright (C) 2001-2002 Convergence Integrated Media GmbH
5         <ralph@convergence.de>,
6         <holger@convergence.de>,
7         <js@convergence.de>
8     
9
10     Philips SU1278/SH
11
12     Copyright (C) 2002 by Peter Schildmann <peter.schildmann@web.de>
13
14
15     LG TDQF-S001F
16
17     Copyright (C) 2002 Felix Domke <tmbinc@elitedvb.net>
18                      & Andreas Oberritter <obi@linuxtv.org>
19
20
21     Support for Samsung TBMU24112IMB used on Technisat SkyStar2 rev. 2.6B
22
23     Copyright (C) 2003 Vadim Catana <skystar@moldova.cc>:
24
25     Support for Philips SU1278 on Technotrend hardware
26
27     Copyright (C) 2004 Andrew de Quincey <adq_dvb@lidskialf.net>
28
29     This program is free software; you can redistribute it and/or modify
30     it under the terms of the GNU General Public License as published by
31     the Free Software Foundation; either version 2 of the License, or
32     (at your option) any later version.
33
34     This program is distributed in the hope that it will be useful,
35     but WITHOUT ANY WARRANTY; without even the implied warranty of
36     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37     GNU General Public License for more details.
38
39     You should have received a copy of the GNU General Public License
40     along with this program; if not, write to the Free Software
41     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
42
43 */    
44
45 #include <linux/init.h>
46 #include <linux/kernel.h>
47 #include <linux/module.h>
48 #include <linux/moduleparam.h>
49 #include <linux/string.h>
50 #include <linux/slab.h>
51 #include <asm/div64.h>
52
53 #include "dvb_frontend.h"
54 #include "stv0299.h"
55
56 struct stv0299_state {
57
58         struct i2c_adapter* i2c;
59
60         struct dvb_frontend_ops ops;
61
62         const struct stv0299_config* config;
63
64         struct dvb_frontend frontend;
65
66         u8 initialised:1;
67         u32 tuner_frequency;
68         u32 symbol_rate;
69         fe_code_rate_t fec_inner;
70         int errmode;
71 };
72
73 #define STATUS_BER 0
74 #define STATUS_UCBLOCKS 1
75
76 static int debug;
77 #define dprintk(args...) \
78         do { \
79                 if (debug) printk(KERN_DEBUG "stv0299: " args); \
80         } while (0)
81
82
83 static int stv0299_writeregI (struct stv0299_state* state, u8 reg, u8 data)
84 {
85         int ret;
86         u8 buf [] = { reg, data };
87         struct i2c_msg msg = { .addr = state->config->demod_address, .flags = 0, .buf = buf, .len = 2 };
88
89         ret = i2c_transfer (state->i2c, &msg, 1);
90
91         if (ret != 1) 
92                 dprintk("%s: writereg error (reg == 0x%02x, val == 0x%02x, "
93                         "ret == %i)\n", __FUNCTION__, reg, data, ret);
94
95         return (ret != 1) ? -EREMOTEIO : 0;
96 }
97
98 int stv0299_writereg (struct dvb_frontend* fe, u8 reg, u8 data)
99 {
100         struct stv0299_state* state = (struct stv0299_state*) fe->demodulator_priv;
101
102         return stv0299_writeregI(state, reg, data);
103 }
104
105
106 static u8 stv0299_readreg (struct stv0299_state* state, u8 reg)
107 {
108         int ret;
109         u8 b0 [] = { reg };
110         u8 b1 [] = { 0 };
111         struct i2c_msg msg [] = { { .addr = state->config->demod_address, .flags = 0, .buf = b0, .len = 1 },
112                            { .addr = state->config->demod_address, .flags = I2C_M_RD, .buf = b1, .len = 1 } };
113
114         ret = i2c_transfer (state->i2c, msg, 2);
115         
116         if (ret != 2) 
117                 dprintk("%s: readreg error (reg == 0x%02x, ret == %i)\n",
118                                 __FUNCTION__, reg, ret);
119
120         return b1[0];
121 }
122
123
124 static int stv0299_readregs (struct stv0299_state* state, u8 reg1, u8 *b, u8 len)
125 {
126         int ret;
127         struct i2c_msg msg [] = { { .addr = state->config->demod_address, .flags = 0, .buf = &reg1, .len = 1 },
128                            { .addr = state->config->demod_address, .flags = I2C_M_RD, .buf = b, .len = len } };
129
130         ret = i2c_transfer (state->i2c, msg, 2);
131
132         if (ret != 2)
133                 dprintk("%s: readreg error (ret == %i)\n", __FUNCTION__, ret);
134
135         return ret == 2 ? 0 : ret;
136 }
137
138
139 static int stv0299_set_FEC (struct stv0299_state* state, fe_code_rate_t fec)
140 {
141         dprintk ("%s\n", __FUNCTION__);
142
143         switch (fec) {
144         case FEC_AUTO:
145         {
146                 return stv0299_writeregI (state, 0x31, 0x1f);
147         }
148         case FEC_1_2:
149         {
150                 return stv0299_writeregI (state, 0x31, 0x01);
151         }
152         case FEC_2_3:
153         {
154                 return stv0299_writeregI (state, 0x31, 0x02);
155         }
156         case FEC_3_4:
157         {
158                 return stv0299_writeregI (state, 0x31, 0x04);
159         }
160         case FEC_5_6:
161         {
162                 return stv0299_writeregI (state, 0x31, 0x08);
163         }
164         case FEC_7_8:
165         {
166                 return stv0299_writeregI (state, 0x31, 0x10);
167         }
168         default:
169         {
170                 return -EINVAL;
171         }
172 }
173 }
174
175
176 static fe_code_rate_t stv0299_get_fec (struct stv0299_state* state)
177 {
178         static fe_code_rate_t fec_tab [] = { FEC_2_3, FEC_3_4, FEC_5_6,
179                                              FEC_7_8, FEC_1_2 };
180         u8 index;
181
182         dprintk ("%s\n", __FUNCTION__);
183
184         index = stv0299_readreg (state, 0x1b);
185         index &= 0x7;
186
187         if (index > 4)
188                 return FEC_AUTO;
189
190         return fec_tab [index];
191 }
192
193
194 static int stv0299_wait_diseqc_fifo (struct stv0299_state* state, int timeout)
195 {
196         unsigned long start = jiffies;
197
198         dprintk ("%s\n", __FUNCTION__);
199
200         while (stv0299_readreg(state, 0x0a) & 1) {
201                 if (jiffies - start > timeout) {
202                         dprintk ("%s: timeout!!\n", __FUNCTION__);
203                         return -ETIMEDOUT;
204                 }
205                 msleep(10);
206         };
207
208         return 0;
209 }
210
211
212 static int stv0299_wait_diseqc_idle (struct stv0299_state* state, int timeout)
213 {
214         unsigned long start = jiffies;
215
216         dprintk ("%s\n", __FUNCTION__);
217
218         while ((stv0299_readreg(state, 0x0a) & 3) != 2 ) {
219                 if (jiffies - start > timeout) {
220                         dprintk ("%s: timeout!!\n", __FUNCTION__);
221                         return -ETIMEDOUT;
222                 }
223                 msleep(10);
224         };
225
226         return 0;
227 }
228
229 static int stv0299_set_symbolrate (struct dvb_frontend* fe, u32 srate)
230 {
231         struct stv0299_state* state = (struct stv0299_state*) fe->demodulator_priv;
232         u64 big = srate;
233         u32 ratio;
234
235         // check rate is within limits
236         if ((srate < 1000000) || (srate > 45000000)) return -EINVAL;
237
238         // calculate value to program
239         big = big << 20;
240         big += (state->config->mclk-1); // round correctly
241         do_div(big, state->config->mclk);
242         ratio = big << 4;
243
244         return state->config->set_symbol_rate(fe, srate, ratio);
245 }
246
247
248 static int stv0299_get_symbolrate (struct stv0299_state* state)
249 {
250         u32 Mclk = state->config->mclk / 4096L;
251         u32 srate;
252         s32 offset;
253         u8 sfr[3];
254         s8 rtf;
255
256         dprintk ("%s\n", __FUNCTION__);
257
258         stv0299_readregs (state, 0x1f, sfr, 3);
259         stv0299_readregs (state, 0x1a, &rtf, 1);
260
261         srate = (sfr[0] << 8) | sfr[1];
262         srate *= Mclk;
263         srate /= 16;
264         srate += (sfr[2] >> 4) * Mclk / 256;
265         offset = (s32) rtf * (srate / 4096L);
266         offset /= 128;
267
268         dprintk ("%s : srate = %i\n", __FUNCTION__, srate);
269         dprintk ("%s : ofset = %i\n", __FUNCTION__, offset);
270
271         srate += offset;
272
273         srate += 1000;
274         srate /= 2000;
275         srate *= 2000;
276
277         return srate;
278 }
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293 static int stv0299_send_diseqc_msg (struct dvb_frontend* fe,
294                              struct dvb_diseqc_master_cmd *m)
295 {
296         struct stv0299_state* state = (struct stv0299_state*) fe->demodulator_priv;
297         u8 val;
298         int i;
299
300         dprintk ("%s\n", __FUNCTION__);
301
302         if (stv0299_wait_diseqc_idle (state, 100) < 0)
303                 return -ETIMEDOUT;
304
305         val = stv0299_readreg (state, 0x08);
306
307         if (stv0299_writeregI (state, 0x08, (val & ~0x7) | 0x6))  /* DiSEqC mode */
308                 return -EREMOTEIO;
309
310         for (i=0; i<m->msg_len; i++) {
311                 if (stv0299_wait_diseqc_fifo (state, 100) < 0)
312                         return -ETIMEDOUT;
313
314                 if (stv0299_writeregI (state, 0x09, m->msg[i]))
315                         return -EREMOTEIO;
316         }
317
318         if (stv0299_wait_diseqc_idle (state, 100) < 0)
319                 return -ETIMEDOUT;
320
321         return 0;
322 }
323
324
325 static int stv0299_send_diseqc_burst (struct dvb_frontend* fe, fe_sec_mini_cmd_t burst)
326 {
327         struct stv0299_state* state = (struct stv0299_state*) fe->demodulator_priv;
328         u8 val;
329
330         dprintk ("%s\n", __FUNCTION__);
331
332         if (stv0299_wait_diseqc_idle (state, 100) < 0)
333                 return -ETIMEDOUT;
334
335         val = stv0299_readreg (state, 0x08);
336
337         if (stv0299_writeregI (state, 0x08, (val & ~0x7) | 0x2))        /* burst mode */
338                 return -EREMOTEIO;
339
340         if (stv0299_writeregI (state, 0x09, burst == SEC_MINI_A ? 0x00 : 0xff))
341                 return -EREMOTEIO;
342
343         if (stv0299_wait_diseqc_idle (state, 100) < 0)
344                 return -ETIMEDOUT;
345
346         if (stv0299_writeregI (state, 0x08, val))
347                 return -EREMOTEIO;
348
349         return 0;
350 }
351
352
353 static int stv0299_set_tone (struct dvb_frontend* fe, fe_sec_tone_mode_t tone)
354 {
355         struct stv0299_state* state = (struct stv0299_state*) fe->demodulator_priv;
356         u8 val;
357
358         if (stv0299_wait_diseqc_idle (state, 100) < 0)
359                 return -ETIMEDOUT;
360
361         val = stv0299_readreg (state, 0x08);
362
363         switch (tone) {
364         case SEC_TONE_ON:
365                 return stv0299_writeregI (state, 0x08, val | 0x3);
366
367         case SEC_TONE_OFF:
368                 return stv0299_writeregI (state, 0x08, (val & ~0x3) | 0x02);
369
370         default:
371                 return -EINVAL;
372         }
373 }
374
375
376 static int stv0299_set_voltage (struct dvb_frontend* fe, fe_sec_voltage_t voltage)
377 {
378         struct stv0299_state* state = (struct stv0299_state*) fe->demodulator_priv;
379         u8 reg0x08;
380         u8 reg0x0c;
381
382         dprintk("%s: %s\n", __FUNCTION__,
383                 voltage == SEC_VOLTAGE_13 ? "SEC_VOLTAGE_13" : 
384                 voltage == SEC_VOLTAGE_18 ? "SEC_VOLTAGE_18" : "??");
385
386         reg0x08 = stv0299_readreg (state, 0x08);
387         reg0x0c = stv0299_readreg (state, 0x0c);
388
389         /**
390          *  H/V switching over OP0, OP1 and OP2 are LNB power enable bits
391          */
392         reg0x0c &= 0x0f;
393
394         if (voltage == SEC_VOLTAGE_OFF) {
395                 stv0299_writeregI (state, 0x0c, 0x00); /*       LNB power off! */
396                 return stv0299_writeregI (state, 0x08, 0x00); /*        LNB power off! */
397         }
398         
399         stv0299_writeregI (state, 0x08, (reg0x08 & 0x3f) | (state->config->lock_output << 6));
400
401         switch (voltage) {
402         case SEC_VOLTAGE_13:
403                 if (state->config->volt13_op0_op1 == STV0299_VOLT13_OP0) reg0x0c |= 0x10;
404                 else reg0x0c |= 0x40;
405
406                 return stv0299_writeregI(state, 0x0c, reg0x0c);
407
408         case SEC_VOLTAGE_18:
409                 return stv0299_writeregI(state, 0x0c, reg0x0c | 0x50);
410         default:
411                 return -EINVAL;
412         };
413 }
414
415
416 static int stv0299_send_legacy_dish_cmd(struct dvb_frontend* fe, u32 cmd)
417 {
418         u8 last = 1;
419         int i;
420
421         /* reset voltage at the end
422         if((0x50 & stv0299_readreg (i2c, 0x0c)) == 0x50)
423                 cmd |= 0x80;
424         else
425                 cmd &= 0x7F;
426         */
427
428         cmd = cmd << 1;
429         dprintk("%s switch command: 0x%04x\n",__FUNCTION__, cmd);
430
431         stv0299_set_voltage(fe,SEC_VOLTAGE_18);
432         msleep(32);
433
434         for (i=0; i<9; i++) {
435                 if((cmd & 0x01) != last) {
436                         stv0299_set_voltage(fe,
437                                             last ? SEC_VOLTAGE_13 :
438                                                    SEC_VOLTAGE_18);
439                         last = (last) ? 0 : 1;
440                 }
441
442                 cmd = cmd >> 1;
443
444                 if (i != 8)
445                         msleep(8);
446         }
447
448         return 0;
449 }
450
451
452 static int stv0299_init (struct dvb_frontend* fe)
453 {
454         struct stv0299_state* state = (struct stv0299_state*) fe->demodulator_priv;
455         int i;
456
457         dprintk("stv0299: init chip\n");
458
459         for (i=0; !(state->config->inittab[i] == 0xff && state->config->inittab[i+1] == 0xff); i+=2)
460                 stv0299_writeregI(state, state->config->inittab[i], state->config->inittab[i+1]);
461
462         if (state->config->pll_init) {
463                 stv0299_writeregI(state, 0x05, 0xb5);   /*  enable i2c repeater on stv0299  */
464                 state->config->pll_init(fe);
465                 stv0299_writeregI(state, 0x05, 0x35);   /*  disable i2c repeater on stv0299  */
466         }
467
468         return 0;
469 }
470
471
472 static int stv0299_read_status(struct dvb_frontend* fe, fe_status_t* status)
473         {
474         struct stv0299_state* state = (struct stv0299_state*) fe->demodulator_priv;
475
476         u8 signal = 0xff - stv0299_readreg (state, 0x18);
477         u8 sync = stv0299_readreg (state, 0x1b);
478
479                 dprintk ("%s : FE_READ_STATUS : VSTATUS: 0x%02x\n", __FUNCTION__, sync);
480                 *status = 0;
481
482                 if (signal > 10)
483                         *status |= FE_HAS_SIGNAL;
484
485                 if (sync & 0x80)
486                         *status |= FE_HAS_CARRIER;
487
488                 if (sync & 0x10)
489                         *status |= FE_HAS_VITERBI;
490
491                 if (sync & 0x08)
492                         *status |= FE_HAS_SYNC;
493
494                 if ((sync & 0x98) == 0x98)
495                         *status |= FE_HAS_LOCK;
496
497         return 0;
498         }
499
500 static int stv0299_read_ber(struct dvb_frontend* fe, u32* ber)
501 {
502         struct stv0299_state* state = (struct stv0299_state*) fe->demodulator_priv;
503
504         if (state->errmode != STATUS_BER) return 0;
505         *ber = (stv0299_readreg (state, 0x1d) << 8) | stv0299_readreg (state, 0x1e);
506
507         return 0;
508                 }
509
510 static int stv0299_read_signal_strength(struct dvb_frontend* fe, u16* strength)
511         {
512         struct stv0299_state* state = (struct stv0299_state*) fe->demodulator_priv;
513
514         s32 signal =  0xffff - ((stv0299_readreg (state, 0x18) << 8)
515                                | stv0299_readreg (state, 0x19));
516
517                 dprintk ("%s : FE_READ_SIGNAL_STRENGTH : AGC2I: 0x%02x%02x, signal=0x%04x\n", __FUNCTION__,
518                  stv0299_readreg (state, 0x18),
519                  stv0299_readreg (state, 0x19), (int) signal);
520
521                 signal = signal * 5 / 4;
522         *strength = (signal > 0xffff) ? 0xffff : (signal < 0) ? 0 : signal;
523
524         return 0;
525                 }
526
527 static int stv0299_read_snr(struct dvb_frontend* fe, u16* snr)
528         {
529         struct stv0299_state* state = (struct stv0299_state*) fe->demodulator_priv;
530
531         s32 xsnr = 0xffff - ((stv0299_readreg (state, 0x24) << 8)
532                            | stv0299_readreg (state, 0x25));
533         xsnr = 3 * (xsnr - 0xa100);
534         *snr = (xsnr > 0xffff) ? 0xffff : (xsnr < 0) ? 0 : xsnr;
535
536         return 0;
537 }
538
539 static int stv0299_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
540 {
541         struct stv0299_state* state = (struct stv0299_state*) fe->demodulator_priv;
542
543         if (state->errmode != STATUS_UCBLOCKS) *ucblocks = 0;
544         else *ucblocks = (stv0299_readreg (state, 0x1d) << 8) | stv0299_readreg (state, 0x1e);
545
546         return 0;
547 }
548
549 static int stv0299_set_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters * p)
550 {
551         struct stv0299_state* state = (struct stv0299_state*) fe->demodulator_priv;
552                 int invval = 0;
553
554                 dprintk ("%s : FE_SET_FRONTEND\n", __FUNCTION__);
555
556                 // set the inversion
557                 if (p->inversion == INVERSION_OFF) invval = 0;
558                 else if (p->inversion == INVERSION_ON) invval = 1;
559                 else {
560                         printk("stv0299 does not support auto-inversion\n");
561                         return -EINVAL;
562                 }
563         if (state->config->invert) invval = (~invval) & 1;
564         stv0299_writeregI(state, 0x0c, (stv0299_readreg(state, 0x0c) & 0xfe) | invval);
565
566         if (state->config->enhanced_tuning) {
567                         /* check if we should do a finetune */
568                         int frequency_delta = p->frequency - state->tuner_frequency;
569                         int minmax = p->u.qpsk.symbol_rate / 2000;
570                         if (minmax < 5000) minmax = 5000;
571                    
572                         if ((frequency_delta > -minmax) && (frequency_delta < minmax) && (frequency_delta != 0) &&
573                             (state->fec_inner == p->u.qpsk.fec_inner) && 
574                             (state->symbol_rate == p->u.qpsk.symbol_rate)) {
575                         int Drot_freq = (frequency_delta << 16) / (state->config->mclk / 1000);
576
577                                 // zap the derotator registers first
578                         stv0299_writeregI(state, 0x22, 0x00);
579                         stv0299_writeregI(state, 0x23, 0x00);
580
581                                 // now set them as we want
582                         stv0299_writeregI(state, 0x22, Drot_freq >> 8);
583                         stv0299_writeregI(state, 0x23, Drot_freq);
584                         } else {
585                                 /* A "normal" tune is requested */
586                         stv0299_writeregI(state, 0x05, 0xb5);   /*  enable i2c repeater on stv0299  */
587                         state->config->pll_set(fe, p);
588                         stv0299_writeregI(state, 0x05, 0x35);   /*  disable i2c repeater on stv0299  */
589
590                         stv0299_writeregI(state, 0x32, 0x80);
591                         stv0299_writeregI(state, 0x22, 0x00);
592                         stv0299_writeregI(state, 0x23, 0x00);
593                         stv0299_writeregI(state, 0x32, 0x19);
594                         stv0299_set_symbolrate (fe, p->u.qpsk.symbol_rate);
595                         stv0299_set_FEC (state, p->u.qpsk.fec_inner);
596                 }
597         } else {
598                 stv0299_writeregI(state, 0x05, 0xb5);   /*  enable i2c repeater on stv0299  */
599                 state->config->pll_set(fe, p);
600                 stv0299_writeregI(state, 0x05, 0x35);   /*  disable i2c repeater on stv0299  */
601
602                 stv0299_set_FEC (state, p->u.qpsk.fec_inner);
603                 stv0299_set_symbolrate (fe, p->u.qpsk.symbol_rate);
604                 stv0299_writeregI(state, 0x22, 0x00);
605                 stv0299_writeregI(state, 0x23, 0x00);
606                 stv0299_readreg (state, 0x23);
607                 stv0299_writeregI(state, 0x12, 0xb9);
608                 }
609
610                 state->tuner_frequency = p->frequency;
611                 state->fec_inner = p->u.qpsk.fec_inner;
612                 state->symbol_rate = p->u.qpsk.symbol_rate;
613
614         return 0;
615         }
616
617 static int stv0299_get_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters * p)
618         {
619         struct stv0299_state* state = (struct stv0299_state*) fe->demodulator_priv;
620                 s32 derot_freq;
621                 int invval;
622
623         derot_freq = (s32)(s16) ((stv0299_readreg (state, 0x22) << 8)
624                                 | stv0299_readreg (state, 0x23));
625
626         derot_freq *= (state->config->mclk >> 16);
627                 derot_freq += 500;
628                 derot_freq /= 1000;
629
630                 p->frequency += derot_freq;
631
632         invval = stv0299_readreg (state, 0x0c) & 1;
633         if (state->config->invert) invval = (~invval) & 1;
634                 p->inversion = invval ? INVERSION_ON : INVERSION_OFF;
635
636         p->u.qpsk.fec_inner = stv0299_get_fec (state);
637         p->u.qpsk.symbol_rate = stv0299_get_symbolrate (state);
638
639         return 0;
640                 }
641
642 static int stv0299_sleep(struct dvb_frontend* fe)
643 {
644         struct stv0299_state* state = (struct stv0299_state*) fe->demodulator_priv;
645
646         stv0299_writeregI(state, 0x02, 0x80);
647         state->initialised = 0;
648
649         return 0;
650 }
651
652 static int stv0299_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings* fesettings)
653         {
654         struct stv0299_state* state = (struct stv0299_state*) fe->demodulator_priv;
655
656         fesettings->min_delay_ms = state->config->min_delay_ms;
657                         if (fesettings->parameters.u.qpsk.symbol_rate < 10000000) {
658                                 fesettings->step_size = fesettings->parameters.u.qpsk.symbol_rate / 32000;
659                                 fesettings->max_drift = 5000;
660                         } else {
661                                 fesettings->step_size = fesettings->parameters.u.qpsk.symbol_rate / 16000;
662                                 fesettings->max_drift = fesettings->parameters.u.qpsk.symbol_rate / 2000;
663                         }
664         return 0;
665 }
666
667 static void stv0299_release(struct dvb_frontend* fe)
668                 {
669         struct stv0299_state* state = (struct stv0299_state*) fe->demodulator_priv;
670         kfree(state);
671 }
672
673 static struct dvb_frontend_ops stv0299_ops;
674
675 struct dvb_frontend* stv0299_attach(const struct stv0299_config* config,
676                                     struct i2c_adapter* i2c)
677 {
678         struct stv0299_state* state = NULL;
679         int id;
680  
681         /* allocate memory for the internal state */
682         state = (struct stv0299_state*) kmalloc(sizeof(struct stv0299_state), GFP_KERNEL);
683         if (state == NULL) goto error;
684
685         /* setup the state */
686         state->config = config;
687         state->i2c = i2c;
688         memcpy(&state->ops, &stv0299_ops, sizeof(struct dvb_frontend_ops));
689         state->initialised = 0;
690         state->tuner_frequency = 0;
691         state->symbol_rate = 0;
692         state->fec_inner = 0;
693         state->errmode = STATUS_BER;
694
695         /* check if the demod is there */
696         stv0299_writeregI(state, 0x02, 0x34); /* standby off */
697         msleep(200);
698         id = stv0299_readreg(state, 0x00);
699
700         /* register 0x00 contains 0xa1 for STV0299 and STV0299B */
701         /* register 0x00 might contain 0x80 when returning from standby */
702         if (id != 0xa1 && id != 0x80) goto error;
703
704         /* create dvb_frontend */
705         state->frontend.ops = &state->ops;
706         state->frontend.demodulator_priv = state;
707         return &state->frontend;
708
709 error:
710         if (state) kfree(state);
711         return NULL;
712 }
713
714 static struct dvb_frontend_ops stv0299_ops = {
715
716         .info = {
717                 .name                   = "ST STV0299 DVB-S",
718                 .type                   = FE_QPSK,
719                 .frequency_min          = 950000,
720                 .frequency_max          = 2150000,
721                 .frequency_stepsize     = 125,   /* kHz for QPSK frontends */
722                 .frequency_tolerance    = 0,
723                 .symbol_rate_min        = 1000000,
724                 .symbol_rate_max        = 45000000,
725                 .symbol_rate_tolerance  = 500,  /* ppm */
726                 .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
727                       FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 |
728                       FE_CAN_QPSK |
729                       FE_CAN_FEC_AUTO
730         },
731
732         .release = stv0299_release,
733
734         .init = stv0299_init,
735         .sleep = stv0299_sleep,
736
737         .set_frontend = stv0299_set_frontend,
738         .get_frontend = stv0299_get_frontend,
739         .get_tune_settings = stv0299_get_tune_settings,
740
741         .read_status = stv0299_read_status,
742         .read_ber = stv0299_read_ber,
743         .read_signal_strength = stv0299_read_signal_strength,
744         .read_snr = stv0299_read_snr,
745         .read_ucblocks = stv0299_read_ucblocks,
746
747         .diseqc_send_master_cmd = stv0299_send_diseqc_msg,
748         .diseqc_send_burst = stv0299_send_diseqc_burst,
749         .set_tone = stv0299_set_tone,
750         .set_voltage = stv0299_set_voltage,
751         .dishnetwork_send_legacy_command = stv0299_send_legacy_dish_cmd,
752 };
753
754 module_param(debug, int, 0644);
755 MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
756
757 MODULE_DESCRIPTION("ST STV0299 DVB Demodulator driver");
758 MODULE_AUTHOR("Ralph Metzler, Holger Waechtler, Peter Schildmann, Felix Domke, "
759               "Andreas Oberritter, Andrew de Quincey, Kenneth Aafløy");
760 MODULE_LICENSE("GPL");
761
762 EXPORT_SYMBOL(stv0299_writereg);
763 EXPORT_SYMBOL(stv0299_attach);