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