drxk: Does not unlock mutex if sanity check failed in scu_command()
[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");