V4L/DVB (9382): Use a delay for tracking acquisition status
[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
808         reg = stb0899_read_reg(state, STB0899_SYNTCTRL);
809         STB0899_SETFIELD_VAL(STANDBY, reg, 1);
810         stb0899_write_reg(state, STB0899_SYNTCTRL, reg);
811
812         return 0;
813 }
814
815 static int stb0899_wakeup(struct dvb_frontend *fe)
816 {
817         int rc;
818         struct stb0899_state *state = fe->demodulator_priv;
819
820         if ((rc = stb0899_write_reg(state, STB0899_SYNTCTRL, STB0899_SELOSCI)))
821                 return rc;
822         /* Activate all clocks; DVB-S2 registers are inaccessible otherwise. */
823         if ((rc = stb0899_write_reg(state, STB0899_STOPCLK1, 0x00)))
824                 return rc;
825         if ((rc = stb0899_write_reg(state, STB0899_STOPCLK2, 0x00)))
826                 return rc;
827
828         return 0;
829 }
830
831 static int stb0899_init(struct dvb_frontend *fe)
832 {
833         int i;
834         struct stb0899_state *state = fe->demodulator_priv;
835         struct stb0899_config *config = state->config;
836
837         dprintk(verbose, FE_DEBUG, 1, "Initializing STB0899 ... ");
838 //      mutex_init(&state->search_lock);
839
840         /* init device          */
841         dprintk(verbose, FE_DEBUG, 1, "init device");
842         for (i = 0; config->init_dev[i].address != 0xffff; i++)
843                 stb0899_write_reg(state, config->init_dev[i].address, config->init_dev[i].data);
844
845         dprintk(verbose, FE_DEBUG, 1, "init S2 demod");
846         /* init S2 demod        */
847         for (i = 0; config->init_s2_demod[i].offset != 0xffff; i++)
848                 stb0899_write_s2reg(state, STB0899_S2DEMOD,
849                                     config->init_s2_demod[i].base_address,
850                                     config->init_s2_demod[i].offset,
851                                     config->init_s2_demod[i].data);
852
853         dprintk(verbose, FE_DEBUG, 1, "init S1 demod");
854         /* init S1 demod        */
855         for (i = 0; config->init_s1_demod[i].address != 0xffff; i++)
856                 stb0899_write_reg(state, config->init_s1_demod[i].address, config->init_s1_demod[i].data);
857
858         dprintk(verbose, FE_DEBUG, 1, "init S2 FEC");
859         /* init S2 fec          */
860         for (i = 0; config->init_s2_fec[i].offset != 0xffff; i++)
861                 stb0899_write_s2reg(state, STB0899_S2FEC,
862                                     config->init_s2_fec[i].base_address,
863                                     config->init_s2_fec[i].offset,
864                                     config->init_s2_fec[i].data);
865
866         dprintk(verbose, FE_DEBUG, 1, "init TST");
867         /* init test            */
868         for (i = 0; config->init_tst[i].address != 0xffff; i++)
869                 stb0899_write_reg(state, config->init_tst[i].address, config->init_tst[i].data);
870
871         stb0899_init_calc(state);
872 //      stb0899_diseqc_init(state);
873
874         return 0;
875 }
876
877 static int stb0899_table_lookup(const struct stb0899_tab *tab, int max, int val)
878 {
879         int res = 0;
880         int min = 0, med;
881
882         if (val < tab[min].read)
883                 res = tab[min].real;
884         else if (val >= tab[max].read)
885                 res = tab[max].real;
886         else {
887                 while ((max - min) > 1) {
888                         med = (max + min) / 2;
889                         if (val >= tab[min].read && val < tab[med].read)
890                                 max = med;
891                         else
892                                 min = med;
893                 }
894                 res = ((val - tab[min].read) *
895                        (tab[max].real - tab[min].real) /
896                        (tab[max].read - tab[min].read)) +
897                         tab[min].real;
898         }
899
900         return res;
901 }
902
903 static int stb0899_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
904 {
905         struct stb0899_state *state             = fe->demodulator_priv;
906         struct stb0899_internal *internal       = &state->internal;
907
908         int val;
909         u32 reg;
910         switch (state->delsys) {
911         case DVBFE_DELSYS_DVBS:
912         case DVBFE_DELSYS_DSS:
913                 if (internal->lock) {
914                         reg  = stb0899_read_reg(state, STB0899_VSTATUS);
915                         if (STB0899_GETFIELD(VSTATUS_LOCKEDVIT, reg)) {
916
917                                 reg = stb0899_read_reg(state, STB0899_AGCIQIN);
918                                 val = (s32)(s8)STB0899_GETFIELD(AGCIQVALUE, reg);
919
920                                 *strength = stb0899_table_lookup(stb0899_dvbsrf_tab, ARRAY_SIZE(stb0899_dvbsrf_tab) - 1, val);
921                                 *strength += 750;
922                                 dprintk(verbose, FE_DEBUG, 1, "AGCIQVALUE = 0x%02x, C = %d * 0.1 dBm",
923                                         val & 0xff, *strength);
924                         }
925                 }
926                 break;
927         case DVBFE_DELSYS_DVBS2:
928                 if (internal->lock) {
929                         reg = STB0899_READ_S2REG(STB0899_DEMOD, IF_AGC_GAIN);
930                         val = STB0899_GETFIELD(IF_AGC_GAIN, reg);
931
932                         *strength = stb0899_table_lookup(stb0899_dvbs2rf_tab, ARRAY_SIZE(stb0899_dvbs2rf_tab) - 1, val);
933                         *strength += 750;
934                         dprintk(verbose, FE_DEBUG, 1, "IF_AGC_GAIN = 0x%04x, C = %d * 0.1 dBm",
935                                 val & 0x3fff, *strength);
936                 }
937                 break;
938         default:
939                 dprintk(verbose, FE_DEBUG, 1, "Unsupported delivery system");
940                 break;
941         }
942
943         return 0;
944 }
945
946 static int stb0899_read_snr(struct dvb_frontend *fe, u16 *snr)
947 {
948         struct stb0899_state *state             = fe->demodulator_priv;
949         struct stb0899_internal *internal       = &state->internal;
950
951         unsigned int val, quant, quantn = -1, est, estn = -1;
952         u8 buf[2];
953         u32 reg;
954
955         reg  = stb0899_read_reg(state, STB0899_VSTATUS);
956         switch (state->delsys) {
957         case DVBFE_DELSYS_DVBS:
958         case DVBFE_DELSYS_DSS:
959                 if (internal->lock) {
960                         if (STB0899_GETFIELD(VSTATUS_LOCKEDVIT, reg)) {
961
962                                 stb0899_read_regs(state, STB0899_NIRM, buf, 2);
963                                 val = MAKEWORD16(buf[0], buf[1]);
964
965                                 *snr = stb0899_table_lookup(stb0899_cn_tab, ARRAY_SIZE(stb0899_cn_tab) - 1, val);
966                                 dprintk(verbose, FE_DEBUG, 1, "NIR = 0x%02x%02x = %u, C/N = %d * 0.1 dBm\n",
967                                         buf[0], buf[1], val, *snr);
968                         }
969                 }
970                 break;
971         case DVBFE_DELSYS_DVBS2:
972                 if (internal->lock) {
973                         reg = STB0899_READ_S2REG(STB0899_S2DEMOD, UWP_CNTRL1);
974                         quant = STB0899_GETFIELD(UWP_ESN0_QUANT, reg);
975                         reg = STB0899_READ_S2REG(STB0899_S2DEMOD, UWP_STAT2);
976                         est = STB0899_GETFIELD(ESN0_EST, reg);
977                         if (est == 1)
978                                 val = 301; /* C/N = 30.1 dB */
979                         else if (est == 2)
980                                 val = 270; /* C/N = 27.0 dB */
981                         else {
982                                 /* quantn = 100 * log(quant^2) */
983                                 quantn = stb0899_table_lookup(stb0899_quant_tab, ARRAY_SIZE(stb0899_quant_tab) - 1, quant * 100);
984                                 /* estn = 100 * log(est) */
985                                 estn = stb0899_table_lookup(stb0899_est_tab, ARRAY_SIZE(stb0899_est_tab) - 1, est);
986                                 /* snr(dBm/10) = -10*(log(est)-log(quant^2)) => snr(dBm/10) = (100*log(quant^2)-100*log(est))/10 */
987                                 val = (quantn - estn) / 10;
988                         }
989                         *snr = val;
990                         dprintk(verbose, FE_DEBUG, 1, "Es/N0 quant = %d (%d) estimate = %u (%d), C/N = %d * 0.1 dBm",
991                                 quant, quantn, est, estn, val);
992                 }
993                 break;
994         default:
995                 dprintk(verbose, FE_DEBUG, 1, "Unsupported delivery system");
996                 break;
997         }
998
999         return 0;
1000 }
1001
1002 static int stb0899_read_status(struct dvb_frontend *fe, enum fe_status *status)
1003 {
1004         struct stb0899_state *state             = fe->demodulator_priv;
1005         struct stb0899_internal *internal       = &state->internal;
1006         u8 reg;
1007         *status = 0;
1008
1009         switch (state->delsys) {
1010         case DVBFE_DELSYS_DVBS:
1011         case DVBFE_DELSYS_DSS:
1012                 dprintk(state->verbose, FE_DEBUG, 1, "Delivery system DVB-S/DSS");
1013                 if (internal->lock) {
1014                         reg  = stb0899_read_reg(state, STB0899_VSTATUS);
1015                         if (STB0899_GETFIELD(VSTATUS_LOCKEDVIT, reg)) {
1016                                 dprintk(state->verbose, FE_DEBUG, 1, "--------> FE_HAS_CARRIER | FE_HAS_LOCK");
1017                                 *status |= FE_HAS_CARRIER | FE_HAS_LOCK;
1018
1019                                 reg = stb0899_read_reg(state, STB0899_PLPARM);
1020                                 if (STB0899_GETFIELD(VITCURPUN, reg)) {
1021                                         dprintk(state->verbose, FE_DEBUG, 1, "--------> FE_HAS_VITERBI | FE_HAS_SYNC");
1022                                         *status |= FE_HAS_VITERBI | FE_HAS_SYNC;
1023                                 }
1024                         }
1025                 }
1026                 break;
1027         case DVBFE_DELSYS_DVBS2:
1028                 dprintk(state->verbose, FE_DEBUG, 1, "Delivery system DVB-S2");
1029                 if (internal->lock) {
1030                         reg = STB0899_READ_S2REG(STB0899_S2DEMOD, DMD_STAT2);
1031                         if (STB0899_GETFIELD(UWP_LOCK, reg) && STB0899_GETFIELD(CSM_LOCK, reg)) {
1032                                 *status |= FE_HAS_CARRIER;
1033                                 dprintk(state->verbose, FE_DEBUG, 1,
1034                                         "UWP & CSM Lock ! ---> DVB-S2 FE_HAS_CARRIER");
1035
1036                                 reg = stb0899_read_reg(state, STB0899_CFGPDELSTATUS1);
1037                                 if (STB0899_GETFIELD(CFGPDELSTATUS_LOCK, reg)) {
1038                                         *status |= FE_HAS_LOCK;
1039                                         dprintk(state->verbose, FE_DEBUG, 1,
1040                                                 "Packet Delineator Locked ! -----> DVB-S2 FE_HAS_LOCK");
1041
1042                                 }
1043                                 if (STB0899_GETFIELD(CONTINUOUS_STREAM, reg)) {
1044                                         *status |= FE_HAS_VITERBI;
1045                                         dprintk(state->verbose, FE_DEBUG, 1,
1046                                                 "Packet Delineator found VITERBI ! -----> DVB-S2 FE_HAS_VITERBI");
1047                                 }
1048                                 if (STB0899_GETFIELD(ACCEPTED_STREAM, reg)) {
1049                                         *status |= FE_HAS_SYNC;
1050                                         dprintk(state->verbose, FE_DEBUG, 1,
1051                                                 "Packet Delineator found SYNC ! -----> DVB-S2 FE_HAS_SYNC");
1052                                 }
1053                         }
1054                 }
1055                 break;
1056         default:
1057                 dprintk(verbose, FE_DEBUG, 1, "Unsupported delivery system");
1058                 break;
1059         }
1060         return 0;
1061 }
1062
1063 /*
1064  * stb0899_get_error
1065  * viterbi error for DVB-S/DSS
1066  * packet error for DVB-S2
1067  * Bit Error Rate or Packet Error Rate * 10 ^ 7
1068  */
1069 static int stb0899_read_ber(struct dvb_frontend *fe, u32 *ber)
1070 {
1071         struct stb0899_state *state             = fe->demodulator_priv;
1072         struct stb0899_internal *internal       = &state->internal;
1073
1074         u8  lsb, msb;
1075         u32 i;
1076
1077         *ber = 0;
1078
1079         switch (state->delsys) {
1080         case DVBFE_DELSYS_DVBS:
1081         case DVBFE_DELSYS_DSS:
1082                 if (internal->lock) {
1083                         /* average 5 BER values */
1084                         for (i = 0; i < 5; i++) {
1085                                 msleep(100);
1086                                 lsb = stb0899_read_reg(state, STB0899_ECNT1L);
1087                                 msb = stb0899_read_reg(state, STB0899_ECNT1M);
1088                                 *ber += MAKEWORD16(msb, lsb);
1089                         }
1090                         *ber /= 5;
1091                         /* Viterbi Check        */
1092                         if (STB0899_GETFIELD(VSTATUS_PRFVIT, internal->v_status)) {
1093                                 /* Error Rate           */
1094                                 *ber *= 9766;
1095                                 /* ber = ber * 10 ^ 7   */
1096                                 *ber /= (-1 + (1 << (2 * STB0899_GETFIELD(NOE, internal->err_ctrl))));
1097                                 *ber /= 8;
1098                         }
1099                 }
1100                 break;
1101         case DVBFE_DELSYS_DVBS2:
1102                 if (internal->lock) {
1103                         /* Average 5 PER values */
1104                         for (i = 0; i < 5; i++) {
1105                                 msleep(100);
1106                                 lsb = stb0899_read_reg(state, STB0899_ECNT1L);
1107                                 msb = stb0899_read_reg(state, STB0899_ECNT1M);
1108                                 *ber += MAKEWORD16(msb, lsb);
1109                         }
1110                         /* ber = ber * 10 ^ 7   */
1111                         *ber *= 10000000;
1112                         *ber /= (-1 + (1 << (4 + 2 * STB0899_GETFIELD(NOE, internal->err_ctrl))));
1113                 }
1114                 break;
1115         default:
1116                 dprintk(verbose, FE_DEBUG, 1, "Unsupported delivery system");
1117         }
1118
1119         return 0;
1120 }
1121
1122 static int stb0899_set_voltage(struct dvb_frontend *fe, fe_sec_voltage_t voltage)
1123 {
1124         struct stb0899_state *state = fe->demodulator_priv;
1125
1126         switch (voltage) {
1127         case SEC_VOLTAGE_13:
1128                 stb0899_write_reg(state, STB0899_GPIO00CFG, 0x82);
1129                 stb0899_write_reg(state, STB0899_GPIO01CFG, 0x02);
1130                 stb0899_write_reg(state, STB0899_GPIO02CFG, 0x00);
1131                 break;
1132         case SEC_VOLTAGE_18:
1133                 stb0899_write_reg(state, STB0899_GPIO00CFG, 0x02);
1134                 stb0899_write_reg(state, STB0899_GPIO01CFG, 0x02);
1135                 stb0899_write_reg(state, STB0899_GPIO02CFG, 0x82);
1136                 break;
1137         case SEC_VOLTAGE_OFF:
1138                 stb0899_write_reg(state, STB0899_GPIO00CFG, 0x82);
1139                 stb0899_write_reg(state, STB0899_GPIO01CFG, 0x82);
1140                 stb0899_write_reg(state, STB0899_GPIO02CFG, 0x82);
1141                 break;
1142         default:
1143                 return -EINVAL;
1144         }
1145
1146         return 0;
1147 }
1148
1149 static int stb0899_set_tone(struct dvb_frontend *fe, fe_sec_tone_mode_t tone)
1150 {
1151         struct stb0899_state *state = fe->demodulator_priv;
1152         struct stb0899_internal *internal = &state->internal;
1153
1154         u8 div;
1155
1156         /* wait for diseqc idle */
1157         if (stb0899_wait_diseqc_txidle(state, 100) < 0)
1158                 return -ETIMEDOUT;
1159
1160         switch (tone) {
1161         case SEC_TONE_ON:
1162                 div = (internal->master_clk / 100) / 5632;
1163                 div = (div + 5) / 10;
1164                 stb0899_write_reg(state, STB0899_DISEQCOCFG, 0x66);
1165                 stb0899_write_reg(state, STB0899_ACRPRESC, 0x31);
1166                 stb0899_write_reg(state, STB0899_ACRDIV1, div);
1167                 break;
1168         case SEC_TONE_OFF:
1169                 stb0899_write_reg(state, STB0899_DISEQCOCFG, 0x20);
1170                 break;
1171         default:
1172                 break;
1173         }
1174         return 0;
1175 }
1176
1177 static int stb0899_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
1178 {
1179         int i2c_stat;
1180         struct stb0899_state *state = fe->demodulator_priv;
1181
1182         i2c_stat = stb0899_read_reg(state, STB0899_I2CRPT);
1183         if (i2c_stat < 0)
1184                 goto err;
1185
1186         if (enable) {
1187                 dprintk(state->verbose, FE_DEBUG, 1, "Enabling I2C Repeater ...");
1188                 i2c_stat |=  STB0899_I2CTON;
1189                 if (stb0899_write_reg(state, STB0899_I2CRPT, i2c_stat) < 0)
1190                         goto err;
1191         }
1192         return 0;
1193 err:
1194         dprintk(state->verbose, FE_ERROR, 1, "I2C Repeater enable failed");
1195         return -EREMOTEIO;
1196 }
1197
1198
1199 static inline void CONVERT32(u32 x, char *str)
1200 {
1201         *str++  = (x >> 24) & 0xff;
1202         *str++  = (x >> 16) & 0xff;
1203         *str++  = (x >>  8) & 0xff;
1204         *str++  = (x >>  0) & 0xff;
1205         *str    = '\0';
1206 }
1207
1208 int stb0899_get_dev_id(struct stb0899_state *state)
1209 {
1210         u8 chip_id, release;
1211         u16 id;
1212         u32 demod_ver = 0, fec_ver = 0;
1213         char demod_str[4] = { 0 };
1214         char fec_str[4] = { 0 };
1215
1216         id = stb0899_read_reg(state, STB0899_DEV_ID);
1217         dprintk(state->verbose, FE_DEBUG, 1, "ID reg=[0x%02x]", id);
1218         chip_id = STB0899_GETFIELD(CHIP_ID, id);
1219         release = STB0899_GETFIELD(CHIP_REL, id);
1220
1221         dprintk(state->verbose, FE_ERROR, 1, "Device ID=[%d], Release=[%d]",
1222                 chip_id, release);
1223
1224         CONVERT32(STB0899_READ_S2REG(STB0899_S2DEMOD, DMD_CORE_ID), (char *)&demod_str);
1225
1226         demod_ver = STB0899_READ_S2REG(STB0899_S2DEMOD, DMD_VERSION_ID);
1227         dprintk(state->verbose, FE_ERROR, 1, "Demodulator Core ID=[%s], Version=[%d]", (char *) &demod_str, demod_ver);
1228         CONVERT32(STB0899_READ_S2REG(STB0899_S2FEC, FEC_CORE_ID_REG), (char *)&fec_str);
1229         fec_ver = STB0899_READ_S2REG(STB0899_S2FEC, FEC_VER_ID_REG);
1230         if (! (chip_id > 0)) {
1231                 dprintk(state->verbose, FE_ERROR, 1, "couldn't find a STB 0899");
1232
1233                 return -ENODEV;
1234         }
1235         dprintk(state->verbose, FE_ERROR, 1, "FEC Core ID=[%s], Version=[%d]", (char*) &fec_str, fec_ver);
1236
1237         return 0;
1238 }
1239
1240 static const struct dvbfe_info dvbs_info        = {
1241         .name                                   = "STB0899 DVB-S",
1242         .delivery                               = DVBFE_DELSYS_DVBS,
1243         .delsys                                 = {
1244                 .dvbs.modulation                = DVBFE_MOD_QPSK,
1245                 .dvbs.fec                       = DVBFE_FEC_1_2 | DVBFE_FEC_2_3 |
1246                                                   DVBFE_FEC_3_4 | DVBFE_FEC_5_6 |
1247                                                   DVBFE_FEC_6_7
1248         },
1249
1250         .frequency_min                          = 950000,
1251         .frequency_max                          = 2150000,
1252         .frequency_step                         = 0,
1253         .symbol_rate_min                        = 1000000,
1254         .symbol_rate_max                        = 45000000,
1255         .symbol_rate_tolerance                  = 0
1256 };
1257
1258 static const struct dvbfe_info dss_info         = {
1259         .name                                   = "STB0899 DSS",
1260         .delivery                               = DVBFE_DELSYS_DSS,
1261         .delsys                                 = {
1262                 .dss.modulation                 = DVBFE_MOD_BPSK | DVBFE_MOD_QPSK,
1263                 .dss.fec                        = DVBFE_FEC_1_2  | DVBFE_FEC_2_3 |
1264                                                   DVBFE_FEC_3_4  | DVBFE_FEC_5_6 |
1265                                                   DVBFE_FEC_6_7
1266         },
1267
1268         .frequency_min                          = 950000,
1269         .frequency_max                          = 2150000,
1270         .frequency_step                         = 0,
1271         .symbol_rate_min                        = 1000000,
1272         .symbol_rate_max                        = 45000000,
1273         .symbol_rate_tolerance                  = 0
1274 };
1275
1276 static const struct dvbfe_info dvbs2_info       = {
1277         .name                                   = "STB0899 DVB-S2",
1278         .delivery                               = DVBFE_DELSYS_DVBS2,
1279         .delsys                                 = {
1280                 .dvbs2.modulation               = DVBFE_MOD_QPSK   | DVBFE_MOD_8PSK |
1281                                                   DVBFE_MOD_16APSK | DVBFE_MOD_32APSK,
1282
1283                 .dvbs2.fec                      = DVBFE_FEC_1_4 | DVBFE_FEC_1_3 |
1284                                                   DVBFE_FEC_2_5 | DVBFE_FEC_1_2 |
1285                                                   DVBFE_FEC_3_5 | DVBFE_FEC_2_3 |
1286                                                   DVBFE_FEC_3_4 | DVBFE_FEC_4_5 |
1287                                                   DVBFE_FEC_5_6 | DVBFE_FEC_8_9 |
1288                                                   DVBFE_FEC_9_10,
1289         },
1290
1291         .frequency_min          = 950000,
1292         .frequency_max          = 2150000,
1293         .frequency_step         = 0,
1294         .symbol_rate_min        = 1000000,
1295         .symbol_rate_max        = 45000000,
1296         .symbol_rate_tolerance  = 0
1297 };
1298
1299 static int stb0899_get_info(struct dvb_frontend *fe, struct dvbfe_info *fe_info)
1300 {
1301         struct stb0899_state *state = fe->demodulator_priv;
1302
1303         dprintk(verbose, FE_DEBUG, 1, "Get Info");
1304
1305         state->delsys = fe_info->delivery;
1306         switch (state->delsys) {
1307         case DVBFE_DELSYS_DVBS:
1308                 dprintk(verbose, FE_ERROR, 1, "Querying DVB-S info");
1309                 memcpy(fe_info, &dvbs_info, sizeof (struct dvbfe_info));
1310                 break;
1311         case DVBFE_DELSYS_DSS:
1312                 dprintk(verbose, FE_ERROR, 1, "Querying DSS info");
1313                 memcpy(fe_info, &dss_info, sizeof (struct dvbfe_info));
1314                 break;
1315         case DVBFE_DELSYS_DVBS2:
1316                 dprintk(verbose, FE_ERROR, 1, "Querying DVB-S2 info");
1317                 memcpy(fe_info, &dvbs2_info, sizeof (struct dvbfe_info));
1318                 break;
1319         default:
1320                 dprintk(verbose, FE_ERROR, 1, "Unsupported delivery system");
1321                 return -EINVAL;
1322         }
1323         dprintk(verbose, FE_DEBUG, 1, "delivery system=%d", state->delsys);
1324
1325         return 0;
1326 }
1327
1328 static int stb0899_get_delsys(struct dvb_frontend *fe, enum dvbfe_delsys *fe_delsys)
1329 {
1330         *fe_delsys = DVBFE_DELSYS_DVBS | DVBFE_DELSYS_DSS | DVBFE_DELSYS_DVBS2;
1331
1332         return 0;
1333 }
1334
1335 void stb0899_set_delsys(struct stb0899_state *state)
1336 {
1337         u8 reg;
1338         u8 stop_clk[2];
1339
1340         stop_clk[0] = stb0899_read_reg(state, STB0899_STOPCLK1);
1341         stop_clk[1] = stb0899_read_reg(state, STB0899_STOPCLK2);
1342
1343         switch (state->delsys) {
1344         case DVBFE_DELSYS_DVBS:
1345                 dprintk(verbose, FE_DEBUG, 1, "Delivery System -- DVB-S");
1346                 /* FECM/Viterbi ON      */
1347                 reg = stb0899_read_reg(state, STB0899_FECM);
1348                 STB0899_SETFIELD_VAL(FECM_RSVD0, reg, 0);
1349                 STB0899_SETFIELD_VAL(FECM_VITERBI_ON, reg, 1);
1350                 stb0899_write_reg(state, STB0899_FECM, reg);
1351
1352                 stb0899_write_reg(state, STB0899_RSULC, 0xb1);
1353                 stb0899_write_reg(state, STB0899_TSULC, 0x40);
1354                 stb0899_write_reg(state, STB0899_RSLLC, 0x42);
1355                 stb0899_write_reg(state, STB0899_TSLPL, 0x12);
1356
1357                 reg = stb0899_read_reg(state, STB0899_TSTRES);
1358                 STB0899_SETFIELD_VAL(FRESLDPC, reg, 1);
1359                 stb0899_write_reg(state, STB0899_TSTRES, reg);
1360
1361                 STB0899_SETFIELD_VAL(STOP_CHK8PSK, stop_clk[0], 1);
1362                 STB0899_SETFIELD_VAL(STOP_CKFEC108, stop_clk[0], 1);
1363                 STB0899_SETFIELD_VAL(STOP_CKFEC216, stop_clk[0], 1);
1364
1365                 STB0899_SETFIELD_VAL(STOP_CKPKDLIN108, stop_clk[1], 1);
1366                 STB0899_SETFIELD_VAL(STOP_CKPKDLIN216, stop_clk[1], 1);
1367
1368                 STB0899_SETFIELD_VAL(STOP_CKINTBUF216, stop_clk[0], 1);
1369                 STB0899_SETFIELD_VAL(STOP_CKCORE216, stop_clk[0], 1);
1370
1371                 STB0899_SETFIELD_VAL(STOP_CKS2DMD108, stop_clk[1], 1);
1372                 break;
1373         case DVBFE_DELSYS_DVBS2:
1374                 /* FECM/Viterbi OFF     */
1375                 reg = stb0899_read_reg(state, STB0899_FECM);
1376                 STB0899_SETFIELD_VAL(FECM_RSVD0, reg, 0);
1377                 STB0899_SETFIELD_VAL(FECM_VITERBI_ON, reg, 0);
1378                 stb0899_write_reg(state, STB0899_FECM, reg);
1379
1380                 stb0899_write_reg(state, STB0899_RSULC, 0xb1);
1381                 stb0899_write_reg(state, STB0899_TSULC, 0x42);
1382                 stb0899_write_reg(state, STB0899_RSLLC, 0x40);
1383                 stb0899_write_reg(state, STB0899_TSLPL, 0x02);
1384
1385                 reg = stb0899_read_reg(state, STB0899_TSTRES);
1386                 STB0899_SETFIELD_VAL(FRESLDPC, reg, 0);
1387                 stb0899_write_reg(state, STB0899_TSTRES, reg);
1388
1389                 STB0899_SETFIELD_VAL(STOP_CHK8PSK, stop_clk[0], 1);
1390                 STB0899_SETFIELD_VAL(STOP_CKFEC108, stop_clk[0], 0);
1391                 STB0899_SETFIELD_VAL(STOP_CKFEC216, stop_clk[0], 0);
1392
1393                 STB0899_SETFIELD_VAL(STOP_CKPKDLIN108, stop_clk[1], 0);
1394                 STB0899_SETFIELD_VAL(STOP_CKPKDLIN216, stop_clk[1], 0);
1395
1396                 STB0899_SETFIELD_VAL(STOP_CKINTBUF216, stop_clk[0], 0);
1397                 STB0899_SETFIELD_VAL(STOP_CKCORE216, stop_clk[0], 0);
1398
1399                 STB0899_SETFIELD_VAL(STOP_CKS2DMD108, stop_clk[1], 0);
1400                 break;
1401         case DVBFE_DELSYS_DSS:
1402                 /* FECM/Viterbi ON      */
1403                 reg = stb0899_read_reg(state, STB0899_FECM);
1404                 STB0899_SETFIELD_VAL(FECM_RSVD0, reg, 1);
1405                 STB0899_SETFIELD_VAL(FECM_VITERBI_ON, reg, 1);
1406                 stb0899_write_reg(state, STB0899_FECM, reg);
1407
1408                 stb0899_write_reg(state, STB0899_RSULC, 0xa1);
1409                 stb0899_write_reg(state, STB0899_TSULC, 0x61);
1410                 stb0899_write_reg(state, STB0899_RSLLC, 0x42);
1411
1412                 reg = stb0899_read_reg(state, STB0899_TSTRES);
1413                 STB0899_SETFIELD_VAL(FRESLDPC, reg, 1);
1414                 stb0899_write_reg(state, STB0899_TSTRES, reg);
1415
1416                 STB0899_SETFIELD_VAL(STOP_CHK8PSK, stop_clk[0], 1);
1417                 STB0899_SETFIELD_VAL(STOP_CKFEC108, stop_clk[0], 1);
1418                 STB0899_SETFIELD_VAL(STOP_CKFEC216, stop_clk[0], 1);
1419
1420                 STB0899_SETFIELD_VAL(STOP_CKPKDLIN108, stop_clk[1], 1);
1421                 STB0899_SETFIELD_VAL(STOP_CKPKDLIN216, stop_clk[1], 1);
1422
1423                 STB0899_SETFIELD_VAL(STOP_CKCORE216, stop_clk[0], 0);
1424
1425                 STB0899_SETFIELD_VAL(STOP_CKS2DMD108, stop_clk[1], 1);
1426                 break;
1427         default:
1428                 dprintk(verbose, FE_ERROR, 1, "Unsupported delivery system");
1429                 break;
1430         }
1431         STB0899_SETFIELD_VAL(STOP_CKADCI108, stop_clk[0], 0);
1432         stb0899_write_regs(state, STB0899_STOPCLK1, stop_clk, 2);
1433 }
1434
1435 /*
1436  * stb0899_set_iterations
1437  * set the LDPC iteration scale function
1438  */
1439 static void stb0899_set_iterations(struct stb0899_state *state)
1440 {
1441         struct stb0899_internal *internal = &state->internal;
1442         struct stb0899_config *config = state->config;
1443
1444         s32 iter_scale;
1445         u32 reg;
1446
1447         iter_scale = 17 * (internal->master_clk / 1000);
1448         iter_scale += 410000;
1449         iter_scale /= (internal->srate / 1000000);
1450         iter_scale /= 1000;
1451
1452         if (iter_scale > config->ldpc_max_iter)
1453                 iter_scale = config->ldpc_max_iter;
1454
1455         reg = STB0899_READ_S2REG(STB0899_S2DEMOD, MAX_ITER);
1456         STB0899_SETFIELD_VAL(MAX_ITERATIONS, reg, iter_scale);
1457         stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_MAX_ITER, STB0899_OFF0_MAX_ITER, reg);
1458 }
1459
1460 static enum dvbfe_search stb0899_search(struct dvb_frontend *fe, struct dvbfe_params *params)
1461 {
1462         struct stb0899_state *state = fe->demodulator_priv;
1463         struct stb0899_params *i_params = &state->params;
1464         struct stb0899_internal *internal = &state->internal;
1465
1466         u32 SearchRange, gain;
1467
1468         switch (state->delsys) {
1469         case DVBFE_DELSYS_DVBS:
1470                 dprintk(verbose, FE_ERROR, 1, "set DVB-S params");
1471                 i_params->freq  = params->frequency;
1472                 i_params->srate = params->delsys.dvbs.symbol_rate;
1473                 break;
1474         case DVBFE_DELSYS_DSS:
1475                 dprintk(verbose, FE_ERROR, 1, "set DSS params");
1476                 i_params->freq  = params->frequency;
1477                 i_params->srate = params->delsys.dss.symbol_rate;
1478                 break;
1479         case DVBFE_DELSYS_DVBS2:
1480                 dprintk(verbose, FE_ERROR, 1, "set DVB-S2 params");
1481                 i_params->freq  = params->frequency;
1482                 i_params->srate = params->delsys.dvbs2.symbol_rate;
1483                 break;
1484         default:
1485                 dprintk(verbose, FE_ERROR, 1, "Unsupported delivery system");
1486                 return -EINVAL;
1487         }
1488         dprintk(verbose, FE_DEBUG, 1, "delivery system=%d", state->delsys);
1489
1490 //      SearchRange = 3000000; /* Search Bandwidth (3 Mhz, was initially 10  Mhz)       */
1491         SearchRange = 10000000; /* Search Bandwidth (3 Mhz, was initially 10  Mhz)      */
1492         dprintk(verbose, FE_DEBUG, 1, "Frequency=%d, Srate=%d", i_params->freq, i_params->srate);
1493         /* checking Search Range is meaningless for a fixed 3 Mhz                       */
1494         if (INRANGE(i_params->srate, 1000000, 45000000)) {
1495                 dprintk(verbose, FE_DEBUG, 1, "Parameters IN RANGE");
1496                 stb0899_set_delsys(state);
1497
1498                 if (state->config->tuner_set_rfsiggain) {
1499                         if (internal->srate > 15000000)
1500                                 gain =  8;      /* 15Mb < srate < 45Mb, gain = 8dB      */
1501                         else if (internal->srate > 5000000)
1502                                 gain = 12;      /*  5Mb < srate < 15Mb, gain = 12dB     */
1503                         else
1504                                 gain = 14;      /*  1Mb < srate <  5Mb, gain = 14db     */
1505                         state->config->tuner_set_rfsiggain(fe, gain);
1506                 }
1507
1508                 if (i_params->srate <= 5000000)
1509                         stb0899_set_mclk(state, 76500000);
1510                 else
1511                         stb0899_set_mclk(state, 90000000);
1512
1513                 switch (state->delsys) {
1514                 case DVBFE_DELSYS_DVBS:
1515                 case DVBFE_DELSYS_DSS:
1516                         dprintk(verbose, FE_DEBUG, 1, "DVB-S delivery system");
1517                         internal->freq  = i_params->freq;
1518                         internal->srate = i_params->srate;
1519                         /*
1520                          * search = user search range +
1521                          *          500Khz +
1522                          *          2 * Tuner_step_size +
1523                          *          10% of the symbol rate
1524                          */
1525                         internal->srch_range    = SearchRange + 1500000 + (i_params->srate / 5);
1526                         internal->derot_percent = 30;
1527
1528                         /* What to do for tuners having no bandwidth setup ?    */
1529                         if (state->config->tuner_set_bandwidth)
1530                                 state->config->tuner_set_bandwidth(fe, (13 * (stb0899_carr_width(state) + 10000000)) / 10);
1531                         if (state->config->tuner_get_bandwidth)
1532                                 state->config->tuner_get_bandwidth(fe, &internal->tuner_bw);
1533                         /* Set DVB-S1 AGC               */
1534                         stb0899_write_reg(state, STB0899_AGCRFCFG, 0x11);
1535
1536                         /* Run the search algorithm     */
1537                         dprintk(verbose, FE_DEBUG, 1, "running DVB-S search algo ..");
1538                         if (stb0899_dvbs_algo(state)    == RANGEOK) {
1539                                 internal->lock          = 1;
1540                                 dprintk(verbose, FE_DEBUG, 1,
1541                                         "-------------------------------------> DVB-S LOCK !");
1542
1543 //                              stb0899_write_reg(state, STB0899_ERRCTRL1, 0x3d); /* Viterbi Errors     */
1544 //                              internal->v_status = stb0899_read_reg(state, STB0899_VSTATUS);
1545 //                              internal->err_ctrl = stb0899_read_reg(state, STB0899_ERRCTRL1);
1546 //                              dprintk(verbose, FE_DEBUG, 1, "VSTATUS=0x%02x", internal->v_status);
1547 //                              dprintk(verbose, FE_DEBUG, 1, "ERR_CTRL=0x%02x", internal->err_ctrl);
1548
1549                                 return DVBFE_ALGO_SEARCH_SUCCESS;
1550                         } else {
1551                                 internal->lock          = 0;
1552
1553                                 return DVBFE_ALGO_SEARCH_FAILED;
1554                         }
1555                         break;
1556                 case DVBFE_DELSYS_DVBS2:
1557                         internal->freq                  = i_params->freq;
1558                         internal->srate                 = i_params->srate;
1559                         internal->srch_range            = SearchRange;
1560
1561                         if (state->config->tuner_set_bandwidth)
1562                                 state->config->tuner_set_bandwidth(fe, (stb0899_carr_width(state) + 10000000));
1563                         if (state->config->tuner_get_bandwidth)
1564                                 state->config->tuner_get_bandwidth(fe, &internal->tuner_bw);
1565
1566 //                      pParams->SpectralInv            = pSearch->IQ_Inversion;
1567
1568                         /* Set DVB-S2 AGC               */
1569                         stb0899_write_reg(state, STB0899_AGCRFCFG, 0x1c);
1570
1571                         /* Set IterScale =f(MCLK,SYMB)  */
1572                         stb0899_set_iterations(state);
1573
1574                         /* Run the search algorithm     */
1575                         dprintk(verbose, FE_DEBUG, 1, "running DVB-S2 search algo ..");
1576                         if (stb0899_dvbs2_algo(state)   == DVBS2_FEC_LOCK) {
1577                                 internal->lock          = 1;
1578                                 dprintk(verbose, FE_DEBUG, 1,
1579                                         "-------------------------------------> DVB-S2 LOCK !");
1580
1581 //                              stb0899_write_reg(state, STB0899_ERRCTRL1, 0xb6); /* Packet Errors      */
1582 //                              internal->v_status = stb0899_read_reg(state, STB0899_VSTATUS);
1583 //                              internal->err_ctrl = stb0899_read_reg(state, STB0899_ERRCTRL1);
1584
1585                                 return DVBFE_ALGO_SEARCH_SUCCESS;
1586                         } else {
1587                                 internal->lock          = 0;
1588
1589                                 return DVBFE_ALGO_SEARCH_FAILED;
1590                         }
1591                         break;
1592                 default:
1593                         dprintk(verbose, FE_ERROR, 1, "Unsupported delivery system");
1594                         return DVBFE_ALGO_SEARCH_INVALID;
1595                 }
1596         }
1597
1598         return DVBFE_ALGO_SEARCH_ERROR;
1599 }
1600
1601 static enum stb0899_status stb0899_track_carrier(struct stb0899_state *state)
1602 {
1603         u8 reg;
1604
1605         reg = stb0899_read_reg(state, STB0899_DSTATUS);
1606         dprintk(verbose, FE_DEBUG, 1, "--------------------> STB0899_DSTATUS=[0x%02x]", reg);
1607         if (STB0899_GETFIELD(CARRIER_FOUND, reg)) {
1608                 dprintk(verbose, FE_DEBUG, 1, "-------------> CARRIEROK !");
1609                 return CARRIEROK;
1610         } else {
1611                 dprintk(verbose, FE_DEBUG, 1, "-------------> NOCARRIER !");
1612                 return NOCARRIER;
1613         }
1614
1615         return NOCARRIER;
1616 }
1617
1618 static enum stb0899_status stb0899_get_ifagc(struct stb0899_state *state)
1619 {
1620         u8 reg;
1621
1622         reg = STB0899_READ_S2REG(STB0899_S2DEMOD, DMD_STATUS);
1623         dprintk(verbose, FE_DEBUG, 1, "DMD_STATUS=[0x%02x]", reg);
1624         if (STB0899_GETFIELD(IF_AGC_LOCK, reg)) {
1625                 dprintk(verbose, FE_DEBUG, 1, "------------->IF AGC LOCKED !");
1626                 return AGC1OK;
1627         } else {
1628                 dprintk(verbose, FE_DEBUG, 1, "------------->IF AGC LOCK LOST !");
1629                 return NOAGC1;
1630         }
1631
1632         return NOAGC1;
1633 }
1634
1635 static int stb0899_get_s1fec(struct stb0899_internal *internal, enum dvbfe_fec *fec)
1636 {
1637         switch (internal->fecrate) {
1638         case STB0899_FEC_1_2:
1639                 *fec = DVBFE_FEC_1_2;
1640                 break;
1641         case STB0899_FEC_2_3:
1642                 *fec = DVBFE_FEC_2_3;
1643                 break;
1644         case STB0899_FEC_3_4:
1645                 *fec = DVBFE_FEC_3_4;
1646                 break;
1647         case STB0899_FEC_5_6:
1648                 *fec = DVBFE_FEC_5_6;
1649                 break;
1650         case STB0899_FEC_6_7:
1651                 *fec = DVBFE_FEC_6_7;
1652                 break;
1653         case STB0899_FEC_7_8:
1654                 *fec = DVBFE_FEC_7_8;
1655                 break;
1656         default:
1657                 return -EINVAL;
1658         }
1659
1660         return 0;
1661 }
1662
1663 static int stb0899_get_modcod(struct stb0899_internal *internal, struct dvbs2_params *params)
1664 {
1665         switch (internal->modcod) {
1666         case STB0899_DUMMY_PLF:
1667                 params->modulation      = DVBFE_MOD_NONE;
1668                 params->fec             = DVBFE_FEC_NONE;
1669                 break;
1670         case STB0899_QPSK_14:
1671                 params->modulation      = DVBFE_MOD_QPSK;
1672                 params->fec             = DVBFE_FEC_1_4;
1673                 break;
1674         case STB0899_QPSK_13:
1675                 params->modulation      = DVBFE_MOD_QPSK;
1676                 params->fec             = DVBFE_FEC_1_3;
1677                 break;
1678         case STB0899_QPSK_25:
1679                 params->modulation      = DVBFE_MOD_QPSK;
1680                 params->fec             = DVBFE_FEC_2_5;
1681                 break;
1682         case STB0899_QPSK_12:
1683                 params->modulation      = DVBFE_MOD_QPSK;
1684                 params->fec             = DVBFE_FEC_1_2;
1685                 break;
1686         case STB0899_QPSK_35:
1687                 params->modulation      = DVBFE_MOD_QPSK;
1688                 params->fec             = DVBFE_FEC_3_5;
1689                 break;
1690         case STB0899_QPSK_23:
1691                 params->modulation      = DVBFE_MOD_QPSK;
1692                 params->fec             = DVBFE_FEC_2_3;
1693                 break;
1694         case STB0899_QPSK_34:
1695                 params->modulation      = DVBFE_MOD_QPSK;
1696                 params->fec             = DVBFE_FEC_3_4;
1697                 break;
1698         case STB0899_QPSK_45:
1699                 params->modulation      = DVBFE_MOD_QPSK;
1700                 params->fec             = DVBFE_FEC_4_5;
1701                 break;
1702         case STB0899_QPSK_56:
1703                 params->modulation      = DVBFE_MOD_QPSK;
1704                 params->fec             = DVBFE_FEC_5_6;
1705                 break;
1706         case STB0899_QPSK_89:
1707                 params->modulation      = DVBFE_MOD_QPSK;
1708                 params->fec             = DVBFE_FEC_8_9;
1709                 break;
1710         case STB0899_QPSK_910:
1711                 params->modulation      = DVBFE_MOD_QPSK;
1712                 params->fec             = DVBFE_FEC_9_10;
1713                 break;
1714         case STB0899_8PSK_35:
1715                 params->modulation      = DVBFE_MOD_8PSK;
1716                 params->fec             = DVBFE_FEC_3_5;
1717                 break;
1718         case STB0899_8PSK_23:
1719                 params->modulation      = DVBFE_MOD_8PSK;
1720                 params->fec             = DVBFE_FEC_2_3;
1721                 break;
1722         case STB0899_8PSK_34:
1723                 params->modulation      = DVBFE_MOD_8PSK;
1724                 params->fec             = DVBFE_FEC_3_4;
1725                 break;
1726         case STB0899_8PSK_56:
1727                 params->modulation      = DVBFE_MOD_8PSK;
1728                 params->fec             = DVBFE_FEC_5_6;
1729                 break;
1730         case STB0899_8PSK_89:
1731                 params->modulation      = DVBFE_MOD_8PSK;
1732                 params->fec             = DVBFE_FEC_8_9;
1733                 break;
1734         case STB0899_8PSK_910:
1735                 params->modulation      = DVBFE_MOD_8PSK;
1736                 params->fec             = DVBFE_FEC_9_10;
1737                 break;
1738         case STB0899_16APSK_23:
1739                 params->modulation      = DVBFE_MOD_16APSK;
1740                 params->fec             = DVBFE_FEC_2_3;
1741                 break;
1742         case STB0899_16APSK_34:
1743                 params->modulation      = DVBFE_MOD_16APSK;
1744                 params->fec             = DVBFE_FEC_3_4;
1745                 break;
1746         case STB0899_16APSK_45:
1747                 params->modulation      = DVBFE_MOD_16APSK;
1748                 params->fec             = DVBFE_FEC_4_5;
1749                 break;
1750         case STB0899_16APSK_56:
1751                 params->modulation      = DVBFE_MOD_16APSK;
1752                 params->fec             = DVBFE_FEC_5_6;
1753                 break;
1754         case STB0899_16APSK_89:
1755                 params->modulation      = DVBFE_MOD_16APSK;
1756                 params->fec             = DVBFE_FEC_8_9;
1757                 break;
1758         case STB0899_16APSK_910:
1759                 params->modulation      = DVBFE_MOD_16APSK;
1760                 params->fec             = DVBFE_FEC_9_10;
1761                 break;
1762         case STB0899_32APSK_34:
1763                 params->modulation      = DVBFE_MOD_32APSK;
1764                 params->fec             = DVBFE_FEC_3_4;
1765                 break;
1766         case STB0899_32APSK_45:
1767                 params->modulation      = DVBFE_MOD_32APSK;
1768                 params->fec             = DVBFE_FEC_4_5;
1769                 break;
1770         case STB0899_32APSK_56:
1771                 params->modulation      = DVBFE_MOD_32APSK;
1772                 params->fec             = DVBFE_FEC_5_6;
1773                 break;
1774         case STB0899_32APSK_89:
1775                 params->modulation      = DVBFE_MOD_32APSK;
1776                 params->fec             = DVBFE_FEC_8_9;
1777                 break;
1778         case STB0899_32APSK_910:
1779                 params->modulation      = DVBFE_MOD_32APSK;
1780                 params->fec             = DVBFE_FEC_9_10;
1781                 break;
1782         default:
1783                 return -EINVAL;
1784         }
1785
1786         return 0;
1787 }
1788
1789 /*
1790  * stb0899_track
1791  * periodically check the signal level against a specified
1792  * threshold level and perform derotator centering.
1793  * called once we have a lock from a succesful search
1794  * event.
1795  *
1796  * Will be called periodically called to maintain the
1797  * lock.
1798  *
1799  * Will be used to get parameters as well as info from
1800  * the decoded baseband header
1801  *
1802  * Once a new lock has established, the internal state
1803  * frequency (internal->freq) is updated
1804  */
1805 static int stb0899_track(struct dvb_frontend *fe, struct dvbfe_params *params, int *delay)
1806 {
1807         u32 lock_lost;
1808
1809         struct stb0899_state *state             = fe->demodulator_priv;
1810         struct stb0899_internal *internal       = &state->internal;
1811
1812         lock_lost = STB0899_READ_S2REG(STB0899_S2DEMOD, LOCK_LOST);
1813         dprintk(verbose, FE_DEBUG, 1, "Lock Lost=[0x%02x]\n", lock_lost);
1814         if (STB0899_GETFIELD(LOCK_LOST, lock_lost))
1815                 dprintk(verbose, FE_ERROR, 1, "Demodulator LOST LOCK !\n");
1816
1817         *delay = HZ/10;
1818
1819         return 0;
1820 }
1821
1822 static int stb0899_get_params(struct dvb_frontend *fe, struct dvbfe_params *params)
1823 {
1824         struct stb0899_state *state             = fe->demodulator_priv;
1825         struct stb0899_internal *internal       = &state->internal;
1826
1827         params->frequency                       = internal->freq;
1828         params->inversion                       = internal->inversion;
1829         params->delivery                        = state->delsys;
1830         switch (state->delsys) {
1831         case DVBFE_DELSYS_DVBS:
1832                 dprintk(verbose, FE_DEBUG, 1, "Get DVB-S params");
1833                 params->delsys.dvbs.symbol_rate         = internal->srate;
1834                 params->delsys.dvbs.modulation          = DVBFE_MOD_QPSK;
1835                 break;
1836         case DVBFE_DELSYS_DSS:
1837                 dprintk(verbose, FE_DEBUG, 1, "Get DSS params");
1838                 params->delsys.dss.symbol_rate          = internal->srate;
1839                 params->delsys.dss.modulation           = DVBFE_MOD_QPSK;
1840
1841                 break;
1842         case DVBFE_DELSYS_DVBS2:
1843                 dprintk(verbose, FE_DEBUG, 1, "Get DVB-S2 params");
1844                 params->delsys.dvbs2.symbol_rate        = internal->srate;
1845                 break;
1846         default:
1847                 dprintk(verbose, FE_ERROR, 1, "Unsupported delivery system");
1848                 return -EINVAL;
1849         }
1850
1851         return 0;
1852 }
1853
1854 static enum dvbfe_algo stb0899_frontend_algo(struct dvb_frontend *fe)
1855 {
1856         return DVBFE_ALGO_CUSTOM;
1857 }
1858
1859 static struct dvb_frontend_ops stb0899_ops = {
1860
1861         .info = {
1862                 .name                   = "STB0899 Multistandard",
1863         },
1864
1865         .release                        = stb0899_release,
1866         .init                           = stb0899_init,
1867         .sleep                          = stb0899_sleep,
1868 //      .wakeup                         = stb0899_wakeup,
1869
1870         .i2c_gate_ctrl                  = stb0899_i2c_gate_ctrl,
1871         .get_info                       = stb0899_get_info,
1872         .get_delsys                     = stb0899_get_delsys,
1873
1874         .get_frontend_algo              = stb0899_frontend_algo,
1875         .search                         = stb0899_search,
1876         .track                          = stb0899_track,
1877         .get_params                     = stb0899_get_params,
1878
1879         .read_status                    = stb0899_read_status,
1880         .read_snr                       = stb0899_read_snr,
1881         .read_signal_strength           = stb0899_read_signal_strength,
1882         .read_status                    = stb0899_read_status,
1883         .read_ber                       = stb0899_read_ber,
1884
1885         .set_voltage                    = stb0899_set_voltage,
1886         .set_tone                       = stb0899_set_tone,
1887
1888         .diseqc_send_master_cmd         = stb0899_send_diseqc_msg,
1889         .diseqc_recv_slave_reply        = stb0899_recv_slave_reply,
1890         .diseqc_send_burst              = stb0899_send_diseqc_burst,
1891 };
1892
1893 struct dvb_frontend *stb0899_attach(struct stb0899_config *config, struct i2c_adapter *i2c)
1894 {
1895         struct stb0899_state *state = NULL;
1896
1897         state = kzalloc(sizeof (struct stb0899_state), GFP_KERNEL);
1898         if (state == NULL)
1899                 goto error;
1900
1901         state->verbose                          = verbose;
1902         state->config                           = config;
1903         state->i2c                              = i2c;
1904         state->frontend.ops                     = stb0899_ops;
1905         state->frontend.demodulator_priv        = state;
1906
1907         stb0899_wakeup(&state->frontend);
1908         if (stb0899_get_dev_id(state) == -ENODEV) {
1909                 printk("%s: Exiting .. !\n", __func__);
1910                 goto error;
1911         }
1912
1913         printk("%s: Attaching STB0899 \n", __func__);
1914         return &state->frontend;
1915
1916 error:
1917         kfree(state);
1918         return NULL;
1919 }
1920 EXPORT_SYMBOL(stb0899_attach);
1921 MODULE_PARM_DESC(verbose, "Set Verbosity level");
1922 MODULE_AUTHOR("Manu Abraham");
1923 MODULE_DESCRIPTION("STB0899 Multi-Std frontend");
1924 MODULE_LICENSE("GPL");