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