V4L/DVB (9383): Let's neither sleep nor wakeup for now
[safe/jmp/linux-2.6] / drivers / media / dvb / frontends / stb0899_drv.c
1 /*
2         STB0899 Multistandard Frontend driver
3         Copyright (C) Manu Abraham (abraham.manu@gmail.com)
4
5         Copyright (C) ST Microelectronics
6
7         This program is free software; you can redistribute it and/or modify
8         it under the terms of the GNU General Public License as published by
9         the Free Software Foundation; either version 2 of the License, or
10         (at your option) any later version.
11
12         This program is distributed in the hope that it will be useful,
13         but WITHOUT ANY WARRANTY; without even the implied warranty of
14         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15         GNU General Public License for more details.
16
17         You should have received a copy of the GNU General Public License
18         along with this program; if not, write to the Free Software
19         Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include <linux/init.h>
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/string.h>
26
27 #include <linux/dvb/frontend.h>
28 #include "dvb_frontend.h"
29
30 #include "stb0899_drv.h"
31 #include "stb0899_priv.h"
32 #include "stb0899_reg.h"
33
34 static unsigned int verbose = 5;
35 module_param(verbose, int, 0644);
36
37 /* C/N in dB/10, NIRM/NIRL */
38 static const struct stb0899_tab stb0899_cn_tab[] = {
39         { 200,  2600 },
40         { 190,  2700 },
41         { 180,  2860 },
42         { 170,  3020 },
43         { 160,  3210 },
44         { 150,  3440 },
45         { 140,  3710 },
46         { 130,  4010 },
47         { 120,  4360 },
48         { 110,  4740 },
49         { 100,  5190 },
50         { 90,   5670 },
51         { 80,   6200 },
52         { 70,   6770 },
53         { 60,   7360 },
54         { 50,   7970 },
55         { 40,   8250 },
56         { 30,   9000 },
57         { 20,   9450 },
58         { 15,   9600 },
59 };
60
61 /* DVB-S AGCIQ_VALUE vs. signal level in dBm/10.
62  * As measured, connected to a modulator.
63  * -8.0 to -50.0 dBm directly connected,
64  * -52.0 to -74.8 with extra attenuation.
65  * Cut-off to AGCIQ_VALUE = 0x80 below -74.8dBm.
66  * Crude linear extrapolation below -84.8dBm and above -8.0dBm.
67  */
68 static const struct stb0899_tab stb0899_dvbsrf_tab[] = {
69         { -950, -128 },
70         { -748,  -94 },
71         { -745,  -92 },
72         { -735,  -90 },
73         { -720,  -87 },
74         { -670,  -77 },
75         { -640,  -70 },
76         { -610,  -62 },
77         { -600,  -60 },
78         { -590,  -56 },
79         { -560,  -41 },
80         { -540,  -25 },
81         { -530,  -17 },
82         { -520,  -11 },
83         { -500,    1 },
84         { -490,    6 },
85         { -480,   10 },
86         { -440,   22 },
87         { -420,   27 },
88         { -400,   31 },
89         { -380,   34 },
90         { -340,   40 },
91         { -320,   43 },
92         { -280,   48 },
93         { -250,   52 },
94         { -230,   55 },
95         { -180,   61 },
96         { -140,   66 },
97         {  -90,   73 },
98         {  -80,   74 },
99         {  500,  127 }
100 };
101
102 /* DVB-S2 IF_AGC_GAIN vs. signal level in dBm/10.
103  * As measured, connected to a modulator.
104  * -8.0 to -50.1 dBm directly connected,
105  * -53.0 to -76.6 with extra attenuation.
106  * Cut-off to IF_AGC_GAIN = 0x3fff below -76.6dBm.
107  * Crude linear extrapolation below -76.6dBm and above -8.0dBm.
108  */
109 static const struct stb0899_tab stb0899_dvbs2rf_tab[] = {
110         {  700,     0 },
111         {  -80,  3217 },
112         { -150,  3893 },
113         { -190,  4217 },
114         { -240,  4621 },
115         { -280,  4945 },
116         { -320,  5273 },
117         { -350,  5545 },
118         { -370,  5741 },
119         { -410,  6147 },
120         { -450,  6671 },
121         { -490,  7413 },
122         { -501,  7665 },
123         { -530,  8767 },
124         { -560, 10219 },
125         { -580, 10939 },
126         { -590, 11518 },
127         { -600, 11723 },
128         { -650, 12659 },
129         { -690, 13219 },
130         { -730, 13645 },
131         { -750, 13909 },
132         { -766, 14153 },
133         { -999, 16383 }
134 };
135
136 /* DVB-S2 Es/N0 quant in dB/100 vs read value * 100*/
137 struct stb0899_tab stb0899_quant_tab[] = {
138         {    0,     0 },
139         {    0,   100 },
140         {  600,   200 },
141         {  950,   299 },
142         { 1200,   398 },
143         { 1400,   501 },
144         { 1560,   603 },
145         { 1690,   700 },
146         { 1810,   804 },
147         { 1910,   902 },
148         { 2000,  1000 },
149         { 2080,  1096 },
150         { 2160,  1202 },
151         { 2230,  1303 },
152         { 2350,  1496 },
153         { 2410,  1603 },
154         { 2460,  1698 },
155         { 2510,  1799 },
156         { 2600,  1995 },
157         { 2650,  2113 },
158         { 2690,  2213 },
159         { 2720,  2291 },
160         { 2760,  2399 },
161         { 2800,  2512 },
162         { 2860,  2692 },
163         { 2930,  2917 },
164         { 2960,  3020 },
165         { 3010,  3199 },
166         { 3040,  3311 },
167         { 3060,  3388 },
168         { 3120,  3631 },
169         { 3190,  3936 },
170         { 3400,  5012 },
171         { 3610,  6383 },
172         { 3800,  7943 },
173         { 4210, 12735 },
174         { 4500, 17783 },
175         { 4690, 22131 },
176         { 4810, 25410 }
177 };
178
179 /* DVB-S2 Es/N0 estimate in dB/100 vs read value */
180 struct stb0899_tab stb0899_est_tab[] = {
181         {    0,      0 },
182         {    0,      1 },
183         {  301,      2 },
184         { 1204,     16 },
185         { 1806,     64 },
186         { 2408,    256 },
187         { 2709,    512 },
188         { 3010,   1023 },
189         { 3311,   2046 },
190         { 3612,   4093 },
191         { 3823,   6653 },
192         { 3913,   8185 },
193         { 4010,  10233 },
194         { 4107,  12794 },
195         { 4214,  16368 },
196         { 4266,  18450 },
197         { 4311,  20464 },
198         { 4353,  22542 },
199         { 4391,  24604 },
200         { 4425,  26607 },
201         { 4457,  28642 },
202         { 4487,  30690 },
203         { 4515,  32734 },
204         { 4612,  40926 },
205         { 4692,  49204 },
206         { 4816,  65464 },
207         { 4913,  81846 },
208         { 4993,  98401 },
209         { 5060, 114815 },
210         { 5118, 131220 },
211         { 5200, 158489 },
212         { 5300, 199526 },
213         { 5400, 251189 },
214         { 5500, 316228 },
215         { 5600, 398107 },
216         { 5720, 524807 },
217         { 5721, 526017 },
218 };
219
220 int _stb0899_read_reg(struct stb0899_state *state, unsigned int reg)
221 {
222         int ret;
223
224         u8 b0[] = { reg >> 8, reg & 0xff };
225         u8 buf;
226
227         struct i2c_msg msg[] = {
228                 {
229                         .addr   = state->config->demod_address,
230                         .flags  = 0,
231                         .buf    = b0,
232                         .len    = 2
233                 },{
234                         .addr   = state->config->demod_address,
235                         .flags  = I2C_M_RD,
236                         .buf    = &buf,
237                         .len    = 1
238                 }
239         };
240
241         ret = i2c_transfer(state->i2c, msg, 2);
242         if (ret != 2) {
243                 if (ret != -ERESTARTSYS)
244                         dprintk(verbose, FE_ERROR, 1,
245                                 "Read error, Reg=[0x%02x], Status=%d",
246                                 reg, ret);
247
248                 return ret < 0 ? ret : -EREMOTEIO;
249         }
250         if (unlikely(verbose >= FE_DEBUGREG))
251                 dprintk(verbose, FE_ERROR, 1, "Reg=[0x%02x], data=%02x",
252                         reg, buf);
253
254
255         return (unsigned int)buf;
256 }
257
258 int stb0899_read_reg(struct stb0899_state *state, unsigned int reg)
259 {
260         int result;
261
262         result = _stb0899_read_reg(state, reg);
263         /*
264          * Bug ID 9:
265          * access to 0xf2xx/0xf6xx
266          * must be followed by read from 0xf2ff/0xf6ff.
267          */
268         if ((reg != 0xf2ff) && (reg != 0xf6ff) &&
269             (((reg & 0xff00) == 0xf200) || ((reg & 0xff00) == 0xf600)))
270                 _stb0899_read_reg(state, (reg | 0x00ff));
271
272         return result;
273 }
274
275 u32 _stb0899_read_s2reg(struct stb0899_state *state,
276                         u32 stb0899_i2cdev,
277                         u32 stb0899_base_addr,
278                         u16 stb0899_reg_offset)
279 {
280         int status;
281         u32 data;
282         u8 buf[7] = { 0 };
283         u16 tmpaddr;
284
285         u8 buf_0[] = {
286                 GETBYTE(stb0899_i2cdev, BYTE1),         /* 0xf3 S2 Base Address (MSB)   */
287                 GETBYTE(stb0899_i2cdev, BYTE0),         /* 0xfc S2 Base Address (LSB)   */
288                 GETBYTE(stb0899_base_addr, BYTE0),      /* 0x00 Base Address (LSB)      */
289                 GETBYTE(stb0899_base_addr, BYTE1),      /* 0x04 Base Address (LSB)      */
290                 GETBYTE(stb0899_base_addr, BYTE2),      /* 0x00 Base Address (MSB)      */
291                 GETBYTE(stb0899_base_addr, BYTE3),      /* 0x00 Base Address (MSB)      */
292         };
293         u8 buf_1[] = {
294                 0x00,   /* 0xf3 Reg Offset      */
295                 0x00,   /* 0x44 Reg Offset      */
296         };
297
298         struct i2c_msg msg_0 = {
299                 .addr   = state->config->demod_address,
300                 .flags  = 0,
301                 .buf    = buf_0,
302                 .len    = 6
303         };
304
305         struct i2c_msg msg_1 = {
306                 .addr   = state->config->demod_address,
307                 .flags  = 0,
308                 .buf    = buf_1,
309                 .len    = 2
310         };
311
312         struct i2c_msg msg_r = {
313                 .addr   = state->config->demod_address,
314                 .flags  = I2C_M_RD,
315                 .buf    = buf,
316                 .len    = 4
317         };
318
319         tmpaddr = stb0899_reg_offset & 0xff00;
320         if (!(stb0899_reg_offset & 0x8))
321                 tmpaddr = stb0899_reg_offset | 0x20;
322
323         buf_1[0] = GETBYTE(tmpaddr, BYTE1);
324         buf_1[1] = GETBYTE(tmpaddr, BYTE0);
325
326         status = i2c_transfer(state->i2c, &msg_0, 1);
327         if (status < 1) {
328                 if (status != -ERESTARTSYS)
329                         printk(KERN_ERR "%s ERR(1), Device=[0x%04x], Base address=[0x%08x], Offset=[0x%04x], Status=%d\n",
330                                __func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, status);
331
332                 goto err;
333         }
334
335         /* Dummy        */
336         status = i2c_transfer(state->i2c, &msg_1, 1);
337         if (status < 1)
338                 goto err;
339
340         status = i2c_transfer(state->i2c, &msg_r, 1);
341         if (status < 1)
342                 goto err;
343
344         buf_1[0] = GETBYTE(stb0899_reg_offset, BYTE1);
345         buf_1[1] = GETBYTE(stb0899_reg_offset, BYTE0);
346
347         /* Actual       */
348         status = i2c_transfer(state->i2c, &msg_1, 1);
349         if (status < 1) {
350                 if (status != -ERESTARTSYS)
351                         printk(KERN_ERR "%s ERR(2), Device=[0x%04x], Base address=[0x%08x], Offset=[0x%04x], Status=%d\n",
352                                __func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, status);
353                 goto err;
354         }
355
356         status = i2c_transfer(state->i2c, &msg_r, 1);
357         if (status < 1) {
358                 if (status != -ERESTARTSYS)
359                         printk(KERN_ERR "%s ERR(3), Device=[0x%04x], Base address=[0x%08x], Offset=[0x%04x], Status=%d\n",
360                                __func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, status);
361                 return status < 0 ? status : -EREMOTEIO;
362         }
363
364         data = MAKEWORD32(buf[3], buf[2], buf[1], buf[0]);
365         if (unlikely(state->verbose >= FE_DEBUGREG))
366                 printk(KERN_DEBUG "%s Device=[0x%04x], Base address=[0x%08x], Offset=[0x%04x], Data=[0x%08x]\n",
367                        __func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, data);
368
369         return data;
370
371 err:
372         return status < 0 ? status : -EREMOTEIO;
373 }
374
375 int stb0899_write_s2reg(struct stb0899_state *state,
376                         u32 stb0899_i2cdev,
377                         u32 stb0899_base_addr,
378                         u16 stb0899_reg_offset,
379                         u32 stb0899_data)
380 {
381         int status;
382
383         /* Base Address Setup   */
384         u8 buf_0[] = {
385                 GETBYTE(stb0899_i2cdev, BYTE1),         /* 0xf3 S2 Base Address (MSB)   */
386                 GETBYTE(stb0899_i2cdev, BYTE0),         /* 0xfc S2 Base Address (LSB)   */
387                 GETBYTE(stb0899_base_addr, BYTE0),      /* 0x00 Base Address (LSB)      */
388                 GETBYTE(stb0899_base_addr, BYTE1),      /* 0x04 Base Address (LSB)      */
389                 GETBYTE(stb0899_base_addr, BYTE2),      /* 0x00 Base Address (MSB)      */
390                 GETBYTE(stb0899_base_addr, BYTE3),      /* 0x00 Base Address (MSB)      */
391         };
392         u8 buf_1[] = {
393                 0x00,   /* 0xf3 Reg Offset      */
394                 0x00,   /* 0x44 Reg Offset      */
395                 0x00,   /* data                 */
396                 0x00,   /* data                 */
397                 0x00,   /* data                 */
398                 0x00,   /* data                 */
399         };
400
401         struct i2c_msg msg_0 = {
402                 .addr   = state->config->demod_address,
403                 .flags  = 0,
404                 .buf    = buf_0,
405                 .len    = 6
406         };
407
408         struct i2c_msg msg_1 = {
409                 .addr   = state->config->demod_address,
410                 .flags  = 0,
411                 .buf    = buf_1,
412                 .len    = 6
413         };
414
415         buf_1[0] = GETBYTE(stb0899_reg_offset, BYTE1);
416         buf_1[1] = GETBYTE(stb0899_reg_offset, BYTE0);
417         buf_1[2] = GETBYTE(stb0899_data, BYTE0);
418         buf_1[3] = GETBYTE(stb0899_data, BYTE1);
419         buf_1[4] = GETBYTE(stb0899_data, BYTE2);
420         buf_1[5] = GETBYTE(stb0899_data, BYTE3);
421
422         if (unlikely(state->verbose >= FE_DEBUGREG))
423                 printk(KERN_DEBUG "%s Device=[0x%04x], Base Address=[0x%08x], Offset=[0x%04x], Data=[0x%08x]\n",
424                        __func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, stb0899_data);
425
426         status = i2c_transfer(state->i2c, &msg_0, 1);
427         if (unlikely(status < 1)) {
428                 if (status != -ERESTARTSYS)
429                         printk(KERN_ERR "%s ERR (1), Device=[0x%04x], Base Address=[0x%08x], Offset=[0x%04x], Data=[0x%08x], status=%d\n",
430                                __func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, stb0899_data, status);
431                 goto err;
432         }
433         status = i2c_transfer(state->i2c, &msg_1, 1);
434         if (unlikely(status < 1)) {
435                 if (status != -ERESTARTSYS)
436                         printk(KERN_ERR "%s ERR (2), Device=[0x%04x], Base Address=[0x%08x], Offset=[0x%04x], Data=[0x%08x], status=%d\n",
437                                __func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, stb0899_data, status);
438
439                 return status < 0 ? status : -EREMOTEIO;
440         }
441
442         return 0;
443
444 err:
445         return status < 0 ? status : -EREMOTEIO;
446 }
447
448 int stb0899_read_regs(struct stb0899_state *state, unsigned int reg, u8 *buf, size_t count)
449 {
450         int status;
451
452         u8 b0[] = { reg >> 8, reg & 0xff };
453
454         struct i2c_msg msg[] = {
455                 {
456                         .addr   = state->config->demod_address,
457                         .flags  = 0,
458                         .buf    = b0,
459                         .len    = 2
460                 },{
461                         .addr   = state->config->demod_address,
462                         .flags  = I2C_M_RD,
463                         .buf    = buf,
464                         .len    = count
465                 }
466         };
467
468         status = i2c_transfer(state->i2c, msg, 2);
469         if (status != 2) {
470                 if (status != -ERESTARTSYS)
471                         printk(KERN_ERR "%s Read error, Reg=[0x%04x], Count=%u, Status=%d\n",
472                                __func__, reg, count, status);
473                 goto err;
474         }
475         /*
476          * Bug ID 9:
477          * access to 0xf2xx/0xf6xx
478          * must be followed by read from 0xf2ff/0xf6ff.
479          */
480         if ((reg != 0xf2ff) && (reg != 0xf6ff) &&
481             (((reg & 0xff00) == 0xf200) || ((reg & 0xff00) == 0xf600)))
482                 _stb0899_read_reg(state, (reg | 0x00ff));
483
484         if (unlikely(state->verbose >= FE_DEBUGREG)) {
485                 int i;
486
487                 printk(KERN_DEBUG "%s [0x%04x]:", __func__, reg);
488                 for (i = 0; i < count; i++) {
489                         printk(" %02x", buf[i]);
490                 }
491                 printk("\n");
492         }
493
494         return 0;
495 err:
496         return status < 0 ? status : -EREMOTEIO;
497 }
498
499 int stb0899_write_regs(struct stb0899_state *state, unsigned int reg, u8 *data, size_t count)
500 {
501         int ret;
502         u8 buf[2 + count];
503         struct i2c_msg i2c_msg = {
504                 .addr   = state->config->demod_address,
505                 .flags  = 0,
506                 .buf    = buf,
507                 .len    = 2 + count
508         };
509
510         buf[0] = reg >> 8;
511         buf[1] = reg & 0xff;
512         memcpy(&buf[2], data, count);
513
514         if (unlikely(state->verbose >= FE_DEBUGREG)) {
515                 int i;
516
517                 printk(KERN_DEBUG "%s [0x%04x]:", __func__, reg);
518                 for (i = 0; i < count; i++)
519                         printk(" %02x", data[i]);
520                 printk("\n");
521         }
522         ret = i2c_transfer(state->i2c, &i2c_msg, 1);
523
524         /*
525          * Bug ID 9:
526          * access to 0xf2xx/0xf6xx
527          * must be followed by read from 0xf2ff/0xf6ff.
528          */
529         if ((((reg & 0xff00) == 0xf200) || ((reg & 0xff00) == 0xf600)))
530                 stb0899_read_reg(state, (reg | 0x00ff));
531
532         if (ret != 1) {
533                 if (ret != -ERESTARTSYS)
534                         dprintk(verbose, FE_ERROR, 1, "Reg=[0x%04x], Data=[0x%02x ...], Count=%u, Status=%d",
535                                 reg, data[0], count, ret);
536                 return ret < 0 ? ret : -EREMOTEIO;
537         }
538
539         return 0;
540 }
541
542 int stb0899_write_reg(struct stb0899_state *state, unsigned int reg, u8 data)
543 {
544         return stb0899_write_regs(state, reg, &data, 1);
545 }
546
547 /*
548  * stb0899_get_mclk
549  * Get STB0899 master clock frequency
550  * ExtClk: external clock frequency (Hz)
551  */
552 static u32 stb0899_get_mclk(struct stb0899_state *state)
553 {
554         u32 mclk = 90000000, div = 0;
555
556         div = stb0899_read_reg(state, STB0899_NCOARSE);
557         mclk = (div + 1) * state->config->xtal_freq / 6;
558         dprintk(verbose, FE_DEBUG, 1, "div=%d, mclk=%d", div, mclk);
559
560         return mclk;
561 }
562
563 /*
564  * stb0899_set_mclk
565  * Set STB0899 master Clock frequency
566  * Mclk: demodulator master clock
567  * ExtClk: external clock frequency (Hz)
568  */
569 static void stb0899_set_mclk(struct stb0899_state *state, u32 Mclk)
570 {
571         struct stb0899_internal *internal = &state->internal;
572         u8 mdiv = 0;
573
574         dprintk(verbose, FE_DEBUG, 1, "state->config=%p", state->config);
575         mdiv = ((6 * Mclk) / state->config->xtal_freq) - 1;
576         dprintk(verbose, FE_DEBUG, 1, "mdiv=%d", mdiv);
577
578         stb0899_write_reg(state, STB0899_NCOARSE, mdiv);
579         internal->master_clk = stb0899_get_mclk(state);
580
581         dprintk(verbose, FE_DEBUG, 1, "MasterCLOCK=%d", internal->master_clk);
582 }
583
584 static void stb0899_release(struct dvb_frontend *fe)
585 {
586         struct stb0899_state *state = fe->demodulator_priv;
587
588         dprintk(verbose, FE_DEBUG, 1, "Release Frontend");
589         kfree(state);
590 }
591
592 /*
593  * stb0899_get_alpha
594  * return: rolloff
595  */
596 static int stb0899_get_alpha(struct stb0899_state *state)
597 {
598         u8 mode_coeff;
599
600         mode_coeff = stb0899_read_reg(state, STB0899_DEMOD);
601
602         if (STB0899_GETFIELD(MODECOEFF, mode_coeff) == 1)
603                 return 20;
604         else
605                 return 35;
606 }
607
608 /*
609  * stb0899_init_calc
610  */
611 static void stb0899_init_calc(struct stb0899_state *state)
612 {
613         struct stb0899_internal *internal = &state->internal;
614         int master_clk;
615         u8 agc[1];
616         u8 agc1cn;
617         u32 reg;
618
619         /* Read registers (in burst mode)       */
620         agc1cn = stb0899_read_reg(state, STB0899_AGC1CN);
621         stb0899_read_regs(state, STB0899_AGC1REF, agc, 2); /* AGC1R and AGC2O   */
622
623         /* Initial calculations */
624         master_clk                      = stb0899_get_mclk(state);
625         internal->t_agc1                = 0;
626         internal->t_agc2                = 0;
627         internal->master_clk            = master_clk;
628         internal->mclk                  = master_clk / 65536L;
629         internal->rolloff               = stb0899_get_alpha(state);
630
631         /* DVBS2 Initial calculations   */
632         /* Set AGC value to the middle  */
633         internal->agc_gain              = 8154;
634         reg = STB0899_READ_S2REG(STB0899_S2DEMOD, IF_AGC_CNTRL);
635         STB0899_SETFIELD_VAL(IF_GAIN_INIT, reg, internal->agc_gain);
636         stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_IF_AGC_CNTRL, STB0899_OFF0_IF_AGC_CNTRL, reg);
637
638         reg = STB0899_READ_S2REG(STB0899_S2DEMOD, RRC_ALPHA);
639         internal->rrc_alpha             = STB0899_GETFIELD(RRC_ALPHA, reg);
640
641         internal->center_freq           = 0;
642         internal->av_frame_coarse       = 10;
643         internal->av_frame_fine         = 20;
644         internal->step_size             = 2;
645 /*
646         if ((pParams->SpectralInv == FE_IQ_NORMAL) || (pParams->SpectralInv == FE_IQ_AUTO))
647                 pParams->IQLocked = 0;
648         else
649                 pParams->IQLocked = 1;
650 */
651 }
652
653 static int stb0899_wait_diseqc_fifo_empty(struct stb0899_state *state, int timeout)
654 {
655         u8 reg = 0;
656         unsigned long start = jiffies;
657
658         while (1) {
659                 reg = stb0899_read_reg(state, STB0899_DISSTATUS);
660                 if (!STB0899_GETFIELD(FIFOFULL, reg))
661                         break;
662                 if ((jiffies - start) > timeout) {
663                         dprintk(verbose, FE_ERROR, 1, "timed out !!");
664                         return -ETIMEDOUT;
665                 }
666         }
667
668         return 0;
669 }
670
671 static int stb0899_send_diseqc_msg(struct dvb_frontend *fe, struct dvb_diseqc_master_cmd *cmd)
672 {
673         struct stb0899_state *state = fe->demodulator_priv;
674         u8 reg, i;
675
676         if (cmd->msg_len > 8)
677                 return -EINVAL;
678
679         /* enable FIFO precharge        */
680         reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
681         STB0899_SETFIELD_VAL(DISPRECHARGE, reg, 1);
682         stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
683         for (i = 0; i < cmd->msg_len; i++) {
684                 /* wait for FIFO empty  */
685                 if (stb0899_wait_diseqc_fifo_empty(state, 10) < 0)
686                         return -ETIMEDOUT;
687
688                 stb0899_write_reg(state, STB0899_DISFIFO, cmd->msg[i]);
689         }
690         reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
691         STB0899_SETFIELD_VAL(DISPRECHARGE, reg, 0);
692         stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
693
694         return 0;
695 }
696
697 static int stb0899_wait_diseqc_rxidle(struct stb0899_state *state, int timeout)
698 {
699         u8 reg = 0;
700         unsigned long start = jiffies;
701
702         while (!STB0899_GETFIELD(RXEND, reg)) {
703                 reg = stb0899_read_reg(state, STB0899_DISRX_ST0);
704                 if (jiffies - start > timeout) {
705                         dprintk(verbose, FE_ERROR, 1, "timed out!!");
706                         return -ETIMEDOUT;
707                 }
708                 msleep(10);
709         }
710
711         return 0;
712 }
713
714 static int stb0899_recv_slave_reply(struct dvb_frontend *fe, struct dvb_diseqc_slave_reply *reply)
715 {
716         struct stb0899_state *state = fe->demodulator_priv;
717         u8 reg, length = 0, i;
718         int result;
719
720         if (stb0899_wait_diseqc_rxidle(state, 100) < 0)
721                 return -ETIMEDOUT;
722
723         reg = stb0899_read_reg(state, STB0899_DISRX_ST0);
724         if (STB0899_GETFIELD(RXEND, reg)) {
725
726                 reg = stb0899_read_reg(state, STB0899_DISRX_ST1);
727                 length = STB0899_GETFIELD(FIFOBYTENBR, reg);
728
729                 if (length > sizeof (reply->msg)) {
730                         result = -EOVERFLOW;
731                         goto exit;
732                 }
733                 reply->msg_len = length;
734
735                 /* extract data */
736                 for (i = 0; i < length; i++)
737                         reply->msg[i] = stb0899_read_reg(state, STB0899_DISFIFO);
738         }
739
740         return 0;
741 exit:
742
743         return result;
744 }
745
746 static int stb0899_wait_diseqc_txidle(struct stb0899_state *state, int timeout)
747 {
748         u8 reg = 0;
749         unsigned long start = jiffies;
750
751         while (!STB0899_GETFIELD(TXIDLE, reg)) {
752                 reg = stb0899_read_reg(state, STB0899_DISSTATUS);
753                 if (jiffies - start > timeout) {
754                         dprintk(verbose, FE_ERROR, 1, "timed out!!");
755                         return -ETIMEDOUT;
756                 }
757                 msleep(10);
758         }
759         return 0;
760 }
761
762 static int stb0899_send_diseqc_burst(struct dvb_frontend *fe, fe_sec_mini_cmd_t burst)
763 {
764         struct stb0899_state *state = fe->demodulator_priv;
765         u8 reg, old_state;
766
767         /* wait for diseqc idle */
768         if (stb0899_wait_diseqc_txidle(state, 100) < 0)
769                 return -ETIMEDOUT;
770
771         reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
772         old_state = reg;
773         /* set to burst mode    */
774         STB0899_SETFIELD_VAL(DISEQCMODE, reg, 0x02);
775         STB0899_SETFIELD_VAL(DISPRECHARGE, reg, 0x01);
776         stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
777         switch (burst) {
778         case SEC_MINI_A:
779                 /* unmodulated  */
780                 stb0899_write_reg(state, STB0899_DISFIFO, 0x00);
781                 break;
782         case SEC_MINI_B:
783                 /* modulated    */
784                 stb0899_write_reg(state, STB0899_DISFIFO, 0xff);
785                 break;
786         }
787         reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
788         STB0899_SETFIELD_VAL(DISPRECHARGE, reg, 0x00);
789         stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
790         /* wait for diseqc idle */
791         if (stb0899_wait_diseqc_txidle(state, 100) < 0)
792                 return -ETIMEDOUT;
793
794         /* restore state        */
795         stb0899_write_reg(state, STB0899_DISCNTRL1, old_state);
796
797         return 0;
798 }
799
800
801 static int stb0899_sleep(struct dvb_frontend *fe)
802 {
803         struct stb0899_state *state = fe->demodulator_priv;
804         u8 reg;
805
806         dprintk(verbose, FE_DEBUG, 1, "Going to Sleep .. (Really tired .. :-))");
807         return 0;
808 }
809
810 static int stb0899_wakeup(struct dvb_frontend *fe)
811 {
812         int rc;
813         struct stb0899_state *state = fe->demodulator_priv;
814
815
816         return 0;
817 }
818
819 static int stb0899_init(struct dvb_frontend *fe)
820 {
821         int i;
822         struct stb0899_state *state = fe->demodulator_priv;
823         struct stb0899_config *config = state->config;
824
825         dprintk(verbose, FE_DEBUG, 1, "Initializing STB0899 ... ");
826 //      mutex_init(&state->search_lock);
827
828         /* init device          */
829         dprintk(verbose, FE_DEBUG, 1, "init device");
830         for (i = 0; config->init_dev[i].address != 0xffff; i++)
831                 stb0899_write_reg(state, config->init_dev[i].address, config->init_dev[i].data);
832
833         dprintk(verbose, FE_DEBUG, 1, "init S2 demod");
834         /* init S2 demod        */
835         for (i = 0; config->init_s2_demod[i].offset != 0xffff; i++)
836                 stb0899_write_s2reg(state, STB0899_S2DEMOD,
837                                     config->init_s2_demod[i].base_address,
838                                     config->init_s2_demod[i].offset,
839                                     config->init_s2_demod[i].data);
840
841         dprintk(verbose, FE_DEBUG, 1, "init S1 demod");
842         /* init S1 demod        */
843         for (i = 0; config->init_s1_demod[i].address != 0xffff; i++)
844                 stb0899_write_reg(state, config->init_s1_demod[i].address, config->init_s1_demod[i].data);
845
846         dprintk(verbose, FE_DEBUG, 1, "init S2 FEC");
847         /* init S2 fec          */
848         for (i = 0; config->init_s2_fec[i].offset != 0xffff; i++)
849                 stb0899_write_s2reg(state, STB0899_S2FEC,
850                                     config->init_s2_fec[i].base_address,
851                                     config->init_s2_fec[i].offset,
852                                     config->init_s2_fec[i].data);
853
854         dprintk(verbose, FE_DEBUG, 1, "init TST");
855         /* init test            */
856         for (i = 0; config->init_tst[i].address != 0xffff; i++)
857                 stb0899_write_reg(state, config->init_tst[i].address, config->init_tst[i].data);
858
859         stb0899_init_calc(state);
860 //      stb0899_diseqc_init(state);
861
862         return 0;
863 }
864
865 static int stb0899_table_lookup(const struct stb0899_tab *tab, int max, int val)
866 {
867         int res = 0;
868         int min = 0, med;
869
870         if (val < tab[min].read)
871                 res = tab[min].real;
872         else if (val >= tab[max].read)
873                 res = tab[max].real;
874         else {
875                 while ((max - min) > 1) {
876                         med = (max + min) / 2;
877                         if (val >= tab[min].read && val < tab[med].read)
878                                 max = med;
879                         else
880                                 min = med;
881                 }
882                 res = ((val - tab[min].read) *
883                        (tab[max].real - tab[min].real) /
884                        (tab[max].read - tab[min].read)) +
885                         tab[min].real;
886         }
887
888         return res;
889 }
890
891 static int stb0899_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
892 {
893         struct stb0899_state *state             = fe->demodulator_priv;
894         struct stb0899_internal *internal       = &state->internal;
895
896         int val;
897         u32 reg;
898         switch (state->delsys) {
899         case DVBFE_DELSYS_DVBS:
900         case DVBFE_DELSYS_DSS:
901                 if (internal->lock) {
902                         reg  = stb0899_read_reg(state, STB0899_VSTATUS);
903                         if (STB0899_GETFIELD(VSTATUS_LOCKEDVIT, reg)) {
904
905                                 reg = stb0899_read_reg(state, STB0899_AGCIQIN);
906                                 val = (s32)(s8)STB0899_GETFIELD(AGCIQVALUE, reg);
907
908                                 *strength = stb0899_table_lookup(stb0899_dvbsrf_tab, ARRAY_SIZE(stb0899_dvbsrf_tab) - 1, val);
909                                 *strength += 750;
910                                 dprintk(verbose, FE_DEBUG, 1, "AGCIQVALUE = 0x%02x, C = %d * 0.1 dBm",
911                                         val & 0xff, *strength);
912                         }
913                 }
914                 break;
915         case DVBFE_DELSYS_DVBS2:
916                 if (internal->lock) {
917                         reg = STB0899_READ_S2REG(STB0899_DEMOD, IF_AGC_GAIN);
918                         val = STB0899_GETFIELD(IF_AGC_GAIN, reg);
919
920                         *strength = stb0899_table_lookup(stb0899_dvbs2rf_tab, ARRAY_SIZE(stb0899_dvbs2rf_tab) - 1, val);
921                         *strength += 750;
922                         dprintk(verbose, FE_DEBUG, 1, "IF_AGC_GAIN = 0x%04x, C = %d * 0.1 dBm",
923                                 val & 0x3fff, *strength);
924                 }
925                 break;
926         default:
927                 dprintk(verbose, FE_DEBUG, 1, "Unsupported delivery system");
928                 break;
929         }
930
931         return 0;
932 }
933
934 static int stb0899_read_snr(struct dvb_frontend *fe, u16 *snr)
935 {
936         struct stb0899_state *state             = fe->demodulator_priv;
937         struct stb0899_internal *internal       = &state->internal;
938
939         unsigned int val, quant, quantn = -1, est, estn = -1;
940         u8 buf[2];
941         u32 reg;
942
943         reg  = stb0899_read_reg(state, STB0899_VSTATUS);
944         switch (state->delsys) {
945         case DVBFE_DELSYS_DVBS:
946         case DVBFE_DELSYS_DSS:
947                 if (internal->lock) {
948                         if (STB0899_GETFIELD(VSTATUS_LOCKEDVIT, reg)) {
949
950                                 stb0899_read_regs(state, STB0899_NIRM, buf, 2);
951                                 val = MAKEWORD16(buf[0], buf[1]);
952
953                                 *snr = stb0899_table_lookup(stb0899_cn_tab, ARRAY_SIZE(stb0899_cn_tab) - 1, val);
954                                 dprintk(verbose, FE_DEBUG, 1, "NIR = 0x%02x%02x = %u, C/N = %d * 0.1 dBm\n",
955                                         buf[0], buf[1], val, *snr);
956                         }
957                 }
958                 break;
959         case DVBFE_DELSYS_DVBS2:
960                 if (internal->lock) {
961                         reg = STB0899_READ_S2REG(STB0899_S2DEMOD, UWP_CNTRL1);
962                         quant = STB0899_GETFIELD(UWP_ESN0_QUANT, reg);
963                         reg = STB0899_READ_S2REG(STB0899_S2DEMOD, UWP_STAT2);
964                         est = STB0899_GETFIELD(ESN0_EST, reg);
965                         if (est == 1)
966                                 val = 301; /* C/N = 30.1 dB */
967                         else if (est == 2)
968                                 val = 270; /* C/N = 27.0 dB */
969                         else {
970                                 /* quantn = 100 * log(quant^2) */
971                                 quantn = stb0899_table_lookup(stb0899_quant_tab, ARRAY_SIZE(stb0899_quant_tab) - 1, quant * 100);
972                                 /* estn = 100 * log(est) */
973                                 estn = stb0899_table_lookup(stb0899_est_tab, ARRAY_SIZE(stb0899_est_tab) - 1, est);
974                                 /* snr(dBm/10) = -10*(log(est)-log(quant^2)) => snr(dBm/10) = (100*log(quant^2)-100*log(est))/10 */
975                                 val = (quantn - estn) / 10;
976                         }
977                         *snr = val;
978                         dprintk(verbose, FE_DEBUG, 1, "Es/N0 quant = %d (%d) estimate = %u (%d), C/N = %d * 0.1 dBm",
979                                 quant, quantn, est, estn, val);
980                 }
981                 break;
982         default:
983                 dprintk(verbose, FE_DEBUG, 1, "Unsupported delivery system");
984                 break;
985         }
986
987         return 0;
988 }
989
990 static int stb0899_read_status(struct dvb_frontend *fe, enum fe_status *status)
991 {
992         struct stb0899_state *state             = fe->demodulator_priv;
993         struct stb0899_internal *internal       = &state->internal;
994         u8 reg;
995         *status = 0;
996
997         switch (state->delsys) {
998         case DVBFE_DELSYS_DVBS:
999         case DVBFE_DELSYS_DSS:
1000                 dprintk(state->verbose, FE_DEBUG, 1, "Delivery system DVB-S/DSS");
1001                 if (internal->lock) {
1002                         reg  = stb0899_read_reg(state, STB0899_VSTATUS);
1003                         if (STB0899_GETFIELD(VSTATUS_LOCKEDVIT, reg)) {
1004                                 dprintk(state->verbose, FE_DEBUG, 1, "--------> FE_HAS_CARRIER | FE_HAS_LOCK");
1005                                 *status |= FE_HAS_CARRIER | FE_HAS_LOCK;
1006
1007                                 reg = stb0899_read_reg(state, STB0899_PLPARM);
1008                                 if (STB0899_GETFIELD(VITCURPUN, reg)) {
1009                                         dprintk(state->verbose, FE_DEBUG, 1, "--------> FE_HAS_VITERBI | FE_HAS_SYNC");
1010                                         *status |= FE_HAS_VITERBI | FE_HAS_SYNC;
1011                                 }
1012                         }
1013                 }
1014                 break;
1015         case DVBFE_DELSYS_DVBS2:
1016                 dprintk(state->verbose, FE_DEBUG, 1, "Delivery system DVB-S2");
1017                 if (internal->lock) {
1018                         reg = STB0899_READ_S2REG(STB0899_S2DEMOD, DMD_STAT2);
1019                         if (STB0899_GETFIELD(UWP_LOCK, reg) && STB0899_GETFIELD(CSM_LOCK, reg)) {
1020                                 *status |= FE_HAS_CARRIER;
1021                                 dprintk(state->verbose, FE_DEBUG, 1,
1022                                         "UWP & CSM Lock ! ---> DVB-S2 FE_HAS_CARRIER");
1023
1024                                 reg = stb0899_read_reg(state, STB0899_CFGPDELSTATUS1);
1025                                 if (STB0899_GETFIELD(CFGPDELSTATUS_LOCK, reg)) {
1026                                         *status |= FE_HAS_LOCK;
1027                                         dprintk(state->verbose, FE_DEBUG, 1,
1028                                                 "Packet Delineator Locked ! -----> DVB-S2 FE_HAS_LOCK");
1029
1030                                 }
1031                                 if (STB0899_GETFIELD(CONTINUOUS_STREAM, reg)) {
1032                                         *status |= FE_HAS_VITERBI;
1033                                         dprintk(state->verbose, FE_DEBUG, 1,
1034                                                 "Packet Delineator found VITERBI ! -----> DVB-S2 FE_HAS_VITERBI");
1035                                 }
1036                                 if (STB0899_GETFIELD(ACCEPTED_STREAM, reg)) {
1037                                         *status |= FE_HAS_SYNC;
1038                                         dprintk(state->verbose, FE_DEBUG, 1,
1039                                                 "Packet Delineator found SYNC ! -----> DVB-S2 FE_HAS_SYNC");
1040                                 }
1041                         }
1042                 }
1043                 break;
1044         default:
1045                 dprintk(verbose, FE_DEBUG, 1, "Unsupported delivery system");
1046                 break;
1047         }
1048         return 0;
1049 }
1050
1051 /*
1052  * stb0899_get_error
1053  * viterbi error for DVB-S/DSS
1054  * packet error for DVB-S2
1055  * Bit Error Rate or Packet Error Rate * 10 ^ 7
1056  */
1057 static int stb0899_read_ber(struct dvb_frontend *fe, u32 *ber)
1058 {
1059         struct stb0899_state *state             = fe->demodulator_priv;
1060         struct stb0899_internal *internal       = &state->internal;
1061
1062         u8  lsb, msb;
1063         u32 i;
1064
1065         *ber = 0;
1066
1067         switch (state->delsys) {
1068         case DVBFE_DELSYS_DVBS:
1069         case DVBFE_DELSYS_DSS:
1070                 if (internal->lock) {
1071                         /* average 5 BER values */
1072                         for (i = 0; i < 5; i++) {
1073                                 msleep(100);
1074                                 lsb = stb0899_read_reg(state, STB0899_ECNT1L);
1075                                 msb = stb0899_read_reg(state, STB0899_ECNT1M);
1076                                 *ber += MAKEWORD16(msb, lsb);
1077                         }
1078                         *ber /= 5;
1079                         /* Viterbi Check        */
1080                         if (STB0899_GETFIELD(VSTATUS_PRFVIT, internal->v_status)) {
1081                                 /* Error Rate           */
1082                                 *ber *= 9766;
1083                                 /* ber = ber * 10 ^ 7   */
1084                                 *ber /= (-1 + (1 << (2 * STB0899_GETFIELD(NOE, internal->err_ctrl))));
1085                                 *ber /= 8;
1086                         }
1087                 }
1088                 break;
1089         case DVBFE_DELSYS_DVBS2:
1090                 if (internal->lock) {
1091                         /* Average 5 PER values */
1092                         for (i = 0; i < 5; i++) {
1093                                 msleep(100);
1094                                 lsb = stb0899_read_reg(state, STB0899_ECNT1L);
1095                                 msb = stb0899_read_reg(state, STB0899_ECNT1M);
1096                                 *ber += MAKEWORD16(msb, lsb);
1097                         }
1098                         /* ber = ber * 10 ^ 7   */
1099                         *ber *= 10000000;
1100                         *ber /= (-1 + (1 << (4 + 2 * STB0899_GETFIELD(NOE, internal->err_ctrl))));
1101                 }
1102                 break;
1103         default:
1104                 dprintk(verbose, FE_DEBUG, 1, "Unsupported delivery system");
1105         }
1106
1107         return 0;
1108 }
1109
1110 static int stb0899_set_voltage(struct dvb_frontend *fe, fe_sec_voltage_t voltage)
1111 {
1112         struct stb0899_state *state = fe->demodulator_priv;
1113
1114         switch (voltage) {
1115         case SEC_VOLTAGE_13:
1116                 stb0899_write_reg(state, STB0899_GPIO00CFG, 0x82);
1117                 stb0899_write_reg(state, STB0899_GPIO01CFG, 0x02);
1118                 stb0899_write_reg(state, STB0899_GPIO02CFG, 0x00);
1119                 break;
1120         case SEC_VOLTAGE_18:
1121                 stb0899_write_reg(state, STB0899_GPIO00CFG, 0x02);
1122                 stb0899_write_reg(state, STB0899_GPIO01CFG, 0x02);
1123                 stb0899_write_reg(state, STB0899_GPIO02CFG, 0x82);
1124                 break;
1125         case SEC_VOLTAGE_OFF:
1126                 stb0899_write_reg(state, STB0899_GPIO00CFG, 0x82);
1127                 stb0899_write_reg(state, STB0899_GPIO01CFG, 0x82);
1128                 stb0899_write_reg(state, STB0899_GPIO02CFG, 0x82);
1129                 break;
1130         default:
1131                 return -EINVAL;
1132         }
1133
1134         return 0;
1135 }
1136
1137 static int stb0899_set_tone(struct dvb_frontend *fe, fe_sec_tone_mode_t tone)
1138 {
1139         struct stb0899_state *state = fe->demodulator_priv;
1140         struct stb0899_internal *internal = &state->internal;
1141
1142         u8 div;
1143
1144         /* wait for diseqc idle */
1145         if (stb0899_wait_diseqc_txidle(state, 100) < 0)
1146                 return -ETIMEDOUT;
1147
1148         switch (tone) {
1149         case SEC_TONE_ON:
1150                 div = (internal->master_clk / 100) / 5632;
1151                 div = (div + 5) / 10;
1152                 stb0899_write_reg(state, STB0899_DISEQCOCFG, 0x66);
1153                 stb0899_write_reg(state, STB0899_ACRPRESC, 0x31);
1154                 stb0899_write_reg(state, STB0899_ACRDIV1, div);
1155                 break;
1156         case SEC_TONE_OFF:
1157                 stb0899_write_reg(state, STB0899_DISEQCOCFG, 0x20);
1158                 break;
1159         default:
1160                 break;
1161         }
1162         return 0;
1163 }
1164
1165 static int stb0899_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
1166 {
1167         int i2c_stat;
1168         struct stb0899_state *state = fe->demodulator_priv;
1169
1170         i2c_stat = stb0899_read_reg(state, STB0899_I2CRPT);
1171         if (i2c_stat < 0)
1172                 goto err;
1173
1174         if (enable) {
1175                 dprintk(state->verbose, FE_DEBUG, 1, "Enabling I2C Repeater ...");
1176                 i2c_stat |=  STB0899_I2CTON;
1177                 if (stb0899_write_reg(state, STB0899_I2CRPT, i2c_stat) < 0)
1178                         goto err;
1179         }
1180         return 0;
1181 err:
1182         dprintk(state->verbose, FE_ERROR, 1, "I2C Repeater enable failed");
1183         return -EREMOTEIO;
1184 }
1185
1186
1187 static inline void CONVERT32(u32 x, char *str)
1188 {
1189         *str++  = (x >> 24) & 0xff;
1190         *str++  = (x >> 16) & 0xff;
1191         *str++  = (x >>  8) & 0xff;
1192         *str++  = (x >>  0) & 0xff;
1193         *str    = '\0';
1194 }
1195
1196 int stb0899_get_dev_id(struct stb0899_state *state)
1197 {
1198         u8 chip_id, release;
1199         u16 id;
1200         u32 demod_ver = 0, fec_ver = 0;
1201         char demod_str[4] = { 0 };
1202         char fec_str[4] = { 0 };
1203
1204         id = stb0899_read_reg(state, STB0899_DEV_ID);
1205         dprintk(state->verbose, FE_DEBUG, 1, "ID reg=[0x%02x]", id);
1206         chip_id = STB0899_GETFIELD(CHIP_ID, id);
1207         release = STB0899_GETFIELD(CHIP_REL, id);
1208
1209         dprintk(state->verbose, FE_ERROR, 1, "Device ID=[%d], Release=[%d]",
1210                 chip_id, release);
1211
1212         CONVERT32(STB0899_READ_S2REG(STB0899_S2DEMOD, DMD_CORE_ID), (char *)&demod_str);
1213
1214         demod_ver = STB0899_READ_S2REG(STB0899_S2DEMOD, DMD_VERSION_ID);
1215         dprintk(state->verbose, FE_ERROR, 1, "Demodulator Core ID=[%s], Version=[%d]", (char *) &demod_str, demod_ver);
1216         CONVERT32(STB0899_READ_S2REG(STB0899_S2FEC, FEC_CORE_ID_REG), (char *)&fec_str);
1217         fec_ver = STB0899_READ_S2REG(STB0899_S2FEC, FEC_VER_ID_REG);
1218         if (! (chip_id > 0)) {
1219                 dprintk(state->verbose, FE_ERROR, 1, "couldn't find a STB 0899");
1220
1221                 return -ENODEV;
1222         }
1223         dprintk(state->verbose, FE_ERROR, 1, "FEC Core ID=[%s], Version=[%d]", (char*) &fec_str, fec_ver);
1224
1225         return 0;
1226 }
1227
1228 static const struct dvbfe_info dvbs_info        = {
1229         .name                                   = "STB0899 DVB-S",
1230         .delivery                               = DVBFE_DELSYS_DVBS,
1231         .delsys                                 = {
1232                 .dvbs.modulation                = DVBFE_MOD_QPSK,
1233                 .dvbs.fec                       = DVBFE_FEC_1_2 | DVBFE_FEC_2_3 |
1234                                                   DVBFE_FEC_3_4 | DVBFE_FEC_5_6 |
1235                                                   DVBFE_FEC_6_7
1236         },
1237
1238         .frequency_min                          = 950000,
1239         .frequency_max                          = 2150000,
1240         .frequency_step                         = 0,
1241         .symbol_rate_min                        = 1000000,
1242         .symbol_rate_max                        = 45000000,
1243         .symbol_rate_tolerance                  = 0
1244 };
1245
1246 static const struct dvbfe_info dss_info         = {
1247         .name                                   = "STB0899 DSS",
1248         .delivery                               = DVBFE_DELSYS_DSS,
1249         .delsys                                 = {
1250                 .dss.modulation                 = DVBFE_MOD_BPSK | DVBFE_MOD_QPSK,
1251                 .dss.fec                        = DVBFE_FEC_1_2  | DVBFE_FEC_2_3 |
1252                                                   DVBFE_FEC_3_4  | DVBFE_FEC_5_6 |
1253                                                   DVBFE_FEC_6_7
1254         },
1255
1256         .frequency_min                          = 950000,
1257         .frequency_max                          = 2150000,
1258         .frequency_step                         = 0,
1259         .symbol_rate_min                        = 1000000,
1260         .symbol_rate_max                        = 45000000,
1261         .symbol_rate_tolerance                  = 0
1262 };
1263
1264 static const struct dvbfe_info dvbs2_info       = {
1265         .name                                   = "STB0899 DVB-S2",
1266         .delivery                               = DVBFE_DELSYS_DVBS2,
1267         .delsys                                 = {
1268                 .dvbs2.modulation               = DVBFE_MOD_QPSK   | DVBFE_MOD_8PSK |
1269                                                   DVBFE_MOD_16APSK | DVBFE_MOD_32APSK,
1270
1271                 .dvbs2.fec                      = DVBFE_FEC_1_4 | DVBFE_FEC_1_3 |
1272                                                   DVBFE_FEC_2_5 | DVBFE_FEC_1_2 |
1273                                                   DVBFE_FEC_3_5 | DVBFE_FEC_2_3 |
1274                                                   DVBFE_FEC_3_4 | DVBFE_FEC_4_5 |
1275                                                   DVBFE_FEC_5_6 | DVBFE_FEC_8_9 |
1276                                                   DVBFE_FEC_9_10,
1277         },
1278
1279         .frequency_min          = 950000,
1280         .frequency_max          = 2150000,
1281         .frequency_step         = 0,
1282         .symbol_rate_min        = 1000000,
1283         .symbol_rate_max        = 45000000,
1284         .symbol_rate_tolerance  = 0
1285 };
1286
1287 static int stb0899_get_info(struct dvb_frontend *fe, struct dvbfe_info *fe_info)
1288 {
1289         struct stb0899_state *state = fe->demodulator_priv;
1290
1291         dprintk(verbose, FE_DEBUG, 1, "Get Info");
1292
1293         state->delsys = fe_info->delivery;
1294         switch (state->delsys) {
1295         case DVBFE_DELSYS_DVBS:
1296                 dprintk(verbose, FE_ERROR, 1, "Querying DVB-S info");
1297                 memcpy(fe_info, &dvbs_info, sizeof (struct dvbfe_info));
1298                 break;
1299         case DVBFE_DELSYS_DSS:
1300                 dprintk(verbose, FE_ERROR, 1, "Querying DSS info");
1301                 memcpy(fe_info, &dss_info, sizeof (struct dvbfe_info));
1302                 break;
1303         case DVBFE_DELSYS_DVBS2:
1304                 dprintk(verbose, FE_ERROR, 1, "Querying DVB-S2 info");
1305                 memcpy(fe_info, &dvbs2_info, sizeof (struct dvbfe_info));
1306                 break;
1307         default:
1308                 dprintk(verbose, FE_ERROR, 1, "Unsupported delivery system");
1309                 return -EINVAL;
1310         }
1311         dprintk(verbose, FE_DEBUG, 1, "delivery system=%d", state->delsys);
1312
1313         return 0;
1314 }
1315
1316 static int stb0899_get_delsys(struct dvb_frontend *fe, enum dvbfe_delsys *fe_delsys)
1317 {
1318         *fe_delsys = DVBFE_DELSYS_DVBS | DVBFE_DELSYS_DSS | DVBFE_DELSYS_DVBS2;
1319
1320         return 0;
1321 }
1322
1323 void stb0899_set_delsys(struct stb0899_state *state)
1324 {
1325         u8 reg;
1326         u8 stop_clk[2];
1327
1328         stop_clk[0] = stb0899_read_reg(state, STB0899_STOPCLK1);
1329         stop_clk[1] = stb0899_read_reg(state, STB0899_STOPCLK2);
1330
1331         switch (state->delsys) {
1332         case DVBFE_DELSYS_DVBS:
1333                 dprintk(verbose, FE_DEBUG, 1, "Delivery System -- DVB-S");
1334                 /* FECM/Viterbi ON      */
1335                 reg = stb0899_read_reg(state, STB0899_FECM);
1336                 STB0899_SETFIELD_VAL(FECM_RSVD0, reg, 0);
1337                 STB0899_SETFIELD_VAL(FECM_VITERBI_ON, reg, 1);
1338                 stb0899_write_reg(state, STB0899_FECM, reg);
1339
1340                 stb0899_write_reg(state, STB0899_RSULC, 0xb1);
1341                 stb0899_write_reg(state, STB0899_TSULC, 0x40);
1342                 stb0899_write_reg(state, STB0899_RSLLC, 0x42);
1343                 stb0899_write_reg(state, STB0899_TSLPL, 0x12);
1344
1345                 reg = stb0899_read_reg(state, STB0899_TSTRES);
1346                 STB0899_SETFIELD_VAL(FRESLDPC, reg, 1);
1347                 stb0899_write_reg(state, STB0899_TSTRES, reg);
1348
1349                 STB0899_SETFIELD_VAL(STOP_CHK8PSK, stop_clk[0], 1);
1350                 STB0899_SETFIELD_VAL(STOP_CKFEC108, stop_clk[0], 1);
1351                 STB0899_SETFIELD_VAL(STOP_CKFEC216, stop_clk[0], 1);
1352
1353                 STB0899_SETFIELD_VAL(STOP_CKPKDLIN108, stop_clk[1], 1);
1354                 STB0899_SETFIELD_VAL(STOP_CKPKDLIN216, stop_clk[1], 1);
1355
1356                 STB0899_SETFIELD_VAL(STOP_CKINTBUF216, stop_clk[0], 1);
1357                 STB0899_SETFIELD_VAL(STOP_CKCORE216, stop_clk[0], 1);
1358
1359                 STB0899_SETFIELD_VAL(STOP_CKS2DMD108, stop_clk[1], 1);
1360                 break;
1361         case DVBFE_DELSYS_DVBS2:
1362                 /* FECM/Viterbi OFF     */
1363                 reg = stb0899_read_reg(state, STB0899_FECM);
1364                 STB0899_SETFIELD_VAL(FECM_RSVD0, reg, 0);
1365                 STB0899_SETFIELD_VAL(FECM_VITERBI_ON, reg, 0);
1366                 stb0899_write_reg(state, STB0899_FECM, reg);
1367
1368                 stb0899_write_reg(state, STB0899_RSULC, 0xb1);
1369                 stb0899_write_reg(state, STB0899_TSULC, 0x42);
1370                 stb0899_write_reg(state, STB0899_RSLLC, 0x40);
1371                 stb0899_write_reg(state, STB0899_TSLPL, 0x02);
1372
1373                 reg = stb0899_read_reg(state, STB0899_TSTRES);
1374                 STB0899_SETFIELD_VAL(FRESLDPC, reg, 0);
1375                 stb0899_write_reg(state, STB0899_TSTRES, reg);
1376
1377                 STB0899_SETFIELD_VAL(STOP_CHK8PSK, stop_clk[0], 1);
1378                 STB0899_SETFIELD_VAL(STOP_CKFEC108, stop_clk[0], 0);
1379                 STB0899_SETFIELD_VAL(STOP_CKFEC216, stop_clk[0], 0);
1380
1381                 STB0899_SETFIELD_VAL(STOP_CKPKDLIN108, stop_clk[1], 0);
1382                 STB0899_SETFIELD_VAL(STOP_CKPKDLIN216, stop_clk[1], 0);
1383
1384                 STB0899_SETFIELD_VAL(STOP_CKINTBUF216, stop_clk[0], 0);
1385                 STB0899_SETFIELD_VAL(STOP_CKCORE216, stop_clk[0], 0);
1386
1387                 STB0899_SETFIELD_VAL(STOP_CKS2DMD108, stop_clk[1], 0);
1388                 break;
1389         case DVBFE_DELSYS_DSS:
1390                 /* FECM/Viterbi ON      */
1391                 reg = stb0899_read_reg(state, STB0899_FECM);
1392                 STB0899_SETFIELD_VAL(FECM_RSVD0, reg, 1);
1393                 STB0899_SETFIELD_VAL(FECM_VITERBI_ON, reg, 1);
1394                 stb0899_write_reg(state, STB0899_FECM, reg);
1395
1396                 stb0899_write_reg(state, STB0899_RSULC, 0xa1);
1397                 stb0899_write_reg(state, STB0899_TSULC, 0x61);
1398                 stb0899_write_reg(state, STB0899_RSLLC, 0x42);
1399
1400                 reg = stb0899_read_reg(state, STB0899_TSTRES);
1401                 STB0899_SETFIELD_VAL(FRESLDPC, reg, 1);
1402                 stb0899_write_reg(state, STB0899_TSTRES, reg);
1403
1404                 STB0899_SETFIELD_VAL(STOP_CHK8PSK, stop_clk[0], 1);
1405                 STB0899_SETFIELD_VAL(STOP_CKFEC108, stop_clk[0], 1);
1406                 STB0899_SETFIELD_VAL(STOP_CKFEC216, stop_clk[0], 1);
1407
1408                 STB0899_SETFIELD_VAL(STOP_CKPKDLIN108, stop_clk[1], 1);
1409                 STB0899_SETFIELD_VAL(STOP_CKPKDLIN216, stop_clk[1], 1);
1410
1411                 STB0899_SETFIELD_VAL(STOP_CKCORE216, stop_clk[0], 0);
1412
1413                 STB0899_SETFIELD_VAL(STOP_CKS2DMD108, stop_clk[1], 1);
1414                 break;
1415         default:
1416                 dprintk(verbose, FE_ERROR, 1, "Unsupported delivery system");
1417                 break;
1418         }
1419         STB0899_SETFIELD_VAL(STOP_CKADCI108, stop_clk[0], 0);
1420         stb0899_write_regs(state, STB0899_STOPCLK1, stop_clk, 2);
1421 }
1422
1423 /*
1424  * stb0899_set_iterations
1425  * set the LDPC iteration scale function
1426  */
1427 static void stb0899_set_iterations(struct stb0899_state *state)
1428 {
1429         struct stb0899_internal *internal = &state->internal;
1430         struct stb0899_config *config = state->config;
1431
1432         s32 iter_scale;
1433         u32 reg;
1434
1435         iter_scale = 17 * (internal->master_clk / 1000);
1436         iter_scale += 410000;
1437         iter_scale /= (internal->srate / 1000000);
1438         iter_scale /= 1000;
1439
1440         if (iter_scale > config->ldpc_max_iter)
1441                 iter_scale = config->ldpc_max_iter;
1442
1443         reg = STB0899_READ_S2REG(STB0899_S2DEMOD, MAX_ITER);
1444         STB0899_SETFIELD_VAL(MAX_ITERATIONS, reg, iter_scale);
1445         stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_MAX_ITER, STB0899_OFF0_MAX_ITER, reg);
1446 }
1447
1448 static enum dvbfe_search stb0899_search(struct dvb_frontend *fe, struct dvbfe_params *params)
1449 {
1450         struct stb0899_state *state = fe->demodulator_priv;
1451         struct stb0899_params *i_params = &state->params;
1452         struct stb0899_internal *internal = &state->internal;
1453
1454         u32 SearchRange, gain;
1455
1456         switch (state->delsys) {
1457         case DVBFE_DELSYS_DVBS:
1458                 dprintk(verbose, FE_ERROR, 1, "set DVB-S params");
1459                 i_params->freq  = params->frequency;
1460                 i_params->srate = params->delsys.dvbs.symbol_rate;
1461                 break;
1462         case DVBFE_DELSYS_DSS:
1463                 dprintk(verbose, FE_ERROR, 1, "set DSS params");
1464                 i_params->freq  = params->frequency;
1465                 i_params->srate = params->delsys.dss.symbol_rate;
1466                 break;
1467         case DVBFE_DELSYS_DVBS2:
1468                 dprintk(verbose, FE_ERROR, 1, "set DVB-S2 params");
1469                 i_params->freq  = params->frequency;
1470                 i_params->srate = params->delsys.dvbs2.symbol_rate;
1471                 break;
1472         default:
1473                 dprintk(verbose, FE_ERROR, 1, "Unsupported delivery system");
1474                 return -EINVAL;
1475         }
1476         dprintk(verbose, FE_DEBUG, 1, "delivery system=%d", state->delsys);
1477
1478 //      SearchRange = 3000000; /* Search Bandwidth (3 Mhz, was initially 10  Mhz)       */
1479         SearchRange = 10000000; /* Search Bandwidth (3 Mhz, was initially 10  Mhz)      */
1480         dprintk(verbose, FE_DEBUG, 1, "Frequency=%d, Srate=%d", i_params->freq, i_params->srate);
1481         /* checking Search Range is meaningless for a fixed 3 Mhz                       */
1482         if (INRANGE(i_params->srate, 1000000, 45000000)) {
1483                 dprintk(verbose, FE_DEBUG, 1, "Parameters IN RANGE");
1484                 stb0899_set_delsys(state);
1485
1486                 if (state->config->tuner_set_rfsiggain) {
1487                         if (internal->srate > 15000000)
1488                                 gain =  8;      /* 15Mb < srate < 45Mb, gain = 8dB      */
1489                         else if (internal->srate > 5000000)
1490                                 gain = 12;      /*  5Mb < srate < 15Mb, gain = 12dB     */
1491                         else
1492                                 gain = 14;      /*  1Mb < srate <  5Mb, gain = 14db     */
1493                         state->config->tuner_set_rfsiggain(fe, gain);
1494                 }
1495
1496                 if (i_params->srate <= 5000000)
1497                         stb0899_set_mclk(state, 76500000);
1498                 else
1499                         stb0899_set_mclk(state, 90000000);
1500
1501                 switch (state->delsys) {
1502                 case DVBFE_DELSYS_DVBS:
1503                 case DVBFE_DELSYS_DSS:
1504                         dprintk(verbose, FE_DEBUG, 1, "DVB-S delivery system");
1505                         internal->freq  = i_params->freq;
1506                         internal->srate = i_params->srate;
1507                         /*
1508                          * search = user search range +
1509                          *          500Khz +
1510                          *          2 * Tuner_step_size +
1511                          *          10% of the symbol rate
1512                          */
1513                         internal->srch_range    = SearchRange + 1500000 + (i_params->srate / 5);
1514                         internal->derot_percent = 30;
1515
1516                         /* What to do for tuners having no bandwidth setup ?    */
1517                         if (state->config->tuner_set_bandwidth)
1518                                 state->config->tuner_set_bandwidth(fe, (13 * (stb0899_carr_width(state) + 10000000)) / 10);
1519                         if (state->config->tuner_get_bandwidth)
1520                                 state->config->tuner_get_bandwidth(fe, &internal->tuner_bw);
1521                         /* Set DVB-S1 AGC               */
1522                         stb0899_write_reg(state, STB0899_AGCRFCFG, 0x11);
1523
1524                         /* Run the search algorithm     */
1525                         dprintk(verbose, FE_DEBUG, 1, "running DVB-S search algo ..");
1526                         if (stb0899_dvbs_algo(state)    == RANGEOK) {
1527                                 internal->lock          = 1;
1528                                 dprintk(verbose, FE_DEBUG, 1,
1529                                         "-------------------------------------> DVB-S LOCK !");
1530
1531 //                              stb0899_write_reg(state, STB0899_ERRCTRL1, 0x3d); /* Viterbi Errors     */
1532 //                              internal->v_status = stb0899_read_reg(state, STB0899_VSTATUS);
1533 //                              internal->err_ctrl = stb0899_read_reg(state, STB0899_ERRCTRL1);
1534 //                              dprintk(verbose, FE_DEBUG, 1, "VSTATUS=0x%02x", internal->v_status);
1535 //                              dprintk(verbose, FE_DEBUG, 1, "ERR_CTRL=0x%02x", internal->err_ctrl);
1536
1537                                 return DVBFE_ALGO_SEARCH_SUCCESS;
1538                         } else {
1539                                 internal->lock          = 0;
1540
1541                                 return DVBFE_ALGO_SEARCH_FAILED;
1542                         }
1543                         break;
1544                 case DVBFE_DELSYS_DVBS2:
1545                         internal->freq                  = i_params->freq;
1546                         internal->srate                 = i_params->srate;
1547                         internal->srch_range            = SearchRange;
1548
1549                         if (state->config->tuner_set_bandwidth)
1550                                 state->config->tuner_set_bandwidth(fe, (stb0899_carr_width(state) + 10000000));
1551                         if (state->config->tuner_get_bandwidth)
1552                                 state->config->tuner_get_bandwidth(fe, &internal->tuner_bw);
1553
1554 //                      pParams->SpectralInv            = pSearch->IQ_Inversion;
1555
1556                         /* Set DVB-S2 AGC               */
1557                         stb0899_write_reg(state, STB0899_AGCRFCFG, 0x1c);
1558
1559                         /* Set IterScale =f(MCLK,SYMB)  */
1560                         stb0899_set_iterations(state);
1561
1562                         /* Run the search algorithm     */
1563                         dprintk(verbose, FE_DEBUG, 1, "running DVB-S2 search algo ..");
1564                         if (stb0899_dvbs2_algo(state)   == DVBS2_FEC_LOCK) {
1565                                 internal->lock          = 1;
1566                                 dprintk(verbose, FE_DEBUG, 1,
1567                                         "-------------------------------------> DVB-S2 LOCK !");
1568
1569 //                              stb0899_write_reg(state, STB0899_ERRCTRL1, 0xb6); /* Packet Errors      */
1570 //                              internal->v_status = stb0899_read_reg(state, STB0899_VSTATUS);
1571 //                              internal->err_ctrl = stb0899_read_reg(state, STB0899_ERRCTRL1);
1572
1573                                 return DVBFE_ALGO_SEARCH_SUCCESS;
1574                         } else {
1575                                 internal->lock          = 0;
1576
1577                                 return DVBFE_ALGO_SEARCH_FAILED;
1578                         }
1579                         break;
1580                 default:
1581                         dprintk(verbose, FE_ERROR, 1, "Unsupported delivery system");
1582                         return DVBFE_ALGO_SEARCH_INVALID;
1583                 }
1584         }
1585
1586         return DVBFE_ALGO_SEARCH_ERROR;
1587 }
1588
1589 static enum stb0899_status stb0899_track_carrier(struct stb0899_state *state)
1590 {
1591         u8 reg;
1592
1593         reg = stb0899_read_reg(state, STB0899_DSTATUS);
1594         dprintk(verbose, FE_DEBUG, 1, "--------------------> STB0899_DSTATUS=[0x%02x]", reg);
1595         if (STB0899_GETFIELD(CARRIER_FOUND, reg)) {
1596                 dprintk(verbose, FE_DEBUG, 1, "-------------> CARRIEROK !");
1597                 return CARRIEROK;
1598         } else {
1599                 dprintk(verbose, FE_DEBUG, 1, "-------------> NOCARRIER !");
1600                 return NOCARRIER;
1601         }
1602
1603         return NOCARRIER;
1604 }
1605
1606 static enum stb0899_status stb0899_get_ifagc(struct stb0899_state *state)
1607 {
1608         u8 reg;
1609
1610         reg = STB0899_READ_S2REG(STB0899_S2DEMOD, DMD_STATUS);
1611         dprintk(verbose, FE_DEBUG, 1, "DMD_STATUS=[0x%02x]", reg);
1612         if (STB0899_GETFIELD(IF_AGC_LOCK, reg)) {
1613                 dprintk(verbose, FE_DEBUG, 1, "------------->IF AGC LOCKED !");
1614                 return AGC1OK;
1615         } else {
1616                 dprintk(verbose, FE_DEBUG, 1, "------------->IF AGC LOCK LOST !");
1617                 return NOAGC1;
1618         }
1619
1620         return NOAGC1;
1621 }
1622
1623 static int stb0899_get_s1fec(struct stb0899_internal *internal, enum dvbfe_fec *fec)
1624 {
1625         switch (internal->fecrate) {
1626         case STB0899_FEC_1_2:
1627                 *fec = DVBFE_FEC_1_2;
1628                 break;
1629         case STB0899_FEC_2_3:
1630                 *fec = DVBFE_FEC_2_3;
1631                 break;
1632         case STB0899_FEC_3_4:
1633                 *fec = DVBFE_FEC_3_4;
1634                 break;
1635         case STB0899_FEC_5_6:
1636                 *fec = DVBFE_FEC_5_6;
1637                 break;
1638         case STB0899_FEC_6_7:
1639                 *fec = DVBFE_FEC_6_7;
1640                 break;
1641         case STB0899_FEC_7_8:
1642                 *fec = DVBFE_FEC_7_8;
1643                 break;
1644         default:
1645                 return -EINVAL;
1646         }
1647
1648         return 0;
1649 }
1650
1651 static int stb0899_get_modcod(struct stb0899_internal *internal, struct dvbs2_params *params)
1652 {
1653         switch (internal->modcod) {
1654         case STB0899_DUMMY_PLF:
1655                 params->modulation      = DVBFE_MOD_NONE;
1656                 params->fec             = DVBFE_FEC_NONE;
1657                 break;
1658         case STB0899_QPSK_14:
1659                 params->modulation      = DVBFE_MOD_QPSK;
1660                 params->fec             = DVBFE_FEC_1_4;
1661                 break;
1662         case STB0899_QPSK_13:
1663                 params->modulation      = DVBFE_MOD_QPSK;
1664                 params->fec             = DVBFE_FEC_1_3;
1665                 break;
1666         case STB0899_QPSK_25:
1667                 params->modulation      = DVBFE_MOD_QPSK;
1668                 params->fec             = DVBFE_FEC_2_5;
1669                 break;
1670         case STB0899_QPSK_12:
1671                 params->modulation      = DVBFE_MOD_QPSK;
1672                 params->fec             = DVBFE_FEC_1_2;
1673                 break;
1674         case STB0899_QPSK_35:
1675                 params->modulation      = DVBFE_MOD_QPSK;
1676                 params->fec             = DVBFE_FEC_3_5;
1677                 break;
1678         case STB0899_QPSK_23:
1679                 params->modulation      = DVBFE_MOD_QPSK;
1680                 params->fec             = DVBFE_FEC_2_3;
1681                 break;
1682         case STB0899_QPSK_34:
1683                 params->modulation      = DVBFE_MOD_QPSK;
1684                 params->fec             = DVBFE_FEC_3_4;
1685                 break;
1686         case STB0899_QPSK_45:
1687                 params->modulation      = DVBFE_MOD_QPSK;
1688                 params->fec             = DVBFE_FEC_4_5;
1689                 break;
1690         case STB0899_QPSK_56:
1691                 params->modulation      = DVBFE_MOD_QPSK;
1692                 params->fec             = DVBFE_FEC_5_6;
1693                 break;
1694         case STB0899_QPSK_89:
1695                 params->modulation      = DVBFE_MOD_QPSK;
1696                 params->fec             = DVBFE_FEC_8_9;
1697                 break;
1698         case STB0899_QPSK_910:
1699                 params->modulation      = DVBFE_MOD_QPSK;
1700                 params->fec             = DVBFE_FEC_9_10;
1701                 break;
1702         case STB0899_8PSK_35:
1703                 params->modulation      = DVBFE_MOD_8PSK;
1704                 params->fec             = DVBFE_FEC_3_5;
1705                 break;
1706         case STB0899_8PSK_23:
1707                 params->modulation      = DVBFE_MOD_8PSK;
1708                 params->fec             = DVBFE_FEC_2_3;
1709                 break;
1710         case STB0899_8PSK_34:
1711                 params->modulation      = DVBFE_MOD_8PSK;
1712                 params->fec             = DVBFE_FEC_3_4;
1713                 break;
1714         case STB0899_8PSK_56:
1715                 params->modulation      = DVBFE_MOD_8PSK;
1716                 params->fec             = DVBFE_FEC_5_6;
1717                 break;
1718         case STB0899_8PSK_89:
1719                 params->modulation      = DVBFE_MOD_8PSK;
1720                 params->fec             = DVBFE_FEC_8_9;
1721                 break;
1722         case STB0899_8PSK_910:
1723                 params->modulation      = DVBFE_MOD_8PSK;
1724                 params->fec             = DVBFE_FEC_9_10;
1725                 break;
1726         case STB0899_16APSK_23:
1727                 params->modulation      = DVBFE_MOD_16APSK;
1728                 params->fec             = DVBFE_FEC_2_3;
1729                 break;
1730         case STB0899_16APSK_34:
1731                 params->modulation      = DVBFE_MOD_16APSK;
1732                 params->fec             = DVBFE_FEC_3_4;
1733                 break;
1734         case STB0899_16APSK_45:
1735                 params->modulation      = DVBFE_MOD_16APSK;
1736                 params->fec             = DVBFE_FEC_4_5;
1737                 break;
1738         case STB0899_16APSK_56:
1739                 params->modulation      = DVBFE_MOD_16APSK;
1740                 params->fec             = DVBFE_FEC_5_6;
1741                 break;
1742         case STB0899_16APSK_89:
1743                 params->modulation      = DVBFE_MOD_16APSK;
1744                 params->fec             = DVBFE_FEC_8_9;
1745                 break;
1746         case STB0899_16APSK_910:
1747                 params->modulation      = DVBFE_MOD_16APSK;
1748                 params->fec             = DVBFE_FEC_9_10;
1749                 break;
1750         case STB0899_32APSK_34:
1751                 params->modulation      = DVBFE_MOD_32APSK;
1752                 params->fec             = DVBFE_FEC_3_4;
1753                 break;
1754         case STB0899_32APSK_45:
1755                 params->modulation      = DVBFE_MOD_32APSK;
1756                 params->fec             = DVBFE_FEC_4_5;
1757                 break;
1758         case STB0899_32APSK_56:
1759                 params->modulation      = DVBFE_MOD_32APSK;
1760                 params->fec             = DVBFE_FEC_5_6;
1761                 break;
1762         case STB0899_32APSK_89:
1763                 params->modulation      = DVBFE_MOD_32APSK;
1764                 params->fec             = DVBFE_FEC_8_9;
1765                 break;
1766         case STB0899_32APSK_910:
1767                 params->modulation      = DVBFE_MOD_32APSK;
1768                 params->fec             = DVBFE_FEC_9_10;
1769                 break;
1770         default:
1771                 return -EINVAL;
1772         }
1773
1774         return 0;
1775 }
1776
1777 /*
1778  * stb0899_track
1779  * periodically check the signal level against a specified
1780  * threshold level and perform derotator centering.
1781  * called once we have a lock from a succesful search
1782  * event.
1783  *
1784  * Will be called periodically called to maintain the
1785  * lock.
1786  *
1787  * Will be used to get parameters as well as info from
1788  * the decoded baseband header
1789  *
1790  * Once a new lock has established, the internal state
1791  * frequency (internal->freq) is updated
1792  */
1793 static int stb0899_track(struct dvb_frontend *fe, struct dvbfe_params *params, int *delay)
1794 {
1795         u32 lock_lost;
1796
1797         struct stb0899_state *state             = fe->demodulator_priv;
1798         struct stb0899_internal *internal       = &state->internal;
1799
1800         lock_lost = STB0899_READ_S2REG(STB0899_S2DEMOD, LOCK_LOST);
1801         dprintk(verbose, FE_DEBUG, 1, "Lock Lost=[0x%02x]\n", lock_lost);
1802         if (STB0899_GETFIELD(LOCK_LOST, lock_lost))
1803                 dprintk(verbose, FE_ERROR, 1, "Demodulator LOST LOCK !\n");
1804
1805         *delay = HZ/10;
1806
1807         return 0;
1808 }
1809
1810 static int stb0899_get_params(struct dvb_frontend *fe, struct dvbfe_params *params)
1811 {
1812         struct stb0899_state *state             = fe->demodulator_priv;
1813         struct stb0899_internal *internal       = &state->internal;
1814
1815         params->frequency                       = internal->freq;
1816         params->inversion                       = internal->inversion;
1817         params->delivery                        = state->delsys;
1818         switch (state->delsys) {
1819         case DVBFE_DELSYS_DVBS:
1820                 dprintk(verbose, FE_DEBUG, 1, "Get DVB-S params");
1821                 params->delsys.dvbs.symbol_rate         = internal->srate;
1822                 params->delsys.dvbs.modulation          = DVBFE_MOD_QPSK;
1823                 break;
1824         case DVBFE_DELSYS_DSS:
1825                 dprintk(verbose, FE_DEBUG, 1, "Get DSS params");
1826                 params->delsys.dss.symbol_rate          = internal->srate;
1827                 params->delsys.dss.modulation           = DVBFE_MOD_QPSK;
1828
1829                 break;
1830         case DVBFE_DELSYS_DVBS2:
1831                 dprintk(verbose, FE_DEBUG, 1, "Get DVB-S2 params");
1832                 params->delsys.dvbs2.symbol_rate        = internal->srate;
1833                 break;
1834         default:
1835                 dprintk(verbose, FE_ERROR, 1, "Unsupported delivery system");
1836                 return -EINVAL;
1837         }
1838
1839         return 0;
1840 }
1841
1842 static enum dvbfe_algo stb0899_frontend_algo(struct dvb_frontend *fe)
1843 {
1844         return DVBFE_ALGO_CUSTOM;
1845 }
1846
1847 static struct dvb_frontend_ops stb0899_ops = {
1848
1849         .info = {
1850                 .name                   = "STB0899 Multistandard",
1851         },
1852
1853         .release                        = stb0899_release,
1854         .init                           = stb0899_init,
1855         .sleep                          = stb0899_sleep,
1856 //      .wakeup                         = stb0899_wakeup,
1857
1858         .i2c_gate_ctrl                  = stb0899_i2c_gate_ctrl,
1859         .get_info                       = stb0899_get_info,
1860         .get_delsys                     = stb0899_get_delsys,
1861
1862         .get_frontend_algo              = stb0899_frontend_algo,
1863         .search                         = stb0899_search,
1864         .track                          = stb0899_track,
1865         .get_params                     = stb0899_get_params,
1866
1867         .read_status                    = stb0899_read_status,
1868         .read_snr                       = stb0899_read_snr,
1869         .read_signal_strength           = stb0899_read_signal_strength,
1870         .read_status                    = stb0899_read_status,
1871         .read_ber                       = stb0899_read_ber,
1872
1873         .set_voltage                    = stb0899_set_voltage,
1874         .set_tone                       = stb0899_set_tone,
1875
1876         .diseqc_send_master_cmd         = stb0899_send_diseqc_msg,
1877         .diseqc_recv_slave_reply        = stb0899_recv_slave_reply,
1878         .diseqc_send_burst              = stb0899_send_diseqc_burst,
1879 };
1880
1881 struct dvb_frontend *stb0899_attach(struct stb0899_config *config, struct i2c_adapter *i2c)
1882 {
1883         struct stb0899_state *state = NULL;
1884
1885         state = kzalloc(sizeof (struct stb0899_state), GFP_KERNEL);
1886         if (state == NULL)
1887                 goto error;
1888
1889         state->verbose                          = verbose;
1890         state->config                           = config;
1891         state->i2c                              = i2c;
1892         state->frontend.ops                     = stb0899_ops;
1893         state->frontend.demodulator_priv        = state;
1894
1895         stb0899_wakeup(&state->frontend);
1896         if (stb0899_get_dev_id(state) == -ENODEV) {
1897                 printk("%s: Exiting .. !\n", __func__);
1898                 goto error;
1899         }
1900
1901         printk("%s: Attaching STB0899 \n", __func__);
1902         return &state->frontend;
1903
1904 error:
1905         kfree(state);
1906         return NULL;
1907 }
1908 EXPORT_SYMBOL(stb0899_attach);
1909 MODULE_PARM_DESC(verbose, "Set Verbosity level");
1910 MODULE_AUTHOR("Manu Abraham");
1911 MODULE_DESCRIPTION("STB0899 Multi-Std frontend");
1912 MODULE_LICENSE("GPL");