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