bb8627ffae73edcd13d53058ae5a152fa64bcf75
[pandora-kernel.git] / drivers / media / dvb / frontends / drxk_hard.c
1 /*
2  * drxk_hard: DRX-K DVB-C/T demodulator driver
3  *
4  * Copyright (C) 2010-2011 Digital Devices GmbH
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * version 2 only, as published by the Free Software Foundation.
9  *
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
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., 51 Franklin Street, Fifth Floor, Boston, MA
20  * 02110-1301, USA
21  * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
22  */
23
24 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <linux/moduleparam.h>
27 #include <linux/init.h>
28 #include <linux/delay.h>
29 #include <linux/firmware.h>
30 #include <linux/i2c.h>
31 #include <linux/version.h>
32 #include <asm/div64.h>
33
34 #include "dvb_frontend.h"
35 #include "drxk.h"
36 #include "drxk_hard.h"
37
38 static int PowerDownDVBT(struct drxk_state *state, bool setPowerMode);
39 static int PowerDownQAM(struct drxk_state *state);
40 static int SetDVBTStandard(struct drxk_state *state,
41                            enum OperationMode oMode);
42 static int SetQAMStandard(struct drxk_state *state,
43                           enum OperationMode oMode);
44 static int SetQAM(struct drxk_state *state, u16 IntermediateFreqkHz,
45                   s32 tunerFreqOffset);
46 static int SetDVBTStandard(struct drxk_state *state,
47                            enum OperationMode oMode);
48 static int DVBTStart(struct drxk_state *state);
49 static int SetDVBT(struct drxk_state *state, u16 IntermediateFreqkHz,
50                    s32 tunerFreqOffset);
51 static int GetQAMLockStatus(struct drxk_state *state, u32 *pLockStatus);
52 static int GetDVBTLockStatus(struct drxk_state *state, u32 *pLockStatus);
53 static int SwitchAntennaToQAM(struct drxk_state *state);
54 static int SwitchAntennaToDVBT(struct drxk_state *state);
55
56 static bool IsDVBT(struct drxk_state *state)
57 {
58         return state->m_OperationMode == OM_DVBT;
59 }
60
61 static bool IsQAM(struct drxk_state *state)
62 {
63         return state->m_OperationMode == OM_QAM_ITU_A ||
64             state->m_OperationMode == OM_QAM_ITU_B ||
65             state->m_OperationMode == OM_QAM_ITU_C;
66 }
67
68 bool IsA1WithPatchCode(struct drxk_state *state)
69 {
70         return state->m_DRXK_A1_PATCH_CODE;
71 }
72
73 bool IsA1WithRomCode(struct drxk_state *state)
74 {
75         return state->m_DRXK_A1_ROM_CODE;
76 }
77
78 #define NOA1ROM 0
79
80 #define DRXDAP_FASI_SHORT_FORMAT(addr) (((addr) & 0xFC30FF80) == 0)
81 #define DRXDAP_FASI_LONG_FORMAT(addr)  (((addr) & 0xFC30FF80) != 0)
82
83 #define DEFAULT_MER_83  165
84 #define DEFAULT_MER_93  250
85
86 #ifndef DRXK_MPEG_SERIAL_OUTPUT_PIN_DRIVE_STRENGTH
87 #define DRXK_MPEG_SERIAL_OUTPUT_PIN_DRIVE_STRENGTH (0x02)
88 #endif
89
90 #ifndef DRXK_MPEG_PARALLEL_OUTPUT_PIN_DRIVE_STRENGTH
91 #define DRXK_MPEG_PARALLEL_OUTPUT_PIN_DRIVE_STRENGTH (0x03)
92 #endif
93
94 #ifndef DRXK_MPEG_OUTPUT_CLK_DRIVE_STRENGTH
95 #define DRXK_MPEG_OUTPUT_CLK_DRIVE_STRENGTH (0x06)
96 #endif
97
98 #define DEFAULT_DRXK_MPEG_LOCK_TIMEOUT 700
99 #define DEFAULT_DRXK_DEMOD_LOCK_TIMEOUT 500
100
101 #ifndef DRXK_KI_RAGC_ATV
102 #define DRXK_KI_RAGC_ATV   4
103 #endif
104 #ifndef DRXK_KI_IAGC_ATV
105 #define DRXK_KI_IAGC_ATV   6
106 #endif
107 #ifndef DRXK_KI_DAGC_ATV
108 #define DRXK_KI_DAGC_ATV   7
109 #endif
110
111 #ifndef DRXK_KI_RAGC_QAM
112 #define DRXK_KI_RAGC_QAM   3
113 #endif
114 #ifndef DRXK_KI_IAGC_QAM
115 #define DRXK_KI_IAGC_QAM   4
116 #endif
117 #ifndef DRXK_KI_DAGC_QAM
118 #define DRXK_KI_DAGC_QAM   7
119 #endif
120 #ifndef DRXK_KI_RAGC_DVBT
121 #define DRXK_KI_RAGC_DVBT  (IsA1WithPatchCode(state) ? 3 : 2)
122 #endif
123 #ifndef DRXK_KI_IAGC_DVBT
124 #define DRXK_KI_IAGC_DVBT  (IsA1WithPatchCode(state) ? 4 : 2)
125 #endif
126 #ifndef DRXK_KI_DAGC_DVBT
127 #define DRXK_KI_DAGC_DVBT  (IsA1WithPatchCode(state) ? 10 : 7)
128 #endif
129
130 #ifndef DRXK_AGC_DAC_OFFSET
131 #define DRXK_AGC_DAC_OFFSET (0x800)
132 #endif
133
134 #ifndef DRXK_BANDWIDTH_8MHZ_IN_HZ
135 #define DRXK_BANDWIDTH_8MHZ_IN_HZ  (0x8B8249L)
136 #endif
137
138 #ifndef DRXK_BANDWIDTH_7MHZ_IN_HZ
139 #define DRXK_BANDWIDTH_7MHZ_IN_HZ  (0x7A1200L)
140 #endif
141
142 #ifndef DRXK_BANDWIDTH_6MHZ_IN_HZ
143 #define DRXK_BANDWIDTH_6MHZ_IN_HZ  (0x68A1B6L)
144 #endif
145
146 #ifndef DRXK_QAM_SYMBOLRATE_MAX
147 #define DRXK_QAM_SYMBOLRATE_MAX         (7233000)
148 #endif
149
150 #define DRXK_BL_ROM_OFFSET_TAPS_DVBT    56
151 #define DRXK_BL_ROM_OFFSET_TAPS_ITU_A   64
152 #define DRXK_BL_ROM_OFFSET_TAPS_ITU_C   0x5FE0
153 #define DRXK_BL_ROM_OFFSET_TAPS_BG      24
154 #define DRXK_BL_ROM_OFFSET_TAPS_DKILLP  32
155 #define DRXK_BL_ROM_OFFSET_TAPS_NTSC    40
156 #define DRXK_BL_ROM_OFFSET_TAPS_FM      48
157 #define DRXK_BL_ROM_OFFSET_UCODE        0
158
159 #define DRXK_BLC_TIMEOUT                100
160
161 #define DRXK_BLCC_NR_ELEMENTS_TAPS      2
162 #define DRXK_BLCC_NR_ELEMENTS_UCODE     6
163
164 #define DRXK_BLDC_NR_ELEMENTS_TAPS      28
165
166 #ifndef DRXK_OFDM_NE_NOTCH_WIDTH
167 #define DRXK_OFDM_NE_NOTCH_WIDTH             (4)
168 #endif
169
170 #define DRXK_QAM_SL_SIG_POWER_QAM16       (40960)
171 #define DRXK_QAM_SL_SIG_POWER_QAM32       (20480)
172 #define DRXK_QAM_SL_SIG_POWER_QAM64       (43008)
173 #define DRXK_QAM_SL_SIG_POWER_QAM128      (20992)
174 #define DRXK_QAM_SL_SIG_POWER_QAM256      (43520)
175
176 static unsigned int debug;
177 module_param(debug, int, 0644);
178 MODULE_PARM_DESC(debug, "enable debug messages");
179
180 #define dprintk(level, fmt, arg...) do {                        \
181 if (debug >= level)                                             \
182         printk(KERN_DEBUG "drxk: %s" fmt, __func__, ## arg);    \
183 } while (0)
184
185
186 static inline u32 MulDiv32(u32 a, u32 b, u32 c)
187 {
188         u64 tmp64;
189
190         tmp64 = (u64) a * (u64) b;
191         do_div(tmp64, c);
192
193         return (u32) tmp64;
194 }
195
196 inline u32 Frac28a(u32 a, u32 c)
197 {
198         int i = 0;
199         u32 Q1 = 0;
200         u32 R0 = 0;
201
202         R0 = (a % c) << 4;      /* 32-28 == 4 shifts possible at max */
203         Q1 = a / c;             /* integer part, only the 4 least significant bits
204                                    will be visible in the result */
205
206         /* division using radix 16, 7 nibbles in the result */
207         for (i = 0; i < 7; i++) {
208                 Q1 = (Q1 << 4) | (R0 / c);
209                 R0 = (R0 % c) << 4;
210         }
211         /* rounding */
212         if ((R0 >> 3) >= c)
213                 Q1++;
214
215         return Q1;
216 }
217
218 static u32 Log10Times100(u32 x)
219 {
220         static const u8 scale = 15;
221         static const u8 indexWidth = 5;
222         u8 i = 0;
223         u32 y = 0;
224         u32 d = 0;
225         u32 k = 0;
226         u32 r = 0;
227         /*
228            log2lut[n] = (1<<scale) * 200 * log2(1.0 + ((1.0/(1<<INDEXWIDTH)) * n))
229            0 <= n < ((1<<INDEXWIDTH)+1)
230          */
231
232         static const u32 log2lut[] = {
233                 0,              /* 0.000000 */
234                 290941,         /* 290941.300628 */
235                 573196,         /* 573196.476418 */
236                 847269,         /* 847269.179851 */
237                 1113620,        /* 1113620.489452 */
238                 1372674,        /* 1372673.576986 */
239                 1624818,        /* 1624817.752104 */
240                 1870412,        /* 1870411.981536 */
241                 2109788,        /* 2109787.962654 */
242                 2343253,        /* 2343252.817465 */
243                 2571091,        /* 2571091.461923 */
244                 2793569,        /* 2793568.696416 */
245                 3010931,        /* 3010931.055901 */
246                 3223408,        /* 3223408.452106 */
247                 3431216,        /* 3431215.635215 */
248                 3634553,        /* 3634553.498355 */
249                 3833610,        /* 3833610.244726 */
250                 4028562,        /* 4028562.434393 */
251                 4219576,        /* 4219575.925308 */
252                 4406807,        /* 4406806.721144 */
253                 4590402,        /* 4590401.736809 */
254                 4770499,        /* 4770499.491025 */
255                 4947231,        /* 4947230.734179 */
256                 5120719,        /* 5120719.018555 */
257                 5291081,        /* 5291081.217197 */
258                 5458428,        /* 5458427.996830 */
259                 5622864,        /* 5622864.249668 */
260                 5784489,        /* 5784489.488298 */
261                 5943398,        /* 5943398.207380 */
262                 6099680,        /* 6099680.215452 */
263                 6253421,        /* 6253420.939751 */
264                 6404702,        /* 6404701.706649 */
265                 6553600,        /* 6553600.000000 */
266         };
267
268
269         if (x == 0)
270                 return 0;
271
272         /* Scale x (normalize) */
273         /* computing y in log(x/y) = log(x) - log(y) */
274         if ((x & ((0xffffffff) << (scale + 1))) == 0) {
275                 for (k = scale; k > 0; k--) {
276                         if (x & (((u32) 1) << scale))
277                                 break;
278                         x <<= 1;
279                 }
280         } else {
281                 for (k = scale; k < 31; k++) {
282                         if ((x & (((u32) (-1)) << (scale + 1))) == 0)
283                                 break;
284                         x >>= 1;
285                 }
286         }
287         /*
288            Now x has binary point between bit[scale] and bit[scale-1]
289            and 1.0 <= x < 2.0 */
290
291         /* correction for divison: log(x) = log(x/y)+log(y) */
292         y = k * ((((u32) 1) << scale) * 200);
293
294         /* remove integer part */
295         x &= ((((u32) 1) << scale) - 1);
296         /* get index */
297         i = (u8) (x >> (scale - indexWidth));
298         /* compute delta (x - a) */
299         d = x & ((((u32) 1) << (scale - indexWidth)) - 1);
300         /* compute log, multiplication (d* (..)) must be within range ! */
301         y += log2lut[i] +
302             ((d * (log2lut[i + 1] - log2lut[i])) >> (scale - indexWidth));
303         /* Conver to log10() */
304         y /= 108853;            /* (log2(10) << scale) */
305         r = (y >> 1);
306         /* rounding */
307         if (y & ((u32) 1))
308                 r++;
309         return r;
310 }
311
312 /****************************************************************************/
313 /* I2C **********************************************************************/
314 /****************************************************************************/
315
316 static int i2c_read1(struct i2c_adapter *adapter, u8 adr, u8 *val)
317 {
318         struct i2c_msg msgs[1] = { {.addr = adr, .flags = I2C_M_RD,
319                                     .buf = val, .len = 1}
320         };
321
322         return i2c_transfer(adapter, msgs, 1);
323 }
324
325 static int i2c_write(struct i2c_adapter *adap, u8 adr, u8 *data, int len)
326 {
327         int status;
328         struct i2c_msg msg = {
329             .addr = adr, .flags = 0, .buf = data, .len = len };
330
331         dprintk(3, ":");
332         if (debug > 2) {
333                 int i;
334                 for (i = 0; i < len; i++)
335                         printk(KERN_CONT " %02x", data[i]);
336                 printk(KERN_CONT "\n");
337         }
338         status = i2c_transfer(adap, &msg, 1);
339         if (status >= 0 && status != 1)
340                 status = -EIO;
341
342         if (status < 0)
343                 printk(KERN_ERR "drxk: i2c write error at addr 0x%02x\n", adr);
344
345         return status;
346 }
347
348 static int i2c_read(struct i2c_adapter *adap,
349                     u8 adr, u8 *msg, int len, u8 *answ, int alen)
350 {
351         int status;
352         struct i2c_msg msgs[2] = {
353                 {.addr = adr, .flags = 0,
354                                     .buf = msg, .len = len},
355                 {.addr = adr, .flags = I2C_M_RD,
356                  .buf = answ, .len = alen}
357         };
358         dprintk(3, ":");
359         if (debug > 2) {
360                 int i;
361                 for (i = 0; i < len; i++)
362                         printk(KERN_CONT " %02x", msg[i]);
363                 printk(KERN_CONT "\n");
364         }
365         status = i2c_transfer(adap, msgs, 2);
366         if (status != 2) {
367                 if (debug > 2)
368                         printk(KERN_CONT ": ERROR!\n");
369                 if (status >= 0)
370                         status = -EIO;
371
372                 printk(KERN_ERR "drxk: i2c read error at addr 0x%02x\n", adr);
373                 return status;
374         }
375         if (debug > 2) {
376                 int i;
377                 printk(KERN_CONT ": Read ");
378                 for (i = 0; i < len; i++)
379                         printk(KERN_CONT " %02x", msg[i]);
380                 printk(KERN_CONT "\n");
381         }
382         return 0;
383 }
384
385 static int read16_flags(struct drxk_state *state, u32 reg, u16 *data, u8 flags)
386 {
387         int status;
388         u8 adr = state->demod_address, mm1[4], mm2[2], len;
389
390         if (state->single_master)
391                 flags |= 0xC0;
392
393         if (DRXDAP_FASI_LONG_FORMAT(reg) || (flags != 0)) {
394                 mm1[0] = (((reg << 1) & 0xFF) | 0x01);
395                 mm1[1] = ((reg >> 16) & 0xFF);
396                 mm1[2] = ((reg >> 24) & 0xFF) | flags;
397                 mm1[3] = ((reg >> 7) & 0xFF);
398                 len = 4;
399         } else {
400                 mm1[0] = ((reg << 1) & 0xFF);
401                 mm1[1] = (((reg >> 16) & 0x0F) | ((reg >> 18) & 0xF0));
402                 len = 2;
403         }
404         dprintk(2, "(0x%08x, 0x%02x)\n", reg, flags);
405         status = i2c_read(state->i2c, adr, mm1, len, mm2, 2);
406         if (status < 0)
407                 return status;
408         if (data)
409                 *data = mm2[0] | (mm2[1] << 8);
410
411         return 0;
412 }
413
414 static int read16(struct drxk_state *state, u32 reg, u16 *data)
415 {
416         return read16_flags(state, reg, data, 0);
417 }
418
419 static int read32_flags(struct drxk_state *state, u32 reg, u32 *data, u8 flags)
420 {
421         int status;
422         u8 adr = state->demod_address, mm1[4], mm2[4], len;
423
424         if (state->single_master)
425                 flags |= 0xC0;
426
427         if (DRXDAP_FASI_LONG_FORMAT(reg) || (flags != 0)) {
428                 mm1[0] = (((reg << 1) & 0xFF) | 0x01);
429                 mm1[1] = ((reg >> 16) & 0xFF);
430                 mm1[2] = ((reg >> 24) & 0xFF) | flags;
431                 mm1[3] = ((reg >> 7) & 0xFF);
432                 len = 4;
433         } else {
434                 mm1[0] = ((reg << 1) & 0xFF);
435                 mm1[1] = (((reg >> 16) & 0x0F) | ((reg >> 18) & 0xF0));
436                 len = 2;
437         }
438         dprintk(2, "(0x%08x, 0x%02x)\n", reg, flags);
439         status = i2c_read(state->i2c, adr, mm1, len, mm2, 4);
440         if (status < 0)
441                 return status;
442         if (data)
443                 *data = mm2[0] | (mm2[1] << 8) |
444                     (mm2[2] << 16) | (mm2[3] << 24);
445
446         return 0;
447 }
448
449 static int read32(struct drxk_state *state, u32 reg, u32 *data)
450 {
451         return read32_flags(state, reg, data, 0);
452 }
453
454 static int write16_flags(struct drxk_state *state, u32 reg, u16 data, u8 flags)
455 {
456         u8 adr = state->demod_address, mm[6], len;
457
458         if (state->single_master)
459                 flags |= 0xC0;
460         if (DRXDAP_FASI_LONG_FORMAT(reg) || (flags != 0)) {
461                 mm[0] = (((reg << 1) & 0xFF) | 0x01);
462                 mm[1] = ((reg >> 16) & 0xFF);
463                 mm[2] = ((reg >> 24) & 0xFF) | flags;
464                 mm[3] = ((reg >> 7) & 0xFF);
465                 len = 4;
466         } else {
467                 mm[0] = ((reg << 1) & 0xFF);
468                 mm[1] = (((reg >> 16) & 0x0F) | ((reg >> 18) & 0xF0));
469                 len = 2;
470         }
471         mm[len] = data & 0xff;
472         mm[len + 1] = (data >> 8) & 0xff;
473
474         dprintk(2, "(0x%08x, 0x%04x, 0x%02x)\n", reg, data, flags);
475         return i2c_write(state->i2c, adr, mm, len + 2);
476 }
477
478 static int write16(struct drxk_state *state, u32 reg, u16 data)
479 {
480         return write16_flags(state, reg, data, 0);
481 }
482
483 static int write32_flags(struct drxk_state *state, u32 reg, u32 data, u8 flags)
484 {
485         u8 adr = state->demod_address, mm[8], len;
486
487         if (state->single_master)
488                 flags |= 0xC0;
489         if (DRXDAP_FASI_LONG_FORMAT(reg) || (flags != 0)) {
490                 mm[0] = (((reg << 1) & 0xFF) | 0x01);
491                 mm[1] = ((reg >> 16) & 0xFF);
492                 mm[2] = ((reg >> 24) & 0xFF) | flags;
493                 mm[3] = ((reg >> 7) & 0xFF);
494                 len = 4;
495         } else {
496                 mm[0] = ((reg << 1) & 0xFF);
497                 mm[1] = (((reg >> 16) & 0x0F) | ((reg >> 18) & 0xF0));
498                 len = 2;
499         }
500         mm[len] = data & 0xff;
501         mm[len + 1] = (data >> 8) & 0xff;
502         mm[len + 2] = (data >> 16) & 0xff;
503         mm[len + 3] = (data >> 24) & 0xff;
504         dprintk(2, "(0x%08x, 0x%08x, 0x%02x)\n", reg, data, flags);
505
506         return i2c_write(state->i2c, adr, mm, len + 4);
507 }
508
509 static int write32(struct drxk_state *state, u32 reg, u32 data)
510 {
511         return write32_flags(state, reg, data, 0);
512 }
513
514 static int write_block(struct drxk_state *state, u32 Address,
515                       const int BlockSize, const u8 pBlock[])
516 {
517         int status = 0, BlkSize = BlockSize;
518         u8 Flags = 0;
519
520         if (state->single_master)
521                 Flags |= 0xC0;
522
523         while (BlkSize > 0) {
524                 int Chunk = BlkSize > state->m_ChunkSize ?
525                     state->m_ChunkSize : BlkSize;
526                 u8 *AdrBuf = &state->Chunk[0];
527                 u32 AdrLength = 0;
528
529                 if (DRXDAP_FASI_LONG_FORMAT(Address) || (Flags != 0)) {
530                         AdrBuf[0] = (((Address << 1) & 0xFF) | 0x01);
531                         AdrBuf[1] = ((Address >> 16) & 0xFF);
532                         AdrBuf[2] = ((Address >> 24) & 0xFF);
533                         AdrBuf[3] = ((Address >> 7) & 0xFF);
534                         AdrBuf[2] |= Flags;
535                         AdrLength = 4;
536                         if (Chunk == state->m_ChunkSize)
537                                 Chunk -= 2;
538                 } else {
539                         AdrBuf[0] = ((Address << 1) & 0xFF);
540                         AdrBuf[1] = (((Address >> 16) & 0x0F) |
541                                      ((Address >> 18) & 0xF0));
542                         AdrLength = 2;
543                 }
544                 memcpy(&state->Chunk[AdrLength], pBlock, Chunk);
545                 dprintk(2, "(0x%08x, 0x%02x)\n", Address, Flags);
546                 if (debug > 1) {
547                         int i;
548                         if (pBlock)
549                                 for (i = 0; i < Chunk; i++)
550                                         printk(KERN_CONT " %02x", pBlock[i]);
551                         printk(KERN_CONT "\n");
552                 }
553                 status = i2c_write(state->i2c, state->demod_address,
554                                    &state->Chunk[0], Chunk + AdrLength);
555                 if (status < 0) {
556                         printk(KERN_ERR "drxk: %s: i2c write error at addr 0x%02x\n",
557                                __func__, Address);
558                         break;
559                 }
560                 pBlock += Chunk;
561                 Address += (Chunk >> 1);
562                 BlkSize -= Chunk;
563         }
564         return status;
565 }
566
567 #ifndef DRXK_MAX_RETRIES_POWERUP
568 #define DRXK_MAX_RETRIES_POWERUP 20
569 #endif
570
571 int PowerUpDevice(struct drxk_state *state)
572 {
573         int status;
574         u8 data = 0;
575         u16 retryCount = 0;
576
577         dprintk(1, "\n");
578
579         status = i2c_read1(state->i2c, state->demod_address, &data);
580         if (status < 0) {
581                 do {
582                         data = 0;
583                         status = i2c_write(state->i2c, state->demod_address,
584                                            &data, 1);
585                         msleep(10);
586                         retryCount++;
587                         if (status < 0)
588                                 continue;
589                         status = i2c_read1(state->i2c, state->demod_address,
590                                            &data);
591                 } while (status < 0 &&
592                          (retryCount < DRXK_MAX_RETRIES_POWERUP));
593                 if (status < 0 && retryCount >= DRXK_MAX_RETRIES_POWERUP)
594                         goto error;
595         }
596
597         /* Make sure all clk domains are active */
598         status = write16(state, SIO_CC_PWD_MODE__A, SIO_CC_PWD_MODE_LEVEL_NONE);
599         if (status < 0)
600                 goto error;
601         status = write16(state, SIO_CC_UPDATE__A, SIO_CC_UPDATE_KEY);
602         if (status < 0)
603                 goto error;
604         /* Enable pll lock tests */
605         status = write16(state, SIO_CC_PLL_LOCK__A, 1);
606         if (status < 0)
607                 goto error;
608
609         state->m_currentPowerMode = DRX_POWER_UP;
610
611 error:
612         if (status < 0)
613                 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
614
615         return status;
616 }
617
618
619 static int init_state(struct drxk_state *state)
620 {
621         /*
622          * FIXME: most (all?) of the values bellow should be moved into
623          * struct drxk_config, as they are probably board-specific
624          */
625         u32 ulVSBIfAgcMode = DRXK_AGC_CTRL_AUTO;
626         u32 ulVSBIfAgcOutputLevel = 0;
627         u32 ulVSBIfAgcMinLevel = 0;
628         u32 ulVSBIfAgcMaxLevel = 0x7FFF;
629         u32 ulVSBIfAgcSpeed = 3;
630
631         u32 ulVSBRfAgcMode = DRXK_AGC_CTRL_AUTO;
632         u32 ulVSBRfAgcOutputLevel = 0;
633         u32 ulVSBRfAgcMinLevel = 0;
634         u32 ulVSBRfAgcMaxLevel = 0x7FFF;
635         u32 ulVSBRfAgcSpeed = 3;
636         u32 ulVSBRfAgcTop = 9500;
637         u32 ulVSBRfAgcCutOffCurrent = 4000;
638
639         u32 ulATVIfAgcMode = DRXK_AGC_CTRL_AUTO;
640         u32 ulATVIfAgcOutputLevel = 0;
641         u32 ulATVIfAgcMinLevel = 0;
642         u32 ulATVIfAgcMaxLevel = 0;
643         u32 ulATVIfAgcSpeed = 3;
644
645         u32 ulATVRfAgcMode = DRXK_AGC_CTRL_OFF;
646         u32 ulATVRfAgcOutputLevel = 0;
647         u32 ulATVRfAgcMinLevel = 0;
648         u32 ulATVRfAgcMaxLevel = 0;
649         u32 ulATVRfAgcTop = 9500;
650         u32 ulATVRfAgcCutOffCurrent = 4000;
651         u32 ulATVRfAgcSpeed = 3;
652
653         u32 ulQual83 = DEFAULT_MER_83;
654         u32 ulQual93 = DEFAULT_MER_93;
655
656         u32 ulDVBTStaticTSClock = 1;
657         u32 ulDVBCStaticTSClock = 1;
658
659         u32 ulMpegLockTimeOut = DEFAULT_DRXK_MPEG_LOCK_TIMEOUT;
660         u32 ulDemodLockTimeOut = DEFAULT_DRXK_DEMOD_LOCK_TIMEOUT;
661
662         /* io_pad_cfg register (8 bit reg.) MSB bit is 1 (default value) */
663         /* io_pad_cfg_mode output mode is drive always */
664         /* io_pad_cfg_drive is set to power 2 (23 mA) */
665         u32 ulGPIOCfg = 0x0113;
666         u32 ulGPIO = 0;
667         u32 ulSerialMode = 1;
668         u32 ulInvertTSClock = 0;
669         u32 ulTSDataStrength = DRXK_MPEG_SERIAL_OUTPUT_PIN_DRIVE_STRENGTH;
670         u32 ulTSClockkStrength = DRXK_MPEG_OUTPUT_CLK_DRIVE_STRENGTH;
671         u32 ulDVBTBitrate = 50000000;
672         u32 ulDVBCBitrate = DRXK_QAM_SYMBOLRATE_MAX * 8;
673
674         u32 ulInsertRSByte = 0;
675
676         u32 ulRfMirror = 1;
677         u32 ulPowerDown = 0;
678
679         dprintk(1, "\n");
680
681         state->m_hasLNA = false;
682         state->m_hasDVBT = false;
683         state->m_hasDVBC = false;
684         state->m_hasATV = false;
685         state->m_hasOOB = false;
686         state->m_hasAudio = false;
687
688         state->m_ChunkSize = 124;
689
690         state->m_oscClockFreq = 0;
691         state->m_smartAntInverted = false;
692         state->m_bPDownOpenBridge = false;
693
694         /* real system clock frequency in kHz */
695         state->m_sysClockFreq = 151875;
696         /* Timing div, 250ns/Psys */
697         /* Timing div, = (delay (nano seconds) * sysclk (kHz))/ 1000 */
698         state->m_HICfgTimingDiv = ((state->m_sysClockFreq / 1000) *
699                                    HI_I2C_DELAY) / 1000;
700         /* Clipping */
701         if (state->m_HICfgTimingDiv > SIO_HI_RA_RAM_PAR_2_CFG_DIV__M)
702                 state->m_HICfgTimingDiv = SIO_HI_RA_RAM_PAR_2_CFG_DIV__M;
703         state->m_HICfgWakeUpKey = (state->demod_address << 1);
704         /* port/bridge/power down ctrl */
705         state->m_HICfgCtrl = SIO_HI_RA_RAM_PAR_5_CFG_SLV0_SLAVE;
706
707         state->m_bPowerDown = (ulPowerDown != 0);
708
709         state->m_DRXK_A1_PATCH_CODE = false;
710         state->m_DRXK_A1_ROM_CODE = false;
711         state->m_DRXK_A2_ROM_CODE = false;
712         state->m_DRXK_A3_ROM_CODE = false;
713         state->m_DRXK_A2_PATCH_CODE = false;
714         state->m_DRXK_A3_PATCH_CODE = false;
715
716         /* Init AGC and PGA parameters */
717         /* VSB IF */
718         state->m_vsbIfAgcCfg.ctrlMode = (ulVSBIfAgcMode);
719         state->m_vsbIfAgcCfg.outputLevel = (ulVSBIfAgcOutputLevel);
720         state->m_vsbIfAgcCfg.minOutputLevel = (ulVSBIfAgcMinLevel);
721         state->m_vsbIfAgcCfg.maxOutputLevel = (ulVSBIfAgcMaxLevel);
722         state->m_vsbIfAgcCfg.speed = (ulVSBIfAgcSpeed);
723         state->m_vsbPgaCfg = 140;
724
725         /* VSB RF */
726         state->m_vsbRfAgcCfg.ctrlMode = (ulVSBRfAgcMode);
727         state->m_vsbRfAgcCfg.outputLevel = (ulVSBRfAgcOutputLevel);
728         state->m_vsbRfAgcCfg.minOutputLevel = (ulVSBRfAgcMinLevel);
729         state->m_vsbRfAgcCfg.maxOutputLevel = (ulVSBRfAgcMaxLevel);
730         state->m_vsbRfAgcCfg.speed = (ulVSBRfAgcSpeed);
731         state->m_vsbRfAgcCfg.top = (ulVSBRfAgcTop);
732         state->m_vsbRfAgcCfg.cutOffCurrent = (ulVSBRfAgcCutOffCurrent);
733         state->m_vsbPreSawCfg.reference = 0x07;
734         state->m_vsbPreSawCfg.usePreSaw = true;
735
736         state->m_Quality83percent = DEFAULT_MER_83;
737         state->m_Quality93percent = DEFAULT_MER_93;
738         if (ulQual93 <= 500 && ulQual83 < ulQual93) {
739                 state->m_Quality83percent = ulQual83;
740                 state->m_Quality93percent = ulQual93;
741         }
742
743         /* ATV IF */
744         state->m_atvIfAgcCfg.ctrlMode = (ulATVIfAgcMode);
745         state->m_atvIfAgcCfg.outputLevel = (ulATVIfAgcOutputLevel);
746         state->m_atvIfAgcCfg.minOutputLevel = (ulATVIfAgcMinLevel);
747         state->m_atvIfAgcCfg.maxOutputLevel = (ulATVIfAgcMaxLevel);
748         state->m_atvIfAgcCfg.speed = (ulATVIfAgcSpeed);
749
750         /* ATV RF */
751         state->m_atvRfAgcCfg.ctrlMode = (ulATVRfAgcMode);
752         state->m_atvRfAgcCfg.outputLevel = (ulATVRfAgcOutputLevel);
753         state->m_atvRfAgcCfg.minOutputLevel = (ulATVRfAgcMinLevel);
754         state->m_atvRfAgcCfg.maxOutputLevel = (ulATVRfAgcMaxLevel);
755         state->m_atvRfAgcCfg.speed = (ulATVRfAgcSpeed);
756         state->m_atvRfAgcCfg.top = (ulATVRfAgcTop);
757         state->m_atvRfAgcCfg.cutOffCurrent = (ulATVRfAgcCutOffCurrent);
758         state->m_atvPreSawCfg.reference = 0x04;
759         state->m_atvPreSawCfg.usePreSaw = true;
760
761
762         /* DVBT RF */
763         state->m_dvbtRfAgcCfg.ctrlMode = DRXK_AGC_CTRL_OFF;
764         state->m_dvbtRfAgcCfg.outputLevel = 0;
765         state->m_dvbtRfAgcCfg.minOutputLevel = 0;
766         state->m_dvbtRfAgcCfg.maxOutputLevel = 0xFFFF;
767         state->m_dvbtRfAgcCfg.top = 0x2100;
768         state->m_dvbtRfAgcCfg.cutOffCurrent = 4000;
769         state->m_dvbtRfAgcCfg.speed = 1;
770
771
772         /* DVBT IF */
773         state->m_dvbtIfAgcCfg.ctrlMode = DRXK_AGC_CTRL_AUTO;
774         state->m_dvbtIfAgcCfg.outputLevel = 0;
775         state->m_dvbtIfAgcCfg.minOutputLevel = 0;
776         state->m_dvbtIfAgcCfg.maxOutputLevel = 9000;
777         state->m_dvbtIfAgcCfg.top = 13424;
778         state->m_dvbtIfAgcCfg.cutOffCurrent = 0;
779         state->m_dvbtIfAgcCfg.speed = 3;
780         state->m_dvbtIfAgcCfg.FastClipCtrlDelay = 30;
781         state->m_dvbtIfAgcCfg.IngainTgtMax = 30000;
782         /* state->m_dvbtPgaCfg = 140; */
783
784         state->m_dvbtPreSawCfg.reference = 4;
785         state->m_dvbtPreSawCfg.usePreSaw = false;
786
787         /* QAM RF */
788         state->m_qamRfAgcCfg.ctrlMode = DRXK_AGC_CTRL_OFF;
789         state->m_qamRfAgcCfg.outputLevel = 0;
790         state->m_qamRfAgcCfg.minOutputLevel = 6023;
791         state->m_qamRfAgcCfg.maxOutputLevel = 27000;
792         state->m_qamRfAgcCfg.top = 0x2380;
793         state->m_qamRfAgcCfg.cutOffCurrent = 4000;
794         state->m_qamRfAgcCfg.speed = 3;
795
796         /* QAM IF */
797         state->m_qamIfAgcCfg.ctrlMode = DRXK_AGC_CTRL_AUTO;
798         state->m_qamIfAgcCfg.outputLevel = 0;
799         state->m_qamIfAgcCfg.minOutputLevel = 0;
800         state->m_qamIfAgcCfg.maxOutputLevel = 9000;
801         state->m_qamIfAgcCfg.top = 0x0511;
802         state->m_qamIfAgcCfg.cutOffCurrent = 0;
803         state->m_qamIfAgcCfg.speed = 3;
804         state->m_qamIfAgcCfg.IngainTgtMax = 5119;
805         state->m_qamIfAgcCfg.FastClipCtrlDelay = 50;
806
807         state->m_qamPgaCfg = 140;
808         state->m_qamPreSawCfg.reference = 4;
809         state->m_qamPreSawCfg.usePreSaw = false;
810
811         state->m_OperationMode = OM_NONE;
812         state->m_DrxkState = DRXK_UNINITIALIZED;
813
814         /* MPEG output configuration */
815         state->m_enableMPEGOutput = true;       /* If TRUE; enable MPEG ouput */
816         state->m_insertRSByte = false;  /* If TRUE; insert RS byte */
817         state->m_enableParallel = true; /* If TRUE;
818                                            parallel out otherwise serial */
819         state->m_invertDATA = false;    /* If TRUE; invert DATA signals */
820         state->m_invertERR = false;     /* If TRUE; invert ERR signal */
821         state->m_invertSTR = false;     /* If TRUE; invert STR signals */
822         state->m_invertVAL = false;     /* If TRUE; invert VAL signals */
823         state->m_invertCLK = (ulInvertTSClock != 0);    /* If TRUE; invert CLK signals */
824         state->m_DVBTStaticCLK = (ulDVBTStaticTSClock != 0);
825         state->m_DVBCStaticCLK = (ulDVBCStaticTSClock != 0);
826         /* If TRUE; static MPEG clockrate will be used;
827            otherwise clockrate will adapt to the bitrate of the TS */
828
829         state->m_DVBTBitrate = ulDVBTBitrate;
830         state->m_DVBCBitrate = ulDVBCBitrate;
831
832         state->m_TSDataStrength = (ulTSDataStrength & 0x07);
833         state->m_TSClockkStrength = (ulTSClockkStrength & 0x07);
834
835         /* Maximum bitrate in b/s in case static clockrate is selected */
836         state->m_mpegTsStaticBitrate = 19392658;
837         state->m_disableTEIhandling = false;
838
839         if (ulInsertRSByte)
840                 state->m_insertRSByte = true;
841
842         state->m_MpegLockTimeOut = DEFAULT_DRXK_MPEG_LOCK_TIMEOUT;
843         if (ulMpegLockTimeOut < 10000)
844                 state->m_MpegLockTimeOut = ulMpegLockTimeOut;
845         state->m_DemodLockTimeOut = DEFAULT_DRXK_DEMOD_LOCK_TIMEOUT;
846         if (ulDemodLockTimeOut < 10000)
847                 state->m_DemodLockTimeOut = ulDemodLockTimeOut;
848
849         /* QAM defaults */
850         state->m_Constellation = DRX_CONSTELLATION_AUTO;
851         state->m_qamInterleaveMode = DRXK_QAM_I12_J17;
852         state->m_fecRsPlen = 204 * 8;   /* fecRsPlen  annex A */
853         state->m_fecRsPrescale = 1;
854
855         state->m_sqiSpeed = DRXK_DVBT_SQI_SPEED_MEDIUM;
856         state->m_agcFastClipCtrlDelay = 0;
857
858         state->m_GPIOCfg = (ulGPIOCfg);
859
860         state->m_bPowerDown = false;
861         state->m_currentPowerMode = DRX_POWER_DOWN;
862
863         state->m_enableParallel = (ulSerialMode == 0);
864
865         state->m_rfmirror = (ulRfMirror == 0);
866         state->m_IfAgcPol = false;
867         return 0;
868 }
869
870 static int DRXX_Open(struct drxk_state *state)
871 {
872         int status = 0;
873         u32 jtag = 0;
874         u16 bid = 0;
875         u16 key = 0;
876
877         dprintk(1, "\n");
878         /* stop lock indicator process */
879         status = write16(state, SCU_RAM_GPIO__A, SCU_RAM_GPIO_HW_LOCK_IND_DISABLE);
880         if (status < 0)
881                 goto error;
882         /* Check device id */
883         status = read16(state, SIO_TOP_COMM_KEY__A, &key);
884         if (status < 0)
885                 goto error;
886         status = write16(state, SIO_TOP_COMM_KEY__A, SIO_TOP_COMM_KEY_KEY);
887         if (status < 0)
888                 goto error;
889         status = read32(state, SIO_TOP_JTAGID_LO__A, &jtag);
890         if (status < 0)
891                 goto error;
892         status = read16(state, SIO_PDR_UIO_IN_HI__A, &bid);
893         if (status < 0)
894                 goto error;
895         status = write16(state, SIO_TOP_COMM_KEY__A, key);
896 error:
897         if (status < 0)
898                 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
899         return status;
900 }
901
902 static int GetDeviceCapabilities(struct drxk_state *state)
903 {
904         u16 sioPdrOhwCfg = 0;
905         u32 sioTopJtagidLo = 0;
906         int status;
907         const char *spin = "";
908
909         dprintk(1, "\n");
910
911         /* driver 0.9.0 */
912         /* stop lock indicator process */
913         status = write16(state, SCU_RAM_GPIO__A, SCU_RAM_GPIO_HW_LOCK_IND_DISABLE);
914         if (status < 0)
915                 goto error;
916         status = write16(state, SIO_TOP_COMM_KEY__A, 0xFABA);
917         if (status < 0)
918                 goto error;
919         status = read16(state, SIO_PDR_OHW_CFG__A, &sioPdrOhwCfg);
920         if (status < 0)
921                 goto error;
922         status = write16(state, SIO_TOP_COMM_KEY__A, 0x0000);
923         if (status < 0)
924                 goto error;
925
926         switch ((sioPdrOhwCfg & SIO_PDR_OHW_CFG_FREF_SEL__M)) {
927         case 0:
928                 /* ignore (bypass ?) */
929                 break;
930         case 1:
931                 /* 27 MHz */
932                 state->m_oscClockFreq = 27000;
933                 break;
934         case 2:
935                 /* 20.25 MHz */
936                 state->m_oscClockFreq = 20250;
937                 break;
938         case 3:
939                 /* 4 MHz */
940                 state->m_oscClockFreq = 20250;
941                 break;
942         default:
943                 printk(KERN_ERR "drxk: Clock Frequency is unkonwn\n");
944                 return -EINVAL;
945         }
946         /*
947                 Determine device capabilities
948                 Based on pinning v14
949                 */
950         status = read32(state, SIO_TOP_JTAGID_LO__A, &sioTopJtagidLo);
951         if (status < 0)
952                 goto error;
953         /* driver 0.9.0 */
954         switch ((sioTopJtagidLo >> 29) & 0xF) {
955         case 0:
956                 state->m_deviceSpin = DRXK_SPIN_A1;
957                 spin = "A1";
958                 break;
959         case 2:
960                 state->m_deviceSpin = DRXK_SPIN_A2;
961                 spin = "A2";
962                 break;
963         case 3:
964                 state->m_deviceSpin = DRXK_SPIN_A3;
965                 spin = "A3";
966                 break;
967         default:
968                 state->m_deviceSpin = DRXK_SPIN_UNKNOWN;
969                 status = -EINVAL;
970                 printk(KERN_ERR "drxk: Spin unknown\n");
971                 goto error2;
972         }
973         switch ((sioTopJtagidLo >> 12) & 0xFF) {
974         case 0x13:
975                 /* typeId = DRX3913K_TYPE_ID */
976                 state->m_hasLNA = false;
977                 state->m_hasOOB = false;
978                 state->m_hasATV = false;
979                 state->m_hasAudio = false;
980                 state->m_hasDVBT = true;
981                 state->m_hasDVBC = true;
982                 state->m_hasSAWSW = true;
983                 state->m_hasGPIO2 = false;
984                 state->m_hasGPIO1 = false;
985                 state->m_hasIRQN = false;
986                 break;
987         case 0x15:
988                 /* typeId = DRX3915K_TYPE_ID */
989                 state->m_hasLNA = false;
990                 state->m_hasOOB = false;
991                 state->m_hasATV = true;
992                 state->m_hasAudio = false;
993                 state->m_hasDVBT = true;
994                 state->m_hasDVBC = false;
995                 state->m_hasSAWSW = true;
996                 state->m_hasGPIO2 = true;
997                 state->m_hasGPIO1 = true;
998                 state->m_hasIRQN = false;
999                 break;
1000         case 0x16:
1001                 /* typeId = DRX3916K_TYPE_ID */
1002                 state->m_hasLNA = false;
1003                 state->m_hasOOB = false;
1004                 state->m_hasATV = true;
1005                 state->m_hasAudio = false;
1006                 state->m_hasDVBT = true;
1007                 state->m_hasDVBC = false;
1008                 state->m_hasSAWSW = true;
1009                 state->m_hasGPIO2 = true;
1010                 state->m_hasGPIO1 = true;
1011                 state->m_hasIRQN = false;
1012                 break;
1013         case 0x18:
1014                 /* typeId = DRX3918K_TYPE_ID */
1015                 state->m_hasLNA = false;
1016                 state->m_hasOOB = false;
1017                 state->m_hasATV = true;
1018                 state->m_hasAudio = true;
1019                 state->m_hasDVBT = true;
1020                 state->m_hasDVBC = false;
1021                 state->m_hasSAWSW = true;
1022                 state->m_hasGPIO2 = true;
1023                 state->m_hasGPIO1 = true;
1024                 state->m_hasIRQN = false;
1025                 break;
1026         case 0x21:
1027                 /* typeId = DRX3921K_TYPE_ID */
1028                 state->m_hasLNA = false;
1029                 state->m_hasOOB = false;
1030                 state->m_hasATV = true;
1031                 state->m_hasAudio = true;
1032                 state->m_hasDVBT = true;
1033                 state->m_hasDVBC = true;
1034                 state->m_hasSAWSW = true;
1035                 state->m_hasGPIO2 = true;
1036                 state->m_hasGPIO1 = true;
1037                 state->m_hasIRQN = false;
1038                 break;
1039         case 0x23:
1040                 /* typeId = DRX3923K_TYPE_ID */
1041                 state->m_hasLNA = false;
1042                 state->m_hasOOB = false;
1043                 state->m_hasATV = true;
1044                 state->m_hasAudio = true;
1045                 state->m_hasDVBT = true;
1046                 state->m_hasDVBC = true;
1047                 state->m_hasSAWSW = true;
1048                 state->m_hasGPIO2 = true;
1049                 state->m_hasGPIO1 = true;
1050                 state->m_hasIRQN = false;
1051                 break;
1052         case 0x25:
1053                 /* typeId = DRX3925K_TYPE_ID */
1054                 state->m_hasLNA = false;
1055                 state->m_hasOOB = false;
1056                 state->m_hasATV = true;
1057                 state->m_hasAudio = true;
1058                 state->m_hasDVBT = true;
1059                 state->m_hasDVBC = true;
1060                 state->m_hasSAWSW = true;
1061                 state->m_hasGPIO2 = true;
1062                 state->m_hasGPIO1 = true;
1063                 state->m_hasIRQN = false;
1064                 break;
1065         case 0x26:
1066                 /* typeId = DRX3926K_TYPE_ID */
1067                 state->m_hasLNA = false;
1068                 state->m_hasOOB = false;
1069                 state->m_hasATV = true;
1070                 state->m_hasAudio = false;
1071                 state->m_hasDVBT = true;
1072                 state->m_hasDVBC = true;
1073                 state->m_hasSAWSW = true;
1074                 state->m_hasGPIO2 = true;
1075                 state->m_hasGPIO1 = true;
1076                 state->m_hasIRQN = false;
1077                 break;
1078         default:
1079                 printk(KERN_ERR "drxk: DeviceID not supported = %02x\n",
1080                         ((sioTopJtagidLo >> 12) & 0xFF));
1081                 status = -EINVAL;
1082                 goto error2;
1083         }
1084
1085         printk(KERN_INFO
1086                "drxk: detected a drx-39%02xk, spin %s, xtal %d.%03d MHz\n",
1087                ((sioTopJtagidLo >> 12) & 0xFF), spin,
1088                state->m_oscClockFreq / 1000,
1089                state->m_oscClockFreq % 1000);
1090
1091 error:
1092         if (status < 0)
1093                 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
1094
1095 error2:
1096         return status;
1097 }
1098
1099 static int HI_Command(struct drxk_state *state, u16 cmd, u16 *pResult)
1100 {
1101         int status;
1102         bool powerdown_cmd;
1103
1104         dprintk(1, "\n");
1105
1106         /* Write command */
1107         status = write16(state, SIO_HI_RA_RAM_CMD__A, cmd);
1108         if (status < 0)
1109                 goto error;
1110         if (cmd == SIO_HI_RA_RAM_CMD_RESET)
1111                 msleep(1);
1112
1113         powerdown_cmd =
1114             (bool) ((cmd == SIO_HI_RA_RAM_CMD_CONFIG) &&
1115                     ((state->m_HICfgCtrl) &
1116                      SIO_HI_RA_RAM_PAR_5_CFG_SLEEP__M) ==
1117                     SIO_HI_RA_RAM_PAR_5_CFG_SLEEP_ZZZ);
1118         if (powerdown_cmd == false) {
1119                 /* Wait until command rdy */
1120                 u32 retryCount = 0;
1121                 u16 waitCmd;
1122
1123                 do {
1124                         msleep(1);
1125                         retryCount += 1;
1126                         status = read16(state, SIO_HI_RA_RAM_CMD__A,
1127                                           &waitCmd);
1128                 } while ((status < 0) && (retryCount < DRXK_MAX_RETRIES)
1129                          && (waitCmd != 0));
1130                 if (status < 0)
1131                         goto error;
1132                 status = read16(state, SIO_HI_RA_RAM_RES__A, pResult);
1133         }
1134 error:
1135         if (status < 0)
1136                 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
1137
1138         return status;
1139 }
1140
1141 static int HI_CfgCommand(struct drxk_state *state)
1142 {
1143         int status;
1144
1145         dprintk(1, "\n");
1146
1147         mutex_lock(&state->mutex);
1148
1149         status = write16(state, SIO_HI_RA_RAM_PAR_6__A, state->m_HICfgTimeout);
1150         if (status < 0)
1151                 goto error;
1152         status = write16(state, SIO_HI_RA_RAM_PAR_5__A, state->m_HICfgCtrl);
1153         if (status < 0)
1154                 goto error;
1155         status = write16(state, SIO_HI_RA_RAM_PAR_4__A, state->m_HICfgWakeUpKey);
1156         if (status < 0)
1157                 goto error;
1158         status = write16(state, SIO_HI_RA_RAM_PAR_3__A, state->m_HICfgBridgeDelay);
1159         if (status < 0)
1160                 goto error;
1161         status = write16(state, SIO_HI_RA_RAM_PAR_2__A, state->m_HICfgTimingDiv);
1162         if (status < 0)
1163                 goto error;
1164         status = write16(state, SIO_HI_RA_RAM_PAR_1__A, SIO_HI_RA_RAM_PAR_1_PAR1_SEC_KEY);
1165         if (status < 0)
1166                 goto error;
1167         status = HI_Command(state, SIO_HI_RA_RAM_CMD_CONFIG, 0);
1168         if (status < 0)
1169                 goto error;
1170
1171         state->m_HICfgCtrl &= ~SIO_HI_RA_RAM_PAR_5_CFG_SLEEP_ZZZ;
1172 error:
1173         mutex_unlock(&state->mutex);
1174         if (status < 0)
1175                 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
1176         return status;
1177 }
1178
1179 static int InitHI(struct drxk_state *state)
1180 {
1181         dprintk(1, "\n");
1182
1183         state->m_HICfgWakeUpKey = (state->demod_address << 1);
1184         state->m_HICfgTimeout = 0x96FF;
1185         /* port/bridge/power down ctrl */
1186         state->m_HICfgCtrl = SIO_HI_RA_RAM_PAR_5_CFG_SLV0_SLAVE;
1187
1188         return HI_CfgCommand(state);
1189 }
1190
1191 static int MPEGTSConfigurePins(struct drxk_state *state, bool mpegEnable)
1192 {
1193         int status = -1;
1194         u16 sioPdrMclkCfg = 0;
1195         u16 sioPdrMdxCfg = 0;
1196
1197         dprintk(1, "\n");
1198
1199         /* stop lock indicator process */
1200         status = write16(state, SCU_RAM_GPIO__A, SCU_RAM_GPIO_HW_LOCK_IND_DISABLE);
1201         if (status < 0)
1202                 goto error;
1203
1204         /*  MPEG TS pad configuration */
1205         status = write16(state, SIO_TOP_COMM_KEY__A, 0xFABA);
1206         if (status < 0)
1207                 goto error;
1208
1209         if (mpegEnable == false) {
1210                 /*  Set MPEG TS pads to inputmode */
1211                 status = write16(state, SIO_PDR_MSTRT_CFG__A, 0x0000);
1212                 if (status < 0)
1213                         goto error;
1214                 status = write16(state, SIO_PDR_MERR_CFG__A, 0x0000);
1215                 if (status < 0)
1216                         goto error;
1217                 status = write16(state, SIO_PDR_MCLK_CFG__A, 0x0000);
1218                 if (status < 0)
1219                         goto error;
1220                 status = write16(state, SIO_PDR_MVAL_CFG__A, 0x0000);
1221                 if (status < 0)
1222                         goto error;
1223                 status = write16(state, SIO_PDR_MD0_CFG__A, 0x0000);
1224                 if (status < 0)
1225                         goto error;
1226                 status = write16(state, SIO_PDR_MD1_CFG__A, 0x0000);
1227                 if (status < 0)
1228                         goto error;
1229                 status = write16(state, SIO_PDR_MD2_CFG__A, 0x0000);
1230                 if (status < 0)
1231                         goto error;
1232                 status = write16(state, SIO_PDR_MD3_CFG__A, 0x0000);
1233                 if (status < 0)
1234                         goto error;
1235                 status = write16(state, SIO_PDR_MD4_CFG__A, 0x0000);
1236                 if (status < 0)
1237                         goto error;
1238                 status = write16(state, SIO_PDR_MD5_CFG__A, 0x0000);
1239                 if (status < 0)
1240                         goto error;
1241                 status = write16(state, SIO_PDR_MD6_CFG__A, 0x0000);
1242                 if (status < 0)
1243                         goto error;
1244                 status = write16(state, SIO_PDR_MD7_CFG__A, 0x0000);
1245                 if (status < 0)
1246                         goto error;
1247         } else {
1248                 /* Enable MPEG output */
1249                 sioPdrMdxCfg =
1250                         ((state->m_TSDataStrength <<
1251                         SIO_PDR_MD0_CFG_DRIVE__B) | 0x0003);
1252                 sioPdrMclkCfg = ((state->m_TSClockkStrength <<
1253                                         SIO_PDR_MCLK_CFG_DRIVE__B) |
1254                                         0x0003);
1255
1256                 status = write16(state, SIO_PDR_MSTRT_CFG__A, sioPdrMdxCfg);
1257                 if (status < 0)
1258                         goto error;
1259                 status = write16(state, SIO_PDR_MERR_CFG__A, 0x0000);   /* Disable */
1260                 if (status < 0)
1261                         goto error;
1262                 status = write16(state, SIO_PDR_MVAL_CFG__A, 0x0000);   /* Disable */
1263                 if (status < 0)
1264                         goto error;
1265                 if (state->m_enableParallel == true) {
1266                         /* paralel -> enable MD1 to MD7 */
1267                         status = write16(state, SIO_PDR_MD1_CFG__A, sioPdrMdxCfg);
1268                         if (status < 0)
1269                                 goto error;
1270                         status = write16(state, SIO_PDR_MD2_CFG__A, sioPdrMdxCfg);
1271                         if (status < 0)
1272                                 goto error;
1273                         status = write16(state, SIO_PDR_MD3_CFG__A, sioPdrMdxCfg);
1274                         if (status < 0)
1275                                 goto error;
1276                         status = write16(state, SIO_PDR_MD4_CFG__A, sioPdrMdxCfg);
1277                         if (status < 0)
1278                                 goto error;
1279                         status = write16(state, SIO_PDR_MD5_CFG__A, sioPdrMdxCfg);
1280                         if (status < 0)
1281                                 goto error;
1282                         status = write16(state, SIO_PDR_MD6_CFG__A, sioPdrMdxCfg);
1283                         if (status < 0)
1284                                 goto error;
1285                         status = write16(state, SIO_PDR_MD7_CFG__A, sioPdrMdxCfg);
1286                         if (status < 0)
1287                                 goto error;
1288                 } else {
1289                         sioPdrMdxCfg = ((state->m_TSDataStrength <<
1290                                                 SIO_PDR_MD0_CFG_DRIVE__B)
1291                                         | 0x0003);
1292                         /* serial -> disable MD1 to MD7 */
1293                         status = write16(state, SIO_PDR_MD1_CFG__A, 0x0000);
1294                         if (status < 0)
1295                                 goto error;
1296                         status = write16(state, SIO_PDR_MD2_CFG__A, 0x0000);
1297                         if (status < 0)
1298                                 goto error;
1299                         status = write16(state, SIO_PDR_MD3_CFG__A, 0x0000);
1300                         if (status < 0)
1301                                 goto error;
1302                         status = write16(state, SIO_PDR_MD4_CFG__A, 0x0000);
1303                         if (status < 0)
1304                                 goto error;
1305                         status = write16(state, SIO_PDR_MD5_CFG__A, 0x0000);
1306                         if (status < 0)
1307                                 goto error;
1308                         status = write16(state, SIO_PDR_MD6_CFG__A, 0x0000);
1309                         if (status < 0)
1310                                 goto error;
1311                         status = write16(state, SIO_PDR_MD7_CFG__A, 0x0000);
1312                         if (status < 0)
1313                                 goto error;
1314                 }
1315                 status = write16(state, SIO_PDR_MCLK_CFG__A, sioPdrMclkCfg);
1316                 if (status < 0)
1317                         goto error;
1318                 status = write16(state, SIO_PDR_MD0_CFG__A, sioPdrMdxCfg);
1319                 if (status < 0)
1320                         goto error;
1321         }
1322         /*  Enable MB output over MPEG pads and ctl input */
1323         status = write16(state, SIO_PDR_MON_CFG__A, 0x0000);
1324         if (status < 0)
1325                 goto error;
1326         /*  Write nomagic word to enable pdr reg write */
1327         status = write16(state, SIO_TOP_COMM_KEY__A, 0x0000);
1328 error:
1329         if (status < 0)
1330                 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
1331         return status;
1332 }
1333
1334 static int MPEGTSDisable(struct drxk_state *state)
1335 {
1336         dprintk(1, "\n");
1337
1338         return MPEGTSConfigurePins(state, false);
1339 }
1340
1341 static int BLChainCmd(struct drxk_state *state,
1342                       u16 romOffset, u16 nrOfElements, u32 timeOut)
1343 {
1344         u16 blStatus = 0;
1345         int status;
1346         unsigned long end;
1347
1348         dprintk(1, "\n");
1349         mutex_lock(&state->mutex);
1350         status = write16(state, SIO_BL_MODE__A, SIO_BL_MODE_CHAIN);
1351         if (status < 0)
1352                 goto error;
1353         status = write16(state, SIO_BL_CHAIN_ADDR__A, romOffset);
1354         if (status < 0)
1355                 goto error;
1356         status = write16(state, SIO_BL_CHAIN_LEN__A, nrOfElements);
1357         if (status < 0)
1358                 goto error;
1359         status = write16(state, SIO_BL_ENABLE__A, SIO_BL_ENABLE_ON);
1360         if (status < 0)
1361                 goto error;
1362
1363         end = jiffies + msecs_to_jiffies(timeOut);
1364         do {
1365                 msleep(1);
1366                 status = read16(state, SIO_BL_STATUS__A, &blStatus);
1367                 if (status < 0)
1368                         goto error;
1369         } while ((blStatus == 0x1) &&
1370                         ((time_is_after_jiffies(end))));
1371
1372         if (blStatus == 0x1) {
1373                 printk(KERN_ERR "drxk: SIO not ready\n");
1374                 status = -EINVAL;
1375                 goto error2;
1376         }
1377 error:
1378         if (status < 0)
1379                 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
1380 error2:
1381         mutex_unlock(&state->mutex);
1382         return status;
1383 }
1384
1385
1386 static int DownloadMicrocode(struct drxk_state *state,
1387                              const u8 pMCImage[], u32 Length)
1388 {
1389         const u8 *pSrc = pMCImage;
1390         u16 Flags;
1391         u16 Drain;
1392         u32 Address;
1393         u16 nBlocks;
1394         u16 BlockSize;
1395         u16 BlockCRC;
1396         u32 offset = 0;
1397         u32 i;
1398         int status = 0;
1399
1400         dprintk(1, "\n");
1401
1402         /* down the drain (we don care about MAGIC_WORD) */
1403         Drain = (pSrc[0] << 8) | pSrc[1];
1404         pSrc += sizeof(u16);
1405         offset += sizeof(u16);
1406         nBlocks = (pSrc[0] << 8) | pSrc[1];
1407         pSrc += sizeof(u16);
1408         offset += sizeof(u16);
1409
1410         for (i = 0; i < nBlocks; i += 1) {
1411                 Address = (pSrc[0] << 24) | (pSrc[1] << 16) |
1412                     (pSrc[2] << 8) | pSrc[3];
1413                 pSrc += sizeof(u32);
1414                 offset += sizeof(u32);
1415
1416                 BlockSize = ((pSrc[0] << 8) | pSrc[1]) * sizeof(u16);
1417                 pSrc += sizeof(u16);
1418                 offset += sizeof(u16);
1419
1420                 Flags = (pSrc[0] << 8) | pSrc[1];
1421                 pSrc += sizeof(u16);
1422                 offset += sizeof(u16);
1423
1424                 BlockCRC = (pSrc[0] << 8) | pSrc[1];
1425                 pSrc += sizeof(u16);
1426                 offset += sizeof(u16);
1427
1428                 if (offset + BlockSize > Length) {
1429                         printk(KERN_ERR "drxk: Firmware is corrupted.\n");
1430                         return -EINVAL;
1431                 }
1432
1433                 status = write_block(state, Address, BlockSize, pSrc);
1434                 if (status < 0) {
1435                         printk(KERN_ERR "drxk: Error %d while loading firmware\n", status);
1436                         break;
1437                 }
1438                 pSrc += BlockSize;
1439                 offset += BlockSize;
1440         }
1441         return status;
1442 }
1443
1444 static int DVBTEnableOFDMTokenRing(struct drxk_state *state, bool enable)
1445 {
1446         int status;
1447         u16 data = 0;
1448         u16 desiredCtrl = SIO_OFDM_SH_OFDM_RING_ENABLE_ON;
1449         u16 desiredStatus = SIO_OFDM_SH_OFDM_RING_STATUS_ENABLED;
1450         unsigned long end;
1451
1452         dprintk(1, "\n");
1453
1454         if (enable == false) {
1455                 desiredCtrl = SIO_OFDM_SH_OFDM_RING_ENABLE_OFF;
1456                 desiredStatus = SIO_OFDM_SH_OFDM_RING_STATUS_DOWN;
1457         }
1458
1459         status = read16(state, SIO_OFDM_SH_OFDM_RING_STATUS__A, &data);
1460         if (status >= 0 && data == desiredStatus) {
1461                 /* tokenring already has correct status */
1462                 return status;
1463         }
1464         /* Disable/enable dvbt tokenring bridge   */
1465         status = write16(state, SIO_OFDM_SH_OFDM_RING_ENABLE__A, desiredCtrl);
1466
1467         end = jiffies + msecs_to_jiffies(DRXK_OFDM_TR_SHUTDOWN_TIMEOUT);
1468         do {
1469                 status = read16(state, SIO_OFDM_SH_OFDM_RING_STATUS__A, &data);
1470                 if ((status >= 0 && data == desiredStatus) || time_is_after_jiffies(end))
1471                         break;
1472                 msleep(1);
1473         } while (1);
1474         if (data != desiredStatus) {
1475                 printk(KERN_ERR "drxk: SIO not ready\n");
1476                 return -EINVAL;
1477         }
1478         return status;
1479 }
1480
1481 static int MPEGTSStop(struct drxk_state *state)
1482 {
1483         int status = 0;
1484         u16 fecOcSncMode = 0;
1485         u16 fecOcIprMode = 0;
1486
1487         dprintk(1, "\n");
1488
1489         /* Gracefull shutdown (byte boundaries) */
1490         status = read16(state, FEC_OC_SNC_MODE__A, &fecOcSncMode);
1491         if (status < 0)
1492                 goto error;
1493         fecOcSncMode |= FEC_OC_SNC_MODE_SHUTDOWN__M;
1494         status = write16(state, FEC_OC_SNC_MODE__A, fecOcSncMode);
1495         if (status < 0)
1496                 goto error;
1497
1498         /* Suppress MCLK during absence of data */
1499         status = read16(state, FEC_OC_IPR_MODE__A, &fecOcIprMode);
1500         if (status < 0)
1501                 goto error;
1502         fecOcIprMode |= FEC_OC_IPR_MODE_MCLK_DIS_DAT_ABS__M;
1503         status = write16(state, FEC_OC_IPR_MODE__A, fecOcIprMode);
1504
1505 error:
1506         if (status < 0)
1507                 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
1508
1509         return status;
1510 }
1511
1512 static int scu_command(struct drxk_state *state,
1513                        u16 cmd, u8 parameterLen,
1514                        u16 *parameter, u8 resultLen, u16 *result)
1515 {
1516 #if (SCU_RAM_PARAM_0__A - SCU_RAM_PARAM_15__A) != 15
1517 #error DRXK register mapping no longer compatible with this routine!
1518 #endif
1519         u16 curCmd = 0;
1520         int status = -EINVAL;
1521         unsigned long end;
1522         u8 buffer[34];
1523         int cnt = 0, ii;
1524
1525         dprintk(1, "\n");
1526
1527         if ((cmd == 0) || ((parameterLen > 0) && (parameter == NULL)) ||
1528             ((resultLen > 0) && (result == NULL)))
1529                 goto error;
1530
1531         mutex_lock(&state->mutex);
1532
1533         /* assume that the command register is ready
1534                 since it is checked afterwards */
1535         for (ii = parameterLen - 1; ii >= 0; ii -= 1) {
1536                 buffer[cnt++] = (parameter[ii] & 0xFF);
1537                 buffer[cnt++] = ((parameter[ii] >> 8) & 0xFF);
1538         }
1539         buffer[cnt++] = (cmd & 0xFF);
1540         buffer[cnt++] = ((cmd >> 8) & 0xFF);
1541
1542         write_block(state, SCU_RAM_PARAM_0__A -
1543                         (parameterLen - 1), cnt, buffer);
1544         /* Wait until SCU has processed command */
1545         end = jiffies + msecs_to_jiffies(DRXK_MAX_WAITTIME);
1546         do {
1547                 msleep(1);
1548                 status = read16(state, SCU_RAM_COMMAND__A, &curCmd);
1549                 if (status < 0)
1550                         goto error;
1551         } while (!(curCmd == DRX_SCU_READY) && (time_is_after_jiffies(end)));
1552         if (curCmd != DRX_SCU_READY) {
1553                 printk(KERN_ERR "drxk: SCU not ready\n");
1554                 status = -EIO;
1555                 goto error2;
1556         }
1557         /* read results */
1558         if ((resultLen > 0) && (result != NULL)) {
1559                 s16 err;
1560                 int ii;
1561
1562                 for (ii = resultLen - 1; ii >= 0; ii -= 1) {
1563                         status = read16(state, SCU_RAM_PARAM_0__A - ii, &result[ii]);
1564                         if (status < 0)
1565                                 goto error;
1566                 }
1567
1568                 /* Check if an error was reported by SCU */
1569                 err = (s16)result[0];
1570
1571                 /* check a few fixed error codes */
1572                 if (err == SCU_RESULT_UNKSTD) {
1573                         printk(KERN_ERR "drxk: SCU_RESULT_UNKSTD\n");
1574                         status = -EINVAL;
1575                         goto error2;
1576                 } else if (err == SCU_RESULT_UNKCMD) {
1577                         printk(KERN_ERR "drxk: SCU_RESULT_UNKCMD\n");
1578                         status = -EINVAL;
1579                         goto error2;
1580                 } else if (err < 0) {
1581                         /*
1582                          * here it is assumed that a nagative result means
1583                          *  error, and positive no error
1584                          */
1585                         printk(KERN_ERR "drxk: %s ERROR: %d\n", __func__, err);
1586                         status = -EINVAL;
1587                         goto error2;
1588                 }
1589         }
1590
1591 error:
1592         if (status < 0)
1593                 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
1594
1595 error2:
1596         mutex_unlock(&state->mutex);
1597         return status;
1598 }
1599
1600 static int SetIqmAf(struct drxk_state *state, bool active)
1601 {
1602         u16 data = 0;
1603         int status;
1604
1605         dprintk(1, "\n");
1606
1607         /* Configure IQM */
1608         status = read16(state, IQM_AF_STDBY__A, &data);
1609         if (status < 0)
1610                 goto error;
1611
1612         if (!active) {
1613                 data |= (IQM_AF_STDBY_STDBY_ADC_STANDBY
1614                                 | IQM_AF_STDBY_STDBY_AMP_STANDBY
1615                                 | IQM_AF_STDBY_STDBY_PD_STANDBY
1616                                 | IQM_AF_STDBY_STDBY_TAGC_IF_STANDBY
1617                                 | IQM_AF_STDBY_STDBY_TAGC_RF_STANDBY);
1618         } else {
1619                 data &= ((~IQM_AF_STDBY_STDBY_ADC_STANDBY)
1620                                 & (~IQM_AF_STDBY_STDBY_AMP_STANDBY)
1621                                 & (~IQM_AF_STDBY_STDBY_PD_STANDBY)
1622                                 & (~IQM_AF_STDBY_STDBY_TAGC_IF_STANDBY)
1623                                 & (~IQM_AF_STDBY_STDBY_TAGC_RF_STANDBY)
1624                         );
1625         }
1626         status = write16(state, IQM_AF_STDBY__A, data);
1627
1628 error:
1629         if (status < 0)
1630                 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
1631         return status;
1632 }
1633
1634 static int CtrlPowerMode(struct drxk_state *state, enum DRXPowerMode *mode)
1635 {
1636         int status = 0;
1637         u16 sioCcPwdMode = 0;
1638
1639         dprintk(1, "\n");
1640
1641         /* Check arguments */
1642         if (mode == NULL)
1643                 return -EINVAL;
1644
1645         switch (*mode) {
1646         case DRX_POWER_UP:
1647                 sioCcPwdMode = SIO_CC_PWD_MODE_LEVEL_NONE;
1648                 break;
1649         case DRXK_POWER_DOWN_OFDM:
1650                 sioCcPwdMode = SIO_CC_PWD_MODE_LEVEL_OFDM;
1651                 break;
1652         case DRXK_POWER_DOWN_CORE:
1653                 sioCcPwdMode = SIO_CC_PWD_MODE_LEVEL_CLOCK;
1654                 break;
1655         case DRXK_POWER_DOWN_PLL:
1656                 sioCcPwdMode = SIO_CC_PWD_MODE_LEVEL_PLL;
1657                 break;
1658         case DRX_POWER_DOWN:
1659                 sioCcPwdMode = SIO_CC_PWD_MODE_LEVEL_OSC;
1660                 break;
1661         default:
1662                 /* Unknow sleep mode */
1663                 return -EINVAL;
1664                 break;
1665         }
1666
1667         /* If already in requested power mode, do nothing */
1668         if (state->m_currentPowerMode == *mode)
1669                 return 0;
1670
1671         /* For next steps make sure to start from DRX_POWER_UP mode */
1672         if (state->m_currentPowerMode != DRX_POWER_UP) {
1673                 status = PowerUpDevice(state);
1674                 if (status < 0)
1675                         goto error;
1676                 status = DVBTEnableOFDMTokenRing(state, true);
1677                 if (status < 0)
1678                         goto error;
1679         }
1680
1681         if (*mode == DRX_POWER_UP) {
1682                 /* Restore analog & pin configuartion */
1683         } else {
1684                 /* Power down to requested mode */
1685                 /* Backup some register settings */
1686                 /* Set pins with possible pull-ups connected
1687                    to them in input mode */
1688                 /* Analog power down */
1689                 /* ADC power down */
1690                 /* Power down device */
1691                 /* stop all comm_exec */
1692                 /* Stop and power down previous standard */
1693                 switch (state->m_OperationMode) {
1694                 case OM_DVBT:
1695                         status = MPEGTSStop(state);
1696                         if (status < 0)
1697                                 goto error;
1698                         status = PowerDownDVBT(state, false);
1699                         if (status < 0)
1700                                 goto error;
1701                         break;
1702                 case OM_QAM_ITU_A:
1703                 case OM_QAM_ITU_C:
1704                         status = MPEGTSStop(state);
1705                         if (status < 0)
1706                                 goto error;
1707                         status = PowerDownQAM(state);
1708                         if (status < 0)
1709                                 goto error;
1710                         break;
1711                 default:
1712                         break;
1713                 }
1714                 status = DVBTEnableOFDMTokenRing(state, false);
1715                 if (status < 0)
1716                         goto error;
1717                 status = write16(state, SIO_CC_PWD_MODE__A, sioCcPwdMode);
1718                 if (status < 0)
1719                         goto error;
1720                 status = write16(state, SIO_CC_UPDATE__A, SIO_CC_UPDATE_KEY);
1721                 if (status < 0)
1722                         goto error;
1723
1724                 if (*mode != DRXK_POWER_DOWN_OFDM) {
1725                         state->m_HICfgCtrl |=
1726                                 SIO_HI_RA_RAM_PAR_5_CFG_SLEEP_ZZZ;
1727                         status = HI_CfgCommand(state);
1728                         if (status < 0)
1729                                 goto error;
1730                 }
1731         }
1732         state->m_currentPowerMode = *mode;
1733
1734 error:
1735         if (status < 0)
1736                 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
1737
1738         return status;
1739 }
1740
1741 static int PowerDownDVBT(struct drxk_state *state, bool setPowerMode)
1742 {
1743         enum DRXPowerMode powerMode = DRXK_POWER_DOWN_OFDM;
1744         u16 cmdResult = 0;
1745         u16 data = 0;
1746         int status;
1747
1748         dprintk(1, "\n");
1749
1750         status = read16(state, SCU_COMM_EXEC__A, &data);
1751         if (status < 0)
1752                 goto error;
1753         if (data == SCU_COMM_EXEC_ACTIVE) {
1754                 /* Send OFDM stop command */
1755                 status = scu_command(state, SCU_RAM_COMMAND_STANDARD_OFDM | SCU_RAM_COMMAND_CMD_DEMOD_STOP, 0, NULL, 1, &cmdResult);
1756                 if (status < 0)
1757                         goto error;
1758                 /* Send OFDM reset command */
1759                 status = scu_command(state, SCU_RAM_COMMAND_STANDARD_OFDM | SCU_RAM_COMMAND_CMD_DEMOD_RESET, 0, NULL, 1, &cmdResult);
1760                 if (status < 0)
1761                         goto error;
1762         }
1763
1764         /* Reset datapath for OFDM, processors first */
1765         status = write16(state, OFDM_SC_COMM_EXEC__A, OFDM_SC_COMM_EXEC_STOP);
1766         if (status < 0)
1767                 goto error;
1768         status = write16(state, OFDM_LC_COMM_EXEC__A, OFDM_LC_COMM_EXEC_STOP);
1769         if (status < 0)
1770                 goto error;
1771         status = write16(state, IQM_COMM_EXEC__A, IQM_COMM_EXEC_B_STOP);
1772         if (status < 0)
1773                 goto error;
1774
1775         /* powerdown AFE                   */
1776         status = SetIqmAf(state, false);
1777         if (status < 0)
1778                 goto error;
1779
1780         /* powerdown to OFDM mode          */
1781         if (setPowerMode) {
1782                 status = CtrlPowerMode(state, &powerMode);
1783                 if (status < 0)
1784                         goto error;
1785         }
1786 error:
1787         if (status < 0)
1788                 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
1789         return status;
1790 }
1791
1792 static int SetOperationMode(struct drxk_state *state,
1793                             enum OperationMode oMode)
1794 {
1795         int status = 0;
1796
1797         dprintk(1, "\n");
1798         /*
1799            Stop and power down previous standard
1800            TODO investigate total power down instead of partial
1801            power down depending on "previous" standard.
1802          */
1803
1804         /* disable HW lock indicator */
1805         status = write16(state, SCU_RAM_GPIO__A, SCU_RAM_GPIO_HW_LOCK_IND_DISABLE);
1806         if (status < 0)
1807                 goto error;
1808
1809         /* Device is already at the required mode */
1810         if (state->m_OperationMode == oMode)
1811                 return 0;
1812
1813         switch (state->m_OperationMode) {
1814                 /* OM_NONE was added for start up */
1815         case OM_NONE:
1816                 break;
1817         case OM_DVBT:
1818                 status = MPEGTSStop(state);
1819                 if (status < 0)
1820                         goto error;
1821                 status = PowerDownDVBT(state, true);
1822                 if (status < 0)
1823                         goto error;
1824                 state->m_OperationMode = OM_NONE;
1825                 break;
1826         case OM_QAM_ITU_A:      /* fallthrough */
1827         case OM_QAM_ITU_C:
1828                 status = MPEGTSStop(state);
1829                 if (status < 0)
1830                         goto error;
1831                 status = PowerDownQAM(state);
1832                 if (status < 0)
1833                         goto error;
1834                 state->m_OperationMode = OM_NONE;
1835                 break;
1836         case OM_QAM_ITU_B:
1837         default:
1838                 status = -EINVAL;
1839                 goto error;
1840         }
1841
1842         /*
1843                 Power up new standard
1844                 */
1845         switch (oMode) {
1846         case OM_DVBT:
1847                 state->m_OperationMode = oMode;
1848                 status = SetDVBTStandard(state, oMode);
1849                 if (status < 0)
1850                         goto error;
1851                 break;
1852         case OM_QAM_ITU_A:      /* fallthrough */
1853         case OM_QAM_ITU_C:
1854                 state->m_OperationMode = oMode;
1855                 status = SetQAMStandard(state, oMode);
1856                 if (status < 0)
1857                         goto error;
1858                 break;
1859         case OM_QAM_ITU_B:
1860         default:
1861                 status = -EINVAL;
1862         }
1863 error:
1864         if (status < 0)
1865                 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
1866         return status;
1867 }
1868
1869 static int Start(struct drxk_state *state, s32 offsetFreq,
1870                  s32 IntermediateFrequency)
1871 {
1872         int status = -EINVAL;
1873
1874         u16 IFreqkHz;
1875         s32 OffsetkHz = offsetFreq / 1000;
1876
1877         dprintk(1, "\n");
1878         if (state->m_DrxkState != DRXK_STOPPED &&
1879                 state->m_DrxkState != DRXK_DTV_STARTED)
1880                 goto error;
1881
1882         state->m_bMirrorFreqSpect = (state->param.inversion == INVERSION_ON);
1883
1884         if (IntermediateFrequency < 0) {
1885                 state->m_bMirrorFreqSpect = !state->m_bMirrorFreqSpect;
1886                 IntermediateFrequency = -IntermediateFrequency;
1887         }
1888
1889         switch (state->m_OperationMode) {
1890         case OM_QAM_ITU_A:
1891         case OM_QAM_ITU_C:
1892                 IFreqkHz = (IntermediateFrequency / 1000);
1893                 status = SetQAM(state, IFreqkHz, OffsetkHz);
1894                 if (status < 0)
1895                         goto error;
1896                 state->m_DrxkState = DRXK_DTV_STARTED;
1897                 break;
1898         case OM_DVBT:
1899                 IFreqkHz = (IntermediateFrequency / 1000);
1900                 status = MPEGTSStop(state);
1901                 if (status < 0)
1902                         goto error;
1903                 status = SetDVBT(state, IFreqkHz, OffsetkHz);
1904                 if (status < 0)
1905                         goto error;
1906                 status = DVBTStart(state);
1907                 if (status < 0)
1908                         goto error;
1909                 state->m_DrxkState = DRXK_DTV_STARTED;
1910                 break;
1911         default:
1912                 break;
1913         }
1914 error:
1915         if (status < 0)
1916                 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
1917         return status;
1918 }
1919
1920 static int ShutDown(struct drxk_state *state)
1921 {
1922         dprintk(1, "\n");
1923
1924         MPEGTSStop(state);
1925         return 0;
1926 }
1927
1928 static int GetLockStatus(struct drxk_state *state, u32 *pLockStatus,
1929                          u32 Time)
1930 {
1931         int status = -EINVAL;
1932
1933         dprintk(1, "\n");
1934
1935         if (pLockStatus == NULL)
1936                 goto error;
1937
1938         *pLockStatus = NOT_LOCKED;
1939
1940         /* define the SCU command code */
1941         switch (state->m_OperationMode) {
1942         case OM_QAM_ITU_A:
1943         case OM_QAM_ITU_B:
1944         case OM_QAM_ITU_C:
1945                 status = GetQAMLockStatus(state, pLockStatus);
1946                 break;
1947         case OM_DVBT:
1948                 status = GetDVBTLockStatus(state, pLockStatus);
1949                 break;
1950         default:
1951                 break;
1952         }
1953 error:
1954         if (status < 0)
1955                 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
1956         return status;
1957 }
1958
1959 static int MPEGTSStart(struct drxk_state *state)
1960 {
1961         int status;
1962
1963         u16 fecOcSncMode = 0;
1964
1965         /* Allow OC to sync again */
1966         status = read16(state, FEC_OC_SNC_MODE__A, &fecOcSncMode);
1967         if (status < 0)
1968                 goto error;
1969         fecOcSncMode &= ~FEC_OC_SNC_MODE_SHUTDOWN__M;
1970         status = write16(state, FEC_OC_SNC_MODE__A, fecOcSncMode);
1971         if (status < 0)
1972                 goto error;
1973         status = write16(state, FEC_OC_SNC_UNLOCK__A, 1);
1974 error:
1975         if (status < 0)
1976                 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
1977         return status;
1978 }
1979
1980 static int MPEGTSDtoInit(struct drxk_state *state)
1981 {
1982         int status;
1983
1984         dprintk(1, "\n");
1985
1986         /* Rate integration settings */
1987         status = write16(state, FEC_OC_RCN_CTL_STEP_LO__A, 0x0000);
1988         if (status < 0)
1989                 goto error;
1990         status = write16(state, FEC_OC_RCN_CTL_STEP_HI__A, 0x000C);
1991         if (status < 0)
1992                 goto error;
1993         status = write16(state, FEC_OC_RCN_GAIN__A, 0x000A);
1994         if (status < 0)
1995                 goto error;
1996         status = write16(state, FEC_OC_AVR_PARM_A__A, 0x0008);
1997         if (status < 0)
1998                 goto error;
1999         status = write16(state, FEC_OC_AVR_PARM_B__A, 0x0006);
2000         if (status < 0)
2001                 goto error;
2002         status = write16(state, FEC_OC_TMD_HI_MARGIN__A, 0x0680);
2003         if (status < 0)
2004                 goto error;
2005         status = write16(state, FEC_OC_TMD_LO_MARGIN__A, 0x0080);
2006         if (status < 0)
2007                 goto error;
2008         status = write16(state, FEC_OC_TMD_COUNT__A, 0x03F4);
2009         if (status < 0)
2010                 goto error;
2011
2012         /* Additional configuration */
2013         status = write16(state, FEC_OC_OCR_INVERT__A, 0);
2014         if (status < 0)
2015                 goto error;
2016         status = write16(state, FEC_OC_SNC_LWM__A, 2);
2017         if (status < 0)
2018                 goto error;
2019         status = write16(state, FEC_OC_SNC_HWM__A, 12);
2020 error:
2021         if (status < 0)
2022                 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
2023
2024         return status;
2025 }
2026
2027 static int MPEGTSDtoSetup(struct drxk_state *state,
2028                           enum OperationMode oMode)
2029 {
2030         int status;
2031
2032         u16 fecOcRegMode = 0;   /* FEC_OC_MODE       register value */
2033         u16 fecOcRegIprMode = 0;        /* FEC_OC_IPR_MODE   register value */
2034         u16 fecOcDtoMode = 0;   /* FEC_OC_IPR_INVERT register value */
2035         u16 fecOcFctMode = 0;   /* FEC_OC_IPR_INVERT register value */
2036         u16 fecOcDtoPeriod = 2; /* FEC_OC_IPR_INVERT register value */
2037         u16 fecOcDtoBurstLen = 188;     /* FEC_OC_IPR_INVERT register value */
2038         u32 fecOcRcnCtlRate = 0;        /* FEC_OC_IPR_INVERT register value */
2039         u16 fecOcTmdMode = 0;
2040         u16 fecOcTmdIntUpdRate = 0;
2041         u32 maxBitRate = 0;
2042         bool staticCLK = false;
2043
2044         dprintk(1, "\n");
2045
2046         /* Check insertion of the Reed-Solomon parity bytes */
2047         status = read16(state, FEC_OC_MODE__A, &fecOcRegMode);
2048         if (status < 0)
2049                 goto error;
2050         status = read16(state, FEC_OC_IPR_MODE__A, &fecOcRegIprMode);
2051         if (status < 0)
2052                 goto error;
2053         fecOcRegMode &= (~FEC_OC_MODE_PARITY__M);
2054         fecOcRegIprMode &= (~FEC_OC_IPR_MODE_MVAL_DIS_PAR__M);
2055         if (state->m_insertRSByte == true) {
2056                 /* enable parity symbol forward */
2057                 fecOcRegMode |= FEC_OC_MODE_PARITY__M;
2058                 /* MVAL disable during parity bytes */
2059                 fecOcRegIprMode |= FEC_OC_IPR_MODE_MVAL_DIS_PAR__M;
2060                 /* TS burst length to 204 */
2061                 fecOcDtoBurstLen = 204;
2062         }
2063
2064         /* Check serial or parrallel output */
2065         fecOcRegIprMode &= (~(FEC_OC_IPR_MODE_SERIAL__M));
2066         if (state->m_enableParallel == false) {
2067                 /* MPEG data output is serial -> set ipr_mode[0] */
2068                 fecOcRegIprMode |= FEC_OC_IPR_MODE_SERIAL__M;
2069         }
2070
2071         switch (oMode) {
2072         case OM_DVBT:
2073                 maxBitRate = state->m_DVBTBitrate;
2074                 fecOcTmdMode = 3;
2075                 fecOcRcnCtlRate = 0xC00000;
2076                 staticCLK = state->m_DVBTStaticCLK;
2077                 break;
2078         case OM_QAM_ITU_A:      /* fallthrough */
2079         case OM_QAM_ITU_C:
2080                 fecOcTmdMode = 0x0004;
2081                 fecOcRcnCtlRate = 0xD2B4EE;     /* good for >63 Mb/s */
2082                 maxBitRate = state->m_DVBCBitrate;
2083                 staticCLK = state->m_DVBCStaticCLK;
2084                 break;
2085         default:
2086                 status = -EINVAL;
2087         }               /* switch (standard) */
2088         if (status < 0)
2089                 goto error;
2090
2091         /* Configure DTO's */
2092         if (staticCLK) {
2093                 u32 bitRate = 0;
2094
2095                 /* Rational DTO for MCLK source (static MCLK rate),
2096                         Dynamic DTO for optimal grouping
2097                         (avoid intra-packet gaps),
2098                         DTO offset enable to sync TS burst with MSTRT */
2099                 fecOcDtoMode = (FEC_OC_DTO_MODE_DYNAMIC__M |
2100                                 FEC_OC_DTO_MODE_OFFSET_ENABLE__M);
2101                 fecOcFctMode = (FEC_OC_FCT_MODE_RAT_ENA__M |
2102                                 FEC_OC_FCT_MODE_VIRT_ENA__M);
2103
2104                 /* Check user defined bitrate */
2105                 bitRate = maxBitRate;
2106                 if (bitRate > 75900000UL) {     /* max is 75.9 Mb/s */
2107                         bitRate = 75900000UL;
2108                 }
2109                 /* Rational DTO period:
2110                         dto_period = (Fsys / bitrate) - 2
2111
2112                         Result should be floored,
2113                         to make sure >= requested bitrate
2114                         */
2115                 fecOcDtoPeriod = (u16) (((state->m_sysClockFreq)
2116                                                 * 1000) / bitRate);
2117                 if (fecOcDtoPeriod <= 2)
2118                         fecOcDtoPeriod = 0;
2119                 else
2120                         fecOcDtoPeriod -= 2;
2121                 fecOcTmdIntUpdRate = 8;
2122         } else {
2123                 /* (commonAttr->staticCLK == false) => dynamic mode */
2124                 fecOcDtoMode = FEC_OC_DTO_MODE_DYNAMIC__M;
2125                 fecOcFctMode = FEC_OC_FCT_MODE__PRE;
2126                 fecOcTmdIntUpdRate = 5;
2127         }
2128
2129         /* Write appropriate registers with requested configuration */
2130         status = write16(state, FEC_OC_DTO_BURST_LEN__A, fecOcDtoBurstLen);
2131         if (status < 0)
2132                 goto error;
2133         status = write16(state, FEC_OC_DTO_PERIOD__A, fecOcDtoPeriod);
2134         if (status < 0)
2135                 goto error;
2136         status = write16(state, FEC_OC_DTO_MODE__A, fecOcDtoMode);
2137         if (status < 0)
2138                 goto error;
2139         status = write16(state, FEC_OC_FCT_MODE__A, fecOcFctMode);
2140         if (status < 0)
2141                 goto error;
2142         status = write16(state, FEC_OC_MODE__A, fecOcRegMode);
2143         if (status < 0)
2144                 goto error;
2145         status = write16(state, FEC_OC_IPR_MODE__A, fecOcRegIprMode);
2146         if (status < 0)
2147                 goto error;
2148
2149         /* Rate integration settings */
2150         status = write32(state, FEC_OC_RCN_CTL_RATE_LO__A, fecOcRcnCtlRate);
2151         if (status < 0)
2152                 goto error;
2153         status = write16(state, FEC_OC_TMD_INT_UPD_RATE__A, fecOcTmdIntUpdRate);
2154         if (status < 0)
2155                 goto error;
2156         status = write16(state, FEC_OC_TMD_MODE__A, fecOcTmdMode);
2157 error:
2158         if (status < 0)
2159                 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
2160         return status;
2161 }
2162
2163 static int MPEGTSConfigurePolarity(struct drxk_state *state)
2164 {
2165         u16 fecOcRegIprInvert = 0;
2166
2167         /* Data mask for the output data byte */
2168         u16 InvertDataMask =
2169             FEC_OC_IPR_INVERT_MD7__M | FEC_OC_IPR_INVERT_MD6__M |
2170             FEC_OC_IPR_INVERT_MD5__M | FEC_OC_IPR_INVERT_MD4__M |
2171             FEC_OC_IPR_INVERT_MD3__M | FEC_OC_IPR_INVERT_MD2__M |
2172             FEC_OC_IPR_INVERT_MD1__M | FEC_OC_IPR_INVERT_MD0__M;
2173
2174         dprintk(1, "\n");
2175
2176         /* Control selective inversion of output bits */
2177         fecOcRegIprInvert &= (~(InvertDataMask));
2178         if (state->m_invertDATA == true)
2179                 fecOcRegIprInvert |= InvertDataMask;
2180         fecOcRegIprInvert &= (~(FEC_OC_IPR_INVERT_MERR__M));
2181         if (state->m_invertERR == true)
2182                 fecOcRegIprInvert |= FEC_OC_IPR_INVERT_MERR__M;
2183         fecOcRegIprInvert &= (~(FEC_OC_IPR_INVERT_MSTRT__M));
2184         if (state->m_invertSTR == true)
2185                 fecOcRegIprInvert |= FEC_OC_IPR_INVERT_MSTRT__M;
2186         fecOcRegIprInvert &= (~(FEC_OC_IPR_INVERT_MVAL__M));
2187         if (state->m_invertVAL == true)
2188                 fecOcRegIprInvert |= FEC_OC_IPR_INVERT_MVAL__M;
2189         fecOcRegIprInvert &= (~(FEC_OC_IPR_INVERT_MCLK__M));
2190         if (state->m_invertCLK == true)
2191                 fecOcRegIprInvert |= FEC_OC_IPR_INVERT_MCLK__M;
2192
2193         return write16(state, FEC_OC_IPR_INVERT__A, fecOcRegIprInvert);
2194 }
2195
2196 #define   SCU_RAM_AGC_KI_INV_RF_POL__M 0x4000
2197
2198 static int SetAgcRf(struct drxk_state *state,
2199                     struct SCfgAgc *pAgcCfg, bool isDTV)
2200 {
2201         int status = -EINVAL;
2202         u16 data = 0;
2203         struct SCfgAgc *pIfAgcSettings;
2204
2205         dprintk(1, "\n");
2206
2207         if (pAgcCfg == NULL)
2208                 goto error;
2209
2210         switch (pAgcCfg->ctrlMode) {
2211         case DRXK_AGC_CTRL_AUTO:
2212                 /* Enable RF AGC DAC */
2213                 status = read16(state, IQM_AF_STDBY__A, &data);
2214                 if (status < 0)
2215                         goto error;
2216                 data &= ~IQM_AF_STDBY_STDBY_TAGC_RF_STANDBY;
2217                 status = write16(state, IQM_AF_STDBY__A, data);
2218                 if (status < 0)
2219                         goto error;
2220                 status = read16(state, SCU_RAM_AGC_CONFIG__A, &data);
2221                 if (status < 0)
2222                         goto error;
2223
2224                 /* Enable SCU RF AGC loop */
2225                 data &= ~SCU_RAM_AGC_CONFIG_DISABLE_RF_AGC__M;
2226
2227                 /* Polarity */
2228                 if (state->m_RfAgcPol)
2229                         data |= SCU_RAM_AGC_CONFIG_INV_RF_POL__M;
2230                 else
2231                         data &= ~SCU_RAM_AGC_CONFIG_INV_RF_POL__M;
2232                 status = write16(state, SCU_RAM_AGC_CONFIG__A, data);
2233                 if (status < 0)
2234                         goto error;
2235
2236                 /* Set speed (using complementary reduction value) */
2237                 status = read16(state, SCU_RAM_AGC_KI_RED__A, &data);
2238                 if (status < 0)
2239                         goto error;
2240
2241                 data &= ~SCU_RAM_AGC_KI_RED_RAGC_RED__M;
2242                 data |= (~(pAgcCfg->speed <<
2243                                 SCU_RAM_AGC_KI_RED_RAGC_RED__B)
2244                                 & SCU_RAM_AGC_KI_RED_RAGC_RED__M);
2245
2246                 status = write16(state, SCU_RAM_AGC_KI_RED__A, data);
2247                 if (status < 0)
2248                         goto error;
2249
2250                 if (IsDVBT(state))
2251                         pIfAgcSettings = &state->m_dvbtIfAgcCfg;
2252                 else if (IsQAM(state))
2253                         pIfAgcSettings = &state->m_qamIfAgcCfg;
2254                 else
2255                         pIfAgcSettings = &state->m_atvIfAgcCfg;
2256                 if (pIfAgcSettings == NULL) {
2257                         status = -EINVAL;
2258                         goto error;
2259                 }
2260
2261                 /* Set TOP, only if IF-AGC is in AUTO mode */
2262                 if (pIfAgcSettings->ctrlMode == DRXK_AGC_CTRL_AUTO)
2263                         status = write16(state, SCU_RAM_AGC_IF_IACCU_HI_TGT_MAX__A, pAgcCfg->top);
2264                         if (status < 0)
2265                                 goto error;
2266
2267                 /* Cut-Off current */
2268                 status = write16(state, SCU_RAM_AGC_RF_IACCU_HI_CO__A, pAgcCfg->cutOffCurrent);
2269                 if (status < 0)
2270                         goto error;
2271
2272                 /* Max. output level */
2273                 status = write16(state, SCU_RAM_AGC_RF_MAX__A, pAgcCfg->maxOutputLevel);
2274                 if (status < 0)
2275                         goto error;
2276
2277                 break;
2278
2279         case DRXK_AGC_CTRL_USER:
2280                 /* Enable RF AGC DAC */
2281                 status = read16(state, IQM_AF_STDBY__A, &data);
2282                 if (status < 0)
2283                         goto error;
2284                 data &= ~IQM_AF_STDBY_STDBY_TAGC_RF_STANDBY;
2285                 status = write16(state, IQM_AF_STDBY__A, data);
2286                 if (status < 0)
2287                         goto error;
2288
2289                 /* Disable SCU RF AGC loop */
2290                 status = read16(state, SCU_RAM_AGC_CONFIG__A, &data);
2291                 if (status < 0)
2292                         goto error;
2293                 data |= SCU_RAM_AGC_CONFIG_DISABLE_RF_AGC__M;
2294                 if (state->m_RfAgcPol)
2295                         data |= SCU_RAM_AGC_CONFIG_INV_RF_POL__M;
2296                 else
2297                         data &= ~SCU_RAM_AGC_CONFIG_INV_RF_POL__M;
2298                 status = write16(state, SCU_RAM_AGC_CONFIG__A, data);
2299                 if (status < 0)
2300                         goto error;
2301
2302                 /* SCU c.o.c. to 0, enabling full control range */
2303                 status = write16(state, SCU_RAM_AGC_RF_IACCU_HI_CO__A, 0);
2304                 if (status < 0)
2305                         goto error;
2306
2307                 /* Write value to output pin */
2308                 status = write16(state, SCU_RAM_AGC_RF_IACCU_HI__A, pAgcCfg->outputLevel);
2309                 if (status < 0)
2310                         goto error;
2311                 break;
2312
2313         case DRXK_AGC_CTRL_OFF:
2314                 /* Disable RF AGC DAC */
2315                 status = read16(state, IQM_AF_STDBY__A, &data);
2316                 if (status < 0)
2317                         goto error;
2318                 data |= IQM_AF_STDBY_STDBY_TAGC_RF_STANDBY;
2319                 status = write16(state, IQM_AF_STDBY__A, data);
2320                 if (status < 0)
2321                         goto error;
2322
2323                 /* Disable SCU RF AGC loop */
2324                 status = read16(state, SCU_RAM_AGC_CONFIG__A, &data);
2325                 if (status < 0)
2326                         goto error;
2327                 data |= SCU_RAM_AGC_CONFIG_DISABLE_RF_AGC__M;
2328                 status = write16(state, SCU_RAM_AGC_CONFIG__A, data);
2329                 if (status < 0)
2330                         goto error;
2331                 break;
2332
2333         default:
2334                 status = -EINVAL;
2335
2336         }
2337 error:
2338         if (status < 0)
2339                 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
2340         return status;
2341 }
2342
2343 #define SCU_RAM_AGC_KI_INV_IF_POL__M 0x2000
2344
2345 static int SetAgcIf(struct drxk_state *state,
2346                     struct SCfgAgc *pAgcCfg, bool isDTV)
2347 {
2348         u16 data = 0;
2349         int status = 0;
2350         struct SCfgAgc *pRfAgcSettings;
2351
2352         dprintk(1, "\n");
2353
2354         switch (pAgcCfg->ctrlMode) {
2355         case DRXK_AGC_CTRL_AUTO:
2356
2357                 /* Enable IF AGC DAC */
2358                 status = read16(state, IQM_AF_STDBY__A, &data);
2359                 if (status < 0)
2360                         goto error;
2361                 data &= ~IQM_AF_STDBY_STDBY_TAGC_IF_STANDBY;
2362                 status = write16(state, IQM_AF_STDBY__A, data);
2363                 if (status < 0)
2364                         goto error;
2365
2366                 status = read16(state, SCU_RAM_AGC_CONFIG__A, &data);
2367                 if (status < 0)
2368                         goto error;
2369
2370                 /* Enable SCU IF AGC loop */
2371                 data &= ~SCU_RAM_AGC_CONFIG_DISABLE_IF_AGC__M;
2372
2373                 /* Polarity */
2374                 if (state->m_IfAgcPol)
2375                         data |= SCU_RAM_AGC_CONFIG_INV_IF_POL__M;
2376                 else
2377                         data &= ~SCU_RAM_AGC_CONFIG_INV_IF_POL__M;
2378                 status = write16(state, SCU_RAM_AGC_CONFIG__A, data);
2379                 if (status < 0)
2380                         goto error;
2381
2382                 /* Set speed (using complementary reduction value) */
2383                 status = read16(state, SCU_RAM_AGC_KI_RED__A, &data);
2384                 if (status < 0)
2385                         goto error;
2386                 data &= ~SCU_RAM_AGC_KI_RED_IAGC_RED__M;
2387                 data |= (~(pAgcCfg->speed <<
2388                                 SCU_RAM_AGC_KI_RED_IAGC_RED__B)
2389                                 & SCU_RAM_AGC_KI_RED_IAGC_RED__M);
2390
2391                 status = write16(state, SCU_RAM_AGC_KI_RED__A, data);
2392                 if (status < 0)
2393                         goto error;
2394
2395                 if (IsQAM(state))
2396                         pRfAgcSettings = &state->m_qamRfAgcCfg;
2397                 else
2398                         pRfAgcSettings = &state->m_atvRfAgcCfg;
2399                 if (pRfAgcSettings == NULL)
2400                         return -1;
2401                 /* Restore TOP */
2402                 status = write16(state, SCU_RAM_AGC_IF_IACCU_HI_TGT_MAX__A, pRfAgcSettings->top);
2403                 if (status < 0)
2404                         goto error;
2405                 break;
2406
2407         case DRXK_AGC_CTRL_USER:
2408
2409                 /* Enable IF AGC DAC */
2410                 status = read16(state, IQM_AF_STDBY__A, &data);
2411                 if (status < 0)
2412                         goto error;
2413                 data &= ~IQM_AF_STDBY_STDBY_TAGC_IF_STANDBY;
2414                 status = write16(state, IQM_AF_STDBY__A, data);
2415                 if (status < 0)
2416                         goto error;
2417
2418                 status = read16(state, SCU_RAM_AGC_CONFIG__A, &data);
2419                 if (status < 0)
2420                         goto error;
2421
2422                 /* Disable SCU IF AGC loop */
2423                 data |= SCU_RAM_AGC_CONFIG_DISABLE_IF_AGC__M;
2424
2425                 /* Polarity */
2426                 if (state->m_IfAgcPol)
2427                         data |= SCU_RAM_AGC_CONFIG_INV_IF_POL__M;
2428                 else
2429                         data &= ~SCU_RAM_AGC_CONFIG_INV_IF_POL__M;
2430                 status = write16(state, SCU_RAM_AGC_CONFIG__A, data);
2431                 if (status < 0)
2432                         goto error;
2433
2434                 /* Write value to output pin */
2435                 status = write16(state, SCU_RAM_AGC_IF_IACCU_HI_TGT_MAX__A, pAgcCfg->outputLevel);
2436                 if (status < 0)
2437                         goto error;
2438                 break;
2439
2440         case DRXK_AGC_CTRL_OFF:
2441
2442                 /* Disable If AGC DAC */
2443                 status = read16(state, IQM_AF_STDBY__A, &data);
2444                 if (status < 0)
2445                         goto error;
2446                 data |= IQM_AF_STDBY_STDBY_TAGC_IF_STANDBY;
2447                 status = write16(state, IQM_AF_STDBY__A, data);
2448                 if (status < 0)
2449                         goto error;
2450
2451                 /* Disable SCU IF AGC loop */
2452                 status = read16(state, SCU_RAM_AGC_CONFIG__A, &data);
2453                 if (status < 0)
2454                         goto error;
2455                 data |= SCU_RAM_AGC_CONFIG_DISABLE_IF_AGC__M;
2456                 status = write16(state, SCU_RAM_AGC_CONFIG__A, data);
2457                 if (status < 0)
2458                         goto error;
2459                 break;
2460         }               /* switch (agcSettingsIf->ctrlMode) */
2461
2462         /* always set the top to support
2463                 configurations without if-loop */
2464         status = write16(state, SCU_RAM_AGC_INGAIN_TGT_MIN__A, pAgcCfg->top);
2465 error:
2466         if (status < 0)
2467                 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
2468         return status;
2469 }
2470
2471 static int ReadIFAgc(struct drxk_state *state, u32 *pValue)
2472 {
2473         u16 agcDacLvl;
2474         int status;
2475         u16 Level = 0;
2476
2477         dprintk(1, "\n");
2478
2479         status = read16(state, IQM_AF_AGC_IF__A, &agcDacLvl);
2480         if (status < 0) {
2481                 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
2482                 return status;
2483         }
2484
2485         *pValue = 0;
2486
2487         if (agcDacLvl > DRXK_AGC_DAC_OFFSET)
2488                 Level = agcDacLvl - DRXK_AGC_DAC_OFFSET;
2489         if (Level < 14000)
2490                 *pValue = (14000 - Level) / 4;
2491         else
2492                 *pValue = 0;
2493
2494         return status;
2495 }
2496
2497 static int GetQAMSignalToNoise(struct drxk_state *state,
2498                                s32 *pSignalToNoise)
2499 {
2500         int status = 0;
2501         u16 qamSlErrPower = 0;  /* accum. error between
2502                                         raw and sliced symbols */
2503         u32 qamSlSigPower = 0;  /* used for MER, depends of
2504                                         QAM constellation */
2505         u32 qamSlMer = 0;       /* QAM MER */
2506
2507         dprintk(1, "\n");
2508
2509         /* MER calculation */
2510
2511         /* get the register value needed for MER */
2512         status = read16(state, QAM_SL_ERR_POWER__A, &qamSlErrPower);
2513         if (status < 0) {
2514                 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
2515                 return -EINVAL;
2516         }
2517
2518         switch (state->param.u.qam.modulation) {
2519         case QAM_16:
2520                 qamSlSigPower = DRXK_QAM_SL_SIG_POWER_QAM16 << 2;
2521                 break;
2522         case QAM_32:
2523                 qamSlSigPower = DRXK_QAM_SL_SIG_POWER_QAM32 << 2;
2524                 break;
2525         case QAM_64:
2526                 qamSlSigPower = DRXK_QAM_SL_SIG_POWER_QAM64 << 2;
2527                 break;
2528         case QAM_128:
2529                 qamSlSigPower = DRXK_QAM_SL_SIG_POWER_QAM128 << 2;
2530                 break;
2531         default:
2532         case QAM_256:
2533                 qamSlSigPower = DRXK_QAM_SL_SIG_POWER_QAM256 << 2;
2534                 break;
2535         }
2536
2537         if (qamSlErrPower > 0) {
2538                 qamSlMer = Log10Times100(qamSlSigPower) -
2539                         Log10Times100((u32) qamSlErrPower);
2540         }
2541         *pSignalToNoise = qamSlMer;
2542
2543         return status;
2544 }
2545
2546 static int GetDVBTSignalToNoise(struct drxk_state *state,
2547                                 s32 *pSignalToNoise)
2548 {
2549         int status;
2550         u16 regData = 0;
2551         u32 EqRegTdSqrErrI = 0;
2552         u32 EqRegTdSqrErrQ = 0;
2553         u16 EqRegTdSqrErrExp = 0;
2554         u16 EqRegTdTpsPwrOfs = 0;
2555         u16 EqRegTdReqSmbCnt = 0;
2556         u32 tpsCnt = 0;
2557         u32 SqrErrIQ = 0;
2558         u32 a = 0;
2559         u32 b = 0;
2560         u32 c = 0;
2561         u32 iMER = 0;
2562         u16 transmissionParams = 0;
2563
2564         dprintk(1, "\n");
2565
2566         status = read16(state, OFDM_EQ_TOP_TD_TPS_PWR_OFS__A, &EqRegTdTpsPwrOfs);
2567         if (status < 0)
2568                 goto error;
2569         status = read16(state, OFDM_EQ_TOP_TD_REQ_SMB_CNT__A, &EqRegTdReqSmbCnt);
2570         if (status < 0)
2571                 goto error;
2572         status = read16(state, OFDM_EQ_TOP_TD_SQR_ERR_EXP__A, &EqRegTdSqrErrExp);
2573         if (status < 0)
2574                 goto error;
2575         status = read16(state, OFDM_EQ_TOP_TD_SQR_ERR_I__A, &regData);
2576         if (status < 0)
2577                 goto error;
2578         /* Extend SQR_ERR_I operational range */
2579         EqRegTdSqrErrI = (u32) regData;
2580         if ((EqRegTdSqrErrExp > 11) &&
2581                 (EqRegTdSqrErrI < 0x00000FFFUL)) {
2582                 EqRegTdSqrErrI += 0x00010000UL;
2583         }
2584         status = read16(state, OFDM_EQ_TOP_TD_SQR_ERR_Q__A, &regData);
2585         if (status < 0)
2586                 goto error;
2587         /* Extend SQR_ERR_Q operational range */
2588         EqRegTdSqrErrQ = (u32) regData;
2589         if ((EqRegTdSqrErrExp > 11) &&
2590                 (EqRegTdSqrErrQ < 0x00000FFFUL))
2591                 EqRegTdSqrErrQ += 0x00010000UL;
2592
2593         status = read16(state, OFDM_SC_RA_RAM_OP_PARAM__A, &transmissionParams);
2594         if (status < 0)
2595                 goto error;
2596
2597         /* Check input data for MER */
2598
2599         /* MER calculation (in 0.1 dB) without math.h */
2600         if ((EqRegTdTpsPwrOfs == 0) || (EqRegTdReqSmbCnt == 0))
2601                 iMER = 0;
2602         else if ((EqRegTdSqrErrI + EqRegTdSqrErrQ) == 0) {
2603                 /* No error at all, this must be the HW reset value
2604                         * Apparently no first measurement yet
2605                         * Set MER to 0.0 */
2606                 iMER = 0;
2607         } else {
2608                 SqrErrIQ = (EqRegTdSqrErrI + EqRegTdSqrErrQ) <<
2609                         EqRegTdSqrErrExp;
2610                 if ((transmissionParams &
2611                         OFDM_SC_RA_RAM_OP_PARAM_MODE__M)
2612                         == OFDM_SC_RA_RAM_OP_PARAM_MODE_2K)
2613                         tpsCnt = 17;
2614                 else
2615                         tpsCnt = 68;
2616
2617                 /* IMER = 100 * log10 (x)
2618                         where x = (EqRegTdTpsPwrOfs^2 *
2619                         EqRegTdReqSmbCnt * tpsCnt)/SqrErrIQ
2620
2621                         => IMER = a + b -c
2622                         where a = 100 * log10 (EqRegTdTpsPwrOfs^2)
2623                         b = 100 * log10 (EqRegTdReqSmbCnt * tpsCnt)
2624                         c = 100 * log10 (SqrErrIQ)
2625                         */
2626
2627                 /* log(x) x = 9bits * 9bits->18 bits  */
2628                 a = Log10Times100(EqRegTdTpsPwrOfs *
2629                                         EqRegTdTpsPwrOfs);
2630                 /* log(x) x = 16bits * 7bits->23 bits  */
2631                 b = Log10Times100(EqRegTdReqSmbCnt * tpsCnt);
2632                 /* log(x) x = (16bits + 16bits) << 15 ->32 bits  */
2633                 c = Log10Times100(SqrErrIQ);
2634
2635                 iMER = a + b;
2636                 /* No negative MER, clip to zero */
2637                 if (iMER > c)
2638                         iMER -= c;
2639                 else
2640                         iMER = 0;
2641         }
2642         *pSignalToNoise = iMER;
2643
2644 error:
2645         if (status < 0)
2646                 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
2647         return status;
2648 }
2649
2650 static int GetSignalToNoise(struct drxk_state *state, s32 *pSignalToNoise)
2651 {
2652         dprintk(1, "\n");
2653
2654         *pSignalToNoise = 0;
2655         switch (state->m_OperationMode) {
2656         case OM_DVBT:
2657                 return GetDVBTSignalToNoise(state, pSignalToNoise);
2658         case OM_QAM_ITU_A:
2659         case OM_QAM_ITU_C:
2660                 return GetQAMSignalToNoise(state, pSignalToNoise);
2661         default:
2662                 break;
2663         }
2664         return 0;
2665 }
2666
2667 #if 0
2668 static int GetDVBTQuality(struct drxk_state *state, s32 *pQuality)
2669 {
2670         /* SNR Values for quasi errorfree reception rom Nordig 2.2 */
2671         int status = 0;
2672
2673         dprintk(1, "\n");
2674
2675         static s32 QE_SN[] = {
2676                 51,             /* QPSK 1/2 */
2677                 69,             /* QPSK 2/3 */
2678                 79,             /* QPSK 3/4 */
2679                 89,             /* QPSK 5/6 */
2680                 97,             /* QPSK 7/8 */
2681                 108,            /* 16-QAM 1/2 */
2682                 131,            /* 16-QAM 2/3 */
2683                 146,            /* 16-QAM 3/4 */
2684                 156,            /* 16-QAM 5/6 */
2685                 160,            /* 16-QAM 7/8 */
2686                 165,            /* 64-QAM 1/2 */
2687                 187,            /* 64-QAM 2/3 */
2688                 202,            /* 64-QAM 3/4 */
2689                 216,            /* 64-QAM 5/6 */
2690                 225,            /* 64-QAM 7/8 */
2691         };
2692
2693         *pQuality = 0;
2694
2695         do {
2696                 s32 SignalToNoise = 0;
2697                 u16 Constellation = 0;
2698                 u16 CodeRate = 0;
2699                 u32 SignalToNoiseRel;
2700                 u32 BERQuality;
2701
2702                 status = GetDVBTSignalToNoise(state, &SignalToNoise);
2703                 if (status < 0)
2704                         break;
2705                 status = read16(state, OFDM_EQ_TOP_TD_TPS_CONST__A, &Constellation);
2706                 if (status < 0)
2707                         break;
2708                 Constellation &= OFDM_EQ_TOP_TD_TPS_CONST__M;
2709
2710                 status = read16(state, OFDM_EQ_TOP_TD_TPS_CODE_HP__A, &CodeRate);
2711                 if (status < 0)
2712                         break;
2713                 CodeRate &= OFDM_EQ_TOP_TD_TPS_CODE_HP__M;
2714
2715                 if (Constellation > OFDM_EQ_TOP_TD_TPS_CONST_64QAM ||
2716                     CodeRate > OFDM_EQ_TOP_TD_TPS_CODE_LP_7_8)
2717                         break;
2718                 SignalToNoiseRel = SignalToNoise -
2719                     QE_SN[Constellation * 5 + CodeRate];
2720                 BERQuality = 100;
2721
2722                 if (SignalToNoiseRel < -70)
2723                         *pQuality = 0;
2724                 else if (SignalToNoiseRel < 30)
2725                         *pQuality = ((SignalToNoiseRel + 70) *
2726                                      BERQuality) / 100;
2727                 else
2728                         *pQuality = BERQuality;
2729         } while (0);
2730         return 0;
2731 };
2732
2733 static int GetDVBCQuality(struct drxk_state *state, s32 *pQuality)
2734 {
2735         int status = 0;
2736         *pQuality = 0;
2737
2738         dprintk(1, "\n");
2739
2740         do {
2741                 u32 SignalToNoise = 0;
2742                 u32 BERQuality = 100;
2743                 u32 SignalToNoiseRel = 0;
2744
2745                 status = GetQAMSignalToNoise(state, &SignalToNoise);
2746                 if (status < 0)
2747                         break;
2748
2749                 switch (state->param.u.qam.modulation) {
2750                 case QAM_16:
2751                         SignalToNoiseRel = SignalToNoise - 200;
2752                         break;
2753                 case QAM_32:
2754                         SignalToNoiseRel = SignalToNoise - 230;
2755                         break;  /* Not in NorDig */
2756                 case QAM_64:
2757                         SignalToNoiseRel = SignalToNoise - 260;
2758                         break;
2759                 case QAM_128:
2760                         SignalToNoiseRel = SignalToNoise - 290;
2761                         break;
2762                 default:
2763                 case QAM_256:
2764                         SignalToNoiseRel = SignalToNoise - 320;
2765                         break;
2766                 }
2767
2768                 if (SignalToNoiseRel < -70)
2769                         *pQuality = 0;
2770                 else if (SignalToNoiseRel < 30)
2771                         *pQuality = ((SignalToNoiseRel + 70) *
2772                                      BERQuality) / 100;
2773                 else
2774                         *pQuality = BERQuality;
2775         } while (0);
2776
2777         return status;
2778 }
2779
2780 static int GetQuality(struct drxk_state *state, s32 *pQuality)
2781 {
2782         dprintk(1, "\n");
2783
2784         switch (state->m_OperationMode) {
2785         case OM_DVBT:
2786                 return GetDVBTQuality(state, pQuality);
2787         case OM_QAM_ITU_A:
2788                 return GetDVBCQuality(state, pQuality);
2789         default:
2790                 break;
2791         }
2792
2793         return 0;
2794 }
2795 #endif
2796
2797 /* Free data ram in SIO HI */
2798 #define SIO_HI_RA_RAM_USR_BEGIN__A 0x420040
2799 #define SIO_HI_RA_RAM_USR_END__A   0x420060
2800
2801 #define DRXK_HI_ATOMIC_BUF_START (SIO_HI_RA_RAM_USR_BEGIN__A)
2802 #define DRXK_HI_ATOMIC_BUF_END   (SIO_HI_RA_RAM_USR_BEGIN__A + 7)
2803 #define DRXK_HI_ATOMIC_READ      SIO_HI_RA_RAM_PAR_3_ACP_RW_READ
2804 #define DRXK_HI_ATOMIC_WRITE     SIO_HI_RA_RAM_PAR_3_ACP_RW_WRITE
2805
2806 #define DRXDAP_FASI_ADDR2BLOCK(addr)  (((addr) >> 22) & 0x3F)
2807 #define DRXDAP_FASI_ADDR2BANK(addr)   (((addr) >> 16) & 0x3F)
2808 #define DRXDAP_FASI_ADDR2OFFSET(addr) ((addr) & 0x7FFF)
2809
2810 static int ConfigureI2CBridge(struct drxk_state *state, bool bEnableBridge)
2811 {
2812         int status = -EINVAL;
2813
2814         dprintk(1, "\n");
2815
2816         if (state->m_DrxkState == DRXK_UNINITIALIZED)
2817                 goto error;
2818         if (state->m_DrxkState == DRXK_POWERED_DOWN)
2819                 goto error;
2820
2821         if (state->no_i2c_bridge)
2822                 return 0;
2823
2824         status = write16(state, SIO_HI_RA_RAM_PAR_1__A, SIO_HI_RA_RAM_PAR_1_PAR1_SEC_KEY);
2825         if (status < 0)
2826                 goto error;
2827         if (bEnableBridge) {
2828                 status = write16(state, SIO_HI_RA_RAM_PAR_2__A, SIO_HI_RA_RAM_PAR_2_BRD_CFG_CLOSED);
2829                 if (status < 0)
2830                         goto error;
2831         } else {
2832                 status = write16(state, SIO_HI_RA_RAM_PAR_2__A, SIO_HI_RA_RAM_PAR_2_BRD_CFG_OPEN);
2833                 if (status < 0)
2834                         goto error;
2835         }
2836
2837         status = HI_Command(state, SIO_HI_RA_RAM_CMD_BRDCTRL, 0);
2838
2839 error:
2840         if (status < 0)
2841                 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
2842         return status;
2843 }
2844
2845 static int SetPreSaw(struct drxk_state *state,
2846                      struct SCfgPreSaw *pPreSawCfg)
2847 {
2848         int status = -EINVAL;
2849
2850         dprintk(1, "\n");
2851
2852         if ((pPreSawCfg == NULL)
2853             || (pPreSawCfg->reference > IQM_AF_PDREF__M))
2854                 goto error;
2855
2856         status = write16(state, IQM_AF_PDREF__A, pPreSawCfg->reference);
2857 error:
2858         if (status < 0)
2859                 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
2860         return status;
2861 }
2862
2863 static int BLDirectCmd(struct drxk_state *state, u32 targetAddr,
2864                        u16 romOffset, u16 nrOfElements, u32 timeOut)
2865 {
2866         u16 blStatus = 0;
2867         u16 offset = (u16) ((targetAddr >> 0) & 0x00FFFF);
2868         u16 blockbank = (u16) ((targetAddr >> 16) & 0x000FFF);
2869         int status;
2870         unsigned long end;
2871
2872         dprintk(1, "\n");
2873
2874         mutex_lock(&state->mutex);
2875         status = write16(state, SIO_BL_MODE__A, SIO_BL_MODE_DIRECT);
2876         if (status < 0)
2877                 goto error;
2878         status = write16(state, SIO_BL_TGT_HDR__A, blockbank);
2879         if (status < 0)
2880                 goto error;
2881         status = write16(state, SIO_BL_TGT_ADDR__A, offset);
2882         if (status < 0)
2883                 goto error;
2884         status = write16(state, SIO_BL_SRC_ADDR__A, romOffset);
2885         if (status < 0)
2886                 goto error;
2887         status = write16(state, SIO_BL_SRC_LEN__A, nrOfElements);
2888         if (status < 0)
2889                 goto error;
2890         status = write16(state, SIO_BL_ENABLE__A, SIO_BL_ENABLE_ON);
2891         if (status < 0)
2892                 goto error;
2893
2894         end = jiffies + msecs_to_jiffies(timeOut);
2895         do {
2896                 status = read16(state, SIO_BL_STATUS__A, &blStatus);
2897                 if (status < 0)
2898                         goto error;
2899         } while ((blStatus == 0x1) && time_is_after_jiffies(end));
2900         if (blStatus == 0x1) {
2901                 printk(KERN_ERR "drxk: SIO not ready\n");
2902                 status = -EINVAL;
2903                 goto error2;
2904         }
2905 error:
2906         if (status < 0)
2907                 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
2908 error2:
2909         mutex_unlock(&state->mutex);
2910         return status;
2911
2912 }
2913
2914 static int ADCSyncMeasurement(struct drxk_state *state, u16 *count)
2915 {
2916         u16 data = 0;
2917         int status;
2918
2919         dprintk(1, "\n");
2920
2921         /* Start measurement */
2922         status = write16(state, IQM_AF_COMM_EXEC__A, IQM_AF_COMM_EXEC_ACTIVE);
2923         if (status < 0)
2924                 goto error;
2925         status = write16(state, IQM_AF_START_LOCK__A, 1);
2926         if (status < 0)
2927                 goto error;
2928
2929         *count = 0;
2930         status = read16(state, IQM_AF_PHASE0__A, &data);
2931         if (status < 0)
2932                 goto error;
2933         if (data == 127)
2934                 *count = *count + 1;
2935         status = read16(state, IQM_AF_PHASE1__A, &data);
2936         if (status < 0)
2937                 goto error;
2938         if (data == 127)
2939                 *count = *count + 1;
2940         status = read16(state, IQM_AF_PHASE2__A, &data);
2941         if (status < 0)
2942                 goto error;
2943         if (data == 127)
2944                 *count = *count + 1;
2945
2946 error:
2947         if (status < 0)
2948                 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
2949         return status;
2950 }
2951
2952 static int ADCSynchronization(struct drxk_state *state)
2953 {
2954         u16 count = 0;
2955         int status;
2956
2957         dprintk(1, "\n");
2958
2959         status = ADCSyncMeasurement(state, &count);
2960         if (status < 0)
2961                 goto error;
2962
2963         if (count == 1) {
2964                 /* Try sampling on a diffrent edge */
2965                 u16 clkNeg = 0;
2966
2967                 status = read16(state, IQM_AF_CLKNEG__A, &clkNeg);
2968                 if (status < 0)
2969                         goto error;
2970                 if ((clkNeg | IQM_AF_CLKNEG_CLKNEGDATA__M) ==
2971                         IQM_AF_CLKNEG_CLKNEGDATA_CLK_ADC_DATA_POS) {
2972                         clkNeg &= (~(IQM_AF_CLKNEG_CLKNEGDATA__M));
2973                         clkNeg |=
2974                                 IQM_AF_CLKNEG_CLKNEGDATA_CLK_ADC_DATA_NEG;
2975                 } else {
2976                         clkNeg &= (~(IQM_AF_CLKNEG_CLKNEGDATA__M));
2977                         clkNeg |=
2978                                 IQM_AF_CLKNEG_CLKNEGDATA_CLK_ADC_DATA_POS;
2979                 }
2980                 status = write16(state, IQM_AF_CLKNEG__A, clkNeg);
2981                 if (status < 0)
2982                         goto error;
2983                 status = ADCSyncMeasurement(state, &count);
2984                 if (status < 0)
2985                         goto error;
2986         }
2987
2988         if (count < 2)
2989                 status = -EINVAL;
2990 error:
2991         if (status < 0)
2992                 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
2993         return status;
2994 }
2995
2996 static int SetFrequencyShifter(struct drxk_state *state,
2997                                u16 intermediateFreqkHz,
2998                                s32 tunerFreqOffset, bool isDTV)
2999 {
3000         bool selectPosImage = false;
3001         u32 rfFreqResidual = tunerFreqOffset;
3002         u32 fmFrequencyShift = 0;
3003         bool tunerMirror = !state->m_bMirrorFreqSpect;
3004         u32 adcFreq;
3005         bool adcFlip;
3006         int status;
3007         u32 ifFreqActual;
3008         u32 samplingFrequency = (u32) (state->m_sysClockFreq / 3);
3009         u32 frequencyShift;
3010         bool imageToSelect;
3011
3012         dprintk(1, "\n");
3013
3014         /*
3015            Program frequency shifter
3016            No need to account for mirroring on RF
3017          */
3018         if (isDTV) {
3019                 if ((state->m_OperationMode == OM_QAM_ITU_A) ||
3020                     (state->m_OperationMode == OM_QAM_ITU_C) ||
3021                     (state->m_OperationMode == OM_DVBT))
3022                         selectPosImage = true;
3023                 else
3024                         selectPosImage = false;
3025         }
3026         if (tunerMirror)
3027                 /* tuner doesn't mirror */
3028                 ifFreqActual = intermediateFreqkHz +
3029                     rfFreqResidual + fmFrequencyShift;
3030         else
3031                 /* tuner mirrors */
3032                 ifFreqActual = intermediateFreqkHz -
3033                     rfFreqResidual - fmFrequencyShift;
3034         if (ifFreqActual > samplingFrequency / 2) {
3035                 /* adc mirrors */
3036                 adcFreq = samplingFrequency - ifFreqActual;
3037                 adcFlip = true;
3038         } else {
3039                 /* adc doesn't mirror */
3040                 adcFreq = ifFreqActual;
3041                 adcFlip = false;
3042         }
3043
3044         frequencyShift = adcFreq;
3045         imageToSelect = state->m_rfmirror ^ tunerMirror ^
3046             adcFlip ^ selectPosImage;
3047         state->m_IqmFsRateOfs =
3048             Frac28a((frequencyShift), samplingFrequency);
3049
3050         if (imageToSelect)
3051                 state->m_IqmFsRateOfs = ~state->m_IqmFsRateOfs + 1;
3052
3053         /* Program frequency shifter with tuner offset compensation */
3054         /* frequencyShift += tunerFreqOffset; TODO */
3055         status = write32(state, IQM_FS_RATE_OFS_LO__A,
3056                          state->m_IqmFsRateOfs);
3057         if (status < 0)
3058                 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
3059         return status;
3060 }
3061
3062 static int InitAGC(struct drxk_state *state, bool isDTV)
3063 {
3064         u16 ingainTgt = 0;
3065         u16 ingainTgtMin = 0;
3066         u16 ingainTgtMax = 0;
3067         u16 clpCyclen = 0;
3068         u16 clpSumMin = 0;
3069         u16 clpDirTo = 0;
3070         u16 snsSumMin = 0;
3071         u16 snsSumMax = 0;
3072         u16 clpSumMax = 0;
3073         u16 snsDirTo = 0;
3074         u16 kiInnergainMin = 0;
3075         u16 ifIaccuHiTgt = 0;
3076         u16 ifIaccuHiTgtMin = 0;
3077         u16 ifIaccuHiTgtMax = 0;
3078         u16 data = 0;
3079         u16 fastClpCtrlDelay = 0;
3080         u16 clpCtrlMode = 0;
3081         int status = 0;
3082
3083         dprintk(1, "\n");
3084
3085         /* Common settings */
3086         snsSumMax = 1023;
3087         ifIaccuHiTgtMin = 2047;
3088         clpCyclen = 500;
3089         clpSumMax = 1023;
3090
3091         /* AGCInit() not available for DVBT; init done in microcode */
3092         if (!IsQAM(state)) {
3093                 printk(KERN_ERR "drxk: %s: mode %d is not DVB-C\n", __func__, state->m_OperationMode);
3094                 return -EINVAL;
3095         }
3096
3097         /* FIXME: Analog TV AGC require different settings */
3098
3099         /* Standard specific settings */
3100         clpSumMin = 8;
3101         clpDirTo = (u16) -9;
3102         clpCtrlMode = 0;
3103         snsSumMin = 8;
3104         snsDirTo = (u16) -9;
3105         kiInnergainMin = (u16) -1030;
3106         ifIaccuHiTgtMax = 0x2380;
3107         ifIaccuHiTgt = 0x2380;
3108         ingainTgtMin = 0x0511;
3109         ingainTgt = 0x0511;
3110         ingainTgtMax = 5119;
3111         fastClpCtrlDelay = state->m_qamIfAgcCfg.FastClipCtrlDelay;
3112
3113         status = write16(state, SCU_RAM_AGC_FAST_CLP_CTRL_DELAY__A, fastClpCtrlDelay);
3114         if (status < 0)
3115                 goto error;
3116
3117         status = write16(state, SCU_RAM_AGC_CLP_CTRL_MODE__A, clpCtrlMode);
3118         if (status < 0)
3119                 goto error;
3120         status = write16(state, SCU_RAM_AGC_INGAIN_TGT__A, ingainTgt);
3121         if (status < 0)
3122                 goto error;
3123         status = write16(state, SCU_RAM_AGC_INGAIN_TGT_MIN__A, ingainTgtMin);
3124         if (status < 0)
3125                 goto error;
3126         status = write16(state, SCU_RAM_AGC_INGAIN_TGT_MAX__A, ingainTgtMax);
3127         if (status < 0)
3128                 goto error;
3129         status = write16(state, SCU_RAM_AGC_IF_IACCU_HI_TGT_MIN__A, ifIaccuHiTgtMin);
3130         if (status < 0)
3131                 goto error;
3132         status = write16(state, SCU_RAM_AGC_IF_IACCU_HI_TGT_MAX__A, ifIaccuHiTgtMax);
3133         if (status < 0)
3134                 goto error;
3135         status = write16(state, SCU_RAM_AGC_IF_IACCU_HI__A, 0);
3136         if (status < 0)
3137                 goto error;
3138         status = write16(state, SCU_RAM_AGC_IF_IACCU_LO__A, 0);
3139         if (status < 0)
3140                 goto error;
3141         status = write16(state, SCU_RAM_AGC_RF_IACCU_HI__A, 0);
3142         if (status < 0)
3143                 goto error;
3144         status = write16(state, SCU_RAM_AGC_RF_IACCU_LO__A, 0);
3145         if (status < 0)
3146                 goto error;
3147         status = write16(state, SCU_RAM_AGC_CLP_SUM_MAX__A, clpSumMax);
3148         if (status < 0)
3149                 goto error;
3150         status = write16(state, SCU_RAM_AGC_SNS_SUM_MAX__A, snsSumMax);
3151         if (status < 0)
3152                 goto error;
3153
3154         status = write16(state, SCU_RAM_AGC_KI_INNERGAIN_MIN__A, kiInnergainMin);
3155         if (status < 0)
3156                 goto error;
3157         status = write16(state, SCU_RAM_AGC_IF_IACCU_HI_TGT__A, ifIaccuHiTgt);
3158         if (status < 0)
3159                 goto error;
3160         status = write16(state, SCU_RAM_AGC_CLP_CYCLEN__A, clpCyclen);
3161         if (status < 0)
3162                 goto error;
3163
3164         status = write16(state, SCU_RAM_AGC_RF_SNS_DEV_MAX__A, 1023);
3165         if (status < 0)
3166                 goto error;
3167         status = write16(state, SCU_RAM_AGC_RF_SNS_DEV_MIN__A, (u16) -1023);
3168         if (status < 0)
3169                 goto error;
3170         status = write16(state, SCU_RAM_AGC_FAST_SNS_CTRL_DELAY__A, 50);
3171         if (status < 0)
3172                 goto error;
3173
3174         status = write16(state, SCU_RAM_AGC_KI_MAXMINGAIN_TH__A, 20);
3175         if (status < 0)
3176                 goto error;
3177         status = write16(state, SCU_RAM_AGC_CLP_SUM_MIN__A, clpSumMin);
3178         if (status < 0)
3179                 goto error;
3180         status = write16(state, SCU_RAM_AGC_SNS_SUM_MIN__A, snsSumMin);
3181         if (status < 0)
3182                 goto error;
3183         status = write16(state, SCU_RAM_AGC_CLP_DIR_TO__A, clpDirTo);
3184         if (status < 0)
3185                 goto error;
3186         status = write16(state, SCU_RAM_AGC_SNS_DIR_TO__A, snsDirTo);
3187         if (status < 0)
3188                 goto error;
3189         status = write16(state, SCU_RAM_AGC_KI_MINGAIN__A, 0x7fff);
3190         if (status < 0)
3191                 goto error;
3192         status = write16(state, SCU_RAM_AGC_KI_MAXGAIN__A, 0x0);
3193         if (status < 0)
3194                 goto error;
3195         status = write16(state, SCU_RAM_AGC_KI_MIN__A, 0x0117);
3196         if (status < 0)
3197                 goto error;
3198         status = write16(state, SCU_RAM_AGC_KI_MAX__A, 0x0657);
3199         if (status < 0)
3200                 goto error;
3201         status = write16(state, SCU_RAM_AGC_CLP_SUM__A, 0);
3202         if (status < 0)
3203                 goto error;
3204         status = write16(state, SCU_RAM_AGC_CLP_CYCCNT__A, 0);
3205         if (status < 0)
3206                 goto error;
3207         status = write16(state, SCU_RAM_AGC_CLP_DIR_WD__A, 0);
3208         if (status < 0)
3209                 goto error;
3210         status = write16(state, SCU_RAM_AGC_CLP_DIR_STP__A, 1);
3211         if (status < 0)
3212                 goto error;
3213         status = write16(state, SCU_RAM_AGC_SNS_SUM__A, 0);
3214         if (status < 0)
3215                 goto error;
3216         status = write16(state, SCU_RAM_AGC_SNS_CYCCNT__A, 0);
3217         if (status < 0)
3218                 goto error;
3219         status = write16(state, SCU_RAM_AGC_SNS_DIR_WD__A, 0);
3220         if (status < 0)
3221                 goto error;
3222         status = write16(state, SCU_RAM_AGC_SNS_DIR_STP__A, 1);
3223         if (status < 0)
3224                 goto error;
3225         status = write16(state, SCU_RAM_AGC_SNS_CYCLEN__A, 500);
3226         if (status < 0)
3227                 goto error;
3228         status = write16(state, SCU_RAM_AGC_KI_CYCLEN__A, 500);
3229         if (status < 0)
3230                 goto error;
3231
3232         /* Initialize inner-loop KI gain factors */
3233         status = read16(state, SCU_RAM_AGC_KI__A, &data);
3234         if (status < 0)
3235                 goto error;
3236
3237         data = 0x0657;
3238         data &= ~SCU_RAM_AGC_KI_RF__M;
3239         data |= (DRXK_KI_RAGC_QAM << SCU_RAM_AGC_KI_RF__B);
3240         data &= ~SCU_RAM_AGC_KI_IF__M;
3241         data |= (DRXK_KI_IAGC_QAM << SCU_RAM_AGC_KI_IF__B);
3242
3243         status = write16(state, SCU_RAM_AGC_KI__A, data);
3244 error:
3245         if (status < 0)
3246                 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
3247         return status;
3248 }
3249
3250 static int DVBTQAMGetAccPktErr(struct drxk_state *state, u16 *packetErr)
3251 {
3252         int status;
3253
3254         dprintk(1, "\n");
3255         if (packetErr == NULL)
3256                 status = write16(state, SCU_RAM_FEC_ACCUM_PKT_FAILURES__A, 0);
3257         else
3258                 status = read16(state, SCU_RAM_FEC_ACCUM_PKT_FAILURES__A, packetErr);
3259         if (status < 0)
3260                 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
3261         return status;
3262 }
3263
3264 static int DVBTScCommand(struct drxk_state *state,
3265                          u16 cmd, u16 subcmd,
3266                          u16 param0, u16 param1, u16 param2,
3267                          u16 param3, u16 param4)
3268 {
3269         u16 curCmd = 0;
3270         u16 errCode = 0;
3271         u16 retryCnt = 0;
3272         u16 scExec = 0;
3273         int status;
3274
3275         dprintk(1, "\n");
3276         status = read16(state, OFDM_SC_COMM_EXEC__A, &scExec);
3277         if (scExec != 1) {
3278                 /* SC is not running */
3279                 status = -EINVAL;
3280         }
3281         if (status < 0)
3282                 goto error;
3283
3284         /* Wait until sc is ready to receive command */
3285         retryCnt = 0;
3286         do {
3287                 msleep(1);
3288                 status = read16(state, OFDM_SC_RA_RAM_CMD__A, &curCmd);
3289                 retryCnt++;
3290         } while ((curCmd != 0) && (retryCnt < DRXK_MAX_RETRIES));
3291         if (retryCnt >= DRXK_MAX_RETRIES && (status < 0))
3292                 goto error;
3293
3294         /* Write sub-command */
3295         switch (cmd) {
3296                 /* All commands using sub-cmd */
3297         case OFDM_SC_RA_RAM_CMD_PROC_START:
3298         case OFDM_SC_RA_RAM_CMD_SET_PREF_PARAM:
3299         case OFDM_SC_RA_RAM_CMD_PROGRAM_PARAM:
3300                 status = write16(state, OFDM_SC_RA_RAM_CMD_ADDR__A, subcmd);
3301                 if (status < 0)
3302                         goto error;
3303                 break;
3304         default:
3305                 /* Do nothing */
3306                 break;
3307         }
3308
3309         /* Write needed parameters and the command */
3310         switch (cmd) {
3311                 /* All commands using 5 parameters */
3312                 /* All commands using 4 parameters */
3313                 /* All commands using 3 parameters */
3314                 /* All commands using 2 parameters */
3315         case OFDM_SC_RA_RAM_CMD_PROC_START:
3316         case OFDM_SC_RA_RAM_CMD_SET_PREF_PARAM:
3317         case OFDM_SC_RA_RAM_CMD_PROGRAM_PARAM:
3318                 status = write16(state, OFDM_SC_RA_RAM_PARAM1__A, param1);
3319                 /* All commands using 1 parameters */
3320         case OFDM_SC_RA_RAM_CMD_SET_ECHO_TIMING:
3321         case OFDM_SC_RA_RAM_CMD_USER_IO:
3322                 status = write16(state, OFDM_SC_RA_RAM_PARAM0__A, param0);
3323                 /* All commands using 0 parameters */
3324         case OFDM_SC_RA_RAM_CMD_GET_OP_PARAM:
3325         case OFDM_SC_RA_RAM_CMD_NULL:
3326                 /* Write command */
3327                 status = write16(state, OFDM_SC_RA_RAM_CMD__A, cmd);
3328                 break;
3329         default:
3330                 /* Unknown command */
3331                 status = -EINVAL;
3332         }
3333         if (status < 0)
3334                 goto error;
3335
3336         /* Wait until sc is ready processing command */
3337         retryCnt = 0;
3338         do {
3339                 msleep(1);
3340                 status = read16(state, OFDM_SC_RA_RAM_CMD__A, &curCmd);
3341                 retryCnt++;
3342         } while ((curCmd != 0) && (retryCnt < DRXK_MAX_RETRIES));
3343         if (retryCnt >= DRXK_MAX_RETRIES && (status < 0))
3344                 goto error;
3345
3346         /* Check for illegal cmd */
3347         status = read16(state, OFDM_SC_RA_RAM_CMD_ADDR__A, &errCode);
3348         if (errCode == 0xFFFF) {
3349                 /* illegal command */
3350                 status = -EINVAL;
3351         }
3352         if (status < 0)
3353                 goto error;
3354
3355         /* Retreive results parameters from SC */
3356         switch (cmd) {
3357                 /* All commands yielding 5 results */
3358                 /* All commands yielding 4 results */
3359                 /* All commands yielding 3 results */
3360                 /* All commands yielding 2 results */
3361                 /* All commands yielding 1 result */
3362         case OFDM_SC_RA_RAM_CMD_USER_IO:
3363         case OFDM_SC_RA_RAM_CMD_GET_OP_PARAM:
3364                 status = read16(state, OFDM_SC_RA_RAM_PARAM0__A, &(param0));
3365                 /* All commands yielding 0 results */
3366         case OFDM_SC_RA_RAM_CMD_SET_ECHO_TIMING:
3367         case OFDM_SC_RA_RAM_CMD_SET_TIMER:
3368         case OFDM_SC_RA_RAM_CMD_PROC_START:
3369         case OFDM_SC_RA_RAM_CMD_SET_PREF_PARAM:
3370         case OFDM_SC_RA_RAM_CMD_PROGRAM_PARAM:
3371         case OFDM_SC_RA_RAM_CMD_NULL:
3372                 break;
3373         default:
3374                 /* Unknown command */
3375                 status = -EINVAL;
3376                 break;
3377         }                       /* switch (cmd->cmd) */
3378 error:
3379         if (status < 0)
3380                 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
3381         return status;
3382 }
3383
3384 static int PowerUpDVBT(struct drxk_state *state)
3385 {
3386         enum DRXPowerMode powerMode = DRX_POWER_UP;
3387         int status;
3388
3389         dprintk(1, "\n");
3390         status = CtrlPowerMode(state, &powerMode);
3391         if (status < 0)
3392                 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
3393         return status;
3394 }
3395
3396 static int DVBTCtrlSetIncEnable(struct drxk_state *state, bool *enabled)
3397 {
3398         int status;
3399
3400         dprintk(1, "\n");
3401         if (*enabled == true)
3402                 status = write16(state, IQM_CF_BYPASSDET__A, 0);
3403         else
3404                 status = write16(state, IQM_CF_BYPASSDET__A, 1);
3405         if (status < 0)
3406                 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
3407         return status;
3408 }
3409
3410 #define DEFAULT_FR_THRES_8K     4000
3411 static int DVBTCtrlSetFrEnable(struct drxk_state *state, bool *enabled)
3412 {
3413
3414         int status;
3415
3416         dprintk(1, "\n");
3417         if (*enabled == true) {
3418                 /* write mask to 1 */
3419                 status = write16(state, OFDM_SC_RA_RAM_FR_THRES_8K__A,
3420                                    DEFAULT_FR_THRES_8K);
3421         } else {
3422                 /* write mask to 0 */
3423                 status = write16(state, OFDM_SC_RA_RAM_FR_THRES_8K__A, 0);
3424         }
3425         if (status < 0)
3426                 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
3427
3428         return status;
3429 }
3430
3431 static int DVBTCtrlSetEchoThreshold(struct drxk_state *state,
3432                                     struct DRXKCfgDvbtEchoThres_t *echoThres)
3433 {
3434         u16 data = 0;
3435         int status;
3436
3437         dprintk(1, "\n");
3438         status = read16(state, OFDM_SC_RA_RAM_ECHO_THRES__A, &data);
3439         if (status < 0)
3440                 goto error;
3441
3442         switch (echoThres->fftMode) {
3443         case DRX_FFTMODE_2K:
3444                 data &= ~OFDM_SC_RA_RAM_ECHO_THRES_2K__M;
3445                 data |= ((echoThres->threshold <<
3446                         OFDM_SC_RA_RAM_ECHO_THRES_2K__B)
3447                         & (OFDM_SC_RA_RAM_ECHO_THRES_2K__M));
3448                 goto error;
3449         case DRX_FFTMODE_8K:
3450                 data &= ~OFDM_SC_RA_RAM_ECHO_THRES_8K__M;
3451                 data |= ((echoThres->threshold <<
3452                         OFDM_SC_RA_RAM_ECHO_THRES_8K__B)
3453                         & (OFDM_SC_RA_RAM_ECHO_THRES_8K__M));
3454                 goto error;
3455         default:
3456                 return -EINVAL;
3457                 goto error;
3458         }
3459
3460         status = write16(state, OFDM_SC_RA_RAM_ECHO_THRES__A, data);
3461 error:
3462         if (status < 0)
3463                 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
3464         return status;
3465 }
3466
3467 static int DVBTCtrlSetSqiSpeed(struct drxk_state *state,
3468                                enum DRXKCfgDvbtSqiSpeed *speed)
3469 {
3470         int status = -EINVAL;
3471
3472         dprintk(1, "\n");
3473
3474         switch (*speed) {
3475         case DRXK_DVBT_SQI_SPEED_FAST:
3476         case DRXK_DVBT_SQI_SPEED_MEDIUM:
3477         case DRXK_DVBT_SQI_SPEED_SLOW:
3478                 break;
3479         default:
3480                 goto error;
3481         }
3482         status = write16(state, SCU_RAM_FEC_PRE_RS_BER_FILTER_SH__A,
3483                            (u16) *speed);
3484 error:
3485         if (status < 0)
3486                 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
3487         return status;
3488 }
3489
3490 /*============================================================================*/
3491
3492 /**
3493 * \brief Activate DVBT specific presets
3494 * \param demod instance of demodulator.
3495 * \return DRXStatus_t.
3496 *
3497 * Called in DVBTSetStandard
3498 *
3499 */
3500 static int DVBTActivatePresets(struct drxk_state *state)
3501 {
3502         int status;
3503         bool setincenable = false;
3504         bool setfrenable = true;
3505
3506         struct DRXKCfgDvbtEchoThres_t echoThres2k = { 0, DRX_FFTMODE_2K };
3507         struct DRXKCfgDvbtEchoThres_t echoThres8k = { 0, DRX_FFTMODE_8K };
3508
3509         dprintk(1, "\n");
3510         status = DVBTCtrlSetIncEnable(state, &setincenable);
3511         if (status < 0)
3512                 goto error;
3513         status = DVBTCtrlSetFrEnable(state, &setfrenable);
3514         if (status < 0)
3515                 goto error;
3516         status = DVBTCtrlSetEchoThreshold(state, &echoThres2k);
3517         if (status < 0)
3518                 goto error;
3519         status = DVBTCtrlSetEchoThreshold(state, &echoThres8k);
3520         if (status < 0)
3521                 goto error;
3522         status = write16(state, SCU_RAM_AGC_INGAIN_TGT_MAX__A, state->m_dvbtIfAgcCfg.IngainTgtMax);
3523 error:
3524         if (status < 0)
3525                 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
3526         return status;
3527 }
3528
3529 /*============================================================================*/
3530
3531 /**
3532 * \brief Initialize channelswitch-independent settings for DVBT.
3533 * \param demod instance of demodulator.
3534 * \return DRXStatus_t.
3535 *
3536 * For ROM code channel filter taps are loaded from the bootloader. For microcode
3537 * the DVB-T taps from the drxk_filters.h are used.
3538 */
3539 static int SetDVBTStandard(struct drxk_state *state,
3540                            enum OperationMode oMode)
3541 {
3542         u16 cmdResult = 0;
3543         u16 data = 0;
3544         int status;
3545
3546         dprintk(1, "\n");
3547
3548         PowerUpDVBT(state);
3549         /* added antenna switch */
3550         SwitchAntennaToDVBT(state);
3551         /* send OFDM reset command */
3552         status = scu_command(state, SCU_RAM_COMMAND_STANDARD_OFDM | SCU_RAM_COMMAND_CMD_DEMOD_RESET, 0, NULL, 1, &cmdResult);
3553         if (status < 0)
3554                 goto error;
3555
3556         /* send OFDM setenv command */
3557         status = scu_command(state, SCU_RAM_COMMAND_STANDARD_OFDM | SCU_RAM_COMMAND_CMD_DEMOD_SET_ENV, 0, NULL, 1, &cmdResult);
3558         if (status < 0)
3559                 goto error;
3560
3561         /* reset datapath for OFDM, processors first */
3562         status = write16(state, OFDM_SC_COMM_EXEC__A, OFDM_SC_COMM_EXEC_STOP);
3563         if (status < 0)
3564                 goto error;
3565         status = write16(state, OFDM_LC_COMM_EXEC__A, OFDM_LC_COMM_EXEC_STOP);
3566         if (status < 0)
3567                 goto error;
3568         status = write16(state, IQM_COMM_EXEC__A, IQM_COMM_EXEC_B_STOP);
3569         if (status < 0)
3570                 goto error;
3571
3572         /* IQM setup */
3573         /* synchronize on ofdstate->m_festart */
3574         status = write16(state, IQM_AF_UPD_SEL__A, 1);
3575         if (status < 0)
3576                 goto error;
3577         /* window size for clipping ADC detection */
3578         status = write16(state, IQM_AF_CLP_LEN__A, 0);
3579         if (status < 0)
3580                 goto error;
3581         /* window size for for sense pre-SAW detection */
3582         status = write16(state, IQM_AF_SNS_LEN__A, 0);
3583         if (status < 0)
3584                 goto error;
3585         /* sense threshold for sense pre-SAW detection */
3586         status = write16(state, IQM_AF_AMUX__A, IQM_AF_AMUX_SIGNAL2ADC);
3587         if (status < 0)
3588                 goto error;
3589         status = SetIqmAf(state, true);
3590         if (status < 0)
3591                 goto error;
3592
3593         status = write16(state, IQM_AF_AGC_RF__A, 0);
3594         if (status < 0)
3595                 goto error;
3596
3597         /* Impulse noise cruncher setup */
3598         status = write16(state, IQM_AF_INC_LCT__A, 0);  /* crunch in IQM_CF */
3599         if (status < 0)
3600                 goto error;
3601         status = write16(state, IQM_CF_DET_LCT__A, 0);  /* detect in IQM_CF */
3602         if (status < 0)
3603                 goto error;
3604         status = write16(state, IQM_CF_WND_LEN__A, 3);  /* peak detector window length */
3605         if (status < 0)
3606                 goto error;
3607
3608         status = write16(state, IQM_RC_STRETCH__A, 16);
3609         if (status < 0)
3610                 goto error;
3611         status = write16(state, IQM_CF_OUT_ENA__A, 0x4);        /* enable output 2 */
3612         if (status < 0)
3613                 goto error;
3614         status = write16(state, IQM_CF_DS_ENA__A, 0x4); /* decimate output 2 */
3615         if (status < 0)
3616                 goto error;
3617         status = write16(state, IQM_CF_SCALE__A, 1600);
3618         if (status < 0)
3619                 goto error;
3620         status = write16(state, IQM_CF_SCALE_SH__A, 0);
3621         if (status < 0)
3622                 goto error;
3623
3624         /* virtual clipping threshold for clipping ADC detection */
3625         status = write16(state, IQM_AF_CLP_TH__A, 448);
3626         if (status < 0)
3627                 goto error;
3628         status = write16(state, IQM_CF_DATATH__A, 495); /* crunching threshold */
3629         if (status < 0)
3630                 goto error;
3631
3632         status = BLChainCmd(state, DRXK_BL_ROM_OFFSET_TAPS_DVBT, DRXK_BLCC_NR_ELEMENTS_TAPS, DRXK_BLC_TIMEOUT);
3633         if (status < 0)
3634                 goto error;
3635
3636         status = write16(state, IQM_CF_PKDTH__A, 2);    /* peak detector threshold */
3637         if (status < 0)
3638                 goto error;
3639         status = write16(state, IQM_CF_POW_MEAS_LEN__A, 2);
3640         if (status < 0)
3641                 goto error;
3642         /* enable power measurement interrupt */
3643         status = write16(state, IQM_CF_COMM_INT_MSK__A, 1);
3644         if (status < 0)
3645                 goto error;
3646         status = write16(state, IQM_COMM_EXEC__A, IQM_COMM_EXEC_B_ACTIVE);
3647         if (status < 0)
3648                 goto error;
3649
3650         /* IQM will not be reset from here, sync ADC and update/init AGC */
3651         status = ADCSynchronization(state);
3652         if (status < 0)
3653                 goto error;
3654         status = SetPreSaw(state, &state->m_dvbtPreSawCfg);
3655         if (status < 0)
3656                 goto error;
3657
3658         /* Halt SCU to enable safe non-atomic accesses */
3659         status = write16(state, SCU_COMM_EXEC__A, SCU_COMM_EXEC_HOLD);
3660         if (status < 0)
3661                 goto error;
3662
3663         status = SetAgcRf(state, &state->m_dvbtRfAgcCfg, true);
3664         if (status < 0)
3665                 goto error;
3666         status = SetAgcIf(state, &state->m_dvbtIfAgcCfg, true);
3667         if (status < 0)
3668                 goto error;
3669
3670         /* Set Noise Estimation notch width and enable DC fix */
3671         status = read16(state, OFDM_SC_RA_RAM_CONFIG__A, &data);
3672         if (status < 0)
3673                 goto error;
3674         data |= OFDM_SC_RA_RAM_CONFIG_NE_FIX_ENABLE__M;
3675         status = write16(state, OFDM_SC_RA_RAM_CONFIG__A, data);
3676         if (status < 0)
3677                 goto error;
3678
3679         /* Activate SCU to enable SCU commands */
3680         status = write16(state, SCU_COMM_EXEC__A, SCU_COMM_EXEC_ACTIVE);
3681         if (status < 0)
3682                 goto error;
3683
3684         if (!state->m_DRXK_A3_ROM_CODE) {
3685                 /* AGCInit() is not done for DVBT, so set agcFastClipCtrlDelay  */
3686                 status = write16(state, SCU_RAM_AGC_FAST_CLP_CTRL_DELAY__A, state->m_dvbtIfAgcCfg.FastClipCtrlDelay);
3687                 if (status < 0)
3688                         goto error;
3689         }
3690
3691         /* OFDM_SC setup */
3692 #ifdef COMPILE_FOR_NONRT
3693         status = write16(state, OFDM_SC_RA_RAM_BE_OPT_DELAY__A, 1);
3694         if (status < 0)
3695                 goto error;
3696         status = write16(state, OFDM_SC_RA_RAM_BE_OPT_INIT_DELAY__A, 2);
3697         if (status < 0)
3698                 goto error;
3699 #endif
3700
3701         /* FEC setup */
3702         status = write16(state, FEC_DI_INPUT_CTL__A, 1);        /* OFDM input */
3703         if (status < 0)
3704                 goto error;
3705
3706
3707 #ifdef COMPILE_FOR_NONRT
3708         status = write16(state, FEC_RS_MEASUREMENT_PERIOD__A, 0x400);
3709         if (status < 0)
3710                 goto error;
3711 #else
3712         status = write16(state, FEC_RS_MEASUREMENT_PERIOD__A, 0x1000);
3713         if (status < 0)
3714                 goto error;
3715 #endif
3716         status = write16(state, FEC_RS_MEASUREMENT_PRESCALE__A, 0x0001);
3717         if (status < 0)
3718                 goto error;
3719
3720         /* Setup MPEG bus */
3721         status = MPEGTSDtoSetup(state, OM_DVBT);
3722         if (status < 0)
3723                 goto error;
3724         /* Set DVBT Presets */
3725         status = DVBTActivatePresets(state);
3726         if (status < 0)
3727                 goto error;
3728
3729 error:
3730         if (status < 0)
3731                 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
3732         return status;
3733 }
3734
3735 /*============================================================================*/
3736 /**
3737 * \brief Start dvbt demodulating for channel.
3738 * \param demod instance of demodulator.
3739 * \return DRXStatus_t.
3740 */
3741 static int DVBTStart(struct drxk_state *state)
3742 {
3743         u16 param1;
3744         int status;
3745         /* DRXKOfdmScCmd_t scCmd; */
3746
3747         dprintk(1, "\n");
3748         /* Start correct processes to get in lock */
3749         /* DRXK: OFDM_SC_RA_RAM_PROC_LOCKTRACK is no longer in mapfile! */
3750         param1 = OFDM_SC_RA_RAM_LOCKTRACK_MIN;
3751         status = DVBTScCommand(state, OFDM_SC_RA_RAM_CMD_PROC_START, 0, OFDM_SC_RA_RAM_SW_EVENT_RUN_NMASK__M, param1, 0, 0, 0);
3752         if (status < 0)
3753                 goto error;
3754         /* Start FEC OC */
3755         status = MPEGTSStart(state);
3756         if (status < 0)
3757                 goto error;
3758         status = write16(state, FEC_COMM_EXEC__A, FEC_COMM_EXEC_ACTIVE);
3759         if (status < 0)
3760                 goto error;
3761 error:
3762         if (status < 0)
3763                 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
3764         return status;
3765 }
3766
3767
3768 /*============================================================================*/
3769
3770 /**
3771 * \brief Set up dvbt demodulator for channel.
3772 * \param demod instance of demodulator.
3773 * \return DRXStatus_t.
3774 * // original DVBTSetChannel()
3775 */
3776 static int SetDVBT(struct drxk_state *state, u16 IntermediateFreqkHz,
3777                    s32 tunerFreqOffset)
3778 {
3779         u16 cmdResult = 0;
3780         u16 transmissionParams = 0;
3781         u16 operationMode = 0;
3782         u32 iqmRcRateOfs = 0;
3783         u32 bandwidth = 0;
3784         u16 param1;
3785         int status;
3786
3787         dprintk(1, "IF =%d, TFO = %d\n", IntermediateFreqkHz, tunerFreqOffset);
3788
3789         status = scu_command(state, SCU_RAM_COMMAND_STANDARD_OFDM | SCU_RAM_COMMAND_CMD_DEMOD_STOP, 0, NULL, 1, &cmdResult);
3790         if (status < 0)
3791                 goto error;
3792
3793         /* Halt SCU to enable safe non-atomic accesses */
3794         status = write16(state, SCU_COMM_EXEC__A, SCU_COMM_EXEC_HOLD);
3795         if (status < 0)
3796                 goto error;
3797
3798         /* Stop processors */
3799         status = write16(state, OFDM_SC_COMM_EXEC__A, OFDM_SC_COMM_EXEC_STOP);
3800         if (status < 0)
3801                 goto error;
3802         status = write16(state, OFDM_LC_COMM_EXEC__A, OFDM_LC_COMM_EXEC_STOP);
3803         if (status < 0)
3804                 goto error;
3805
3806         /* Mandatory fix, always stop CP, required to set spl offset back to
3807                 hardware default (is set to 0 by ucode during pilot detection */
3808         status = write16(state, OFDM_CP_COMM_EXEC__A, OFDM_CP_COMM_EXEC_STOP);
3809         if (status < 0)
3810                 goto error;
3811
3812         /*== Write channel settings to device =====================================*/
3813
3814         /* mode */
3815         switch (state->param.u.ofdm.transmission_mode) {
3816         case TRANSMISSION_MODE_AUTO:
3817         default:
3818                 operationMode |= OFDM_SC_RA_RAM_OP_AUTO_MODE__M;
3819                 /* fall through , try first guess DRX_FFTMODE_8K */
3820         case TRANSMISSION_MODE_8K:
3821                 transmissionParams |= OFDM_SC_RA_RAM_OP_PARAM_MODE_8K;
3822                 goto error;
3823         case TRANSMISSION_MODE_2K:
3824                 transmissionParams |= OFDM_SC_RA_RAM_OP_PARAM_MODE_2K;
3825                 goto error;
3826         }
3827
3828         /* guard */
3829         switch (state->param.u.ofdm.guard_interval) {
3830         default:
3831         case GUARD_INTERVAL_AUTO:
3832                 operationMode |= OFDM_SC_RA_RAM_OP_AUTO_GUARD__M;
3833                 /* fall through , try first guess DRX_GUARD_1DIV4 */
3834         case GUARD_INTERVAL_1_4:
3835                 transmissionParams |= OFDM_SC_RA_RAM_OP_PARAM_GUARD_4;
3836                 goto error;
3837         case GUARD_INTERVAL_1_32:
3838                 transmissionParams |= OFDM_SC_RA_RAM_OP_PARAM_GUARD_32;
3839                 goto error;
3840         case GUARD_INTERVAL_1_16:
3841                 transmissionParams |= OFDM_SC_RA_RAM_OP_PARAM_GUARD_16;
3842                 goto error;
3843         case GUARD_INTERVAL_1_8:
3844                 transmissionParams |= OFDM_SC_RA_RAM_OP_PARAM_GUARD_8;
3845                 goto error;
3846         }
3847
3848         /* hierarchy */
3849         switch (state->param.u.ofdm.hierarchy_information) {
3850         case HIERARCHY_AUTO:
3851         case HIERARCHY_NONE:
3852         default:
3853                 operationMode |= OFDM_SC_RA_RAM_OP_AUTO_HIER__M;
3854                 /* fall through , try first guess SC_RA_RAM_OP_PARAM_HIER_NO */
3855                 /* transmissionParams |= OFDM_SC_RA_RAM_OP_PARAM_HIER_NO; */
3856                 /* break; */
3857         case HIERARCHY_1:
3858                 transmissionParams |= OFDM_SC_RA_RAM_OP_PARAM_HIER_A1;
3859                 break;
3860         case HIERARCHY_2:
3861                 transmissionParams |= OFDM_SC_RA_RAM_OP_PARAM_HIER_A2;
3862                 break;
3863         case HIERARCHY_4:
3864                 transmissionParams |= OFDM_SC_RA_RAM_OP_PARAM_HIER_A4;
3865                 break;
3866         }
3867
3868
3869         /* constellation */
3870         switch (state->param.u.ofdm.constellation) {
3871         case QAM_AUTO:
3872         default:
3873                 operationMode |= OFDM_SC_RA_RAM_OP_AUTO_CONST__M;
3874                 /* fall through , try first guess DRX_CONSTELLATION_QAM64 */
3875         case QAM_64:
3876                 transmissionParams |= OFDM_SC_RA_RAM_OP_PARAM_CONST_QAM64;
3877                 break;
3878         case QPSK:
3879                 transmissionParams |= OFDM_SC_RA_RAM_OP_PARAM_CONST_QPSK;
3880                 break;
3881         case QAM_16:
3882                 transmissionParams |= OFDM_SC_RA_RAM_OP_PARAM_CONST_QAM16;
3883                 break;
3884         }
3885 #if 0
3886         /* No hierachical channels support in BDA */
3887         /* Priority (only for hierarchical channels) */
3888         switch (channel->priority) {
3889         case DRX_PRIORITY_LOW:
3890                 transmissionParams |= OFDM_SC_RA_RAM_OP_PARAM_PRIO_LO;
3891                 WR16(devAddr, OFDM_EC_SB_PRIOR__A,
3892                         OFDM_EC_SB_PRIOR_LO);
3893                 break;
3894         case DRX_PRIORITY_HIGH:
3895                 transmissionParams |= OFDM_SC_RA_RAM_OP_PARAM_PRIO_HI;
3896                 WR16(devAddr, OFDM_EC_SB_PRIOR__A,
3897                         OFDM_EC_SB_PRIOR_HI));
3898                 break;
3899         case DRX_PRIORITY_UNKNOWN:      /* fall through */
3900         default:
3901                 status = -EINVAL;
3902                 goto error;
3903         }
3904 #else
3905         /* Set Priorty high */
3906         transmissionParams |= OFDM_SC_RA_RAM_OP_PARAM_PRIO_HI;
3907         status = write16(state, OFDM_EC_SB_PRIOR__A, OFDM_EC_SB_PRIOR_HI);
3908         if (status < 0)
3909                 goto error;
3910 #endif
3911
3912         /* coderate */
3913         switch (state->param.u.ofdm.code_rate_HP) {
3914         case FEC_AUTO:
3915         default:
3916                 operationMode |= OFDM_SC_RA_RAM_OP_AUTO_RATE__M;
3917                 /* fall through , try first guess DRX_CODERATE_2DIV3 */
3918         case FEC_2_3:
3919                 transmissionParams |= OFDM_SC_RA_RAM_OP_PARAM_RATE_2_3;
3920                 break;
3921         case FEC_1_2:
3922                 transmissionParams |= OFDM_SC_RA_RAM_OP_PARAM_RATE_1_2;
3923                 break;
3924         case FEC_3_4:
3925                 transmissionParams |= OFDM_SC_RA_RAM_OP_PARAM_RATE_3_4;
3926                 break;
3927         case FEC_5_6:
3928                 transmissionParams |= OFDM_SC_RA_RAM_OP_PARAM_RATE_5_6;
3929                 break;
3930         case FEC_7_8:
3931                 transmissionParams |= OFDM_SC_RA_RAM_OP_PARAM_RATE_7_8;
3932                 break;
3933         }
3934
3935         /* SAW filter selection: normaly not necesarry, but if wanted
3936                 the application can select a SAW filter via the driver by using UIOs */
3937         /* First determine real bandwidth (Hz) */
3938         /* Also set delay for impulse noise cruncher */
3939         /* Also set parameters for EC_OC fix, note EC_OC_REG_TMD_HIL_MAR is changed
3940                 by SC for fix for some 8K,1/8 guard but is restored by InitEC and ResetEC
3941                 functions */
3942         switch (state->param.u.ofdm.bandwidth) {
3943         case BANDWIDTH_AUTO:
3944         case BANDWIDTH_8_MHZ:
3945                 bandwidth = DRXK_BANDWIDTH_8MHZ_IN_HZ;
3946                 status = write16(state, OFDM_SC_RA_RAM_SRMM_FIX_FACT_8K__A, 3052);
3947                 if (status < 0)
3948                         goto error;
3949                 /* cochannel protection for PAL 8 MHz */
3950                 status = write16(state, OFDM_SC_RA_RAM_NI_INIT_8K_PER_LEFT__A, 7);
3951                 if (status < 0)
3952                         goto error;
3953                 status = write16(state, OFDM_SC_RA_RAM_NI_INIT_8K_PER_RIGHT__A, 7);
3954                 if (status < 0)
3955                         goto error;
3956                 status = write16(state, OFDM_SC_RA_RAM_NI_INIT_2K_PER_LEFT__A, 7);
3957                 if (status < 0)
3958                         goto error;
3959                 status = write16(state, OFDM_SC_RA_RAM_NI_INIT_2K_PER_RIGHT__A, 1);
3960                 if (status < 0)
3961                         goto error;
3962                 break;
3963         case BANDWIDTH_7_MHZ:
3964                 bandwidth = DRXK_BANDWIDTH_7MHZ_IN_HZ;
3965                 status = write16(state, OFDM_SC_RA_RAM_SRMM_FIX_FACT_8K__A, 3491);
3966                 if (status < 0)
3967                         goto error;
3968                 /* cochannel protection for PAL 7 MHz */
3969                 status = write16(state, OFDM_SC_RA_RAM_NI_INIT_8K_PER_LEFT__A, 8);
3970                 if (status < 0)
3971                         goto error;
3972                 status = write16(state, OFDM_SC_RA_RAM_NI_INIT_8K_PER_RIGHT__A, 8);
3973                 if (status < 0)
3974                         goto error;
3975                 status = write16(state, OFDM_SC_RA_RAM_NI_INIT_2K_PER_LEFT__A, 4);
3976                 if (status < 0)
3977                         goto error;
3978                 status = write16(state, OFDM_SC_RA_RAM_NI_INIT_2K_PER_RIGHT__A, 1);
3979                 if (status < 0)
3980                         goto error;
3981                 break;
3982         case BANDWIDTH_6_MHZ:
3983                 bandwidth = DRXK_BANDWIDTH_6MHZ_IN_HZ;
3984                 status = write16(state, OFDM_SC_RA_RAM_SRMM_FIX_FACT_8K__A, 4073);
3985                 if (status < 0)
3986                         goto error;
3987                 /* cochannel protection for NTSC 6 MHz */
3988                 status = write16(state, OFDM_SC_RA_RAM_NI_INIT_8K_PER_LEFT__A, 19);
3989                 if (status < 0)
3990                         goto error;
3991                 status = write16(state, OFDM_SC_RA_RAM_NI_INIT_8K_PER_RIGHT__A, 19);
3992                 if (status < 0)
3993                         goto error;
3994                 status = write16(state, OFDM_SC_RA_RAM_NI_INIT_2K_PER_LEFT__A, 14);
3995                 if (status < 0)
3996                         goto error;
3997                 status = write16(state, OFDM_SC_RA_RAM_NI_INIT_2K_PER_RIGHT__A, 1);
3998                 if (status < 0)
3999                         goto error;
4000                 break;
4001         default:
4002                 status = -EINVAL;
4003                 goto error;
4004         }
4005
4006         if (iqmRcRateOfs == 0) {
4007                 /* Now compute IQM_RC_RATE_OFS
4008                         (((SysFreq/BandWidth)/2)/2) -1) * 2^23)
4009                         =>
4010                         ((SysFreq / BandWidth) * (2^21)) - (2^23)
4011                         */
4012                 /* (SysFreq / BandWidth) * (2^28)  */
4013                 /* assert (MAX(sysClk)/MIN(bandwidth) < 16)
4014                         => assert(MAX(sysClk) < 16*MIN(bandwidth))
4015                         => assert(109714272 > 48000000) = true so Frac 28 can be used  */
4016                 iqmRcRateOfs = Frac28a((u32)
4017                                         ((state->m_sysClockFreq *
4018                                                 1000) / 3), bandwidth);
4019                 /* (SysFreq / BandWidth) * (2^21), rounding before truncating  */
4020                 if ((iqmRcRateOfs & 0x7fL) >= 0x40)
4021                         iqmRcRateOfs += 0x80L;
4022                 iqmRcRateOfs = iqmRcRateOfs >> 7;
4023                 /* ((SysFreq / BandWidth) * (2^21)) - (2^23)  */
4024                 iqmRcRateOfs = iqmRcRateOfs - (1 << 23);
4025         }
4026
4027         iqmRcRateOfs &=
4028                 ((((u32) IQM_RC_RATE_OFS_HI__M) <<
4029                 IQM_RC_RATE_OFS_LO__W) | IQM_RC_RATE_OFS_LO__M);
4030         status = write32(state, IQM_RC_RATE_OFS_LO__A, iqmRcRateOfs);
4031         if (status < 0)
4032                 goto error;
4033
4034         /* Bandwidth setting done */
4035
4036 #if 0
4037         status = DVBTSetFrequencyShift(demod, channel, tunerOffset);
4038         if (status < 0)
4039                 goto error;
4040 #endif
4041         status = SetFrequencyShifter(state, IntermediateFreqkHz, tunerFreqOffset, true);
4042         if (status < 0)
4043                 goto error;
4044
4045         /*== Start SC, write channel settings to SC ===============================*/
4046
4047         /* Activate SCU to enable SCU commands */
4048         status = write16(state, SCU_COMM_EXEC__A, SCU_COMM_EXEC_ACTIVE);
4049         if (status < 0)
4050                 goto error;
4051
4052         /* Enable SC after setting all other parameters */
4053         status = write16(state, OFDM_SC_COMM_STATE__A, 0);
4054         if (status < 0)
4055                 goto error;
4056         status = write16(state, OFDM_SC_COMM_EXEC__A, 1);
4057         if (status < 0)
4058                 goto error;
4059
4060
4061         status = scu_command(state, SCU_RAM_COMMAND_STANDARD_OFDM | SCU_RAM_COMMAND_CMD_DEMOD_START, 0, NULL, 1, &cmdResult);
4062         if (status < 0)
4063                 goto error;
4064
4065         /* Write SC parameter registers, set all AUTO flags in operation mode */
4066         param1 = (OFDM_SC_RA_RAM_OP_AUTO_MODE__M |
4067                         OFDM_SC_RA_RAM_OP_AUTO_GUARD__M |
4068                         OFDM_SC_RA_RAM_OP_AUTO_CONST__M |
4069                         OFDM_SC_RA_RAM_OP_AUTO_HIER__M |
4070                         OFDM_SC_RA_RAM_OP_AUTO_RATE__M);
4071         status = DVBTScCommand(state, OFDM_SC_RA_RAM_CMD_SET_PREF_PARAM,
4072                                 0, transmissionParams, param1, 0, 0, 0);
4073         if (status < 0)
4074                 goto error;
4075
4076         if (!state->m_DRXK_A3_ROM_CODE)
4077                 status = DVBTCtrlSetSqiSpeed(state, &state->m_sqiSpeed);
4078 error:
4079         if (status < 0)
4080                 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
4081
4082         return status;
4083 }
4084
4085
4086 /*============================================================================*/
4087
4088 /**
4089 * \brief Retreive lock status .
4090 * \param demod    Pointer to demodulator instance.
4091 * \param lockStat Pointer to lock status structure.
4092 * \return DRXStatus_t.
4093 *
4094 */
4095 static int GetDVBTLockStatus(struct drxk_state *state, u32 *pLockStatus)
4096 {
4097         int status;
4098         const u16 mpeg_lock_mask = (OFDM_SC_RA_RAM_LOCK_MPEG__M |
4099                                     OFDM_SC_RA_RAM_LOCK_FEC__M);
4100         const u16 fec_lock_mask = (OFDM_SC_RA_RAM_LOCK_FEC__M);
4101         const u16 demod_lock_mask = OFDM_SC_RA_RAM_LOCK_DEMOD__M;
4102
4103         u16 ScRaRamLock = 0;
4104         u16 ScCommExec = 0;
4105
4106         dprintk(1, "\n");
4107
4108         *pLockStatus = NOT_LOCKED;
4109         /* driver 0.9.0 */
4110         /* Check if SC is running */
4111         status = read16(state, OFDM_SC_COMM_EXEC__A, &ScCommExec);
4112         if (status < 0)
4113                 goto end;
4114         if (ScCommExec == OFDM_SC_COMM_EXEC_STOP)
4115                 goto end;
4116
4117         status = read16(state, OFDM_SC_RA_RAM_LOCK__A, &ScRaRamLock);
4118         if (status < 0)
4119                 goto end;
4120
4121         if ((ScRaRamLock & mpeg_lock_mask) == mpeg_lock_mask)
4122                 *pLockStatus = MPEG_LOCK;
4123         else if ((ScRaRamLock & fec_lock_mask) == fec_lock_mask)
4124                 *pLockStatus = FEC_LOCK;
4125         else if ((ScRaRamLock & demod_lock_mask) == demod_lock_mask)
4126                 *pLockStatus = DEMOD_LOCK;
4127         else if (ScRaRamLock & OFDM_SC_RA_RAM_LOCK_NODVBT__M)
4128                 *pLockStatus = NEVER_LOCK;
4129 end:
4130         if (status < 0)
4131                 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
4132
4133         return status;
4134 }
4135
4136 static int PowerUpQAM(struct drxk_state *state)
4137 {
4138         enum DRXPowerMode powerMode = DRXK_POWER_DOWN_OFDM;
4139         int status;
4140
4141         dprintk(1, "\n");
4142         status = CtrlPowerMode(state, &powerMode);
4143         if (status < 0)
4144                 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
4145
4146         return status;
4147 }
4148
4149
4150 /** Power Down QAM */
4151 static int PowerDownQAM(struct drxk_state *state)
4152 {
4153         u16 data = 0;
4154         u16 cmdResult;
4155         int status = 0;
4156
4157         dprintk(1, "\n");
4158         status = read16(state, SCU_COMM_EXEC__A, &data);
4159         if (status < 0)
4160                 goto error;
4161         if (data == SCU_COMM_EXEC_ACTIVE) {
4162                 /*
4163                         STOP demodulator
4164                         QAM and HW blocks
4165                         */
4166                 /* stop all comstate->m_exec */
4167                 status = write16(state, QAM_COMM_EXEC__A, QAM_COMM_EXEC_STOP);
4168                 if (status < 0)
4169                         goto error;
4170                 status = scu_command(state, SCU_RAM_COMMAND_STANDARD_QAM | SCU_RAM_COMMAND_CMD_DEMOD_STOP, 0, NULL, 1, &cmdResult);
4171                 if (status < 0)
4172                         goto error;
4173         }
4174         /* powerdown AFE                   */
4175         status = SetIqmAf(state, false);
4176
4177 error:
4178         if (status < 0)
4179                 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
4180
4181         return status;
4182 }
4183
4184 /*============================================================================*/
4185
4186 /**
4187 * \brief Setup of the QAM Measurement intervals for signal quality
4188 * \param demod instance of demod.
4189 * \param constellation current constellation.
4190 * \return DRXStatus_t.
4191 *
4192 *  NOTE:
4193 *  Take into account that for certain settings the errorcounters can overflow.
4194 *  The implementation does not check this.
4195 *
4196 */
4197 static int SetQAMMeasurement(struct drxk_state *state,
4198                              enum EDrxkConstellation constellation,
4199                              u32 symbolRate)
4200 {
4201         u32 fecBitsDesired = 0; /* BER accounting period */
4202         u32 fecRsPeriodTotal = 0;       /* Total period */
4203         u16 fecRsPrescale = 0;  /* ReedSolomon Measurement Prescale */
4204         u16 fecRsPeriod = 0;    /* Value for corresponding I2C register */
4205         int status = 0;
4206
4207         dprintk(1, "\n");
4208
4209         fecRsPrescale = 1;
4210         /* fecBitsDesired = symbolRate [kHz] *
4211                 FrameLenght [ms] *
4212                 (constellation + 1) *
4213                 SyncLoss (== 1) *
4214                 ViterbiLoss (==1)
4215                 */
4216         switch (constellation) {
4217         case DRX_CONSTELLATION_QAM16:
4218                 fecBitsDesired = 4 * symbolRate;
4219                 break;
4220         case DRX_CONSTELLATION_QAM32:
4221                 fecBitsDesired = 5 * symbolRate;
4222                 break;
4223         case DRX_CONSTELLATION_QAM64:
4224                 fecBitsDesired = 6 * symbolRate;
4225                 break;
4226         case DRX_CONSTELLATION_QAM128:
4227                 fecBitsDesired = 7 * symbolRate;
4228                 break;
4229         case DRX_CONSTELLATION_QAM256:
4230                 fecBitsDesired = 8 * symbolRate;
4231                 break;
4232         default:
4233                 status = -EINVAL;
4234         }
4235         if (status < 0)
4236                 goto error;
4237
4238         fecBitsDesired /= 1000; /* symbolRate [Hz] -> symbolRate [kHz]  */
4239         fecBitsDesired *= 500;  /* meas. period [ms] */
4240
4241         /* Annex A/C: bits/RsPeriod = 204 * 8 = 1632 */
4242         /* fecRsPeriodTotal = fecBitsDesired / 1632 */
4243         fecRsPeriodTotal = (fecBitsDesired / 1632UL) + 1;       /* roughly ceil */
4244
4245         /* fecRsPeriodTotal =  fecRsPrescale * fecRsPeriod  */
4246         fecRsPrescale = 1 + (u16) (fecRsPeriodTotal >> 16);
4247         if (fecRsPrescale == 0) {
4248                 /* Divide by zero (though impossible) */
4249                 status = -EINVAL;
4250                 if (status < 0)
4251                         goto error;
4252         }
4253         fecRsPeriod =
4254                 ((u16) fecRsPeriodTotal +
4255                 (fecRsPrescale >> 1)) / fecRsPrescale;
4256
4257         /* write corresponding registers */
4258         status = write16(state, FEC_RS_MEASUREMENT_PERIOD__A, fecRsPeriod);
4259         if (status < 0)
4260                 goto error;
4261         status = write16(state, FEC_RS_MEASUREMENT_PRESCALE__A, fecRsPrescale);
4262         if (status < 0)
4263                 goto error;
4264         status = write16(state, FEC_OC_SNC_FAIL_PERIOD__A, fecRsPeriod);
4265 error:
4266         if (status < 0)
4267                 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
4268         return status;
4269 }
4270
4271 static int SetQAM16(struct drxk_state *state)
4272 {
4273         int status = 0;
4274
4275         dprintk(1, "\n");
4276         /* QAM Equalizer Setup */
4277         /* Equalizer */
4278         status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD0__A, 13517);
4279         if (status < 0)
4280                 goto error;
4281         status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD1__A, 13517);
4282         if (status < 0)
4283                 goto error;
4284         status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD2__A, 13517);
4285         if (status < 0)
4286                 goto error;
4287         status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD3__A, 13517);
4288         if (status < 0)
4289                 goto error;
4290         status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD4__A, 13517);
4291         if (status < 0)
4292                 goto error;
4293         status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD5__A, 13517);
4294         if (status < 0)
4295                 goto error;
4296         /* Decision Feedback Equalizer */
4297         status = write16(state, QAM_DQ_QUAL_FUN0__A, 2);
4298         if (status < 0)
4299                 goto error;
4300         status = write16(state, QAM_DQ_QUAL_FUN1__A, 2);
4301         if (status < 0)
4302                 goto error;
4303         status = write16(state, QAM_DQ_QUAL_FUN2__A, 2);
4304         if (status < 0)
4305                 goto error;
4306         status = write16(state, QAM_DQ_QUAL_FUN3__A, 2);
4307         if (status < 0)
4308                 goto error;
4309         status = write16(state, QAM_DQ_QUAL_FUN4__A, 2);
4310         if (status < 0)
4311                 goto error;
4312         status = write16(state, QAM_DQ_QUAL_FUN5__A, 0);
4313         if (status < 0)
4314                 goto error;
4315
4316         status = write16(state, QAM_SY_SYNC_HWM__A, 5);
4317         if (status < 0)
4318                 goto error;
4319         status = write16(state, QAM_SY_SYNC_AWM__A, 4);
4320         if (status < 0)
4321                 goto error;
4322         status = write16(state, QAM_SY_SYNC_LWM__A, 3);
4323         if (status < 0)
4324                 goto error;
4325
4326         /* QAM Slicer Settings */
4327         status = write16(state, SCU_RAM_QAM_SL_SIG_POWER__A, DRXK_QAM_SL_SIG_POWER_QAM16);
4328         if (status < 0)
4329                 goto error;
4330
4331         /* QAM Loop Controller Coeficients */
4332         status = write16(state, SCU_RAM_QAM_LC_CA_FINE__A, 15);
4333         if (status < 0)
4334                 goto error;
4335         status = write16(state, SCU_RAM_QAM_LC_CA_COARSE__A, 40);
4336         if (status < 0)
4337                 goto error;
4338         status = write16(state, SCU_RAM_QAM_LC_EP_FINE__A, 12);
4339         if (status < 0)
4340                 goto error;
4341         status = write16(state, SCU_RAM_QAM_LC_EP_MEDIUM__A, 24);
4342         if (status < 0)
4343                 goto error;
4344         status = write16(state, SCU_RAM_QAM_LC_EP_COARSE__A, 24);
4345         if (status < 0)
4346                 goto error;
4347         status = write16(state, SCU_RAM_QAM_LC_EI_FINE__A, 12);
4348         if (status < 0)
4349                 goto error;
4350         status = write16(state, SCU_RAM_QAM_LC_EI_MEDIUM__A, 16);
4351         if (status < 0)
4352                 goto error;
4353         status = write16(state, SCU_RAM_QAM_LC_EI_COARSE__A, 16);
4354         if (status < 0)
4355                 goto error;
4356
4357         status = write16(state, SCU_RAM_QAM_LC_CP_FINE__A, 5);
4358         if (status < 0)
4359                 goto error;
4360         status = write16(state, SCU_RAM_QAM_LC_CP_MEDIUM__A, 20);
4361         if (status < 0)
4362                 goto error;
4363         status = write16(state, SCU_RAM_QAM_LC_CP_COARSE__A, 80);
4364         if (status < 0)
4365                 goto error;
4366         status = write16(state, SCU_RAM_QAM_LC_CI_FINE__A, 5);
4367         if (status < 0)
4368                 goto error;
4369         status = write16(state, SCU_RAM_QAM_LC_CI_MEDIUM__A, 20);
4370         if (status < 0)
4371                 goto error;
4372         status = write16(state, SCU_RAM_QAM_LC_CI_COARSE__A, 50);
4373         if (status < 0)
4374                 goto error;
4375         status = write16(state, SCU_RAM_QAM_LC_CF_FINE__A, 16);
4376         if (status < 0)
4377                 goto error;
4378         status = write16(state, SCU_RAM_QAM_LC_CF_MEDIUM__A, 16);
4379         if (status < 0)
4380                 goto error;
4381         status = write16(state, SCU_RAM_QAM_LC_CF_COARSE__A, 32);
4382         if (status < 0)
4383                 goto error;
4384         status = write16(state, SCU_RAM_QAM_LC_CF1_FINE__A, 5);
4385         if (status < 0)
4386                 goto error;
4387         status = write16(state, SCU_RAM_QAM_LC_CF1_MEDIUM__A, 10);
4388         if (status < 0)
4389                 goto error;
4390         status = write16(state, SCU_RAM_QAM_LC_CF1_COARSE__A, 10);
4391         if (status < 0)
4392                 goto error;
4393
4394
4395         /* QAM State Machine (FSM) Thresholds */
4396
4397         status = write16(state, SCU_RAM_QAM_FSM_RTH__A, 140);
4398         if (status < 0)
4399                 goto error;
4400         status = write16(state, SCU_RAM_QAM_FSM_FTH__A, 50);
4401         if (status < 0)
4402                 goto error;
4403         status = write16(state, SCU_RAM_QAM_FSM_CTH__A, 95);
4404         if (status < 0)
4405                 goto error;
4406         status = write16(state, SCU_RAM_QAM_FSM_PTH__A, 120);
4407         if (status < 0)
4408                 goto error;
4409         status = write16(state, SCU_RAM_QAM_FSM_QTH__A, 230);
4410         if (status < 0)
4411                 goto error;
4412         status = write16(state, SCU_RAM_QAM_FSM_MTH__A, 105);
4413         if (status < 0)
4414                 goto error;
4415
4416         status = write16(state, SCU_RAM_QAM_FSM_RATE_LIM__A, 40);
4417         if (status < 0)
4418                 goto error;
4419         status = write16(state, SCU_RAM_QAM_FSM_COUNT_LIM__A, 4);
4420         if (status < 0)
4421                 goto error;
4422         status = write16(state, SCU_RAM_QAM_FSM_FREQ_LIM__A, 24);
4423         if (status < 0)
4424                 goto error;
4425
4426
4427         /* QAM FSM Tracking Parameters */
4428
4429         status = write16(state, SCU_RAM_QAM_FSM_MEDIAN_AV_MULT__A, (u16) 16);
4430         if (status < 0)
4431                 goto error;
4432         status = write16(state, SCU_RAM_QAM_FSM_RADIUS_AV_LIMIT__A, (u16) 220);
4433         if (status < 0)
4434                 goto error;
4435         status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET1__A, (u16) 25);
4436         if (status < 0)
4437                 goto error;
4438         status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET2__A, (u16) 6);
4439         if (status < 0)
4440                 goto error;
4441         status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET3__A, (u16) -24);
4442         if (status < 0)
4443                 goto error;
4444         status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET4__A, (u16) -65);
4445         if (status < 0)
4446                 goto error;
4447         status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET5__A, (u16) -127);
4448         if (status < 0)
4449                 goto error;
4450
4451 error:
4452         if (status < 0)
4453                 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
4454         return status;
4455 }
4456
4457 /*============================================================================*/
4458
4459 /**
4460 * \brief QAM32 specific setup
4461 * \param demod instance of demod.
4462 * \return DRXStatus_t.
4463 */
4464 static int SetQAM32(struct drxk_state *state)
4465 {
4466         int status = 0;
4467
4468         dprintk(1, "\n");
4469
4470         /* QAM Equalizer Setup */
4471         /* Equalizer */
4472         status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD0__A, 6707);
4473         if (status < 0)
4474                 goto error;
4475         status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD1__A, 6707);
4476         if (status < 0)
4477                 goto error;
4478         status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD2__A, 6707);
4479         if (status < 0)
4480                 goto error;
4481         status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD3__A, 6707);
4482         if (status < 0)
4483                 goto error;
4484         status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD4__A, 6707);
4485         if (status < 0)
4486                 goto error;
4487         status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD5__A, 6707);
4488         if (status < 0)
4489                 goto error;
4490
4491         /* Decision Feedback Equalizer */
4492         status = write16(state, QAM_DQ_QUAL_FUN0__A, 3);
4493         if (status < 0)
4494                 goto error;
4495         status = write16(state, QAM_DQ_QUAL_FUN1__A, 3);
4496         if (status < 0)
4497                 goto error;
4498         status = write16(state, QAM_DQ_QUAL_FUN2__A, 3);
4499         if (status < 0)
4500                 goto error;
4501         status = write16(state, QAM_DQ_QUAL_FUN3__A, 3);
4502         if (status < 0)
4503                 goto error;
4504         status = write16(state, QAM_DQ_QUAL_FUN4__A, 3);
4505         if (status < 0)
4506                 goto error;
4507         status = write16(state, QAM_DQ_QUAL_FUN5__A, 0);
4508         if (status < 0)
4509                 goto error;
4510
4511         status = write16(state, QAM_SY_SYNC_HWM__A, 6);
4512         if (status < 0)
4513                 goto error;
4514         status = write16(state, QAM_SY_SYNC_AWM__A, 5);
4515         if (status < 0)
4516                 goto error;
4517         status = write16(state, QAM_SY_SYNC_LWM__A, 3);
4518         if (status < 0)
4519                 goto error;
4520
4521         /* QAM Slicer Settings */
4522
4523         status = write16(state, SCU_RAM_QAM_SL_SIG_POWER__A, DRXK_QAM_SL_SIG_POWER_QAM32);
4524         if (status < 0)
4525                 goto error;
4526
4527
4528         /* QAM Loop Controller Coeficients */
4529
4530         status = write16(state, SCU_RAM_QAM_LC_CA_FINE__A, 15);
4531         if (status < 0)
4532                 goto error;
4533         status = write16(state, SCU_RAM_QAM_LC_CA_COARSE__A, 40);
4534         if (status < 0)
4535                 goto error;
4536         status = write16(state, SCU_RAM_QAM_LC_EP_FINE__A, 12);
4537         if (status < 0)
4538                 goto error;
4539         status = write16(state, SCU_RAM_QAM_LC_EP_MEDIUM__A, 24);
4540         if (status < 0)
4541                 goto error;
4542         status = write16(state, SCU_RAM_QAM_LC_EP_COARSE__A, 24);
4543         if (status < 0)
4544                 goto error;
4545         status = write16(state, SCU_RAM_QAM_LC_EI_FINE__A, 12);
4546         if (status < 0)
4547                 goto error;
4548         status = write16(state, SCU_RAM_QAM_LC_EI_MEDIUM__A, 16);
4549         if (status < 0)
4550                 goto error;
4551         status = write16(state, SCU_RAM_QAM_LC_EI_COARSE__A, 16);
4552         if (status < 0)
4553                 goto error;
4554
4555         status = write16(state, SCU_RAM_QAM_LC_CP_FINE__A, 5);
4556         if (status < 0)
4557                 goto error;
4558         status = write16(state, SCU_RAM_QAM_LC_CP_MEDIUM__A, 20);
4559         if (status < 0)
4560                 goto error;
4561         status = write16(state, SCU_RAM_QAM_LC_CP_COARSE__A, 80);
4562         if (status < 0)
4563                 goto error;
4564         status = write16(state, SCU_RAM_QAM_LC_CI_FINE__A, 5);
4565         if (status < 0)
4566                 goto error;
4567         status = write16(state, SCU_RAM_QAM_LC_CI_MEDIUM__A, 20);
4568         if (status < 0)
4569                 goto error;
4570         status = write16(state, SCU_RAM_QAM_LC_CI_COARSE__A, 50);
4571         if (status < 0)
4572                 goto error;
4573         status = write16(state, SCU_RAM_QAM_LC_CF_FINE__A, 16);
4574         if (status < 0)
4575                 goto error;
4576         status = write16(state, SCU_RAM_QAM_LC_CF_MEDIUM__A, 16);
4577         if (status < 0)
4578                 goto error;
4579         status = write16(state, SCU_RAM_QAM_LC_CF_COARSE__A, 16);
4580         if (status < 0)
4581                 goto error;
4582         status = write16(state, SCU_RAM_QAM_LC_CF1_FINE__A, 5);
4583         if (status < 0)
4584                 goto error;
4585         status = write16(state, SCU_RAM_QAM_LC_CF1_MEDIUM__A, 10);
4586         if (status < 0)
4587                 goto error;
4588         status = write16(state, SCU_RAM_QAM_LC_CF1_COARSE__A, 0);
4589         if (status < 0)
4590                 goto error;
4591
4592
4593         /* QAM State Machine (FSM) Thresholds */
4594
4595         status = write16(state, SCU_RAM_QAM_FSM_RTH__A, 90);
4596         if (status < 0)
4597                 goto error;
4598         status = write16(state, SCU_RAM_QAM_FSM_FTH__A, 50);
4599         if (status < 0)
4600                 goto error;
4601         status = write16(state, SCU_RAM_QAM_FSM_CTH__A, 80);
4602         if (status < 0)
4603                 goto error;
4604         status = write16(state, SCU_RAM_QAM_FSM_PTH__A, 100);
4605         if (status < 0)
4606                 goto error;
4607         status = write16(state, SCU_RAM_QAM_FSM_QTH__A, 170);
4608         if (status < 0)
4609                 goto error;
4610         status = write16(state, SCU_RAM_QAM_FSM_MTH__A, 100);
4611         if (status < 0)
4612                 goto error;
4613
4614         status = write16(state, SCU_RAM_QAM_FSM_RATE_LIM__A, 40);
4615         if (status < 0)
4616                 goto error;
4617         status = write16(state, SCU_RAM_QAM_FSM_COUNT_LIM__A, 4);
4618         if (status < 0)
4619                 goto error;
4620         status = write16(state, SCU_RAM_QAM_FSM_FREQ_LIM__A, 10);
4621         if (status < 0)
4622                 goto error;
4623
4624
4625         /* QAM FSM Tracking Parameters */
4626
4627         status = write16(state, SCU_RAM_QAM_FSM_MEDIAN_AV_MULT__A, (u16) 12);
4628         if (status < 0)
4629                 goto error;
4630         status = write16(state, SCU_RAM_QAM_FSM_RADIUS_AV_LIMIT__A, (u16) 140);
4631         if (status < 0)
4632                 goto error;
4633         status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET1__A, (u16) -8);
4634         if (status < 0)
4635                 goto error;
4636         status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET2__A, (u16) -16);
4637         if (status < 0)
4638                 goto error;
4639         status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET3__A, (u16) -26);
4640         if (status < 0)
4641                 goto error;
4642         status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET4__A, (u16) -56);
4643         if (status < 0)
4644                 goto error;
4645         status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET5__A, (u16) -86);
4646 error:
4647         if (status < 0)
4648                 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
4649         return status;
4650 }
4651
4652 /*============================================================================*/
4653
4654 /**
4655 * \brief QAM64 specific setup
4656 * \param demod instance of demod.
4657 * \return DRXStatus_t.
4658 */
4659 static int SetQAM64(struct drxk_state *state)
4660 {
4661         int status = 0;
4662
4663         dprintk(1, "\n");
4664         /* QAM Equalizer Setup */
4665         /* Equalizer */
4666         status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD0__A, 13336);
4667         if (status < 0)
4668                 goto error;
4669         status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD1__A, 12618);
4670         if (status < 0)
4671                 goto error;
4672         status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD2__A, 11988);
4673         if (status < 0)
4674                 goto error;
4675         status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD3__A, 13809);
4676         if (status < 0)
4677                 goto error;
4678         status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD4__A, 13809);
4679         if (status < 0)
4680                 goto error;
4681         status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD5__A, 15609);
4682         if (status < 0)
4683                 goto error;
4684
4685         /* Decision Feedback Equalizer */
4686         status = write16(state, QAM_DQ_QUAL_FUN0__A, 4);
4687         if (status < 0)
4688                 goto error;
4689         status = write16(state, QAM_DQ_QUAL_FUN1__A, 4);
4690         if (status < 0)
4691                 goto error;
4692         status = write16(state, QAM_DQ_QUAL_FUN2__A, 4);
4693         if (status < 0)
4694                 goto error;
4695         status = write16(state, QAM_DQ_QUAL_FUN3__A, 4);
4696         if (status < 0)
4697                 goto error;
4698         status = write16(state, QAM_DQ_QUAL_FUN4__A, 3);
4699         if (status < 0)
4700                 goto error;
4701         status = write16(state, QAM_DQ_QUAL_FUN5__A, 0);
4702         if (status < 0)
4703                 goto error;
4704
4705         status = write16(state, QAM_SY_SYNC_HWM__A, 5);
4706         if (status < 0)
4707                 goto error;
4708         status = write16(state, QAM_SY_SYNC_AWM__A, 4);
4709         if (status < 0)
4710                 goto error;
4711         status = write16(state, QAM_SY_SYNC_LWM__A, 3);
4712         if (status < 0)
4713                 goto error;
4714
4715         /* QAM Slicer Settings */
4716         status = write16(state, SCU_RAM_QAM_SL_SIG_POWER__A, DRXK_QAM_SL_SIG_POWER_QAM64);
4717         if (status < 0)
4718                 goto error;
4719
4720
4721         /* QAM Loop Controller Coeficients */
4722
4723         status = write16(state, SCU_RAM_QAM_LC_CA_FINE__A, 15);
4724         if (status < 0)
4725                 goto error;
4726         status = write16(state, SCU_RAM_QAM_LC_CA_COARSE__A, 40);
4727         if (status < 0)
4728                 goto error;
4729         status = write16(state, SCU_RAM_QAM_LC_EP_FINE__A, 12);
4730         if (status < 0)
4731                 goto error;
4732         status = write16(state, SCU_RAM_QAM_LC_EP_MEDIUM__A, 24);
4733         if (status < 0)
4734                 goto error;
4735         status = write16(state, SCU_RAM_QAM_LC_EP_COARSE__A, 24);
4736         if (status < 0)
4737                 goto error;
4738         status = write16(state, SCU_RAM_QAM_LC_EI_FINE__A, 12);
4739         if (status < 0)
4740                 goto error;
4741         status = write16(state, SCU_RAM_QAM_LC_EI_MEDIUM__A, 16);
4742         if (status < 0)
4743                 goto error;
4744         status = write16(state, SCU_RAM_QAM_LC_EI_COARSE__A, 16);
4745         if (status < 0)
4746                 goto error;
4747
4748         status = write16(state, SCU_RAM_QAM_LC_CP_FINE__A, 5);
4749         if (status < 0)
4750                 goto error;
4751         status = write16(state, SCU_RAM_QAM_LC_CP_MEDIUM__A, 30);
4752         if (status < 0)
4753                 goto error;
4754         status = write16(state, SCU_RAM_QAM_LC_CP_COARSE__A, 100);
4755         if (status < 0)
4756                 goto error;
4757         status = write16(state, SCU_RAM_QAM_LC_CI_FINE__A, 5);
4758         if (status < 0)
4759                 goto error;
4760         status = write16(state, SCU_RAM_QAM_LC_CI_MEDIUM__A, 30);
4761         if (status < 0)
4762                 goto error;
4763         status = write16(state, SCU_RAM_QAM_LC_CI_COARSE__A, 50);
4764         if (status < 0)
4765                 goto error;
4766         status = write16(state, SCU_RAM_QAM_LC_CF_FINE__A, 16);
4767         if (status < 0)
4768                 goto error;
4769         status = write16(state, SCU_RAM_QAM_LC_CF_MEDIUM__A, 25);
4770         if (status < 0)
4771                 goto error;
4772         status = write16(state, SCU_RAM_QAM_LC_CF_COARSE__A, 48);
4773         if (status < 0)
4774                 goto error;
4775         status = write16(state, SCU_RAM_QAM_LC_CF1_FINE__A, 5);
4776         if (status < 0)
4777                 goto error;
4778         status = write16(state, SCU_RAM_QAM_LC_CF1_MEDIUM__A, 10);
4779         if (status < 0)
4780                 goto error;
4781         status = write16(state, SCU_RAM_QAM_LC_CF1_COARSE__A, 10);
4782         if (status < 0)
4783                 goto error;
4784
4785
4786         /* QAM State Machine (FSM) Thresholds */
4787
4788         status = write16(state, SCU_RAM_QAM_FSM_RTH__A, 100);
4789         if (status < 0)
4790                 goto error;
4791         status = write16(state, SCU_RAM_QAM_FSM_FTH__A, 60);
4792         if (status < 0)
4793                 goto error;
4794         status = write16(state, SCU_RAM_QAM_FSM_CTH__A, 80);
4795         if (status < 0)
4796                 goto error;
4797         status = write16(state, SCU_RAM_QAM_FSM_PTH__A, 110);
4798         if (status < 0)
4799                 goto error;
4800         status = write16(state, SCU_RAM_QAM_FSM_QTH__A, 200);
4801         if (status < 0)
4802                 goto error;
4803         status = write16(state, SCU_RAM_QAM_FSM_MTH__A, 95);
4804         if (status < 0)
4805                 goto error;
4806
4807         status = write16(state, SCU_RAM_QAM_FSM_RATE_LIM__A, 40);
4808         if (status < 0)
4809                 goto error;
4810         status = write16(state, SCU_RAM_QAM_FSM_COUNT_LIM__A, 4);
4811         if (status < 0)
4812                 goto error;
4813         status = write16(state, SCU_RAM_QAM_FSM_FREQ_LIM__A, 15);
4814         if (status < 0)
4815                 goto error;
4816
4817
4818         /* QAM FSM Tracking Parameters */
4819
4820         status = write16(state, SCU_RAM_QAM_FSM_MEDIAN_AV_MULT__A, (u16) 12);
4821         if (status < 0)
4822                 goto error;
4823         status = write16(state, SCU_RAM_QAM_FSM_RADIUS_AV_LIMIT__A, (u16) 141);
4824         if (status < 0)
4825                 goto error;
4826         status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET1__A, (u16) 7);
4827         if (status < 0)
4828                 goto error;
4829         status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET2__A, (u16) 0);
4830         if (status < 0)
4831                 goto error;
4832         status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET3__A, (u16) -15);
4833         if (status < 0)
4834                 goto error;
4835         status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET4__A, (u16) -45);
4836         if (status < 0)
4837                 goto error;
4838         status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET5__A, (u16) -80);
4839 error:
4840         if (status < 0)
4841                 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
4842
4843         return status;
4844 }
4845
4846 /*============================================================================*/
4847
4848 /**
4849 * \brief QAM128 specific setup
4850 * \param demod: instance of demod.
4851 * \return DRXStatus_t.
4852 */
4853 static int SetQAM128(struct drxk_state *state)
4854 {
4855         int status = 0;
4856
4857         dprintk(1, "\n");
4858         /* QAM Equalizer Setup */
4859         /* Equalizer */
4860         status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD0__A, 6564);
4861         if (status < 0)
4862                 goto error;
4863         status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD1__A, 6598);
4864         if (status < 0)
4865                 goto error;
4866         status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD2__A, 6394);
4867         if (status < 0)
4868                 goto error;
4869         status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD3__A, 6409);
4870         if (status < 0)
4871                 goto error;
4872         status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD4__A, 6656);
4873         if (status < 0)
4874                 goto error;
4875         status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD5__A, 7238);
4876         if (status < 0)
4877                 goto error;
4878
4879         /* Decision Feedback Equalizer */
4880         status = write16(state, QAM_DQ_QUAL_FUN0__A, 6);
4881         if (status < 0)
4882                 goto error;
4883         status = write16(state, QAM_DQ_QUAL_FUN1__A, 6);
4884         if (status < 0)
4885                 goto error;
4886         status = write16(state, QAM_DQ_QUAL_FUN2__A, 6);
4887         if (status < 0)
4888                 goto error;
4889         status = write16(state, QAM_DQ_QUAL_FUN3__A, 6);
4890         if (status < 0)
4891                 goto error;
4892         status = write16(state, QAM_DQ_QUAL_FUN4__A, 5);
4893         if (status < 0)
4894                 goto error;
4895         status = write16(state, QAM_DQ_QUAL_FUN5__A, 0);
4896         if (status < 0)
4897                 goto error;
4898
4899         status = write16(state, QAM_SY_SYNC_HWM__A, 6);
4900         if (status < 0)
4901                 goto error;
4902         status = write16(state, QAM_SY_SYNC_AWM__A, 5);
4903         if (status < 0)
4904                 goto error;
4905         status = write16(state, QAM_SY_SYNC_LWM__A, 3);
4906         if (status < 0)
4907                 goto error;
4908
4909
4910         /* QAM Slicer Settings */
4911
4912         status = write16(state, SCU_RAM_QAM_SL_SIG_POWER__A, DRXK_QAM_SL_SIG_POWER_QAM128);
4913         if (status < 0)
4914                 goto error;
4915
4916
4917         /* QAM Loop Controller Coeficients */
4918
4919         status = write16(state, SCU_RAM_QAM_LC_CA_FINE__A, 15);
4920         if (status < 0)
4921                 goto error;
4922         status = write16(state, SCU_RAM_QAM_LC_CA_COARSE__A, 40);
4923         if (status < 0)
4924                 goto error;
4925         status = write16(state, SCU_RAM_QAM_LC_EP_FINE__A, 12);
4926         if (status < 0)
4927                 goto error;
4928         status = write16(state, SCU_RAM_QAM_LC_EP_MEDIUM__A, 24);
4929         if (status < 0)
4930                 goto error;
4931         status = write16(state, SCU_RAM_QAM_LC_EP_COARSE__A, 24);
4932         if (status < 0)
4933                 goto error;
4934         status = write16(state, SCU_RAM_QAM_LC_EI_FINE__A, 12);
4935         if (status < 0)
4936                 goto error;
4937         status = write16(state, SCU_RAM_QAM_LC_EI_MEDIUM__A, 16);
4938         if (status < 0)
4939                 goto error;
4940         status = write16(state, SCU_RAM_QAM_LC_EI_COARSE__A, 16);
4941         if (status < 0)
4942                 goto error;
4943
4944         status = write16(state, SCU_RAM_QAM_LC_CP_FINE__A, 5);
4945         if (status < 0)
4946                 goto error;
4947         status = write16(state, SCU_RAM_QAM_LC_CP_MEDIUM__A, 40);
4948         if (status < 0)
4949                 goto error;
4950         status = write16(state, SCU_RAM_QAM_LC_CP_COARSE__A, 120);
4951         if (status < 0)
4952                 goto error;
4953         status = write16(state, SCU_RAM_QAM_LC_CI_FINE__A, 5);
4954         if (status < 0)
4955                 goto error;
4956         status = write16(state, SCU_RAM_QAM_LC_CI_MEDIUM__A, 40);
4957         if (status < 0)
4958                 goto error;
4959         status = write16(state, SCU_RAM_QAM_LC_CI_COARSE__A, 60);
4960         if (status < 0)
4961                 goto error;
4962         status = write16(state, SCU_RAM_QAM_LC_CF_FINE__A, 16);
4963         if (status < 0)
4964                 goto error;
4965         status = write16(state, SCU_RAM_QAM_LC_CF_MEDIUM__A, 25);
4966         if (status < 0)
4967                 goto error;
4968         status = write16(state, SCU_RAM_QAM_LC_CF_COARSE__A, 64);
4969         if (status < 0)
4970                 goto error;
4971         status = write16(state, SCU_RAM_QAM_LC_CF1_FINE__A, 5);
4972         if (status < 0)
4973                 goto error;
4974         status = write16(state, SCU_RAM_QAM_LC_CF1_MEDIUM__A, 10);
4975         if (status < 0)
4976                 goto error;
4977         status = write16(state, SCU_RAM_QAM_LC_CF1_COARSE__A, 0);
4978         if (status < 0)
4979                 goto error;
4980
4981
4982         /* QAM State Machine (FSM) Thresholds */
4983
4984         status = write16(state, SCU_RAM_QAM_FSM_RTH__A, 50);
4985         if (status < 0)
4986                 goto error;
4987         status = write16(state, SCU_RAM_QAM_FSM_FTH__A, 60);
4988         if (status < 0)
4989                 goto error;
4990         status = write16(state, SCU_RAM_QAM_FSM_CTH__A, 80);
4991         if (status < 0)
4992                 goto error;
4993         status = write16(state, SCU_RAM_QAM_FSM_PTH__A, 100);
4994         if (status < 0)
4995                 goto error;
4996         status = write16(state, SCU_RAM_QAM_FSM_QTH__A, 140);
4997         if (status < 0)
4998                 goto error;
4999         status = write16(state, SCU_RAM_QAM_FSM_MTH__A, 100);
5000         if (status < 0)
5001                 goto error;
5002
5003         status = write16(state, SCU_RAM_QAM_FSM_RATE_LIM__A, 40);
5004         if (status < 0)
5005                 goto error;
5006         status = write16(state, SCU_RAM_QAM_FSM_COUNT_LIM__A, 5);
5007         if (status < 0)
5008                 goto error;
5009
5010         status = write16(state, SCU_RAM_QAM_FSM_FREQ_LIM__A, 12);
5011         if (status < 0)
5012                 goto error;
5013
5014         /* QAM FSM Tracking Parameters */
5015
5016         status = write16(state, SCU_RAM_QAM_FSM_MEDIAN_AV_MULT__A, (u16) 8);
5017         if (status < 0)
5018                 goto error;
5019         status = write16(state, SCU_RAM_QAM_FSM_RADIUS_AV_LIMIT__A, (u16) 65);
5020         if (status < 0)
5021                 goto error;
5022         status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET1__A, (u16) 5);
5023         if (status < 0)
5024                 goto error;
5025         status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET2__A, (u16) 3);
5026         if (status < 0)
5027                 goto error;
5028         status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET3__A, (u16) -1);
5029         if (status < 0)
5030                 goto error;
5031         status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET4__A, (u16) -12);
5032         if (status < 0)
5033                 goto error;
5034         status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET5__A, (u16) -23);
5035 error:
5036         if (status < 0)
5037                 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
5038
5039         return status;
5040 }
5041
5042 /*============================================================================*/
5043
5044 /**
5045 * \brief QAM256 specific setup
5046 * \param demod: instance of demod.
5047 * \return DRXStatus_t.
5048 */
5049 static int SetQAM256(struct drxk_state *state)
5050 {
5051         int status = 0;
5052
5053         dprintk(1, "\n");
5054         /* QAM Equalizer Setup */
5055         /* Equalizer */
5056         status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD0__A, 11502);
5057         if (status < 0)
5058                 goto error;
5059         status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD1__A, 12084);
5060         if (status < 0)
5061                 goto error;
5062         status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD2__A, 12543);
5063         if (status < 0)
5064                 goto error;
5065         status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD3__A, 12931);
5066         if (status < 0)
5067                 goto error;
5068         status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD4__A, 13629);
5069         if (status < 0)
5070                 goto error;
5071         status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD5__A, 15385);
5072         if (status < 0)
5073                 goto error;
5074
5075         /* Decision Feedback Equalizer */
5076         status = write16(state, QAM_DQ_QUAL_FUN0__A, 8);
5077         if (status < 0)
5078                 goto error;
5079         status = write16(state, QAM_DQ_QUAL_FUN1__A, 8);
5080         if (status < 0)
5081                 goto error;
5082         status = write16(state, QAM_DQ_QUAL_FUN2__A, 8);
5083         if (status < 0)
5084                 goto error;
5085         status = write16(state, QAM_DQ_QUAL_FUN3__A, 8);
5086         if (status < 0)
5087                 goto error;
5088         status = write16(state, QAM_DQ_QUAL_FUN4__A, 6);
5089         if (status < 0)
5090                 goto error;
5091         status = write16(state, QAM_DQ_QUAL_FUN5__A, 0);
5092         if (status < 0)
5093                 goto error;
5094
5095         status = write16(state, QAM_SY_SYNC_HWM__A, 5);
5096         if (status < 0)
5097                 goto error;
5098         status = write16(state, QAM_SY_SYNC_AWM__A, 4);
5099         if (status < 0)
5100                 goto error;
5101         status = write16(state, QAM_SY_SYNC_LWM__A, 3);
5102         if (status < 0)
5103                 goto error;
5104
5105         /* QAM Slicer Settings */
5106
5107         status = write16(state, SCU_RAM_QAM_SL_SIG_POWER__A, DRXK_QAM_SL_SIG_POWER_QAM256);
5108         if (status < 0)
5109                 goto error;
5110
5111
5112         /* QAM Loop Controller Coeficients */
5113
5114         status = write16(state, SCU_RAM_QAM_LC_CA_FINE__A, 15);
5115         if (status < 0)
5116                 goto error;
5117         status = write16(state, SCU_RAM_QAM_LC_CA_COARSE__A, 40);
5118         if (status < 0)
5119                 goto error;
5120         status = write16(state, SCU_RAM_QAM_LC_EP_FINE__A, 12);
5121         if (status < 0)
5122                 goto error;
5123         status = write16(state, SCU_RAM_QAM_LC_EP_MEDIUM__A, 24);
5124         if (status < 0)
5125                 goto error;
5126         status = write16(state, SCU_RAM_QAM_LC_EP_COARSE__A, 24);
5127         if (status < 0)
5128                 goto error;
5129         status = write16(state, SCU_RAM_QAM_LC_EI_FINE__A, 12);
5130         if (status < 0)
5131                 goto error;
5132         status = write16(state, SCU_RAM_QAM_LC_EI_MEDIUM__A, 16);
5133         if (status < 0)
5134                 goto error;
5135         status = write16(state, SCU_RAM_QAM_LC_EI_COARSE__A, 16);
5136         if (status < 0)
5137                 goto error;
5138
5139         status = write16(state, SCU_RAM_QAM_LC_CP_FINE__A, 5);
5140         if (status < 0)
5141                 goto error;
5142         status = write16(state, SCU_RAM_QAM_LC_CP_MEDIUM__A, 50);
5143         if (status < 0)
5144                 goto error;
5145         status = write16(state, SCU_RAM_QAM_LC_CP_COARSE__A, 250);
5146         if (status < 0)
5147                 goto error;
5148         status = write16(state, SCU_RAM_QAM_LC_CI_FINE__A, 5);
5149         if (status < 0)
5150                 goto error;
5151         status = write16(state, SCU_RAM_QAM_LC_CI_MEDIUM__A, 50);
5152         if (status < 0)
5153                 goto error;
5154         status = write16(state, SCU_RAM_QAM_LC_CI_COARSE__A, 125);
5155         if (status < 0)
5156                 goto error;
5157         status = write16(state, SCU_RAM_QAM_LC_CF_FINE__A, 16);
5158         if (status < 0)
5159                 goto error;
5160         status = write16(state, SCU_RAM_QAM_LC_CF_MEDIUM__A, 25);
5161         if (status < 0)
5162                 goto error;
5163         status = write16(state, SCU_RAM_QAM_LC_CF_COARSE__A, 48);
5164         if (status < 0)
5165                 goto error;
5166         status = write16(state, SCU_RAM_QAM_LC_CF1_FINE__A, 5);
5167         if (status < 0)
5168                 goto error;
5169         status = write16(state, SCU_RAM_QAM_LC_CF1_MEDIUM__A, 10);
5170         if (status < 0)
5171                 goto error;
5172         status = write16(state, SCU_RAM_QAM_LC_CF1_COARSE__A, 10);
5173         if (status < 0)
5174                 goto error;
5175
5176
5177         /* QAM State Machine (FSM) Thresholds */
5178
5179         status = write16(state, SCU_RAM_QAM_FSM_RTH__A, 50);
5180         if (status < 0)
5181                 goto error;
5182         status = write16(state, SCU_RAM_QAM_FSM_FTH__A, 60);
5183         if (status < 0)
5184                 goto error;
5185         status = write16(state, SCU_RAM_QAM_FSM_CTH__A, 80);
5186         if (status < 0)
5187                 goto error;
5188         status = write16(state, SCU_RAM_QAM_FSM_PTH__A, 100);
5189         if (status < 0)
5190                 goto error;
5191         status = write16(state, SCU_RAM_QAM_FSM_QTH__A, 150);
5192         if (status < 0)
5193                 goto error;
5194         status = write16(state, SCU_RAM_QAM_FSM_MTH__A, 110);
5195         if (status < 0)
5196                 goto error;
5197
5198         status = write16(state, SCU_RAM_QAM_FSM_RATE_LIM__A, 40);
5199         if (status < 0)
5200                 goto error;
5201         status = write16(state, SCU_RAM_QAM_FSM_COUNT_LIM__A, 4);
5202         if (status < 0)
5203                 goto error;
5204         status = write16(state, SCU_RAM_QAM_FSM_FREQ_LIM__A, 12);
5205         if (status < 0)
5206                 goto error;
5207
5208
5209         /* QAM FSM Tracking Parameters */
5210
5211         status = write16(state, SCU_RAM_QAM_FSM_MEDIAN_AV_MULT__A, (u16) 8);
5212         if (status < 0)
5213                 goto error;
5214         status = write16(state, SCU_RAM_QAM_FSM_RADIUS_AV_LIMIT__A, (u16) 74);
5215         if (status < 0)
5216                 goto error;
5217         status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET1__A, (u16) 18);
5218         if (status < 0)
5219                 goto error;
5220         status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET2__A, (u16) 13);
5221         if (status < 0)
5222                 goto error;
5223         status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET3__A, (u16) 7);
5224         if (status < 0)
5225                 goto error;
5226         status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET4__A, (u16) 0);
5227         if (status < 0)
5228                 goto error;
5229         status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET5__A, (u16) -8);
5230 error:
5231         if (status < 0)
5232                 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
5233         return status;
5234 }
5235
5236
5237 /*============================================================================*/
5238 /**
5239 * \brief Reset QAM block.
5240 * \param demod:   instance of demod.
5241 * \param channel: pointer to channel data.
5242 * \return DRXStatus_t.
5243 */
5244 static int QAMResetQAM(struct drxk_state *state)
5245 {
5246         int status;
5247         u16 cmdResult;
5248
5249         dprintk(1, "\n");
5250         /* Stop QAM comstate->m_exec */
5251         status = write16(state, QAM_COMM_EXEC__A, QAM_COMM_EXEC_STOP);
5252         if (status < 0)
5253                 goto error;
5254
5255         status = scu_command(state, SCU_RAM_COMMAND_STANDARD_QAM | SCU_RAM_COMMAND_CMD_DEMOD_RESET, 0, NULL, 1, &cmdResult);
5256 error:
5257         if (status < 0)
5258                 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
5259         return status;
5260 }
5261
5262 /*============================================================================*/
5263
5264 /**
5265 * \brief Set QAM symbolrate.
5266 * \param demod:   instance of demod.
5267 * \param channel: pointer to channel data.
5268 * \return DRXStatus_t.
5269 */
5270 static int QAMSetSymbolrate(struct drxk_state *state)
5271 {
5272         u32 adcFrequency = 0;
5273         u32 symbFreq = 0;
5274         u32 iqmRcRate = 0;
5275         u16 ratesel = 0;
5276         u32 lcSymbRate = 0;
5277         int status;
5278
5279         dprintk(1, "\n");
5280         /* Select & calculate correct IQM rate */
5281         adcFrequency = (state->m_sysClockFreq * 1000) / 3;
5282         ratesel = 0;
5283         /* printk(KERN_DEBUG "drxk: SR %d\n", state->param.u.qam.symbol_rate); */
5284         if (state->param.u.qam.symbol_rate <= 1188750)
5285                 ratesel = 3;
5286         else if (state->param.u.qam.symbol_rate <= 2377500)
5287                 ratesel = 2;
5288         else if (state->param.u.qam.symbol_rate <= 4755000)
5289                 ratesel = 1;
5290         status = write16(state, IQM_FD_RATESEL__A, ratesel);
5291         if (status < 0)
5292                 goto error;
5293
5294         /*
5295                 IqmRcRate = ((Fadc / (symbolrate * (4<<ratesel))) - 1) * (1<<23)
5296                 */
5297         symbFreq = state->param.u.qam.symbol_rate * (1 << ratesel);
5298         if (symbFreq == 0) {
5299                 /* Divide by zero */
5300                 status = -EINVAL;
5301                 goto error;
5302         }
5303         iqmRcRate = (adcFrequency / symbFreq) * (1 << 21) +
5304                 (Frac28a((adcFrequency % symbFreq), symbFreq) >> 7) -
5305                 (1 << 23);
5306         status = write32(state, IQM_RC_RATE_OFS_LO__A, iqmRcRate);
5307         if (status < 0)
5308                 goto error;
5309         state->m_iqmRcRate = iqmRcRate;
5310         /*
5311                 LcSymbFreq = round (.125 *  symbolrate / adcFreq * (1<<15))
5312                 */
5313         symbFreq = state->param.u.qam.symbol_rate;
5314         if (adcFrequency == 0) {
5315                 /* Divide by zero */
5316                 status = -EINVAL;
5317                 goto error;
5318         }
5319         lcSymbRate = (symbFreq / adcFrequency) * (1 << 12) +
5320                 (Frac28a((symbFreq % adcFrequency), adcFrequency) >>
5321                 16);
5322         if (lcSymbRate > 511)
5323                 lcSymbRate = 511;
5324         status = write16(state, QAM_LC_SYMBOL_FREQ__A, (u16) lcSymbRate);
5325
5326 error:
5327         if (status < 0)
5328                 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
5329         return status;
5330 }
5331
5332 /*============================================================================*/
5333
5334 /**
5335 * \brief Get QAM lock status.
5336 * \param demod:   instance of demod.
5337 * \param channel: pointer to channel data.
5338 * \return DRXStatus_t.
5339 */
5340
5341 static int GetQAMLockStatus(struct drxk_state *state, u32 *pLockStatus)
5342 {
5343         int status;
5344         u16 Result[2] = { 0, 0 };
5345
5346         dprintk(1, "\n");
5347         *pLockStatus = NOT_LOCKED;
5348         status = scu_command(state,
5349                         SCU_RAM_COMMAND_STANDARD_QAM |
5350                         SCU_RAM_COMMAND_CMD_DEMOD_GET_LOCK, 0, NULL, 2,
5351                         Result);
5352         if (status < 0)
5353                 printk(KERN_ERR "drxk: %s status = %08x\n", __func__, status);
5354
5355         if (Result[1] < SCU_RAM_QAM_LOCKED_LOCKED_DEMOD_LOCKED) {
5356                 /* 0x0000 NOT LOCKED */
5357         } else if (Result[1] < SCU_RAM_QAM_LOCKED_LOCKED_LOCKED) {
5358                 /* 0x4000 DEMOD LOCKED */
5359                 *pLockStatus = DEMOD_LOCK;
5360         } else if (Result[1] < SCU_RAM_QAM_LOCKED_LOCKED_NEVER_LOCK) {
5361                 /* 0x8000 DEMOD + FEC LOCKED (system lock) */
5362                 *pLockStatus = MPEG_LOCK;
5363         } else {
5364                 /* 0xC000 NEVER LOCKED */
5365                 /* (system will never be able to lock to the signal) */
5366                 /* TODO: check this, intermediate & standard specific lock states are not
5367                    taken into account here */
5368                 *pLockStatus = NEVER_LOCK;
5369         }
5370         return status;
5371 }
5372
5373 #define QAM_MIRROR__M         0x03
5374 #define QAM_MIRROR_NORMAL     0x00
5375 #define QAM_MIRRORED          0x01
5376 #define QAM_MIRROR_AUTO_ON    0x02
5377 #define QAM_LOCKRANGE__M      0x10
5378 #define QAM_LOCKRANGE_NORMAL  0x10
5379
5380 static int SetQAM(struct drxk_state *state, u16 IntermediateFreqkHz,
5381                   s32 tunerFreqOffset)
5382 {
5383         int status;
5384         u8 parameterLen;
5385         u16 setEnvParameters[5];
5386         u16 setParamParameters[4] = { 0, 0, 0, 0 };
5387         u16 cmdResult;
5388
5389         dprintk(1, "\n");
5390         /*
5391                 STEP 1: reset demodulator
5392                 resets FEC DI and FEC RS
5393                 resets QAM block
5394                 resets SCU variables
5395                 */
5396         status = write16(state, FEC_DI_COMM_EXEC__A, FEC_DI_COMM_EXEC_STOP);
5397         if (status < 0)
5398                 goto error;
5399         status = write16(state, FEC_RS_COMM_EXEC__A, FEC_RS_COMM_EXEC_STOP);
5400         if (status < 0)
5401                 goto error;
5402         status = QAMResetQAM(state);
5403         if (status < 0)
5404                 goto error;
5405
5406         /*
5407                 STEP 2: configure demodulator
5408                 -set env
5409                 -set params; resets IQM,QAM,FEC HW; initializes some SCU variables
5410                 */
5411         status = QAMSetSymbolrate(state);
5412         if (status < 0)
5413                 goto error;
5414
5415         /* Env parameters */
5416         setEnvParameters[2] = QAM_TOP_ANNEX_A;  /* Annex */
5417         if (state->m_OperationMode == OM_QAM_ITU_C)
5418                 setEnvParameters[2] = QAM_TOP_ANNEX_C;  /* Annex */
5419         setParamParameters[3] |= (QAM_MIRROR_AUTO_ON);
5420         /* check for LOCKRANGE Extented */
5421         /* setParamParameters[3] |= QAM_LOCKRANGE_NORMAL; */
5422         parameterLen = 4;
5423
5424         /* Set params */
5425         switch (state->param.u.qam.modulation) {
5426         case QAM_256:
5427                 state->m_Constellation = DRX_CONSTELLATION_QAM256;
5428                 break;
5429         case QAM_AUTO:
5430         case QAM_64:
5431                 state->m_Constellation = DRX_CONSTELLATION_QAM64;
5432                 break;
5433         case QAM_16:
5434                 state->m_Constellation = DRX_CONSTELLATION_QAM16;
5435                 break;
5436         case QAM_32:
5437                 state->m_Constellation = DRX_CONSTELLATION_QAM32;
5438                 break;
5439         case QAM_128:
5440                 state->m_Constellation = DRX_CONSTELLATION_QAM128;
5441                 break;
5442         default:
5443                 status = -EINVAL;
5444                 break;
5445         }
5446         if (status < 0)
5447                 goto error;
5448         setParamParameters[0] = state->m_Constellation; /* constellation     */
5449         setParamParameters[1] = DRXK_QAM_I12_J17;       /* interleave mode   */
5450
5451         status = scu_command(state, SCU_RAM_COMMAND_STANDARD_QAM | SCU_RAM_COMMAND_CMD_DEMOD_SET_PARAM, 4, setParamParameters, 1, &cmdResult);
5452         if (status < 0)
5453                 goto error;
5454
5455
5456         /* STEP 3: enable the system in a mode where the ADC provides valid signal
5457                 setup constellation independent registers */
5458 #if 0
5459         status = SetFrequency(channel, tunerFreqOffset));
5460         if (status < 0)
5461                 goto error;
5462 #endif
5463         status = SetFrequencyShifter(state, IntermediateFreqkHz, tunerFreqOffset, true);
5464         if (status < 0)
5465                 goto error;
5466
5467         /* Setup BER measurement */
5468         status = SetQAMMeasurement(state, state->m_Constellation, state->param.u. qam.symbol_rate);
5469         if (status < 0)
5470                 goto error;
5471
5472         /* Reset default values */
5473         status = write16(state, IQM_CF_SCALE_SH__A, IQM_CF_SCALE_SH__PRE);
5474         if (status < 0)
5475                 goto error;
5476         status = write16(state, QAM_SY_TIMEOUT__A, QAM_SY_TIMEOUT__PRE);
5477         if (status < 0)
5478                 goto error;
5479
5480         /* Reset default LC values */
5481         status = write16(state, QAM_LC_RATE_LIMIT__A, 3);
5482         if (status < 0)
5483                 goto error;
5484         status = write16(state, QAM_LC_LPF_FACTORP__A, 4);
5485         if (status < 0)
5486                 goto error;
5487         status = write16(state, QAM_LC_LPF_FACTORI__A, 4);
5488         if (status < 0)
5489                 goto error;
5490         status = write16(state, QAM_LC_MODE__A, 7);
5491         if (status < 0)
5492                 goto error;
5493
5494         status = write16(state, QAM_LC_QUAL_TAB0__A, 1);
5495         if (status < 0)
5496                 goto error;
5497         status = write16(state, QAM_LC_QUAL_TAB1__A, 1);
5498         if (status < 0)
5499                 goto error;
5500         status = write16(state, QAM_LC_QUAL_TAB2__A, 1);
5501         if (status < 0)
5502                 goto error;
5503         status = write16(state, QAM_LC_QUAL_TAB3__A, 1);
5504         if (status < 0)
5505                 goto error;
5506         status = write16(state, QAM_LC_QUAL_TAB4__A, 2);
5507         if (status < 0)
5508                 goto error;
5509         status = write16(state, QAM_LC_QUAL_TAB5__A, 2);
5510         if (status < 0)
5511                 goto error;
5512         status = write16(state, QAM_LC_QUAL_TAB6__A, 2);
5513         if (status < 0)
5514                 goto error;
5515         status = write16(state, QAM_LC_QUAL_TAB8__A, 2);
5516         if (status < 0)
5517                 goto error;
5518         status = write16(state, QAM_LC_QUAL_TAB9__A, 2);
5519         if (status < 0)
5520                 goto error;
5521         status = write16(state, QAM_LC_QUAL_TAB10__A, 2);
5522         if (status < 0)
5523                 goto error;
5524         status = write16(state, QAM_LC_QUAL_TAB12__A, 2);
5525         if (status < 0)
5526                 goto error;
5527         status = write16(state, QAM_LC_QUAL_TAB15__A, 3);
5528         if (status < 0)
5529                 goto error;
5530         status = write16(state, QAM_LC_QUAL_TAB16__A, 3);
5531         if (status < 0)
5532                 goto error;
5533         status = write16(state, QAM_LC_QUAL_TAB20__A, 4);
5534         if (status < 0)
5535                 goto error;
5536         status = write16(state, QAM_LC_QUAL_TAB25__A, 4);
5537         if (status < 0)
5538                 goto error;
5539
5540         /* Mirroring, QAM-block starting point not inverted */
5541         status = write16(state, QAM_SY_SP_INV__A, QAM_SY_SP_INV_SPECTRUM_INV_DIS);
5542         if (status < 0)
5543                 goto error;
5544
5545         /* Halt SCU to enable safe non-atomic accesses */
5546         status = write16(state, SCU_COMM_EXEC__A, SCU_COMM_EXEC_HOLD);
5547         if (status < 0)
5548                 goto error;
5549
5550         /* STEP 4: constellation specific setup */
5551         switch (state->param.u.qam.modulation) {
5552         case QAM_16:
5553                 status = SetQAM16(state);
5554                 break;
5555         case QAM_32:
5556                 status = SetQAM32(state);
5557                 break;
5558         case QAM_AUTO:
5559         case QAM_64:
5560                 status = SetQAM64(state);
5561                 break;
5562         case QAM_128:
5563                 status = SetQAM128(state);
5564                 break;
5565         case QAM_256:
5566                 status = SetQAM256(state);
5567                 break;
5568         default:
5569                 status = -EINVAL;
5570                 break;
5571         }
5572         if (status < 0)
5573                 goto error;
5574
5575         /* Activate SCU to enable SCU commands */
5576         status = write16(state, SCU_COMM_EXEC__A, SCU_COMM_EXEC_ACTIVE);
5577         if (status < 0)
5578                 goto error;
5579
5580         /* Re-configure MPEG output, requires knowledge of channel bitrate */
5581         /* extAttr->currentChannel.constellation = channel->constellation; */
5582         /* extAttr->currentChannel.symbolrate    = channel->symbolrate; */
5583         status = MPEGTSDtoSetup(state, state->m_OperationMode);
5584         if (status < 0)
5585                 goto error;
5586
5587         /* Start processes */
5588         status = MPEGTSStart(state);
5589         if (status < 0)
5590                 goto error;
5591         status = write16(state, FEC_COMM_EXEC__A, FEC_COMM_EXEC_ACTIVE);
5592         if (status < 0)
5593                 goto error;
5594         status = write16(state, QAM_COMM_EXEC__A, QAM_COMM_EXEC_ACTIVE);
5595         if (status < 0)
5596                 goto error;
5597         status = write16(state, IQM_COMM_EXEC__A, IQM_COMM_EXEC_B_ACTIVE);
5598         if (status < 0)
5599                 goto error;
5600
5601         /* STEP 5: start QAM demodulator (starts FEC, QAM and IQM HW) */
5602         status = scu_command(state, SCU_RAM_COMMAND_STANDARD_QAM | SCU_RAM_COMMAND_CMD_DEMOD_START, 0, NULL, 1, &cmdResult);
5603         if (status < 0)
5604                 goto error;
5605
5606         /* update global DRXK data container */
5607 /*?     extAttr->qamInterleaveMode = DRXK_QAM_I12_J17; */
5608
5609 error:
5610         if (status < 0)
5611                 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
5612         return status;
5613 }
5614
5615 static int SetQAMStandard(struct drxk_state *state,
5616                           enum OperationMode oMode)
5617 {
5618         int status;
5619 #ifdef DRXK_QAM_TAPS
5620 #define DRXK_QAMA_TAPS_SELECT
5621 #include "drxk_filters.h"
5622 #undef DRXK_QAMA_TAPS_SELECT
5623 #endif
5624
5625         dprintk(1, "\n");
5626
5627         /* added antenna switch */
5628         SwitchAntennaToQAM(state);
5629
5630         /* Ensure correct power-up mode */
5631         status = PowerUpQAM(state);
5632         if (status < 0)
5633                 goto error;
5634         /* Reset QAM block */
5635         status = QAMResetQAM(state);
5636         if (status < 0)
5637                 goto error;
5638
5639         /* Setup IQM */
5640
5641         status = write16(state, IQM_COMM_EXEC__A, IQM_COMM_EXEC_B_STOP);
5642         if (status < 0)
5643                 goto error;
5644         status = write16(state, IQM_AF_AMUX__A, IQM_AF_AMUX_SIGNAL2ADC);
5645         if (status < 0)
5646                 goto error;
5647
5648         /* Upload IQM Channel Filter settings by
5649                 boot loader from ROM table */
5650         switch (oMode) {
5651         case OM_QAM_ITU_A:
5652                 status = BLChainCmd(state, DRXK_BL_ROM_OFFSET_TAPS_ITU_A, DRXK_BLCC_NR_ELEMENTS_TAPS, DRXK_BLC_TIMEOUT);
5653                 break;
5654         case OM_QAM_ITU_C:
5655                 status = BLDirectCmd(state, IQM_CF_TAP_RE0__A, DRXK_BL_ROM_OFFSET_TAPS_ITU_C, DRXK_BLDC_NR_ELEMENTS_TAPS, DRXK_BLC_TIMEOUT);
5656                 if (status < 0)
5657                         goto error;
5658                 status = BLDirectCmd(state, IQM_CF_TAP_IM0__A, DRXK_BL_ROM_OFFSET_TAPS_ITU_C, DRXK_BLDC_NR_ELEMENTS_TAPS, DRXK_BLC_TIMEOUT);
5659                 break;
5660         default:
5661                 status = -EINVAL;
5662         }
5663         if (status < 0)
5664                 goto error;
5665
5666         status = write16(state, IQM_CF_OUT_ENA__A, (1 << IQM_CF_OUT_ENA_QAM__B));
5667         if (status < 0)
5668                 goto error;
5669         status = write16(state, IQM_CF_SYMMETRIC__A, 0);
5670         if (status < 0)
5671                 goto error;
5672         status = write16(state, IQM_CF_MIDTAP__A, ((1 << IQM_CF_MIDTAP_RE__B) | (1 << IQM_CF_MIDTAP_IM__B)));
5673         if (status < 0)
5674                 goto error;
5675
5676         status = write16(state, IQM_RC_STRETCH__A, 21);
5677         if (status < 0)
5678                 goto error;
5679         status = write16(state, IQM_AF_CLP_LEN__A, 0);
5680         if (status < 0)
5681                 goto error;
5682         status = write16(state, IQM_AF_CLP_TH__A, 448);
5683         if (status < 0)
5684                 goto error;
5685         status = write16(state, IQM_AF_SNS_LEN__A, 0);
5686         if (status < 0)
5687                 goto error;
5688         status = write16(state, IQM_CF_POW_MEAS_LEN__A, 0);
5689         if (status < 0)
5690                 goto error;
5691
5692         status = write16(state, IQM_FS_ADJ_SEL__A, 1);
5693         if (status < 0)
5694                 goto error;
5695         status = write16(state, IQM_RC_ADJ_SEL__A, 1);
5696         if (status < 0)
5697                 goto error;
5698         status = write16(state, IQM_CF_ADJ_SEL__A, 1);
5699         if (status < 0)
5700                 goto error;
5701         status = write16(state, IQM_AF_UPD_SEL__A, 0);
5702         if (status < 0)
5703                 goto error;
5704
5705         /* IQM Impulse Noise Processing Unit */
5706         status = write16(state, IQM_CF_CLP_VAL__A, 500);
5707         if (status < 0)
5708                 goto error;
5709         status = write16(state, IQM_CF_DATATH__A, 1000);
5710         if (status < 0)
5711                 goto error;
5712         status = write16(state, IQM_CF_BYPASSDET__A, 1);
5713         if (status < 0)
5714                 goto error;
5715         status = write16(state, IQM_CF_DET_LCT__A, 0);
5716         if (status < 0)
5717                 goto error;
5718         status = write16(state, IQM_CF_WND_LEN__A, 1);
5719         if (status < 0)
5720                 goto error;
5721         status = write16(state, IQM_CF_PKDTH__A, 1);
5722         if (status < 0)
5723                 goto error;
5724         status = write16(state, IQM_AF_INC_BYPASS__A, 1);
5725         if (status < 0)
5726                 goto error;
5727
5728         /* turn on IQMAF. Must be done before setAgc**() */
5729         status = SetIqmAf(state, true);
5730         if (status < 0)
5731                 goto error;
5732         status = write16(state, IQM_AF_START_LOCK__A, 0x01);
5733         if (status < 0)
5734                 goto error;
5735
5736         /* IQM will not be reset from here, sync ADC and update/init AGC */
5737         status = ADCSynchronization(state);
5738         if (status < 0)
5739                 goto error;
5740
5741         /* Set the FSM step period */
5742         status = write16(state, SCU_RAM_QAM_FSM_STEP_PERIOD__A, 2000);
5743         if (status < 0)
5744                 goto error;
5745
5746         /* Halt SCU to enable safe non-atomic accesses */
5747         status = write16(state, SCU_COMM_EXEC__A, SCU_COMM_EXEC_HOLD);
5748         if (status < 0)
5749                 goto error;
5750
5751         /* No more resets of the IQM, current standard correctly set =>
5752                 now AGCs can be configured. */
5753
5754         status = InitAGC(state, true);
5755         if (status < 0)
5756                 goto error;
5757         status = SetPreSaw(state, &(state->m_qamPreSawCfg));
5758         if (status < 0)
5759                 goto error;
5760
5761         /* Configure AGC's */
5762         status = SetAgcRf(state, &(state->m_qamRfAgcCfg), true);
5763         if (status < 0)
5764                 goto error;
5765         status = SetAgcIf(state, &(state->m_qamIfAgcCfg), true);
5766         if (status < 0)
5767                 goto error;
5768
5769         /* Activate SCU to enable SCU commands */
5770         status = write16(state, SCU_COMM_EXEC__A, SCU_COMM_EXEC_ACTIVE);
5771 error:
5772         if (status < 0)
5773                 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
5774         return status;
5775 }
5776
5777 static int WriteGPIO(struct drxk_state *state)
5778 {
5779         int status;
5780         u16 value = 0;
5781
5782         dprintk(1, "\n");
5783         /* stop lock indicator process */
5784         status = write16(state, SCU_RAM_GPIO__A, SCU_RAM_GPIO_HW_LOCK_IND_DISABLE);
5785         if (status < 0)
5786                 goto error;
5787
5788         /*  Write magic word to enable pdr reg write               */
5789         status = write16(state, SIO_TOP_COMM_KEY__A, SIO_TOP_COMM_KEY_KEY);
5790         if (status < 0)
5791                 goto error;
5792
5793         if (state->m_hasSAWSW) {
5794                 if (state->UIO_mask & 0x0001) { /* UIO-1 */
5795                         /* write to io pad configuration register - output mode */
5796                         status = write16(state, SIO_PDR_SMA_TX_CFG__A, state->m_GPIOCfg);
5797                         if (status < 0)
5798                                 goto error;
5799
5800                         /* use corresponding bit in io data output registar */
5801                         status = read16(state, SIO_PDR_UIO_OUT_LO__A, &value);
5802                         if (status < 0)
5803                                 goto error;
5804                         if ((state->m_GPIO & 0x0001) == 0)
5805                                 value &= 0x7FFF;        /* write zero to 15th bit - 1st UIO */
5806                         else
5807                                 value |= 0x8000;        /* write one to 15th bit - 1st UIO */
5808                         /* write back to io data output register */
5809                         status = write16(state, SIO_PDR_UIO_OUT_LO__A, value);
5810                         if (status < 0)
5811                                 goto error;
5812                 }
5813                 if (state->UIO_mask & 0x0002) { /* UIO-2 */
5814                         /* write to io pad configuration register - output mode */
5815                         status = write16(state, SIO_PDR_SMA_TX_CFG__A, state->m_GPIOCfg);
5816                         if (status < 0)
5817                                 goto error;
5818
5819                         /* use corresponding bit in io data output registar */
5820                         status = read16(state, SIO_PDR_UIO_OUT_LO__A, &value);
5821                         if (status < 0)
5822                                 goto error;
5823                         if ((state->m_GPIO & 0x0002) == 0)
5824                                 value &= 0xBFFF;        /* write zero to 14th bit - 2st UIO */
5825                         else
5826                                 value |= 0x4000;        /* write one to 14th bit - 2st UIO */
5827                         /* write back to io data output register */
5828                         status = write16(state, SIO_PDR_UIO_OUT_LO__A, value);
5829                         if (status < 0)
5830                                 goto error;
5831                 }
5832                 if (state->UIO_mask & 0x0004) { /* UIO-3 */
5833                         /* write to io pad configuration register - output mode */
5834                         status = write16(state, SIO_PDR_SMA_TX_CFG__A, state->m_GPIOCfg);
5835                         if (status < 0)
5836                                 goto error;
5837
5838                         /* use corresponding bit in io data output registar */
5839                         status = read16(state, SIO_PDR_UIO_OUT_LO__A, &value);
5840                         if (status < 0)
5841                                 goto error;
5842                         if ((state->m_GPIO & 0x0004) == 0)
5843                                 value &= 0xFFFB;            /* write zero to 2nd bit - 3rd UIO */
5844                         else
5845                                 value |= 0x0004;            /* write one to 2nd bit - 3rd UIO */
5846                         /* write back to io data output register */
5847                         status = write16(state, SIO_PDR_UIO_OUT_LO__A, value);
5848                         if (status < 0)
5849                                 goto error;
5850                 }
5851         }
5852         /*  Write magic word to disable pdr reg write               */
5853         status = write16(state, SIO_TOP_COMM_KEY__A, 0x0000);
5854 error:
5855         if (status < 0)
5856                 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
5857         return status;
5858 }
5859
5860 static int SwitchAntennaToQAM(struct drxk_state *state)
5861 {
5862         int status = 0;
5863         bool gpio_state;
5864
5865         dprintk(1, "\n");
5866
5867         if (!state->antenna_gpio)
5868                 return 0;
5869
5870         gpio_state = state->m_GPIO & state->antenna_gpio;
5871
5872         if (state->antenna_dvbt ^ gpio_state) {
5873                 /* Antenna is on DVB-T mode. Switch */
5874                 if (state->antenna_dvbt)
5875                         state->m_GPIO &= ~state->antenna_gpio;
5876                 else
5877                         state->m_GPIO |= state->antenna_gpio;
5878                 status = WriteGPIO(state);
5879         }
5880         if (status < 0)
5881                 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
5882         return status;
5883 }
5884
5885 static int SwitchAntennaToDVBT(struct drxk_state *state)
5886 {
5887         int status = 0;
5888         bool gpio_state;
5889
5890         dprintk(1, "\n");
5891
5892         if (!state->antenna_gpio)
5893                 return 0;
5894
5895         gpio_state = state->m_GPIO & state->antenna_gpio;
5896
5897         if (!(state->antenna_dvbt ^ gpio_state)) {
5898                 /* Antenna is on DVB-C mode. Switch */
5899                 if (state->antenna_dvbt)
5900                         state->m_GPIO |= state->antenna_gpio;
5901                 else
5902                         state->m_GPIO &= ~state->antenna_gpio;
5903                 status = WriteGPIO(state);
5904         }
5905         if (status < 0)
5906                 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
5907         return status;
5908 }
5909
5910
5911 static int PowerDownDevice(struct drxk_state *state)
5912 {
5913         /* Power down to requested mode */
5914         /* Backup some register settings */
5915         /* Set pins with possible pull-ups connected to them in input mode */
5916         /* Analog power down */
5917         /* ADC power down */
5918         /* Power down device */
5919         int status;
5920
5921         dprintk(1, "\n");
5922         if (state->m_bPDownOpenBridge) {
5923                 /* Open I2C bridge before power down of DRXK */
5924                 status = ConfigureI2CBridge(state, true);
5925                 if (status < 0)
5926                         goto error;
5927         }
5928         /* driver 0.9.0 */
5929         status = DVBTEnableOFDMTokenRing(state, false);
5930         if (status < 0)
5931                 goto error;
5932
5933         status = write16(state, SIO_CC_PWD_MODE__A, SIO_CC_PWD_MODE_LEVEL_CLOCK);
5934         if (status < 0)
5935                 goto error;
5936         status = write16(state, SIO_CC_UPDATE__A, SIO_CC_UPDATE_KEY);
5937         if (status < 0)
5938                 goto error;
5939         state->m_HICfgCtrl |= SIO_HI_RA_RAM_PAR_5_CFG_SLEEP_ZZZ;
5940         status = HI_CfgCommand(state);
5941 error:
5942         if (status < 0)
5943                 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
5944
5945         return status;
5946 }
5947
5948 static int load_microcode(struct drxk_state *state, const char *mc_name)
5949 {
5950         const struct firmware *fw = NULL;
5951         int err = 0;
5952
5953         dprintk(1, "\n");
5954
5955         err = request_firmware(&fw, mc_name, state->i2c->dev.parent);
5956         if (err < 0) {
5957                 printk(KERN_ERR
5958                        "drxk: Could not load firmware file %s.\n", mc_name);
5959                 printk(KERN_INFO
5960                        "drxk: Copy %s to your hotplug directory!\n", mc_name);
5961                 return err;
5962         }
5963         err = DownloadMicrocode(state, fw->data, fw->size);
5964         release_firmware(fw);
5965         return err;
5966 }
5967
5968 static int init_drxk(struct drxk_state *state)
5969 {
5970         int status = 0;
5971         enum DRXPowerMode powerMode = DRXK_POWER_DOWN_OFDM;
5972         u16 driverVersion;
5973
5974         dprintk(1, "\n");
5975         if ((state->m_DrxkState == DRXK_UNINITIALIZED)) {
5976                 status = PowerUpDevice(state);
5977                 if (status < 0)
5978                         goto error;
5979                 status = DRXX_Open(state);
5980                 if (status < 0)
5981                         goto error;
5982                 /* Soft reset of OFDM-, sys- and osc-clockdomain */
5983                 status = write16(state, SIO_CC_SOFT_RST__A, SIO_CC_SOFT_RST_OFDM__M | SIO_CC_SOFT_RST_SYS__M | SIO_CC_SOFT_RST_OSC__M);
5984                 if (status < 0)
5985                         goto error;
5986                 status = write16(state, SIO_CC_UPDATE__A, SIO_CC_UPDATE_KEY);
5987                 if (status < 0)
5988                         goto error;
5989                 /* TODO is this needed, if yes how much delay in worst case scenario */
5990                 msleep(1);
5991                 state->m_DRXK_A3_PATCH_CODE = true;
5992                 status = GetDeviceCapabilities(state);
5993                 if (status < 0)
5994                         goto error;
5995
5996                 /* Bridge delay, uses oscilator clock */
5997                 /* Delay = (delay (nano seconds) * oscclk (kHz))/ 1000 */
5998                 /* SDA brdige delay */
5999                 state->m_HICfgBridgeDelay =
6000                         (u16) ((state->m_oscClockFreq / 1000) *
6001                                 HI_I2C_BRIDGE_DELAY) / 1000;
6002                 /* Clipping */
6003                 if (state->m_HICfgBridgeDelay >
6004                         SIO_HI_RA_RAM_PAR_3_CFG_DBL_SDA__M) {
6005                         state->m_HICfgBridgeDelay =
6006                                 SIO_HI_RA_RAM_PAR_3_CFG_DBL_SDA__M;
6007                 }
6008                 /* SCL bridge delay, same as SDA for now */
6009                 state->m_HICfgBridgeDelay +=
6010                         state->m_HICfgBridgeDelay <<
6011                         SIO_HI_RA_RAM_PAR_3_CFG_DBL_SCL__B;
6012
6013                 status = InitHI(state);
6014                 if (status < 0)
6015                         goto error;
6016                 /* disable various processes */
6017 #if NOA1ROM
6018                 if (!(state->m_DRXK_A1_ROM_CODE)
6019                         && !(state->m_DRXK_A2_ROM_CODE))
6020 #endif
6021                 {
6022                         status = write16(state, SCU_RAM_GPIO__A, SCU_RAM_GPIO_HW_LOCK_IND_DISABLE);
6023                         if (status < 0)
6024                                 goto error;
6025                 }
6026
6027                 /* disable MPEG port */
6028                 status = MPEGTSDisable(state);
6029                 if (status < 0)
6030                         goto error;
6031
6032                 /* Stop AUD and SCU */
6033                 status = write16(state, AUD_COMM_EXEC__A, AUD_COMM_EXEC_STOP);
6034                 if (status < 0)
6035                         goto error;
6036                 status = write16(state, SCU_COMM_EXEC__A, SCU_COMM_EXEC_STOP);
6037                 if (status < 0)
6038                         goto error;
6039
6040                 /* enable token-ring bus through OFDM block for possible ucode upload */
6041                 status = write16(state, SIO_OFDM_SH_OFDM_RING_ENABLE__A, SIO_OFDM_SH_OFDM_RING_ENABLE_ON);
6042                 if (status < 0)
6043                         goto error;
6044
6045                 /* include boot loader section */
6046                 status = write16(state, SIO_BL_COMM_EXEC__A, SIO_BL_COMM_EXEC_ACTIVE);
6047                 if (status < 0)
6048                         goto error;
6049                 status = BLChainCmd(state, 0, 6, 100);
6050                 if (status < 0)
6051                         goto error;
6052
6053                 if (!state->microcode_name)
6054                         load_microcode(state, "drxk_a3.mc");
6055                 else
6056                         load_microcode(state, state->microcode_name);
6057
6058                 /* disable token-ring bus through OFDM block for possible ucode upload */
6059                 status = write16(state, SIO_OFDM_SH_OFDM_RING_ENABLE__A, SIO_OFDM_SH_OFDM_RING_ENABLE_OFF);
6060                 if (status < 0)
6061                         goto error;
6062
6063                 /* Run SCU for a little while to initialize microcode version numbers */
6064                 status = write16(state, SCU_COMM_EXEC__A, SCU_COMM_EXEC_ACTIVE);
6065                 if (status < 0)
6066                         goto error;
6067                 status = DRXX_Open(state);
6068                 if (status < 0)
6069                         goto error;
6070                 /* added for test */
6071                 msleep(30);
6072
6073                 powerMode = DRXK_POWER_DOWN_OFDM;
6074                 status = CtrlPowerMode(state, &powerMode);
6075                 if (status < 0)
6076                         goto error;
6077
6078                 /* Stamp driver version number in SCU data RAM in BCD code
6079                         Done to enable field application engineers to retreive drxdriver version
6080                         via I2C from SCU RAM.
6081                         Not using SCU command interface for SCU register access since no
6082                         microcode may be present.
6083                         */
6084                 driverVersion =
6085                         (((DRXK_VERSION_MAJOR / 100) % 10) << 12) +
6086                         (((DRXK_VERSION_MAJOR / 10) % 10) << 8) +
6087                         ((DRXK_VERSION_MAJOR % 10) << 4) +
6088                         (DRXK_VERSION_MINOR % 10);
6089                 status = write16(state, SCU_RAM_DRIVER_VER_HI__A, driverVersion);
6090                 if (status < 0)
6091                         goto error;
6092                 driverVersion =
6093                         (((DRXK_VERSION_PATCH / 1000) % 10) << 12) +
6094                         (((DRXK_VERSION_PATCH / 100) % 10) << 8) +
6095                         (((DRXK_VERSION_PATCH / 10) % 10) << 4) +
6096                         (DRXK_VERSION_PATCH % 10);
6097                 status = write16(state, SCU_RAM_DRIVER_VER_LO__A, driverVersion);
6098                 if (status < 0)
6099                         goto error;
6100
6101                 printk(KERN_INFO "DRXK driver version %d.%d.%d\n",
6102                         DRXK_VERSION_MAJOR, DRXK_VERSION_MINOR,
6103                         DRXK_VERSION_PATCH);
6104
6105                 /* Dirty fix of default values for ROM/PATCH microcode
6106                         Dirty because this fix makes it impossible to setup suitable values
6107                         before calling DRX_Open. This solution requires changes to RF AGC speed
6108                         to be done via the CTRL function after calling DRX_Open */
6109
6110                 /* m_dvbtRfAgcCfg.speed = 3; */
6111
6112                 /* Reset driver debug flags to 0 */
6113                 status = write16(state, SCU_RAM_DRIVER_DEBUG__A, 0);
6114                 if (status < 0)
6115                         goto error;
6116                 /* driver 0.9.0 */
6117                 /* Setup FEC OC:
6118                         NOTE: No more full FEC resets allowed afterwards!! */
6119                 status = write16(state, FEC_COMM_EXEC__A, FEC_COMM_EXEC_STOP);
6120                 if (status < 0)
6121                         goto error;
6122                 /* MPEGTS functions are still the same */
6123                 status = MPEGTSDtoInit(state);
6124                 if (status < 0)
6125                         goto error;
6126                 status = MPEGTSStop(state);
6127                 if (status < 0)
6128                         goto error;
6129                 status = MPEGTSConfigurePolarity(state);
6130                 if (status < 0)
6131                         goto error;
6132                 status = MPEGTSConfigurePins(state, state->m_enableMPEGOutput);
6133                 if (status < 0)
6134                         goto error;
6135                 /* added: configure GPIO */
6136                 status = WriteGPIO(state);
6137                 if (status < 0)
6138                         goto error;
6139
6140                 state->m_DrxkState = DRXK_STOPPED;
6141
6142                 if (state->m_bPowerDown) {
6143                         status = PowerDownDevice(state);
6144                         if (status < 0)
6145                                 goto error;
6146                         state->m_DrxkState = DRXK_POWERED_DOWN;
6147                 } else
6148                         state->m_DrxkState = DRXK_STOPPED;
6149         }
6150 error:
6151         if (status < 0)
6152                 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
6153
6154         return 0;
6155 }
6156
6157 static void drxk_c_release(struct dvb_frontend *fe)
6158 {
6159         struct drxk_state *state = fe->demodulator_priv;
6160
6161         dprintk(1, "\n");
6162         kfree(state);
6163 }
6164
6165 static int drxk_c_init(struct dvb_frontend *fe)
6166 {
6167         struct drxk_state *state = fe->demodulator_priv;
6168
6169         dprintk(1, "\n");
6170         if (mutex_trylock(&state->ctlock) == 0)
6171                 return -EBUSY;
6172         SetOperationMode(state, OM_QAM_ITU_A);
6173         return 0;
6174 }
6175
6176 static int drxk_c_sleep(struct dvb_frontend *fe)
6177 {
6178         struct drxk_state *state = fe->demodulator_priv;
6179
6180         dprintk(1, "\n");
6181         ShutDown(state);
6182         mutex_unlock(&state->ctlock);
6183         return 0;
6184 }
6185
6186 static int drxk_gate_ctrl(struct dvb_frontend *fe, int enable)
6187 {
6188         struct drxk_state *state = fe->demodulator_priv;
6189
6190         dprintk(1, "%s\n", enable ? "enable" : "disable");
6191         return ConfigureI2CBridge(state, enable ? true : false);
6192 }
6193
6194 static int drxk_set_parameters(struct dvb_frontend *fe,
6195                                struct dvb_frontend_parameters *p)
6196 {
6197         struct drxk_state *state = fe->demodulator_priv;
6198         u32 IF;
6199
6200         dprintk(1, "\n");
6201         if (fe->ops.i2c_gate_ctrl)
6202                 fe->ops.i2c_gate_ctrl(fe, 1);
6203         if (fe->ops.tuner_ops.set_params)
6204                 fe->ops.tuner_ops.set_params(fe, p);
6205         if (fe->ops.i2c_gate_ctrl)
6206                 fe->ops.i2c_gate_ctrl(fe, 0);
6207         state->param = *p;
6208         fe->ops.tuner_ops.get_frequency(fe, &IF);
6209         Start(state, 0, IF);
6210
6211         /* printk(KERN_DEBUG "drxk: %s IF=%d done\n", __func__, IF); */
6212
6213         return 0;
6214 }
6215
6216 static int drxk_c_get_frontend(struct dvb_frontend *fe,
6217                                struct dvb_frontend_parameters *p)
6218 {
6219         dprintk(1, "\n");
6220         return 0;
6221 }
6222
6223 static int drxk_read_status(struct dvb_frontend *fe, fe_status_t *status)
6224 {
6225         struct drxk_state *state = fe->demodulator_priv;
6226         u32 stat;
6227
6228         dprintk(1, "\n");
6229         *status = 0;
6230         GetLockStatus(state, &stat, 0);
6231         if (stat == MPEG_LOCK)
6232                 *status |= 0x1f;
6233         if (stat == FEC_LOCK)
6234                 *status |= 0x0f;
6235         if (stat == DEMOD_LOCK)
6236                 *status |= 0x07;
6237         return 0;
6238 }
6239
6240 static int drxk_read_ber(struct dvb_frontend *fe, u32 *ber)
6241 {
6242         dprintk(1, "\n");
6243
6244         *ber = 0;
6245         return 0;
6246 }
6247
6248 static int drxk_read_signal_strength(struct dvb_frontend *fe,
6249                                      u16 *strength)
6250 {
6251         struct drxk_state *state = fe->demodulator_priv;
6252         u32 val = 0;
6253
6254         dprintk(1, "\n");
6255         ReadIFAgc(state, &val);
6256         *strength = val & 0xffff;
6257         return 0;
6258 }
6259
6260 static int drxk_read_snr(struct dvb_frontend *fe, u16 *snr)
6261 {
6262         struct drxk_state *state = fe->demodulator_priv;
6263         s32 snr2;
6264
6265         dprintk(1, "\n");
6266         GetSignalToNoise(state, &snr2);
6267         *snr = snr2 & 0xffff;
6268         return 0;
6269 }
6270
6271 static int drxk_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
6272 {
6273         struct drxk_state *state = fe->demodulator_priv;
6274         u16 err;
6275
6276         dprintk(1, "\n");
6277         DVBTQAMGetAccPktErr(state, &err);
6278         *ucblocks = (u32) err;
6279         return 0;
6280 }
6281
6282 static int drxk_c_get_tune_settings(struct dvb_frontend *fe, struct dvb_frontend_tune_settings
6283                                     *sets)
6284 {
6285         dprintk(1, "\n");
6286         sets->min_delay_ms = 3000;
6287         sets->max_drift = 0;
6288         sets->step_size = 0;
6289         return 0;
6290 }
6291
6292 static void drxk_t_release(struct dvb_frontend *fe)
6293 {
6294 #if 0
6295         struct drxk_state *state = fe->demodulator_priv;
6296
6297         dprintk(1, "\n");
6298         kfree(state);
6299 #endif
6300 }
6301
6302 static int drxk_t_init(struct dvb_frontend *fe)
6303 {
6304         struct drxk_state *state = fe->demodulator_priv;
6305
6306         dprintk(1, "\n");
6307         if (mutex_trylock(&state->ctlock) == 0)
6308                 return -EBUSY;
6309         SetOperationMode(state, OM_DVBT);
6310         return 0;
6311 }
6312
6313 static int drxk_t_sleep(struct dvb_frontend *fe)
6314 {
6315         struct drxk_state *state = fe->demodulator_priv;
6316
6317         dprintk(1, "\n");
6318         mutex_unlock(&state->ctlock);
6319         return 0;
6320 }
6321
6322 static int drxk_t_get_frontend(struct dvb_frontend *fe,
6323                                struct dvb_frontend_parameters *p)
6324 {
6325         dprintk(1, "\n");
6326
6327         return 0;
6328 }
6329
6330 static struct dvb_frontend_ops drxk_c_ops = {
6331         .info = {
6332                  .name = "DRXK DVB-C",
6333                  .type = FE_QAM,
6334                  .frequency_stepsize = 62500,
6335                  .frequency_min = 47000000,
6336                  .frequency_max = 862000000,
6337                  .symbol_rate_min = 870000,
6338                  .symbol_rate_max = 11700000,
6339                  .caps = FE_CAN_QAM_16 | FE_CAN_QAM_32 | FE_CAN_QAM_64 |
6340                  FE_CAN_QAM_128 | FE_CAN_QAM_256 | FE_CAN_FEC_AUTO},
6341         .release = drxk_c_release,
6342         .init = drxk_c_init,
6343         .sleep = drxk_c_sleep,
6344         .i2c_gate_ctrl = drxk_gate_ctrl,
6345
6346         .set_frontend = drxk_set_parameters,
6347         .get_frontend = drxk_c_get_frontend,
6348         .get_tune_settings = drxk_c_get_tune_settings,
6349
6350         .read_status = drxk_read_status,
6351         .read_ber = drxk_read_ber,
6352         .read_signal_strength = drxk_read_signal_strength,
6353         .read_snr = drxk_read_snr,
6354         .read_ucblocks = drxk_read_ucblocks,
6355 };
6356
6357 static struct dvb_frontend_ops drxk_t_ops = {
6358         .info = {
6359                  .name = "DRXK DVB-T",
6360                  .type = FE_OFDM,
6361                  .frequency_min = 47125000,
6362                  .frequency_max = 865000000,
6363                  .frequency_stepsize = 166667,
6364                  .frequency_tolerance = 0,
6365                  .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 |
6366                  FE_CAN_FEC_3_4 | FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 |
6367                  FE_CAN_FEC_AUTO |
6368                  FE_CAN_QAM_16 | FE_CAN_QAM_64 |
6369                  FE_CAN_QAM_AUTO |
6370                  FE_CAN_TRANSMISSION_MODE_AUTO |
6371                  FE_CAN_GUARD_INTERVAL_AUTO |
6372                  FE_CAN_HIERARCHY_AUTO | FE_CAN_RECOVER | FE_CAN_MUTE_TS},
6373         .release = drxk_t_release,
6374         .init = drxk_t_init,
6375         .sleep = drxk_t_sleep,
6376         .i2c_gate_ctrl = drxk_gate_ctrl,
6377
6378         .set_frontend = drxk_set_parameters,
6379         .get_frontend = drxk_t_get_frontend,
6380
6381         .read_status = drxk_read_status,
6382         .read_ber = drxk_read_ber,
6383         .read_signal_strength = drxk_read_signal_strength,
6384         .read_snr = drxk_read_snr,
6385         .read_ucblocks = drxk_read_ucblocks,
6386 };
6387
6388 struct dvb_frontend *drxk_attach(const struct drxk_config *config,
6389                                  struct i2c_adapter *i2c,
6390                                  struct dvb_frontend **fe_t)
6391 {
6392         struct drxk_state *state = NULL;
6393         u8 adr = config->adr;
6394
6395         dprintk(1, "\n");
6396         state = kzalloc(sizeof(struct drxk_state), GFP_KERNEL);
6397         if (!state)
6398                 return NULL;
6399
6400         state->i2c = i2c;
6401         state->demod_address = adr;
6402         state->single_master = config->single_master;
6403         state->microcode_name = config->microcode_name;
6404         state->no_i2c_bridge = config->no_i2c_bridge;
6405         state->antenna_gpio = config->antenna_gpio;
6406         state->antenna_dvbt = config->antenna_dvbt;
6407
6408         /* NOTE: as more UIO bits will be used, add them to the mask */
6409         state->UIO_mask = config->antenna_gpio;
6410
6411         /* Default gpio to DVB-C */
6412         if (!state->antenna_dvbt && state->antenna_gpio)
6413                 state->m_GPIO |= state->antenna_gpio;
6414         else
6415                 state->m_GPIO &= ~state->antenna_gpio;
6416
6417         mutex_init(&state->mutex);
6418         mutex_init(&state->ctlock);
6419
6420         memcpy(&state->c_frontend.ops, &drxk_c_ops,
6421                sizeof(struct dvb_frontend_ops));
6422         memcpy(&state->t_frontend.ops, &drxk_t_ops,
6423                sizeof(struct dvb_frontend_ops));
6424         state->c_frontend.demodulator_priv = state;
6425         state->t_frontend.demodulator_priv = state;
6426
6427         init_state(state);
6428         if (init_drxk(state) < 0)
6429                 goto error;
6430         *fe_t = &state->t_frontend;
6431
6432 #ifdef CONFIG_MEDIA_ATTACH
6433         /*
6434          * HACK: As this function initializes both DVB-T and DVB-C fe symbols,
6435          * and calling it twice would create the state twice, leading into
6436          * memory leaks, the right way is to call it only once. However, dvb
6437          * release functions will call symbol_put twice. So, the solution is to
6438          * artificially increment the usage count, in order to allow the
6439          * driver to be released.
6440          */
6441         symbol_get(drxk_attach);
6442 #endif
6443         return &state->c_frontend;
6444
6445 error:
6446         printk(KERN_ERR "drxk: not found\n");
6447         kfree(state);
6448         return NULL;
6449 }
6450 EXPORT_SYMBOL(drxk_attach);
6451
6452 MODULE_DESCRIPTION("DRX-K driver");
6453 MODULE_AUTHOR("Ralph Metzler");
6454 MODULE_LICENSE("GPL");