blob: 0d288a7f01630645c8745bc6751ac5c8a1183979 [file] [log] [blame]
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001/*
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
38static int PowerDownDVBT(struct drxk_state *state, bool setPowerMode);
39static int PowerDownQAM(struct drxk_state *state);
Oliver Endrissebc7de22011-07-03 13:49:44 -030040static int SetDVBTStandard(struct drxk_state *state,
41 enum OperationMode oMode);
42static int SetQAMStandard(struct drxk_state *state,
43 enum OperationMode oMode);
44static int SetQAM(struct drxk_state *state, u16 IntermediateFreqkHz,
Ralph Metzler43dd07f2011-07-03 13:42:18 -030045 s32 tunerFreqOffset);
Oliver Endrissebc7de22011-07-03 13:49:44 -030046static int SetDVBTStandard(struct drxk_state *state,
47 enum OperationMode oMode);
Ralph Metzler43dd07f2011-07-03 13:42:18 -030048static int DVBTStart(struct drxk_state *state);
Oliver Endrissebc7de22011-07-03 13:49:44 -030049static int SetDVBT(struct drxk_state *state, u16 IntermediateFreqkHz,
50 s32 tunerFreqOffset);
Ralph Metzler43dd07f2011-07-03 13:42:18 -030051static int GetQAMLockStatus(struct drxk_state *state, u32 *pLockStatus);
52static int GetDVBTLockStatus(struct drxk_state *state, u32 *pLockStatus);
53static int SwitchAntennaToQAM(struct drxk_state *state);
54static int SwitchAntennaToDVBT(struct drxk_state *state);
55
56static bool IsDVBT(struct drxk_state *state)
57{
58 return state->m_OperationMode == OM_DVBT;
59}
60
61static bool IsQAM(struct drxk_state *state)
62{
63 return state->m_OperationMode == OM_QAM_ITU_A ||
Oliver Endrissebc7de22011-07-03 13:49:44 -030064 state->m_OperationMode == OM_QAM_ITU_B ||
65 state->m_OperationMode == OM_QAM_ITU_C;
Ralph Metzler43dd07f2011-07-03 13:42:18 -030066}
67
68bool IsA1WithPatchCode(struct drxk_state *state)
69{
70 return state->m_DRXK_A1_PATCH_CODE;
71}
72
73bool IsA1WithRomCode(struct drxk_state *state)
74{
75 return state->m_DRXK_A1_ROM_CODE;
76}
77
78#define NOA1ROM 0
79
Ralph Metzler43dd07f2011-07-03 13:42:18 -030080#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
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -0300176static unsigned int debug;
177module_param(debug, int, 0644);
178MODULE_PARM_DESC(debug, "enable debug messages");
179
180#define dprintk(level, fmt, arg...) do { \
181if (debug >= level) \
182 printk(KERN_DEBUG "drxk: %s" fmt, __func__, ## arg); \
183} while (0)
184
185
Mauro Carvalho Chehabb01fbc12011-07-03 17:18:57 -0300186static inline u32 MulDiv32(u32 a, u32 b, u32 c)
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300187{
188 u64 tmp64;
189
Oliver Endrissebc7de22011-07-03 13:49:44 -0300190 tmp64 = (u64) a * (u64) b;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300191 do_div(tmp64, c);
192
193 return (u32) tmp64;
194}
195
196inline u32 Frac28a(u32 a, u32 c)
197{
198 int i = 0;
199 u32 Q1 = 0;
200 u32 R0 = 0;
201
Oliver Endrissebc7de22011-07-03 13:49:44 -0300202 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 */
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300205
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
218static u32 Log10Times100(u32 x)
219{
220 static const u8 scale = 15;
221 static const u8 indexWidth = 5;
Oliver Endrissebc7de22011-07-03 13:49:44 -0300222 u8 i = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300223 u32 y = 0;
224 u32 d = 0;
225 u32 k = 0;
226 u32 r = 0;
227 /*
Oliver Endrissebc7de22011-07-03 13:49:44 -0300228 log2lut[n] = (1<<scale) * 200 * log2(1.0 + ((1.0/(1<<INDEXWIDTH)) * n))
229 0 <= n < ((1<<INDEXWIDTH)+1)
230 */
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300231
232 static const u32 log2lut[] = {
Oliver Endrissebc7de22011-07-03 13:49:44 -0300233 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 */
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300266 };
267
268
269 if (x == 0)
Oliver Endrissebc7de22011-07-03 13:49:44 -0300270 return 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300271
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--) {
Oliver Endrissebc7de22011-07-03 13:49:44 -0300276 if (x & (((u32) 1) << scale))
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300277 break;
278 x <<= 1;
279 }
280 } else {
Oliver Endrissebc7de22011-07-03 13:49:44 -0300281 for (k = scale; k < 31; k++) {
282 if ((x & (((u32) (-1)) << (scale + 1))) == 0)
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300283 break;
284 x >>= 1;
Oliver Endrissebc7de22011-07-03 13:49:44 -0300285 }
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300286 }
287 /*
Oliver Endrissebc7de22011-07-03 13:49:44 -0300288 Now x has binary point between bit[scale] and bit[scale-1]
289 and 1.0 <= x < 2.0 */
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300290
291 /* correction for divison: log(x) = log(x/y)+log(y) */
Oliver Endrissebc7de22011-07-03 13:49:44 -0300292 y = k * ((((u32) 1) << scale) * 200);
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300293
294 /* remove integer part */
Oliver Endrissebc7de22011-07-03 13:49:44 -0300295 x &= ((((u32) 1) << scale) - 1);
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300296 /* get index */
297 i = (u8) (x >> (scale - indexWidth));
298 /* compute delta (x - a) */
Oliver Endrissebc7de22011-07-03 13:49:44 -0300299 d = x & ((((u32) 1) << (scale - indexWidth)) - 1);
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300300 /* compute log, multiplication (d* (..)) must be within range ! */
301 y += log2lut[i] +
Oliver Endrissebc7de22011-07-03 13:49:44 -0300302 ((d * (log2lut[i + 1] - log2lut[i])) >> (scale - indexWidth));
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300303 /* Conver to log10() */
Oliver Endrissebc7de22011-07-03 13:49:44 -0300304 y /= 108853; /* (log2(10) << scale) */
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300305 r = (y >> 1);
306 /* rounding */
Oliver Endrissebc7de22011-07-03 13:49:44 -0300307 if (y & ((u32) 1))
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300308 r++;
Oliver Endrissebc7de22011-07-03 13:49:44 -0300309 return r;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300310}
311
312/****************************************************************************/
313/* I2C **********************************************************************/
314/****************************************************************************/
315
316static int i2c_read1(struct i2c_adapter *adapter, u8 adr, u8 *val)
317{
Oliver Endrissebc7de22011-07-03 13:49:44 -0300318 struct i2c_msg msgs[1] = { {.addr = adr, .flags = I2C_M_RD,
319 .buf = val, .len = 1}
320 };
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300321
322 return i2c_transfer(adapter, msgs, 1);
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300323}
324
325static int i2c_write(struct i2c_adapter *adap, u8 adr, u8 *data, int len)
326{
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300327 int status;
Oliver Endrissebc7de22011-07-03 13:49:44 -0300328 struct i2c_msg msg = {
329 .addr = adr, .flags = 0, .buf = data, .len = len };
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300330
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -0300331 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 }
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300338 status = i2c_transfer(adap, &msg, 1);
339 if (status >= 0 && status != 1)
340 status = -EIO;
341
342 if (status < 0)
Mauro Carvalho Chehabe0e6eca2011-07-04 08:27:47 -0300343 printk(KERN_ERR "drxk: i2c write error at addr 0x%02x\n", adr);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300344
345 return status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300346}
347
348static int i2c_read(struct i2c_adapter *adap,
349 u8 adr, u8 *msg, int len, u8 *answ, int alen)
350{
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300351 int status;
Mauro Carvalho Chehabe4f4f872011-07-09 17:35:26 -0300352 struct i2c_msg msgs[2] = {
353 {.addr = adr, .flags = 0,
Oliver Endrissebc7de22011-07-03 13:49:44 -0300354 .buf = msg, .len = len},
Mauro Carvalho Chehabe4f4f872011-07-09 17:35:26 -0300355 {.addr = adr, .flags = I2C_M_RD,
356 .buf = answ, .len = alen}
Oliver Endrissebc7de22011-07-03 13:49:44 -0300357 };
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -0300358 dprintk(3, ":");
359 if (debug > 2) {
360 int i;
361 for (i = 0; i < len; i++)
362 printk(KERN_CONT " %02x", msg[i]);
363 printk(KERN_CONT "\n");
364 }
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300365 status = i2c_transfer(adap, msgs, 2);
366 if (status != 2) {
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -0300367 if (debug > 2)
368 printk(KERN_CONT ": ERROR!\n");
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300369 if (status >= 0)
370 status = -EIO;
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -0300371
Mauro Carvalho Chehabe0e6eca2011-07-04 08:27:47 -0300372 printk(KERN_ERR "drxk: i2c read error at addr 0x%02x\n", adr);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300373 return status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300374 }
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -0300375 if (debug > 2) {
376 int i;
377 printk(KERN_CONT ": Read ");
378 for (i = 0; i < len; i++)
379 printk(KERN_CONT " %02x", msg[i]);
380 printk(KERN_CONT "\n");
381 }
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300382 return 0;
383}
384
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -0300385static int read16_flags(struct drxk_state *state, u32 reg, u16 *data, u8 flags)
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300386{
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300387 int status;
Oliver Endrissebc7de22011-07-03 13:49:44 -0300388 u8 adr = state->demod_address, mm1[4], mm2[2], len;
Mauro Carvalho Chehabe076c922011-07-09 13:06:12 -0300389
390 if (state->single_master)
391 flags |= 0xC0;
392
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300393 if (DRXDAP_FASI_LONG_FORMAT(reg) || (flags != 0)) {
394 mm1[0] = (((reg << 1) & 0xFF) | 0x01);
395 mm1[1] = ((reg >> 16) & 0xFF);
396 mm1[2] = ((reg >> 24) & 0xFF) | flags;
397 mm1[3] = ((reg >> 7) & 0xFF);
398 len = 4;
399 } else {
400 mm1[0] = ((reg << 1) & 0xFF);
401 mm1[1] = (((reg >> 16) & 0x0F) | ((reg >> 18) & 0xF0));
402 len = 2;
403 }
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -0300404 dprintk(2, "(0x%08x, 0x%02x)\n", reg, flags);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300405 status = i2c_read(state->i2c, adr, mm1, len, mm2, 2);
406 if (status < 0)
407 return status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300408 if (data)
409 *data = mm2[0] | (mm2[1] << 8);
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -0300410
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300411 return 0;
412}
413
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -0300414static int read16(struct drxk_state *state, u32 reg, u16 *data)
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300415{
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -0300416 return read16_flags(state, reg, data, 0);
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300417}
418
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -0300419static int read32_flags(struct drxk_state *state, u32 reg, u32 *data, u8 flags)
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300420{
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300421 int status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300422 u8 adr = state->demod_address, mm1[4], mm2[4], len;
Mauro Carvalho Chehabe076c922011-07-09 13:06:12 -0300423
424 if (state->single_master)
425 flags |= 0xC0;
426
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300427 if (DRXDAP_FASI_LONG_FORMAT(reg) || (flags != 0)) {
428 mm1[0] = (((reg << 1) & 0xFF) | 0x01);
429 mm1[1] = ((reg >> 16) & 0xFF);
430 mm1[2] = ((reg >> 24) & 0xFF) | flags;
431 mm1[3] = ((reg >> 7) & 0xFF);
432 len = 4;
433 } else {
434 mm1[0] = ((reg << 1) & 0xFF);
435 mm1[1] = (((reg >> 16) & 0x0F) | ((reg >> 18) & 0xF0));
436 len = 2;
437 }
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -0300438 dprintk(2, "(0x%08x, 0x%02x)\n", reg, flags);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300439 status = i2c_read(state->i2c, adr, mm1, len, mm2, 4);
440 if (status < 0)
441 return status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300442 if (data)
443 *data = mm2[0] | (mm2[1] << 8) |
Oliver Endrissebc7de22011-07-03 13:49:44 -0300444 (mm2[2] << 16) | (mm2[3] << 24);
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -0300445
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300446 return 0;
447}
448
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -0300449static int read32(struct drxk_state *state, u32 reg, u32 *data)
450{
451 return read32_flags(state, reg, data, 0);
452}
453
454static int write16_flags(struct drxk_state *state, u32 reg, u16 data, u8 flags)
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300455{
456 u8 adr = state->demod_address, mm[6], len;
Mauro Carvalho Chehabe076c922011-07-09 13:06:12 -0300457
458 if (state->single_master)
459 flags |= 0xC0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300460 if (DRXDAP_FASI_LONG_FORMAT(reg) || (flags != 0)) {
461 mm[0] = (((reg << 1) & 0xFF) | 0x01);
462 mm[1] = ((reg >> 16) & 0xFF);
463 mm[2] = ((reg >> 24) & 0xFF) | flags;
464 mm[3] = ((reg >> 7) & 0xFF);
465 len = 4;
466 } else {
467 mm[0] = ((reg << 1) & 0xFF);
468 mm[1] = (((reg >> 16) & 0x0F) | ((reg >> 18) & 0xF0));
469 len = 2;
470 }
471 mm[len] = data & 0xff;
Oliver Endrissebc7de22011-07-03 13:49:44 -0300472 mm[len + 1] = (data >> 8) & 0xff;
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -0300473
474 dprintk(2, "(0x%08x, 0x%04x, 0x%02x)\n", reg, data, flags);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300475 return i2c_write(state->i2c, adr, mm, len + 2);
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300476}
477
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -0300478static int write16(struct drxk_state *state, u32 reg, u16 data)
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300479{
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -0300480 return write16_flags(state, reg, data, 0);
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300481}
482
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -0300483static int write32_flags(struct drxk_state *state, u32 reg, u32 data, u8 flags)
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300484{
485 u8 adr = state->demod_address, mm[8], len;
Mauro Carvalho Chehabe076c922011-07-09 13:06:12 -0300486
487 if (state->single_master)
488 flags |= 0xC0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300489 if (DRXDAP_FASI_LONG_FORMAT(reg) || (flags != 0)) {
490 mm[0] = (((reg << 1) & 0xFF) | 0x01);
491 mm[1] = ((reg >> 16) & 0xFF);
492 mm[2] = ((reg >> 24) & 0xFF) | flags;
493 mm[3] = ((reg >> 7) & 0xFF);
494 len = 4;
495 } else {
496 mm[0] = ((reg << 1) & 0xFF);
497 mm[1] = (((reg >> 16) & 0x0F) | ((reg >> 18) & 0xF0));
498 len = 2;
499 }
500 mm[len] = data & 0xff;
Oliver Endrissebc7de22011-07-03 13:49:44 -0300501 mm[len + 1] = (data >> 8) & 0xff;
502 mm[len + 2] = (data >> 16) & 0xff;
503 mm[len + 3] = (data >> 24) & 0xff;
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -0300504 dprintk(2, "(0x%08x, 0x%08x, 0x%02x)\n", reg, data, flags);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300505
506 return i2c_write(state->i2c, adr, mm, len + 4);
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300507}
508
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -0300509static int write32(struct drxk_state *state, u32 reg, u32 data)
510{
511 return write32_flags(state, reg, data, 0);
512}
513
514static int write_block(struct drxk_state *state, u32 Address,
515 const int BlockSize, const u8 pBlock[])
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300516{
517 int status = 0, BlkSize = BlockSize;
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -0300518 u8 Flags = 0;
Mauro Carvalho Chehabe076c922011-07-09 13:06:12 -0300519
520 if (state->single_master)
521 Flags |= 0xC0;
522
Oliver Endrissebc7de22011-07-03 13:49:44 -0300523 while (BlkSize > 0) {
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300524 int Chunk = BlkSize > state->m_ChunkSize ?
Oliver Endrissebc7de22011-07-03 13:49:44 -0300525 state->m_ChunkSize : BlkSize;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300526 u8 *AdrBuf = &state->Chunk[0];
527 u32 AdrLength = 0;
528
Oliver Endrissebc7de22011-07-03 13:49:44 -0300529 if (DRXDAP_FASI_LONG_FORMAT(Address) || (Flags != 0)) {
530 AdrBuf[0] = (((Address << 1) & 0xFF) | 0x01);
531 AdrBuf[1] = ((Address >> 16) & 0xFF);
532 AdrBuf[2] = ((Address >> 24) & 0xFF);
533 AdrBuf[3] = ((Address >> 7) & 0xFF);
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300534 AdrBuf[2] |= Flags;
535 AdrLength = 4;
536 if (Chunk == state->m_ChunkSize)
537 Chunk -= 2;
Oliver Endrissebc7de22011-07-03 13:49:44 -0300538 } else {
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300539 AdrBuf[0] = ((Address << 1) & 0xFF);
540 AdrBuf[1] = (((Address >> 16) & 0x0F) |
541 ((Address >> 18) & 0xF0));
542 AdrLength = 2;
543 }
544 memcpy(&state->Chunk[AdrLength], pBlock, Chunk);
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -0300545 dprintk(2, "(0x%08x, 0x%02x)\n", Address, Flags);
546 if (debug > 1) {
547 int i;
548 if (pBlock)
549 for (i = 0; i < Chunk; i++)
550 printk(KERN_CONT " %02x", pBlock[i]);
551 printk(KERN_CONT "\n");
552 }
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300553 status = i2c_write(state->i2c, state->demod_address,
Oliver Endrissebc7de22011-07-03 13:49:44 -0300554 &state->Chunk[0], Chunk + AdrLength);
555 if (status < 0) {
Mauro Carvalho Chehabe0e6eca2011-07-04 08:27:47 -0300556 printk(KERN_ERR "drxk: %s: i2c write error at addr 0x%02x\n",
557 __func__, Address);
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300558 break;
559 }
560 pBlock += Chunk;
561 Address += (Chunk >> 1);
562 BlkSize -= Chunk;
563 }
Oliver Endrissebc7de22011-07-03 13:49:44 -0300564 return status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300565}
566
567#ifndef DRXK_MAX_RETRIES_POWERUP
568#define DRXK_MAX_RETRIES_POWERUP 20
569#endif
570
571int PowerUpDevice(struct drxk_state *state)
572{
573 int status;
574 u8 data = 0;
575 u16 retryCount = 0;
576
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -0300577 dprintk(1, "\n");
578
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300579 status = i2c_read1(state->i2c, state->demod_address, &data);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300580 if (status < 0) {
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300581 do {
582 data = 0;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300583 status = i2c_write(state->i2c, state->demod_address,
584 &data, 1);
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300585 msleep(10);
Oliver Endrissebc7de22011-07-03 13:49:44 -0300586 retryCount++;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300587 if (status < 0)
588 continue;
589 status = i2c_read1(state->i2c, state->demod_address,
590 &data);
591 } while (status < 0 &&
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300592 (retryCount < DRXK_MAX_RETRIES_POWERUP));
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300593 if (status < 0 && retryCount >= DRXK_MAX_RETRIES_POWERUP)
594 goto error;
595 }
596
597 /* Make sure all clk domains are active */
598 status = write16(state, SIO_CC_PWD_MODE__A, SIO_CC_PWD_MODE_LEVEL_NONE);
599 if (status < 0)
600 goto error;
601 status = write16(state, SIO_CC_UPDATE__A, SIO_CC_UPDATE_KEY);
602 if (status < 0)
603 goto error;
604 /* Enable pll lock tests */
605 status = write16(state, SIO_CC_PLL_LOCK__A, 1);
606 if (status < 0)
607 goto error;
608
609 state->m_currentPowerMode = DRX_POWER_UP;
610
611error:
612 if (status < 0)
613 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
614
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300615 return status;
616}
617
618
619static int init_state(struct drxk_state *state)
620{
Mauro Carvalho Chehab147e1102011-07-10 08:24:26 -0300621 /*
622 * FIXME: most (all?) of the values bellow should be moved into
623 * struct drxk_config, as they are probably board-specific
624 */
Oliver Endrissebc7de22011-07-03 13:49:44 -0300625 u32 ulVSBIfAgcMode = DRXK_AGC_CTRL_AUTO;
626 u32 ulVSBIfAgcOutputLevel = 0;
627 u32 ulVSBIfAgcMinLevel = 0;
628 u32 ulVSBIfAgcMaxLevel = 0x7FFF;
629 u32 ulVSBIfAgcSpeed = 3;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300630
Oliver Endrissebc7de22011-07-03 13:49:44 -0300631 u32 ulVSBRfAgcMode = DRXK_AGC_CTRL_AUTO;
632 u32 ulVSBRfAgcOutputLevel = 0;
633 u32 ulVSBRfAgcMinLevel = 0;
634 u32 ulVSBRfAgcMaxLevel = 0x7FFF;
635 u32 ulVSBRfAgcSpeed = 3;
636 u32 ulVSBRfAgcTop = 9500;
637 u32 ulVSBRfAgcCutOffCurrent = 4000;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300638
Oliver Endrissebc7de22011-07-03 13:49:44 -0300639 u32 ulATVIfAgcMode = DRXK_AGC_CTRL_AUTO;
640 u32 ulATVIfAgcOutputLevel = 0;
641 u32 ulATVIfAgcMinLevel = 0;
642 u32 ulATVIfAgcMaxLevel = 0;
643 u32 ulATVIfAgcSpeed = 3;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300644
Oliver Endrissebc7de22011-07-03 13:49:44 -0300645 u32 ulATVRfAgcMode = DRXK_AGC_CTRL_OFF;
646 u32 ulATVRfAgcOutputLevel = 0;
647 u32 ulATVRfAgcMinLevel = 0;
648 u32 ulATVRfAgcMaxLevel = 0;
649 u32 ulATVRfAgcTop = 9500;
650 u32 ulATVRfAgcCutOffCurrent = 4000;
651 u32 ulATVRfAgcSpeed = 3;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300652
653 u32 ulQual83 = DEFAULT_MER_83;
654 u32 ulQual93 = DEFAULT_MER_93;
655
656 u32 ulDVBTStaticTSClock = 1;
657 u32 ulDVBCStaticTSClock = 1;
658
659 u32 ulMpegLockTimeOut = DEFAULT_DRXK_MPEG_LOCK_TIMEOUT;
660 u32 ulDemodLockTimeOut = DEFAULT_DRXK_DEMOD_LOCK_TIMEOUT;
661
662 /* io_pad_cfg register (8 bit reg.) MSB bit is 1 (default value) */
663 /* io_pad_cfg_mode output mode is drive always */
664 /* io_pad_cfg_drive is set to power 2 (23 mA) */
665 u32 ulGPIOCfg = 0x0113;
Oliver Endrissebc7de22011-07-03 13:49:44 -0300666 u32 ulGPIO = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300667 u32 ulSerialMode = 1;
668 u32 ulInvertTSClock = 0;
669 u32 ulTSDataStrength = DRXK_MPEG_SERIAL_OUTPUT_PIN_DRIVE_STRENGTH;
670 u32 ulTSClockkStrength = DRXK_MPEG_OUTPUT_CLK_DRIVE_STRENGTH;
671 u32 ulDVBTBitrate = 50000000;
672 u32 ulDVBCBitrate = DRXK_QAM_SYMBOLRATE_MAX * 8;
673
674 u32 ulInsertRSByte = 0;
675
676 u32 ulRfMirror = 1;
677 u32 ulPowerDown = 0;
678
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -0300679 dprintk(1, "\n");
680
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300681 state->m_hasLNA = false;
Oliver Endrissebc7de22011-07-03 13:49:44 -0300682 state->m_hasDVBT = false;
683 state->m_hasDVBC = false;
684 state->m_hasATV = false;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300685 state->m_hasOOB = false;
686 state->m_hasAudio = false;
687
688 state->m_ChunkSize = 124;
689
690 state->m_oscClockFreq = 0;
691 state->m_smartAntInverted = false;
692 state->m_bPDownOpenBridge = false;
693
694 /* real system clock frequency in kHz */
Oliver Endrissebc7de22011-07-03 13:49:44 -0300695 state->m_sysClockFreq = 151875;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300696 /* Timing div, 250ns/Psys */
697 /* Timing div, = (delay (nano seconds) * sysclk (kHz))/ 1000 */
698 state->m_HICfgTimingDiv = ((state->m_sysClockFreq / 1000) *
699 HI_I2C_DELAY) / 1000;
700 /* Clipping */
701 if (state->m_HICfgTimingDiv > SIO_HI_RA_RAM_PAR_2_CFG_DIV__M)
702 state->m_HICfgTimingDiv = SIO_HI_RA_RAM_PAR_2_CFG_DIV__M;
703 state->m_HICfgWakeUpKey = (state->demod_address << 1);
704 /* port/bridge/power down ctrl */
705 state->m_HICfgCtrl = SIO_HI_RA_RAM_PAR_5_CFG_SLV0_SLAVE;
706
707 state->m_bPowerDown = (ulPowerDown != 0);
708
709 state->m_DRXK_A1_PATCH_CODE = false;
710 state->m_DRXK_A1_ROM_CODE = false;
711 state->m_DRXK_A2_ROM_CODE = false;
712 state->m_DRXK_A3_ROM_CODE = false;
713 state->m_DRXK_A2_PATCH_CODE = false;
714 state->m_DRXK_A3_PATCH_CODE = false;
715
716 /* Init AGC and PGA parameters */
717 /* VSB IF */
Oliver Endrissebc7de22011-07-03 13:49:44 -0300718 state->m_vsbIfAgcCfg.ctrlMode = (ulVSBIfAgcMode);
719 state->m_vsbIfAgcCfg.outputLevel = (ulVSBIfAgcOutputLevel);
720 state->m_vsbIfAgcCfg.minOutputLevel = (ulVSBIfAgcMinLevel);
721 state->m_vsbIfAgcCfg.maxOutputLevel = (ulVSBIfAgcMaxLevel);
722 state->m_vsbIfAgcCfg.speed = (ulVSBIfAgcSpeed);
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300723 state->m_vsbPgaCfg = 140;
724
725 /* VSB RF */
Oliver Endrissebc7de22011-07-03 13:49:44 -0300726 state->m_vsbRfAgcCfg.ctrlMode = (ulVSBRfAgcMode);
727 state->m_vsbRfAgcCfg.outputLevel = (ulVSBRfAgcOutputLevel);
728 state->m_vsbRfAgcCfg.minOutputLevel = (ulVSBRfAgcMinLevel);
729 state->m_vsbRfAgcCfg.maxOutputLevel = (ulVSBRfAgcMaxLevel);
730 state->m_vsbRfAgcCfg.speed = (ulVSBRfAgcSpeed);
731 state->m_vsbRfAgcCfg.top = (ulVSBRfAgcTop);
732 state->m_vsbRfAgcCfg.cutOffCurrent = (ulVSBRfAgcCutOffCurrent);
733 state->m_vsbPreSawCfg.reference = 0x07;
734 state->m_vsbPreSawCfg.usePreSaw = true;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300735
736 state->m_Quality83percent = DEFAULT_MER_83;
737 state->m_Quality93percent = DEFAULT_MER_93;
738 if (ulQual93 <= 500 && ulQual83 < ulQual93) {
739 state->m_Quality83percent = ulQual83;
740 state->m_Quality93percent = ulQual93;
741 }
742
743 /* ATV IF */
Oliver Endrissebc7de22011-07-03 13:49:44 -0300744 state->m_atvIfAgcCfg.ctrlMode = (ulATVIfAgcMode);
745 state->m_atvIfAgcCfg.outputLevel = (ulATVIfAgcOutputLevel);
746 state->m_atvIfAgcCfg.minOutputLevel = (ulATVIfAgcMinLevel);
747 state->m_atvIfAgcCfg.maxOutputLevel = (ulATVIfAgcMaxLevel);
748 state->m_atvIfAgcCfg.speed = (ulATVIfAgcSpeed);
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300749
750 /* ATV RF */
Oliver Endrissebc7de22011-07-03 13:49:44 -0300751 state->m_atvRfAgcCfg.ctrlMode = (ulATVRfAgcMode);
752 state->m_atvRfAgcCfg.outputLevel = (ulATVRfAgcOutputLevel);
753 state->m_atvRfAgcCfg.minOutputLevel = (ulATVRfAgcMinLevel);
754 state->m_atvRfAgcCfg.maxOutputLevel = (ulATVRfAgcMaxLevel);
755 state->m_atvRfAgcCfg.speed = (ulATVRfAgcSpeed);
756 state->m_atvRfAgcCfg.top = (ulATVRfAgcTop);
757 state->m_atvRfAgcCfg.cutOffCurrent = (ulATVRfAgcCutOffCurrent);
758 state->m_atvPreSawCfg.reference = 0x04;
759 state->m_atvPreSawCfg.usePreSaw = true;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300760
761
762 /* DVBT RF */
Oliver Endrissebc7de22011-07-03 13:49:44 -0300763 state->m_dvbtRfAgcCfg.ctrlMode = DRXK_AGC_CTRL_OFF;
764 state->m_dvbtRfAgcCfg.outputLevel = 0;
765 state->m_dvbtRfAgcCfg.minOutputLevel = 0;
766 state->m_dvbtRfAgcCfg.maxOutputLevel = 0xFFFF;
767 state->m_dvbtRfAgcCfg.top = 0x2100;
768 state->m_dvbtRfAgcCfg.cutOffCurrent = 4000;
769 state->m_dvbtRfAgcCfg.speed = 1;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300770
771
772 /* DVBT IF */
Oliver Endrissebc7de22011-07-03 13:49:44 -0300773 state->m_dvbtIfAgcCfg.ctrlMode = DRXK_AGC_CTRL_AUTO;
774 state->m_dvbtIfAgcCfg.outputLevel = 0;
775 state->m_dvbtIfAgcCfg.minOutputLevel = 0;
776 state->m_dvbtIfAgcCfg.maxOutputLevel = 9000;
777 state->m_dvbtIfAgcCfg.top = 13424;
778 state->m_dvbtIfAgcCfg.cutOffCurrent = 0;
779 state->m_dvbtIfAgcCfg.speed = 3;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300780 state->m_dvbtIfAgcCfg.FastClipCtrlDelay = 30;
Oliver Endrissebc7de22011-07-03 13:49:44 -0300781 state->m_dvbtIfAgcCfg.IngainTgtMax = 30000;
782 /* state->m_dvbtPgaCfg = 140; */
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300783
Oliver Endrissebc7de22011-07-03 13:49:44 -0300784 state->m_dvbtPreSawCfg.reference = 4;
785 state->m_dvbtPreSawCfg.usePreSaw = false;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300786
787 /* QAM RF */
Oliver Endrissebc7de22011-07-03 13:49:44 -0300788 state->m_qamRfAgcCfg.ctrlMode = DRXK_AGC_CTRL_OFF;
789 state->m_qamRfAgcCfg.outputLevel = 0;
790 state->m_qamRfAgcCfg.minOutputLevel = 6023;
791 state->m_qamRfAgcCfg.maxOutputLevel = 27000;
792 state->m_qamRfAgcCfg.top = 0x2380;
793 state->m_qamRfAgcCfg.cutOffCurrent = 4000;
794 state->m_qamRfAgcCfg.speed = 3;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300795
796 /* QAM IF */
Oliver Endrissebc7de22011-07-03 13:49:44 -0300797 state->m_qamIfAgcCfg.ctrlMode = DRXK_AGC_CTRL_AUTO;
798 state->m_qamIfAgcCfg.outputLevel = 0;
799 state->m_qamIfAgcCfg.minOutputLevel = 0;
800 state->m_qamIfAgcCfg.maxOutputLevel = 9000;
801 state->m_qamIfAgcCfg.top = 0x0511;
802 state->m_qamIfAgcCfg.cutOffCurrent = 0;
803 state->m_qamIfAgcCfg.speed = 3;
804 state->m_qamIfAgcCfg.IngainTgtMax = 5119;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300805 state->m_qamIfAgcCfg.FastClipCtrlDelay = 50;
806
Oliver Endrissebc7de22011-07-03 13:49:44 -0300807 state->m_qamPgaCfg = 140;
808 state->m_qamPreSawCfg.reference = 4;
809 state->m_qamPreSawCfg.usePreSaw = false;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300810
811 state->m_OperationMode = OM_NONE;
812 state->m_DrxkState = DRXK_UNINITIALIZED;
813
814 /* MPEG output configuration */
Oliver Endrissebc7de22011-07-03 13:49:44 -0300815 state->m_enableMPEGOutput = true; /* If TRUE; enable MPEG ouput */
816 state->m_insertRSByte = false; /* If TRUE; insert RS byte */
817 state->m_enableParallel = true; /* If TRUE;
818 parallel out otherwise serial */
819 state->m_invertDATA = false; /* If TRUE; invert DATA signals */
820 state->m_invertERR = false; /* If TRUE; invert ERR signal */
821 state->m_invertSTR = false; /* If TRUE; invert STR signals */
822 state->m_invertVAL = false; /* If TRUE; invert VAL signals */
823 state->m_invertCLK = (ulInvertTSClock != 0); /* If TRUE; invert CLK signals */
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300824 state->m_DVBTStaticCLK = (ulDVBTStaticTSClock != 0);
Oliver Endrissebc7de22011-07-03 13:49:44 -0300825 state->m_DVBCStaticCLK = (ulDVBCStaticTSClock != 0);
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300826 /* If TRUE; static MPEG clockrate will be used;
827 otherwise clockrate will adapt to the bitrate of the TS */
828
829 state->m_DVBTBitrate = ulDVBTBitrate;
830 state->m_DVBCBitrate = ulDVBCBitrate;
831
832 state->m_TSDataStrength = (ulTSDataStrength & 0x07);
833 state->m_TSClockkStrength = (ulTSClockkStrength & 0x07);
834
835 /* Maximum bitrate in b/s in case static clockrate is selected */
836 state->m_mpegTsStaticBitrate = 19392658;
837 state->m_disableTEIhandling = false;
838
839 if (ulInsertRSByte)
840 state->m_insertRSByte = true;
841
842 state->m_MpegLockTimeOut = DEFAULT_DRXK_MPEG_LOCK_TIMEOUT;
843 if (ulMpegLockTimeOut < 10000)
844 state->m_MpegLockTimeOut = ulMpegLockTimeOut;
845 state->m_DemodLockTimeOut = DEFAULT_DRXK_DEMOD_LOCK_TIMEOUT;
846 if (ulDemodLockTimeOut < 10000)
847 state->m_DemodLockTimeOut = ulDemodLockTimeOut;
848
Oliver Endrissebc7de22011-07-03 13:49:44 -0300849 /* QAM defaults */
850 state->m_Constellation = DRX_CONSTELLATION_AUTO;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300851 state->m_qamInterleaveMode = DRXK_QAM_I12_J17;
Oliver Endrissebc7de22011-07-03 13:49:44 -0300852 state->m_fecRsPlen = 204 * 8; /* fecRsPlen annex A */
853 state->m_fecRsPrescale = 1;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300854
855 state->m_sqiSpeed = DRXK_DVBT_SQI_SPEED_MEDIUM;
856 state->m_agcFastClipCtrlDelay = 0;
857
858 state->m_GPIOCfg = (ulGPIOCfg);
Oliver Endrissebc7de22011-07-03 13:49:44 -0300859 state->m_GPIO = (ulGPIO == 0 ? 0 : 1);
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300860
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300861 state->m_bPowerDown = false;
862 state->m_currentPowerMode = DRX_POWER_DOWN;
863
864 state->m_enableParallel = (ulSerialMode == 0);
865
866 state->m_rfmirror = (ulRfMirror == 0);
867 state->m_IfAgcPol = false;
868 return 0;
869}
870
871static int DRXX_Open(struct drxk_state *state)
872{
873 int status = 0;
874 u32 jtag = 0;
875 u16 bid = 0;
876 u16 key = 0;
877
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -0300878 dprintk(1, "\n");
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300879 /* stop lock indicator process */
880 status = write16(state, SCU_RAM_GPIO__A, SCU_RAM_GPIO_HW_LOCK_IND_DISABLE);
881 if (status < 0)
882 goto error;
883 /* Check device id */
884 status = read16(state, SIO_TOP_COMM_KEY__A, &key);
885 if (status < 0)
886 goto error;
887 status = write16(state, SIO_TOP_COMM_KEY__A, SIO_TOP_COMM_KEY_KEY);
888 if (status < 0)
889 goto error;
890 status = read32(state, SIO_TOP_JTAGID_LO__A, &jtag);
891 if (status < 0)
892 goto error;
893 status = read16(state, SIO_PDR_UIO_IN_HI__A, &bid);
894 if (status < 0)
895 goto error;
896 status = write16(state, SIO_TOP_COMM_KEY__A, key);
897error:
898 if (status < 0)
899 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300900 return status;
901}
902
903static int GetDeviceCapabilities(struct drxk_state *state)
904{
Oliver Endrissebc7de22011-07-03 13:49:44 -0300905 u16 sioPdrOhwCfg = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300906 u32 sioTopJtagidLo = 0;
907 int status;
Mauro Carvalho Chehab9c6e1822011-07-10 08:38:18 -0300908 const char *spin = "";
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300909
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -0300910 dprintk(1, "\n");
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300911
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300912 /* driver 0.9.0 */
913 /* stop lock indicator process */
914 status = write16(state, SCU_RAM_GPIO__A, SCU_RAM_GPIO_HW_LOCK_IND_DISABLE);
915 if (status < 0)
916 goto error;
917 status = write16(state, SIO_TOP_COMM_KEY__A, 0xFABA);
918 if (status < 0)
919 goto error;
920 status = read16(state, SIO_PDR_OHW_CFG__A, &sioPdrOhwCfg);
921 if (status < 0)
922 goto error;
923 status = write16(state, SIO_TOP_COMM_KEY__A, 0x0000);
924 if (status < 0)
925 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300926
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300927 switch ((sioPdrOhwCfg & SIO_PDR_OHW_CFG_FREF_SEL__M)) {
928 case 0:
929 /* ignore (bypass ?) */
930 break;
931 case 1:
932 /* 27 MHz */
933 state->m_oscClockFreq = 27000;
934 break;
935 case 2:
936 /* 20.25 MHz */
937 state->m_oscClockFreq = 20250;
938 break;
939 case 3:
940 /* 4 MHz */
941 state->m_oscClockFreq = 20250;
942 break;
943 default:
944 printk(KERN_ERR "drxk: Clock Frequency is unkonwn\n");
945 return -EINVAL;
946 }
947 /*
948 Determine device capabilities
949 Based on pinning v14
950 */
951 status = read32(state, SIO_TOP_JTAGID_LO__A, &sioTopJtagidLo);
952 if (status < 0)
953 goto error;
954 /* driver 0.9.0 */
955 switch ((sioTopJtagidLo >> 29) & 0xF) {
956 case 0:
957 state->m_deviceSpin = DRXK_SPIN_A1;
Mauro Carvalho Chehab9c6e1822011-07-10 08:38:18 -0300958 spin = "A1";
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300959 break;
960 case 2:
961 state->m_deviceSpin = DRXK_SPIN_A2;
Mauro Carvalho Chehab9c6e1822011-07-10 08:38:18 -0300962 spin = "A2";
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300963 break;
964 case 3:
965 state->m_deviceSpin = DRXK_SPIN_A3;
Mauro Carvalho Chehab9c6e1822011-07-10 08:38:18 -0300966 spin = "A3";
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300967 break;
968 default:
969 state->m_deviceSpin = DRXK_SPIN_UNKNOWN;
970 status = -EINVAL;
971 printk(KERN_ERR "drxk: Spin unknown\n");
972 goto error2;
973 }
974 switch ((sioTopJtagidLo >> 12) & 0xFF) {
975 case 0x13:
976 /* typeId = DRX3913K_TYPE_ID */
977 state->m_hasLNA = false;
978 state->m_hasOOB = false;
979 state->m_hasATV = false;
980 state->m_hasAudio = false;
981 state->m_hasDVBT = true;
982 state->m_hasDVBC = true;
983 state->m_hasSAWSW = true;
984 state->m_hasGPIO2 = false;
985 state->m_hasGPIO1 = false;
986 state->m_hasIRQN = false;
987 break;
988 case 0x15:
989 /* typeId = DRX3915K_TYPE_ID */
990 state->m_hasLNA = false;
991 state->m_hasOOB = false;
992 state->m_hasATV = true;
993 state->m_hasAudio = false;
994 state->m_hasDVBT = true;
995 state->m_hasDVBC = false;
996 state->m_hasSAWSW = true;
997 state->m_hasGPIO2 = true;
998 state->m_hasGPIO1 = true;
999 state->m_hasIRQN = false;
1000 break;
1001 case 0x16:
1002 /* typeId = DRX3916K_TYPE_ID */
1003 state->m_hasLNA = false;
1004 state->m_hasOOB = false;
1005 state->m_hasATV = true;
1006 state->m_hasAudio = false;
1007 state->m_hasDVBT = true;
1008 state->m_hasDVBC = false;
1009 state->m_hasSAWSW = true;
1010 state->m_hasGPIO2 = true;
1011 state->m_hasGPIO1 = true;
1012 state->m_hasIRQN = false;
1013 break;
1014 case 0x18:
1015 /* typeId = DRX3918K_TYPE_ID */
1016 state->m_hasLNA = false;
1017 state->m_hasOOB = false;
1018 state->m_hasATV = true;
1019 state->m_hasAudio = true;
1020 state->m_hasDVBT = true;
1021 state->m_hasDVBC = false;
1022 state->m_hasSAWSW = true;
1023 state->m_hasGPIO2 = true;
1024 state->m_hasGPIO1 = true;
1025 state->m_hasIRQN = false;
1026 break;
1027 case 0x21:
1028 /* typeId = DRX3921K_TYPE_ID */
1029 state->m_hasLNA = false;
1030 state->m_hasOOB = false;
1031 state->m_hasATV = true;
1032 state->m_hasAudio = true;
1033 state->m_hasDVBT = true;
1034 state->m_hasDVBC = true;
1035 state->m_hasSAWSW = true;
1036 state->m_hasGPIO2 = true;
1037 state->m_hasGPIO1 = true;
1038 state->m_hasIRQN = false;
1039 break;
1040 case 0x23:
1041 /* typeId = DRX3923K_TYPE_ID */
1042 state->m_hasLNA = false;
1043 state->m_hasOOB = false;
1044 state->m_hasATV = true;
1045 state->m_hasAudio = true;
1046 state->m_hasDVBT = true;
1047 state->m_hasDVBC = true;
1048 state->m_hasSAWSW = true;
1049 state->m_hasGPIO2 = true;
1050 state->m_hasGPIO1 = true;
1051 state->m_hasIRQN = false;
1052 break;
1053 case 0x25:
1054 /* typeId = DRX3925K_TYPE_ID */
1055 state->m_hasLNA = false;
1056 state->m_hasOOB = false;
1057 state->m_hasATV = true;
1058 state->m_hasAudio = true;
1059 state->m_hasDVBT = true;
1060 state->m_hasDVBC = true;
1061 state->m_hasSAWSW = true;
1062 state->m_hasGPIO2 = true;
1063 state->m_hasGPIO1 = true;
1064 state->m_hasIRQN = false;
1065 break;
1066 case 0x26:
1067 /* typeId = DRX3926K_TYPE_ID */
1068 state->m_hasLNA = false;
1069 state->m_hasOOB = false;
1070 state->m_hasATV = true;
1071 state->m_hasAudio = false;
1072 state->m_hasDVBT = true;
1073 state->m_hasDVBC = true;
1074 state->m_hasSAWSW = true;
1075 state->m_hasGPIO2 = true;
1076 state->m_hasGPIO1 = true;
1077 state->m_hasIRQN = false;
1078 break;
1079 default:
1080 printk(KERN_ERR "drxk: DeviceID not supported = %02x\n",
1081 ((sioTopJtagidLo >> 12) & 0xFF));
1082 status = -EINVAL;
1083 goto error2;
1084 }
1085
Mauro Carvalho Chehab9c6e1822011-07-10 08:38:18 -03001086 printk(KERN_INFO
1087 "drxk: detected a drx-39%02xk, spin %s, xtal %d.%03d MHz\n",
1088 ((sioTopJtagidLo >> 12) & 0xFF), spin,
1089 state->m_oscClockFreq / 1000,
1090 state->m_oscClockFreq % 1000);
1091
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001092error:
1093 if (status < 0)
1094 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
1095
1096error2:
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001097 return status;
1098}
1099
1100static int HI_Command(struct drxk_state *state, u16 cmd, u16 *pResult)
1101{
1102 int status;
1103 bool powerdown_cmd;
1104
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03001105 dprintk(1, "\n");
1106
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001107 /* Write command */
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -03001108 status = write16(state, SIO_HI_RA_RAM_CMD__A, cmd);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001109 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001110 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001111 if (cmd == SIO_HI_RA_RAM_CMD_RESET)
1112 msleep(1);
1113
1114 powerdown_cmd =
Oliver Endrissebc7de22011-07-03 13:49:44 -03001115 (bool) ((cmd == SIO_HI_RA_RAM_CMD_CONFIG) &&
1116 ((state->m_HICfgCtrl) &
1117 SIO_HI_RA_RAM_PAR_5_CFG_SLEEP__M) ==
1118 SIO_HI_RA_RAM_PAR_5_CFG_SLEEP_ZZZ);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001119 if (powerdown_cmd == false) {
1120 /* Wait until command rdy */
1121 u32 retryCount = 0;
1122 u16 waitCmd;
1123
1124 do {
1125 msleep(1);
1126 retryCount += 1;
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -03001127 status = read16(state, SIO_HI_RA_RAM_CMD__A,
1128 &waitCmd);
Oliver Endrissebc7de22011-07-03 13:49:44 -03001129 } while ((status < 0) && (retryCount < DRXK_MAX_RETRIES)
1130 && (waitCmd != 0));
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001131 if (status < 0)
1132 goto error;
1133 status = read16(state, SIO_HI_RA_RAM_RES__A, pResult);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001134 }
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001135error:
1136 if (status < 0)
1137 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
1138
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001139 return status;
1140}
1141
1142static int HI_CfgCommand(struct drxk_state *state)
1143{
1144 int status;
1145
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03001146 dprintk(1, "\n");
1147
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001148 mutex_lock(&state->mutex);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001149
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001150 status = write16(state, SIO_HI_RA_RAM_PAR_6__A, state->m_HICfgTimeout);
1151 if (status < 0)
1152 goto error;
1153 status = write16(state, SIO_HI_RA_RAM_PAR_5__A, state->m_HICfgCtrl);
1154 if (status < 0)
1155 goto error;
1156 status = write16(state, SIO_HI_RA_RAM_PAR_4__A, state->m_HICfgWakeUpKey);
1157 if (status < 0)
1158 goto error;
1159 status = write16(state, SIO_HI_RA_RAM_PAR_3__A, state->m_HICfgBridgeDelay);
1160 if (status < 0)
1161 goto error;
1162 status = write16(state, SIO_HI_RA_RAM_PAR_2__A, state->m_HICfgTimingDiv);
1163 if (status < 0)
1164 goto error;
1165 status = write16(state, SIO_HI_RA_RAM_PAR_1__A, SIO_HI_RA_RAM_PAR_1_PAR1_SEC_KEY);
1166 if (status < 0)
1167 goto error;
1168 status = HI_Command(state, SIO_HI_RA_RAM_CMD_CONFIG, 0);
1169 if (status < 0)
1170 goto error;
1171
1172 state->m_HICfgCtrl &= ~SIO_HI_RA_RAM_PAR_5_CFG_SLEEP_ZZZ;
1173error:
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001174 mutex_unlock(&state->mutex);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001175 if (status < 0)
1176 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001177 return status;
1178}
1179
1180static int InitHI(struct drxk_state *state)
1181{
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03001182 dprintk(1, "\n");
1183
Oliver Endrissebc7de22011-07-03 13:49:44 -03001184 state->m_HICfgWakeUpKey = (state->demod_address << 1);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001185 state->m_HICfgTimeout = 0x96FF;
1186 /* port/bridge/power down ctrl */
1187 state->m_HICfgCtrl = SIO_HI_RA_RAM_PAR_5_CFG_SLV0_SLAVE;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001188
Oliver Endrissebc7de22011-07-03 13:49:44 -03001189 return HI_CfgCommand(state);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001190}
1191
1192static int MPEGTSConfigurePins(struct drxk_state *state, bool mpegEnable)
1193{
1194 int status = -1;
Oliver Endrissebc7de22011-07-03 13:49:44 -03001195 u16 sioPdrMclkCfg = 0;
1196 u16 sioPdrMdxCfg = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001197
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03001198 dprintk(1, "\n");
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001199
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001200 /* stop lock indicator process */
1201 status = write16(state, SCU_RAM_GPIO__A, SCU_RAM_GPIO_HW_LOCK_IND_DISABLE);
1202 if (status < 0)
1203 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001204
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001205 /* MPEG TS pad configuration */
1206 status = write16(state, SIO_TOP_COMM_KEY__A, 0xFABA);
1207 if (status < 0)
1208 goto error;
1209
1210 if (mpegEnable == false) {
1211 /* Set MPEG TS pads to inputmode */
1212 status = write16(state, SIO_PDR_MSTRT_CFG__A, 0x0000);
1213 if (status < 0)
1214 goto error;
1215 status = write16(state, SIO_PDR_MERR_CFG__A, 0x0000);
1216 if (status < 0)
1217 goto error;
1218 status = write16(state, SIO_PDR_MCLK_CFG__A, 0x0000);
1219 if (status < 0)
1220 goto error;
1221 status = write16(state, SIO_PDR_MVAL_CFG__A, 0x0000);
1222 if (status < 0)
1223 goto error;
1224 status = write16(state, SIO_PDR_MD0_CFG__A, 0x0000);
1225 if (status < 0)
1226 goto error;
1227 status = write16(state, SIO_PDR_MD1_CFG__A, 0x0000);
1228 if (status < 0)
1229 goto error;
1230 status = write16(state, SIO_PDR_MD2_CFG__A, 0x0000);
1231 if (status < 0)
1232 goto error;
1233 status = write16(state, SIO_PDR_MD3_CFG__A, 0x0000);
1234 if (status < 0)
1235 goto error;
1236 status = write16(state, SIO_PDR_MD4_CFG__A, 0x0000);
1237 if (status < 0)
1238 goto error;
1239 status = write16(state, SIO_PDR_MD5_CFG__A, 0x0000);
1240 if (status < 0)
1241 goto error;
1242 status = write16(state, SIO_PDR_MD6_CFG__A, 0x0000);
1243 if (status < 0)
1244 goto error;
1245 status = write16(state, SIO_PDR_MD7_CFG__A, 0x0000);
1246 if (status < 0)
1247 goto error;
1248 } else {
1249 /* Enable MPEG output */
1250 sioPdrMdxCfg =
1251 ((state->m_TSDataStrength <<
1252 SIO_PDR_MD0_CFG_DRIVE__B) | 0x0003);
1253 sioPdrMclkCfg = ((state->m_TSClockkStrength <<
1254 SIO_PDR_MCLK_CFG_DRIVE__B) |
1255 0x0003);
1256
1257 status = write16(state, SIO_PDR_MSTRT_CFG__A, sioPdrMdxCfg);
1258 if (status < 0)
1259 goto error;
1260 status = write16(state, SIO_PDR_MERR_CFG__A, 0x0000); /* Disable */
1261 if (status < 0)
1262 goto error;
1263 status = write16(state, SIO_PDR_MVAL_CFG__A, 0x0000); /* Disable */
1264 if (status < 0)
1265 goto error;
1266 if (state->m_enableParallel == true) {
1267 /* paralel -> enable MD1 to MD7 */
1268 status = write16(state, SIO_PDR_MD1_CFG__A, sioPdrMdxCfg);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03001269 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001270 goto error;
1271 status = write16(state, SIO_PDR_MD2_CFG__A, sioPdrMdxCfg);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03001272 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001273 goto error;
1274 status = write16(state, SIO_PDR_MD3_CFG__A, sioPdrMdxCfg);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03001275 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001276 goto error;
1277 status = write16(state, SIO_PDR_MD4_CFG__A, sioPdrMdxCfg);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03001278 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001279 goto error;
1280 status = write16(state, SIO_PDR_MD5_CFG__A, sioPdrMdxCfg);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03001281 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001282 goto error;
1283 status = write16(state, SIO_PDR_MD6_CFG__A, sioPdrMdxCfg);
1284 if (status < 0)
1285 goto error;
1286 status = write16(state, SIO_PDR_MD7_CFG__A, sioPdrMdxCfg);
1287 if (status < 0)
1288 goto error;
1289 } else {
1290 sioPdrMdxCfg = ((state->m_TSDataStrength <<
1291 SIO_PDR_MD0_CFG_DRIVE__B)
1292 | 0x0003);
1293 /* serial -> disable MD1 to MD7 */
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -03001294 status = write16(state, SIO_PDR_MD1_CFG__A, 0x0000);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03001295 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001296 goto error;
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -03001297 status = write16(state, SIO_PDR_MD2_CFG__A, 0x0000);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03001298 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001299 goto error;
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -03001300 status = write16(state, SIO_PDR_MD3_CFG__A, 0x0000);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03001301 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001302 goto error;
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -03001303 status = write16(state, SIO_PDR_MD4_CFG__A, 0x0000);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03001304 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001305 goto error;
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -03001306 status = write16(state, SIO_PDR_MD5_CFG__A, 0x0000);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03001307 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001308 goto error;
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -03001309 status = write16(state, SIO_PDR_MD6_CFG__A, 0x0000);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03001310 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001311 goto error;
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -03001312 status = write16(state, SIO_PDR_MD7_CFG__A, 0x0000);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03001313 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001314 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001315 }
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001316 status = write16(state, SIO_PDR_MCLK_CFG__A, sioPdrMclkCfg);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03001317 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001318 goto error;
1319 status = write16(state, SIO_PDR_MD0_CFG__A, sioPdrMdxCfg);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03001320 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001321 goto error;
1322 }
1323 /* Enable MB output over MPEG pads and ctl input */
1324 status = write16(state, SIO_PDR_MON_CFG__A, 0x0000);
1325 if (status < 0)
1326 goto error;
1327 /* Write nomagic word to enable pdr reg write */
1328 status = write16(state, SIO_TOP_COMM_KEY__A, 0x0000);
1329error:
1330 if (status < 0)
1331 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001332 return status;
1333}
1334
1335static int MPEGTSDisable(struct drxk_state *state)
1336{
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03001337 dprintk(1, "\n");
1338
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001339 return MPEGTSConfigurePins(state, false);
1340}
1341
1342static int BLChainCmd(struct drxk_state *state,
1343 u16 romOffset, u16 nrOfElements, u32 timeOut)
1344{
1345 u16 blStatus = 0;
1346 int status;
1347 unsigned long end;
1348
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03001349 dprintk(1, "\n");
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001350 mutex_lock(&state->mutex);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001351 status = write16(state, SIO_BL_MODE__A, SIO_BL_MODE_CHAIN);
1352 if (status < 0)
1353 goto error;
1354 status = write16(state, SIO_BL_CHAIN_ADDR__A, romOffset);
1355 if (status < 0)
1356 goto error;
1357 status = write16(state, SIO_BL_CHAIN_LEN__A, nrOfElements);
1358 if (status < 0)
1359 goto error;
1360 status = write16(state, SIO_BL_ENABLE__A, SIO_BL_ENABLE_ON);
1361 if (status < 0)
1362 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001363
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001364 end = jiffies + msecs_to_jiffies(timeOut);
1365 do {
1366 msleep(1);
1367 status = read16(state, SIO_BL_STATUS__A, &blStatus);
1368 if (status < 0)
1369 goto error;
1370 } while ((blStatus == 0x1) &&
1371 ((time_is_after_jiffies(end))));
1372
1373 if (blStatus == 0x1) {
1374 printk(KERN_ERR "drxk: SIO not ready\n");
1375 status = -EINVAL;
1376 goto error2;
1377 }
1378error:
1379 if (status < 0)
1380 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
1381error2:
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001382 mutex_unlock(&state->mutex);
1383 return status;
1384}
1385
1386
1387static int DownloadMicrocode(struct drxk_state *state,
Oliver Endrissebc7de22011-07-03 13:49:44 -03001388 const u8 pMCImage[], u32 Length)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001389{
1390 const u8 *pSrc = pMCImage;
1391 u16 Flags;
1392 u16 Drain;
1393 u32 Address;
1394 u16 nBlocks;
1395 u16 BlockSize;
1396 u16 BlockCRC;
1397 u32 offset = 0;
1398 u32 i;
Mauro Carvalho Chehab1bd09dd2011-07-03 18:21:59 -03001399 int status = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001400
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03001401 dprintk(1, "\n");
1402
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001403 /* down the drain (we don care about MAGIC_WORD) */
1404 Drain = (pSrc[0] << 8) | pSrc[1];
Oliver Endrissebc7de22011-07-03 13:49:44 -03001405 pSrc += sizeof(u16);
1406 offset += sizeof(u16);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001407 nBlocks = (pSrc[0] << 8) | pSrc[1];
Oliver Endrissebc7de22011-07-03 13:49:44 -03001408 pSrc += sizeof(u16);
1409 offset += sizeof(u16);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001410
1411 for (i = 0; i < nBlocks; i += 1) {
1412 Address = (pSrc[0] << 24) | (pSrc[1] << 16) |
Oliver Endrissebc7de22011-07-03 13:49:44 -03001413 (pSrc[2] << 8) | pSrc[3];
1414 pSrc += sizeof(u32);
1415 offset += sizeof(u32);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001416
1417 BlockSize = ((pSrc[0] << 8) | pSrc[1]) * sizeof(u16);
Oliver Endrissebc7de22011-07-03 13:49:44 -03001418 pSrc += sizeof(u16);
1419 offset += sizeof(u16);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001420
1421 Flags = (pSrc[0] << 8) | pSrc[1];
Oliver Endrissebc7de22011-07-03 13:49:44 -03001422 pSrc += sizeof(u16);
1423 offset += sizeof(u16);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001424
1425 BlockCRC = (pSrc[0] << 8) | pSrc[1];
Oliver Endrissebc7de22011-07-03 13:49:44 -03001426 pSrc += sizeof(u16);
1427 offset += sizeof(u16);
Mauro Carvalho Chehabbcd2ebb2011-07-09 18:57:54 -03001428
1429 if (offset + BlockSize > Length) {
1430 printk(KERN_ERR "drxk: Firmware is corrupted.\n");
1431 return -EINVAL;
1432 }
1433
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -03001434 status = write_block(state, Address, BlockSize, pSrc);
Mauro Carvalho Chehab39624f72011-07-09 19:23:44 -03001435 if (status < 0) {
1436 printk(KERN_ERR "drxk: Error %d while loading firmware\n", status);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001437 break;
Mauro Carvalho Chehab39624f72011-07-09 19:23:44 -03001438 }
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001439 pSrc += BlockSize;
1440 offset += BlockSize;
1441 }
1442 return status;
1443}
1444
1445static int DVBTEnableOFDMTokenRing(struct drxk_state *state, bool enable)
1446{
1447 int status;
Oliver Endrissebc7de22011-07-03 13:49:44 -03001448 u16 data = 0;
1449 u16 desiredCtrl = SIO_OFDM_SH_OFDM_RING_ENABLE_ON;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001450 u16 desiredStatus = SIO_OFDM_SH_OFDM_RING_STATUS_ENABLED;
1451 unsigned long end;
1452
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03001453 dprintk(1, "\n");
1454
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001455 if (enable == false) {
Oliver Endrissebc7de22011-07-03 13:49:44 -03001456 desiredCtrl = SIO_OFDM_SH_OFDM_RING_ENABLE_OFF;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001457 desiredStatus = SIO_OFDM_SH_OFDM_RING_STATUS_DOWN;
1458 }
1459
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001460 status = read16(state, SIO_OFDM_SH_OFDM_RING_STATUS__A, &data);
1461 if (status >= 0 && data == desiredStatus) {
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001462 /* tokenring already has correct status */
1463 return status;
1464 }
1465 /* Disable/enable dvbt tokenring bridge */
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001466 status = write16(state, SIO_OFDM_SH_OFDM_RING_ENABLE__A, desiredCtrl);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001467
Oliver Endrissebc7de22011-07-03 13:49:44 -03001468 end = jiffies + msecs_to_jiffies(DRXK_OFDM_TR_SHUTDOWN_TIMEOUT);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03001469 do {
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -03001470 status = read16(state, SIO_OFDM_SH_OFDM_RING_STATUS__A, &data);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001471 if ((status >= 0 && data == desiredStatus) || time_is_after_jiffies(end))
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03001472 break;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001473 msleep(1);
1474 } while (1);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001475 if (data != desiredStatus) {
Mauro Carvalho Chehabe0e6eca2011-07-04 08:27:47 -03001476 printk(KERN_ERR "drxk: SIO not ready\n");
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001477 return -EINVAL;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001478 }
1479 return status;
1480}
1481
1482static int MPEGTSStop(struct drxk_state *state)
1483{
1484 int status = 0;
1485 u16 fecOcSncMode = 0;
1486 u16 fecOcIprMode = 0;
1487
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03001488 dprintk(1, "\n");
1489
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001490 /* Gracefull shutdown (byte boundaries) */
1491 status = read16(state, FEC_OC_SNC_MODE__A, &fecOcSncMode);
1492 if (status < 0)
1493 goto error;
1494 fecOcSncMode |= FEC_OC_SNC_MODE_SHUTDOWN__M;
1495 status = write16(state, FEC_OC_SNC_MODE__A, fecOcSncMode);
1496 if (status < 0)
1497 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001498
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001499 /* Suppress MCLK during absence of data */
1500 status = read16(state, FEC_OC_IPR_MODE__A, &fecOcIprMode);
1501 if (status < 0)
1502 goto error;
1503 fecOcIprMode |= FEC_OC_IPR_MODE_MCLK_DIS_DAT_ABS__M;
1504 status = write16(state, FEC_OC_IPR_MODE__A, fecOcIprMode);
1505
1506error:
1507 if (status < 0)
1508 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
1509
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001510 return status;
1511}
1512
1513static int scu_command(struct drxk_state *state,
1514 u16 cmd, u8 parameterLen,
Oliver Endrissebc7de22011-07-03 13:49:44 -03001515 u16 *parameter, u8 resultLen, u16 *result)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001516{
1517#if (SCU_RAM_PARAM_0__A - SCU_RAM_PARAM_15__A) != 15
1518#error DRXK register mapping no longer compatible with this routine!
1519#endif
1520 u16 curCmd = 0;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001521 int status = -EINVAL;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001522 unsigned long end;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001523 u8 buffer[34];
1524 int cnt = 0, ii;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001525
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03001526 dprintk(1, "\n");
1527
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001528 if ((cmd == 0) || ((parameterLen > 0) && (parameter == NULL)) ||
1529 ((resultLen > 0) && (result == NULL)))
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001530 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001531
1532 mutex_lock(&state->mutex);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001533
1534 /* assume that the command register is ready
1535 since it is checked afterwards */
1536 for (ii = parameterLen - 1; ii >= 0; ii -= 1) {
1537 buffer[cnt++] = (parameter[ii] & 0xFF);
1538 buffer[cnt++] = ((parameter[ii] >> 8) & 0xFF);
1539 }
1540 buffer[cnt++] = (cmd & 0xFF);
1541 buffer[cnt++] = ((cmd >> 8) & 0xFF);
1542
1543 write_block(state, SCU_RAM_PARAM_0__A -
1544 (parameterLen - 1), cnt, buffer);
1545 /* Wait until SCU has processed command */
1546 end = jiffies + msecs_to_jiffies(DRXK_MAX_WAITTIME);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001547 do {
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001548 msleep(1);
1549 status = read16(state, SCU_RAM_COMMAND__A, &curCmd);
1550 if (status < 0)
1551 goto error;
1552 } while (!(curCmd == DRX_SCU_READY) && (time_is_after_jiffies(end)));
1553 if (curCmd != DRX_SCU_READY) {
1554 printk(KERN_ERR "drxk: SCU not ready\n");
1555 status = -EIO;
1556 goto error2;
1557 }
1558 /* read results */
1559 if ((resultLen > 0) && (result != NULL)) {
1560 s16 err;
1561 int ii;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001562
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001563 for (ii = resultLen - 1; ii >= 0; ii -= 1) {
1564 status = read16(state, SCU_RAM_PARAM_0__A - ii, &result[ii]);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03001565 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001566 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001567 }
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001568
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001569 /* Check if an error was reported by SCU */
1570 err = (s16)result[0];
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001571
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001572 /* check a few fixed error codes */
1573 if (err == SCU_RESULT_UNKSTD) {
1574 printk(KERN_ERR "drxk: SCU_RESULT_UNKSTD\n");
1575 status = -EINVAL;
1576 goto error2;
1577 } else if (err == SCU_RESULT_UNKCMD) {
1578 printk(KERN_ERR "drxk: SCU_RESULT_UNKCMD\n");
1579 status = -EINVAL;
1580 goto error2;
1581 } else if (err < 0) {
1582 /*
1583 * here it is assumed that a nagative result means
1584 * error, and positive no error
1585 */
1586 printk(KERN_ERR "drxk: %s ERROR: %d\n", __func__, err);
1587 status = -EINVAL;
1588 goto error2;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001589 }
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001590 }
1591
1592error:
Oliver Endrissebc7de22011-07-03 13:49:44 -03001593 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001594 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001595
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001596error2:
1597 mutex_unlock(&state->mutex);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001598 return status;
1599}
1600
1601static int SetIqmAf(struct drxk_state *state, bool active)
1602{
1603 u16 data = 0;
1604 int status;
1605
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03001606 dprintk(1, "\n");
1607
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001608 /* Configure IQM */
1609 status = read16(state, IQM_AF_STDBY__A, &data);
1610 if (status < 0)
1611 goto error;
Oliver Endrissebc7de22011-07-03 13:49:44 -03001612
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001613 if (!active) {
1614 data |= (IQM_AF_STDBY_STDBY_ADC_STANDBY
1615 | IQM_AF_STDBY_STDBY_AMP_STANDBY
1616 | IQM_AF_STDBY_STDBY_PD_STANDBY
1617 | IQM_AF_STDBY_STDBY_TAGC_IF_STANDBY
1618 | IQM_AF_STDBY_STDBY_TAGC_RF_STANDBY);
1619 } else {
1620 data &= ((~IQM_AF_STDBY_STDBY_ADC_STANDBY)
1621 & (~IQM_AF_STDBY_STDBY_AMP_STANDBY)
1622 & (~IQM_AF_STDBY_STDBY_PD_STANDBY)
1623 & (~IQM_AF_STDBY_STDBY_TAGC_IF_STANDBY)
1624 & (~IQM_AF_STDBY_STDBY_TAGC_RF_STANDBY)
1625 );
1626 }
1627 status = write16(state, IQM_AF_STDBY__A, data);
1628
1629error:
1630 if (status < 0)
1631 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001632 return status;
1633}
1634
Oliver Endrissebc7de22011-07-03 13:49:44 -03001635static int CtrlPowerMode(struct drxk_state *state, enum DRXPowerMode *mode)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001636{
1637 int status = 0;
Oliver Endrissebc7de22011-07-03 13:49:44 -03001638 u16 sioCcPwdMode = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001639
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03001640 dprintk(1, "\n");
1641
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001642 /* Check arguments */
1643 if (mode == NULL)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001644 return -EINVAL;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001645
1646 switch (*mode) {
1647 case DRX_POWER_UP:
1648 sioCcPwdMode = SIO_CC_PWD_MODE_LEVEL_NONE;
1649 break;
1650 case DRXK_POWER_DOWN_OFDM:
1651 sioCcPwdMode = SIO_CC_PWD_MODE_LEVEL_OFDM;
1652 break;
1653 case DRXK_POWER_DOWN_CORE:
1654 sioCcPwdMode = SIO_CC_PWD_MODE_LEVEL_CLOCK;
1655 break;
1656 case DRXK_POWER_DOWN_PLL:
1657 sioCcPwdMode = SIO_CC_PWD_MODE_LEVEL_PLL;
1658 break;
1659 case DRX_POWER_DOWN:
1660 sioCcPwdMode = SIO_CC_PWD_MODE_LEVEL_OSC;
1661 break;
1662 default:
1663 /* Unknow sleep mode */
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001664 return -EINVAL;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001665 break;
1666 }
1667
1668 /* If already in requested power mode, do nothing */
1669 if (state->m_currentPowerMode == *mode)
1670 return 0;
1671
1672 /* For next steps make sure to start from DRX_POWER_UP mode */
Oliver Endrissebc7de22011-07-03 13:49:44 -03001673 if (state->m_currentPowerMode != DRX_POWER_UP) {
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001674 status = PowerUpDevice(state);
1675 if (status < 0)
1676 goto error;
1677 status = DVBTEnableOFDMTokenRing(state, true);
1678 if (status < 0)
1679 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001680 }
1681
1682 if (*mode == DRX_POWER_UP) {
1683 /* Restore analog & pin configuartion */
1684 } else {
1685 /* Power down to requested mode */
1686 /* Backup some register settings */
1687 /* Set pins with possible pull-ups connected
1688 to them in input mode */
1689 /* Analog power down */
1690 /* ADC power down */
1691 /* Power down device */
1692 /* stop all comm_exec */
1693 /* Stop and power down previous standard */
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001694 switch (state->m_OperationMode) {
1695 case OM_DVBT:
1696 status = MPEGTSStop(state);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03001697 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001698 goto error;
1699 status = PowerDownDVBT(state, false);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03001700 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001701 goto error;
1702 break;
1703 case OM_QAM_ITU_A:
1704 case OM_QAM_ITU_C:
1705 status = MPEGTSStop(state);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03001706 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001707 goto error;
1708 status = PowerDownQAM(state);
1709 if (status < 0)
1710 goto error;
1711 break;
1712 default:
1713 break;
1714 }
1715 status = DVBTEnableOFDMTokenRing(state, false);
1716 if (status < 0)
1717 goto error;
1718 status = write16(state, SIO_CC_PWD_MODE__A, sioCcPwdMode);
1719 if (status < 0)
1720 goto error;
1721 status = write16(state, SIO_CC_UPDATE__A, SIO_CC_UPDATE_KEY);
1722 if (status < 0)
1723 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001724
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001725 if (*mode != DRXK_POWER_DOWN_OFDM) {
1726 state->m_HICfgCtrl |=
1727 SIO_HI_RA_RAM_PAR_5_CFG_SLEEP_ZZZ;
1728 status = HI_CfgCommand(state);
1729 if (status < 0)
1730 goto error;
1731 }
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001732 }
1733 state->m_currentPowerMode = *mode;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001734
1735error:
1736 if (status < 0)
1737 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
1738
Oliver Endrissebc7de22011-07-03 13:49:44 -03001739 return status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001740}
1741
1742static int PowerDownDVBT(struct drxk_state *state, bool setPowerMode)
1743{
Oliver Endrissebc7de22011-07-03 13:49:44 -03001744 enum DRXPowerMode powerMode = DRXK_POWER_DOWN_OFDM;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001745 u16 cmdResult = 0;
1746 u16 data = 0;
1747 int status;
1748
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03001749 dprintk(1, "\n");
1750
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001751 status = read16(state, SCU_COMM_EXEC__A, &data);
1752 if (status < 0)
1753 goto error;
1754 if (data == SCU_COMM_EXEC_ACTIVE) {
1755 /* Send OFDM stop command */
1756 status = scu_command(state, SCU_RAM_COMMAND_STANDARD_OFDM | SCU_RAM_COMMAND_CMD_DEMOD_STOP, 0, NULL, 1, &cmdResult);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03001757 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001758 goto error;
1759 /* Send OFDM reset command */
1760 status = scu_command(state, SCU_RAM_COMMAND_STANDARD_OFDM | SCU_RAM_COMMAND_CMD_DEMOD_RESET, 0, NULL, 1, &cmdResult);
1761 if (status < 0)
1762 goto error;
1763 }
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001764
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001765 /* Reset datapath for OFDM, processors first */
1766 status = write16(state, OFDM_SC_COMM_EXEC__A, OFDM_SC_COMM_EXEC_STOP);
1767 if (status < 0)
1768 goto error;
1769 status = write16(state, OFDM_LC_COMM_EXEC__A, OFDM_LC_COMM_EXEC_STOP);
1770 if (status < 0)
1771 goto error;
1772 status = write16(state, IQM_COMM_EXEC__A, IQM_COMM_EXEC_B_STOP);
1773 if (status < 0)
1774 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001775
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001776 /* powerdown AFE */
1777 status = SetIqmAf(state, false);
1778 if (status < 0)
1779 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001780
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001781 /* powerdown to OFDM mode */
1782 if (setPowerMode) {
1783 status = CtrlPowerMode(state, &powerMode);
1784 if (status < 0)
1785 goto error;
1786 }
1787error:
1788 if (status < 0)
1789 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001790 return status;
1791}
1792
Oliver Endrissebc7de22011-07-03 13:49:44 -03001793static int SetOperationMode(struct drxk_state *state,
1794 enum OperationMode oMode)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001795{
1796 int status = 0;
1797
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03001798 dprintk(1, "\n");
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001799 /*
Oliver Endrissebc7de22011-07-03 13:49:44 -03001800 Stop and power down previous standard
1801 TODO investigate total power down instead of partial
1802 power down depending on "previous" standard.
1803 */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001804
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001805 /* disable HW lock indicator */
1806 status = write16(state, SCU_RAM_GPIO__A, SCU_RAM_GPIO_HW_LOCK_IND_DISABLE);
1807 if (status < 0)
1808 goto error;
1809
1810 if (state->m_OperationMode != oMode) {
1811 switch (state->m_OperationMode) {
1812 /* OM_NONE was added for start up */
1813 case OM_NONE:
1814 break;
1815 case OM_DVBT:
1816 status = MPEGTSStop(state);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03001817 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001818 goto error;
1819 status = PowerDownDVBT(state, true);
1820 if (status < 0)
1821 goto error;
1822 state->m_OperationMode = OM_NONE;
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03001823 break;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001824 case OM_QAM_ITU_A: /* fallthrough */
1825 case OM_QAM_ITU_C:
1826 status = MPEGTSStop(state);
1827 if (status < 0)
1828 goto error;
1829 status = PowerDownQAM(state);
1830 if (status < 0)
1831 goto error;
1832 state->m_OperationMode = OM_NONE;
1833 break;
1834 case OM_QAM_ITU_B:
1835 default:
1836 status = -EINVAL;
1837 goto error;
1838 }
1839
1840 /*
1841 Power up new standard
1842 */
1843 switch (oMode) {
1844 case OM_DVBT:
Mauro Carvalho Chehabd6a05402011-07-10 13:07:36 -03001845 state->m_OperationMode = oMode;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001846 status = SetDVBTStandard(state, oMode);
1847 if (status < 0)
1848 goto error;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001849 break;
1850 case OM_QAM_ITU_A: /* fallthrough */
1851 case OM_QAM_ITU_C:
Mauro Carvalho Chehabd6a05402011-07-10 13:07:36 -03001852 state->m_OperationMode = oMode;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001853 status = SetQAMStandard(state, oMode);
1854 if (status < 0)
1855 goto error;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001856 break;
1857 case OM_QAM_ITU_B:
1858 default:
1859 status = -EINVAL;
1860 }
1861 }
1862error:
1863 if (status < 0)
1864 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
1865 return status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001866}
1867
1868static int Start(struct drxk_state *state, s32 offsetFreq,
1869 s32 IntermediateFrequency)
1870{
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001871 int status = -EINVAL;
1872
1873 u16 IFreqkHz;
1874 s32 OffsetkHz = offsetFreq / 1000;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001875
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03001876 dprintk(1, "\n");
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001877 if (state->m_DrxkState != DRXK_STOPPED &&
1878 state->m_DrxkState != DRXK_DTV_STARTED)
1879 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001880
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001881 state->m_bMirrorFreqSpect = (state->param.inversion == INVERSION_ON);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001882
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001883 if (IntermediateFrequency < 0) {
1884 state->m_bMirrorFreqSpect = !state->m_bMirrorFreqSpect;
1885 IntermediateFrequency = -IntermediateFrequency;
1886 }
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001887
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001888 switch (state->m_OperationMode) {
1889 case OM_QAM_ITU_A:
1890 case OM_QAM_ITU_C:
1891 IFreqkHz = (IntermediateFrequency / 1000);
1892 status = SetQAM(state, IFreqkHz, OffsetkHz);
1893 if (status < 0)
1894 goto error;
1895 state->m_DrxkState = DRXK_DTV_STARTED;
1896 break;
1897 case OM_DVBT:
1898 IFreqkHz = (IntermediateFrequency / 1000);
1899 status = MPEGTSStop(state);
1900 if (status < 0)
1901 goto error;
1902 status = SetDVBT(state, IFreqkHz, OffsetkHz);
1903 if (status < 0)
1904 goto error;
1905 status = DVBTStart(state);
1906 if (status < 0)
1907 goto error;
1908 state->m_DrxkState = DRXK_DTV_STARTED;
1909 break;
1910 default:
1911 break;
1912 }
1913error:
1914 if (status < 0)
1915 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001916 return status;
1917}
1918
1919static int ShutDown(struct drxk_state *state)
1920{
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03001921 dprintk(1, "\n");
1922
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001923 MPEGTSStop(state);
1924 return 0;
1925}
1926
Oliver Endrissebc7de22011-07-03 13:49:44 -03001927static int GetLockStatus(struct drxk_state *state, u32 *pLockStatus,
1928 u32 Time)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001929{
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001930 int status = -EINVAL;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001931
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03001932 dprintk(1, "\n");
1933
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001934 if (pLockStatus == NULL)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001935 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001936
1937 *pLockStatus = NOT_LOCKED;
1938
1939 /* define the SCU command code */
1940 switch (state->m_OperationMode) {
1941 case OM_QAM_ITU_A:
1942 case OM_QAM_ITU_B:
1943 case OM_QAM_ITU_C:
1944 status = GetQAMLockStatus(state, pLockStatus);
1945 break;
1946 case OM_DVBT:
1947 status = GetDVBTLockStatus(state, pLockStatus);
1948 break;
1949 default:
1950 break;
1951 }
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001952error:
1953 if (status < 0)
1954 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001955 return status;
1956}
1957
1958static int MPEGTSStart(struct drxk_state *state)
1959{
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001960 int status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001961
1962 u16 fecOcSncMode = 0;
1963
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001964 /* Allow OC to sync again */
1965 status = read16(state, FEC_OC_SNC_MODE__A, &fecOcSncMode);
1966 if (status < 0)
1967 goto error;
1968 fecOcSncMode &= ~FEC_OC_SNC_MODE_SHUTDOWN__M;
1969 status = write16(state, FEC_OC_SNC_MODE__A, fecOcSncMode);
1970 if (status < 0)
1971 goto error;
1972 status = write16(state, FEC_OC_SNC_UNLOCK__A, 1);
1973error:
1974 if (status < 0)
1975 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001976 return status;
1977}
1978
1979static int MPEGTSDtoInit(struct drxk_state *state)
1980{
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001981 int status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001982
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03001983 dprintk(1, "\n");
1984
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001985 /* Rate integration settings */
1986 status = write16(state, FEC_OC_RCN_CTL_STEP_LO__A, 0x0000);
1987 if (status < 0)
1988 goto error;
1989 status = write16(state, FEC_OC_RCN_CTL_STEP_HI__A, 0x000C);
1990 if (status < 0)
1991 goto error;
1992 status = write16(state, FEC_OC_RCN_GAIN__A, 0x000A);
1993 if (status < 0)
1994 goto error;
1995 status = write16(state, FEC_OC_AVR_PARM_A__A, 0x0008);
1996 if (status < 0)
1997 goto error;
1998 status = write16(state, FEC_OC_AVR_PARM_B__A, 0x0006);
1999 if (status < 0)
2000 goto error;
2001 status = write16(state, FEC_OC_TMD_HI_MARGIN__A, 0x0680);
2002 if (status < 0)
2003 goto error;
2004 status = write16(state, FEC_OC_TMD_LO_MARGIN__A, 0x0080);
2005 if (status < 0)
2006 goto error;
2007 status = write16(state, FEC_OC_TMD_COUNT__A, 0x03F4);
2008 if (status < 0)
2009 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002010
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002011 /* Additional configuration */
2012 status = write16(state, FEC_OC_OCR_INVERT__A, 0);
2013 if (status < 0)
2014 goto error;
2015 status = write16(state, FEC_OC_SNC_LWM__A, 2);
2016 if (status < 0)
2017 goto error;
2018 status = write16(state, FEC_OC_SNC_HWM__A, 12);
2019error:
2020 if (status < 0)
2021 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
2022
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002023 return status;
2024}
2025
Oliver Endrissebc7de22011-07-03 13:49:44 -03002026static int MPEGTSDtoSetup(struct drxk_state *state,
2027 enum OperationMode oMode)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002028{
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002029 int status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002030
Oliver Endrissebc7de22011-07-03 13:49:44 -03002031 u16 fecOcRegMode = 0; /* FEC_OC_MODE register value */
2032 u16 fecOcRegIprMode = 0; /* FEC_OC_IPR_MODE register value */
2033 u16 fecOcDtoMode = 0; /* FEC_OC_IPR_INVERT register value */
2034 u16 fecOcFctMode = 0; /* FEC_OC_IPR_INVERT register value */
2035 u16 fecOcDtoPeriod = 2; /* FEC_OC_IPR_INVERT register value */
2036 u16 fecOcDtoBurstLen = 188; /* FEC_OC_IPR_INVERT register value */
2037 u32 fecOcRcnCtlRate = 0; /* FEC_OC_IPR_INVERT register value */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002038 u16 fecOcTmdMode = 0;
2039 u16 fecOcTmdIntUpdRate = 0;
Oliver Endrissebc7de22011-07-03 13:49:44 -03002040 u32 maxBitRate = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002041 bool staticCLK = false;
2042
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03002043 dprintk(1, "\n");
2044
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002045 /* Check insertion of the Reed-Solomon parity bytes */
2046 status = read16(state, FEC_OC_MODE__A, &fecOcRegMode);
2047 if (status < 0)
2048 goto error;
2049 status = read16(state, FEC_OC_IPR_MODE__A, &fecOcRegIprMode);
2050 if (status < 0)
2051 goto error;
2052 fecOcRegMode &= (~FEC_OC_MODE_PARITY__M);
2053 fecOcRegIprMode &= (~FEC_OC_IPR_MODE_MVAL_DIS_PAR__M);
2054 if (state->m_insertRSByte == true) {
2055 /* enable parity symbol forward */
2056 fecOcRegMode |= FEC_OC_MODE_PARITY__M;
2057 /* MVAL disable during parity bytes */
2058 fecOcRegIprMode |= FEC_OC_IPR_MODE_MVAL_DIS_PAR__M;
2059 /* TS burst length to 204 */
2060 fecOcDtoBurstLen = 204;
2061 }
2062
2063 /* Check serial or parrallel output */
2064 fecOcRegIprMode &= (~(FEC_OC_IPR_MODE_SERIAL__M));
2065 if (state->m_enableParallel == false) {
2066 /* MPEG data output is serial -> set ipr_mode[0] */
2067 fecOcRegIprMode |= FEC_OC_IPR_MODE_SERIAL__M;
2068 }
2069
2070 switch (oMode) {
2071 case OM_DVBT:
2072 maxBitRate = state->m_DVBTBitrate;
2073 fecOcTmdMode = 3;
2074 fecOcRcnCtlRate = 0xC00000;
2075 staticCLK = state->m_DVBTStaticCLK;
2076 break;
2077 case OM_QAM_ITU_A: /* fallthrough */
2078 case OM_QAM_ITU_C:
2079 fecOcTmdMode = 0x0004;
2080 fecOcRcnCtlRate = 0xD2B4EE; /* good for >63 Mb/s */
2081 maxBitRate = state->m_DVBCBitrate;
2082 staticCLK = state->m_DVBCStaticCLK;
2083 break;
2084 default:
2085 status = -EINVAL;
2086 } /* switch (standard) */
2087 if (status < 0)
2088 goto error;
2089
2090 /* Configure DTO's */
2091 if (staticCLK) {
2092 u32 bitRate = 0;
2093
2094 /* Rational DTO for MCLK source (static MCLK rate),
2095 Dynamic DTO for optimal grouping
2096 (avoid intra-packet gaps),
2097 DTO offset enable to sync TS burst with MSTRT */
2098 fecOcDtoMode = (FEC_OC_DTO_MODE_DYNAMIC__M |
2099 FEC_OC_DTO_MODE_OFFSET_ENABLE__M);
2100 fecOcFctMode = (FEC_OC_FCT_MODE_RAT_ENA__M |
2101 FEC_OC_FCT_MODE_VIRT_ENA__M);
2102
2103 /* Check user defined bitrate */
2104 bitRate = maxBitRate;
2105 if (bitRate > 75900000UL) { /* max is 75.9 Mb/s */
2106 bitRate = 75900000UL;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002107 }
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002108 /* Rational DTO period:
2109 dto_period = (Fsys / bitrate) - 2
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002110
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002111 Result should be floored,
2112 to make sure >= requested bitrate
2113 */
2114 fecOcDtoPeriod = (u16) (((state->m_sysClockFreq)
2115 * 1000) / bitRate);
2116 if (fecOcDtoPeriod <= 2)
2117 fecOcDtoPeriod = 0;
2118 else
2119 fecOcDtoPeriod -= 2;
2120 fecOcTmdIntUpdRate = 8;
2121 } else {
2122 /* (commonAttr->staticCLK == false) => dynamic mode */
2123 fecOcDtoMode = FEC_OC_DTO_MODE_DYNAMIC__M;
2124 fecOcFctMode = FEC_OC_FCT_MODE__PRE;
2125 fecOcTmdIntUpdRate = 5;
2126 }
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002127
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002128 /* Write appropriate registers with requested configuration */
2129 status = write16(state, FEC_OC_DTO_BURST_LEN__A, fecOcDtoBurstLen);
2130 if (status < 0)
2131 goto error;
2132 status = write16(state, FEC_OC_DTO_PERIOD__A, fecOcDtoPeriod);
2133 if (status < 0)
2134 goto error;
2135 status = write16(state, FEC_OC_DTO_MODE__A, fecOcDtoMode);
2136 if (status < 0)
2137 goto error;
2138 status = write16(state, FEC_OC_FCT_MODE__A, fecOcFctMode);
2139 if (status < 0)
2140 goto error;
2141 status = write16(state, FEC_OC_MODE__A, fecOcRegMode);
2142 if (status < 0)
2143 goto error;
2144 status = write16(state, FEC_OC_IPR_MODE__A, fecOcRegIprMode);
2145 if (status < 0)
2146 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002147
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002148 /* Rate integration settings */
2149 status = write32(state, FEC_OC_RCN_CTL_RATE_LO__A, fecOcRcnCtlRate);
2150 if (status < 0)
2151 goto error;
2152 status = write16(state, FEC_OC_TMD_INT_UPD_RATE__A, fecOcTmdIntUpdRate);
2153 if (status < 0)
2154 goto error;
2155 status = write16(state, FEC_OC_TMD_MODE__A, fecOcTmdMode);
2156error:
2157 if (status < 0)
2158 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002159 return status;
2160}
2161
2162static int MPEGTSConfigurePolarity(struct drxk_state *state)
2163{
Oliver Endrissebc7de22011-07-03 13:49:44 -03002164 u16 fecOcRegIprInvert = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002165
2166 /* Data mask for the output data byte */
2167 u16 InvertDataMask =
Oliver Endrissebc7de22011-07-03 13:49:44 -03002168 FEC_OC_IPR_INVERT_MD7__M | FEC_OC_IPR_INVERT_MD6__M |
2169 FEC_OC_IPR_INVERT_MD5__M | FEC_OC_IPR_INVERT_MD4__M |
2170 FEC_OC_IPR_INVERT_MD3__M | FEC_OC_IPR_INVERT_MD2__M |
2171 FEC_OC_IPR_INVERT_MD1__M | FEC_OC_IPR_INVERT_MD0__M;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002172
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002173 dprintk(1, "\n");
2174
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002175 /* Control selective inversion of output bits */
2176 fecOcRegIprInvert &= (~(InvertDataMask));
2177 if (state->m_invertDATA == true)
2178 fecOcRegIprInvert |= InvertDataMask;
2179 fecOcRegIprInvert &= (~(FEC_OC_IPR_INVERT_MERR__M));
2180 if (state->m_invertERR == true)
2181 fecOcRegIprInvert |= FEC_OC_IPR_INVERT_MERR__M;
2182 fecOcRegIprInvert &= (~(FEC_OC_IPR_INVERT_MSTRT__M));
2183 if (state->m_invertSTR == true)
2184 fecOcRegIprInvert |= FEC_OC_IPR_INVERT_MSTRT__M;
2185 fecOcRegIprInvert &= (~(FEC_OC_IPR_INVERT_MVAL__M));
2186 if (state->m_invertVAL == true)
2187 fecOcRegIprInvert |= FEC_OC_IPR_INVERT_MVAL__M;
2188 fecOcRegIprInvert &= (~(FEC_OC_IPR_INVERT_MCLK__M));
2189 if (state->m_invertCLK == true)
2190 fecOcRegIprInvert |= FEC_OC_IPR_INVERT_MCLK__M;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002191
2192 return write16(state, FEC_OC_IPR_INVERT__A, fecOcRegIprInvert);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002193}
2194
2195#define SCU_RAM_AGC_KI_INV_RF_POL__M 0x4000
2196
2197static int SetAgcRf(struct drxk_state *state,
2198 struct SCfgAgc *pAgcCfg, bool isDTV)
2199{
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002200 int status = -EINVAL;
2201 u16 data = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002202 struct SCfgAgc *pIfAgcSettings;
2203
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03002204 dprintk(1, "\n");
2205
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002206 if (pAgcCfg == NULL)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002207 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002208
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002209 switch (pAgcCfg->ctrlMode) {
2210 case DRXK_AGC_CTRL_AUTO:
2211 /* Enable RF AGC DAC */
2212 status = read16(state, IQM_AF_STDBY__A, &data);
2213 if (status < 0)
2214 goto error;
2215 data &= ~IQM_AF_STDBY_STDBY_TAGC_RF_STANDBY;
2216 status = write16(state, IQM_AF_STDBY__A, data);
2217 if (status < 0)
2218 goto error;
2219 status = read16(state, SCU_RAM_AGC_CONFIG__A, &data);
2220 if (status < 0)
2221 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002222
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002223 /* Enable SCU RF AGC loop */
2224 data &= ~SCU_RAM_AGC_CONFIG_DISABLE_RF_AGC__M;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002225
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002226 /* Polarity */
2227 if (state->m_RfAgcPol)
2228 data |= SCU_RAM_AGC_CONFIG_INV_RF_POL__M;
2229 else
2230 data &= ~SCU_RAM_AGC_CONFIG_INV_RF_POL__M;
2231 status = write16(state, SCU_RAM_AGC_CONFIG__A, data);
2232 if (status < 0)
2233 goto error;
2234
2235 /* Set speed (using complementary reduction value) */
2236 status = read16(state, SCU_RAM_AGC_KI_RED__A, &data);
2237 if (status < 0)
2238 goto error;
2239
2240 data &= ~SCU_RAM_AGC_KI_RED_RAGC_RED__M;
2241 data |= (~(pAgcCfg->speed <<
2242 SCU_RAM_AGC_KI_RED_RAGC_RED__B)
2243 & SCU_RAM_AGC_KI_RED_RAGC_RED__M);
2244
2245 status = write16(state, SCU_RAM_AGC_KI_RED__A, data);
2246 if (status < 0)
2247 goto error;
2248
2249 if (IsDVBT(state))
2250 pIfAgcSettings = &state->m_dvbtIfAgcCfg;
2251 else if (IsQAM(state))
2252 pIfAgcSettings = &state->m_qamIfAgcCfg;
2253 else
2254 pIfAgcSettings = &state->m_atvIfAgcCfg;
2255 if (pIfAgcSettings == NULL) {
2256 status = -EINVAL;
2257 goto error;
2258 }
2259
2260 /* Set TOP, only if IF-AGC is in AUTO mode */
2261 if (pIfAgcSettings->ctrlMode == DRXK_AGC_CTRL_AUTO)
2262 status = write16(state, SCU_RAM_AGC_IF_IACCU_HI_TGT_MAX__A, pAgcCfg->top);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03002263 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002264 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002265
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002266 /* Cut-Off current */
2267 status = write16(state, SCU_RAM_AGC_RF_IACCU_HI_CO__A, pAgcCfg->cutOffCurrent);
2268 if (status < 0)
2269 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002270
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002271 /* Max. output level */
2272 status = write16(state, SCU_RAM_AGC_RF_MAX__A, pAgcCfg->maxOutputLevel);
2273 if (status < 0)
2274 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002275
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002276 break;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002277
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002278 case DRXK_AGC_CTRL_USER:
2279 /* Enable RF AGC DAC */
2280 status = read16(state, IQM_AF_STDBY__A, &data);
2281 if (status < 0)
2282 goto error;
2283 data &= ~IQM_AF_STDBY_STDBY_TAGC_RF_STANDBY;
2284 status = write16(state, IQM_AF_STDBY__A, data);
2285 if (status < 0)
2286 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002287
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002288 /* Disable SCU RF AGC loop */
2289 status = read16(state, SCU_RAM_AGC_CONFIG__A, &data);
2290 if (status < 0)
2291 goto error;
2292 data |= SCU_RAM_AGC_CONFIG_DISABLE_RF_AGC__M;
2293 if (state->m_RfAgcPol)
2294 data |= SCU_RAM_AGC_CONFIG_INV_RF_POL__M;
2295 else
2296 data &= ~SCU_RAM_AGC_CONFIG_INV_RF_POL__M;
2297 status = write16(state, SCU_RAM_AGC_CONFIG__A, data);
2298 if (status < 0)
2299 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002300
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002301 /* SCU c.o.c. to 0, enabling full control range */
2302 status = write16(state, SCU_RAM_AGC_RF_IACCU_HI_CO__A, 0);
2303 if (status < 0)
2304 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002305
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002306 /* Write value to output pin */
2307 status = write16(state, SCU_RAM_AGC_RF_IACCU_HI__A, pAgcCfg->outputLevel);
2308 if (status < 0)
2309 goto error;
2310 break;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002311
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002312 case DRXK_AGC_CTRL_OFF:
2313 /* Disable RF AGC DAC */
2314 status = read16(state, IQM_AF_STDBY__A, &data);
2315 if (status < 0)
2316 goto error;
2317 data |= IQM_AF_STDBY_STDBY_TAGC_RF_STANDBY;
2318 status = write16(state, IQM_AF_STDBY__A, data);
2319 if (status < 0)
2320 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002321
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002322 /* Disable SCU RF AGC loop */
2323 status = read16(state, SCU_RAM_AGC_CONFIG__A, &data);
2324 if (status < 0)
2325 goto error;
2326 data |= SCU_RAM_AGC_CONFIG_DISABLE_RF_AGC__M;
2327 status = write16(state, SCU_RAM_AGC_CONFIG__A, data);
2328 if (status < 0)
2329 goto error;
2330 break;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002331
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002332 default:
2333 status = -EINVAL;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002334
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002335 }
2336error:
2337 if (status < 0)
2338 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002339 return status;
2340}
2341
2342#define SCU_RAM_AGC_KI_INV_IF_POL__M 0x2000
2343
Oliver Endrissebc7de22011-07-03 13:49:44 -03002344static int SetAgcIf(struct drxk_state *state,
2345 struct SCfgAgc *pAgcCfg, bool isDTV)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002346{
2347 u16 data = 0;
2348 int status = 0;
2349 struct SCfgAgc *pRfAgcSettings;
2350
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03002351 dprintk(1, "\n");
2352
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002353 switch (pAgcCfg->ctrlMode) {
2354 case DRXK_AGC_CTRL_AUTO:
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002355
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002356 /* Enable IF AGC DAC */
2357 status = read16(state, IQM_AF_STDBY__A, &data);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03002358 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002359 goto error;
2360 data &= ~IQM_AF_STDBY_STDBY_TAGC_IF_STANDBY;
2361 status = write16(state, IQM_AF_STDBY__A, data);
2362 if (status < 0)
2363 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002364
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002365 status = read16(state, SCU_RAM_AGC_CONFIG__A, &data);
2366 if (status < 0)
2367 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002368
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002369 /* Enable SCU IF AGC loop */
2370 data &= ~SCU_RAM_AGC_CONFIG_DISABLE_IF_AGC__M;
2371
2372 /* Polarity */
2373 if (state->m_IfAgcPol)
2374 data |= SCU_RAM_AGC_CONFIG_INV_IF_POL__M;
2375 else
2376 data &= ~SCU_RAM_AGC_CONFIG_INV_IF_POL__M;
2377 status = write16(state, SCU_RAM_AGC_CONFIG__A, data);
2378 if (status < 0)
2379 goto error;
2380
2381 /* Set speed (using complementary reduction value) */
2382 status = read16(state, SCU_RAM_AGC_KI_RED__A, &data);
2383 if (status < 0)
2384 goto error;
2385 data &= ~SCU_RAM_AGC_KI_RED_IAGC_RED__M;
2386 data |= (~(pAgcCfg->speed <<
2387 SCU_RAM_AGC_KI_RED_IAGC_RED__B)
2388 & SCU_RAM_AGC_KI_RED_IAGC_RED__M);
2389
2390 status = write16(state, SCU_RAM_AGC_KI_RED__A, data);
2391 if (status < 0)
2392 goto error;
2393
2394 if (IsQAM(state))
2395 pRfAgcSettings = &state->m_qamRfAgcCfg;
2396 else
2397 pRfAgcSettings = &state->m_atvRfAgcCfg;
2398 if (pRfAgcSettings == NULL)
2399 return -1;
2400 /* Restore TOP */
2401 status = write16(state, SCU_RAM_AGC_IF_IACCU_HI_TGT_MAX__A, pRfAgcSettings->top);
2402 if (status < 0)
2403 goto error;
2404 break;
2405
2406 case DRXK_AGC_CTRL_USER:
2407
2408 /* Enable IF AGC DAC */
2409 status = read16(state, IQM_AF_STDBY__A, &data);
2410 if (status < 0)
2411 goto error;
2412 data &= ~IQM_AF_STDBY_STDBY_TAGC_IF_STANDBY;
2413 status = write16(state, IQM_AF_STDBY__A, data);
2414 if (status < 0)
2415 goto error;
2416
2417 status = read16(state, SCU_RAM_AGC_CONFIG__A, &data);
2418 if (status < 0)
2419 goto error;
2420
2421 /* Disable SCU IF AGC loop */
2422 data |= SCU_RAM_AGC_CONFIG_DISABLE_IF_AGC__M;
2423
2424 /* Polarity */
2425 if (state->m_IfAgcPol)
2426 data |= SCU_RAM_AGC_CONFIG_INV_IF_POL__M;
2427 else
2428 data &= ~SCU_RAM_AGC_CONFIG_INV_IF_POL__M;
2429 status = write16(state, SCU_RAM_AGC_CONFIG__A, data);
2430 if (status < 0)
2431 goto error;
2432
2433 /* Write value to output pin */
2434 status = write16(state, SCU_RAM_AGC_IF_IACCU_HI_TGT_MAX__A, pAgcCfg->outputLevel);
2435 if (status < 0)
2436 goto error;
2437 break;
2438
2439 case DRXK_AGC_CTRL_OFF:
2440
2441 /* Disable If AGC DAC */
2442 status = read16(state, IQM_AF_STDBY__A, &data);
2443 if (status < 0)
2444 goto error;
2445 data |= IQM_AF_STDBY_STDBY_TAGC_IF_STANDBY;
2446 status = write16(state, IQM_AF_STDBY__A, data);
2447 if (status < 0)
2448 goto error;
2449
2450 /* Disable SCU IF AGC loop */
2451 status = read16(state, SCU_RAM_AGC_CONFIG__A, &data);
2452 if (status < 0)
2453 goto error;
2454 data |= SCU_RAM_AGC_CONFIG_DISABLE_IF_AGC__M;
2455 status = write16(state, SCU_RAM_AGC_CONFIG__A, data);
2456 if (status < 0)
2457 goto error;
2458 break;
2459 } /* switch (agcSettingsIf->ctrlMode) */
2460
2461 /* always set the top to support
2462 configurations without if-loop */
2463 status = write16(state, SCU_RAM_AGC_INGAIN_TGT_MIN__A, pAgcCfg->top);
2464error:
2465 if (status < 0)
2466 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002467 return status;
2468}
2469
2470static int ReadIFAgc(struct drxk_state *state, u32 *pValue)
2471{
2472 u16 agcDacLvl;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002473 int status;
2474 u16 Level = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002475
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03002476 dprintk(1, "\n");
2477
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002478 status = read16(state, IQM_AF_AGC_IF__A, &agcDacLvl);
2479 if (status < 0) {
2480 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
2481 return status;
2482 }
2483
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002484 *pValue = 0;
2485
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002486 if (agcDacLvl > DRXK_AGC_DAC_OFFSET)
2487 Level = agcDacLvl - DRXK_AGC_DAC_OFFSET;
2488 if (Level < 14000)
2489 *pValue = (14000 - Level) / 4;
2490 else
2491 *pValue = 0;
2492
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002493 return status;
2494}
2495
Oliver Endrissebc7de22011-07-03 13:49:44 -03002496static int GetQAMSignalToNoise(struct drxk_state *state,
2497 s32 *pSignalToNoise)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002498{
2499 int status = 0;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002500 u16 qamSlErrPower = 0; /* accum. error between
2501 raw and sliced symbols */
2502 u32 qamSlSigPower = 0; /* used for MER, depends of
2503 QAM constellation */
2504 u32 qamSlMer = 0; /* QAM MER */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002505
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03002506 dprintk(1, "\n");
2507
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002508 /* MER calculation */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002509
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002510 /* get the register value needed for MER */
2511 status = read16(state, QAM_SL_ERR_POWER__A, &qamSlErrPower);
2512 if (status < 0) {
2513 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
2514 return -EINVAL;
2515 }
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002516
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002517 switch (state->param.u.qam.modulation) {
2518 case QAM_16:
2519 qamSlSigPower = DRXK_QAM_SL_SIG_POWER_QAM16 << 2;
2520 break;
2521 case QAM_32:
2522 qamSlSigPower = DRXK_QAM_SL_SIG_POWER_QAM32 << 2;
2523 break;
2524 case QAM_64:
2525 qamSlSigPower = DRXK_QAM_SL_SIG_POWER_QAM64 << 2;
2526 break;
2527 case QAM_128:
2528 qamSlSigPower = DRXK_QAM_SL_SIG_POWER_QAM128 << 2;
2529 break;
2530 default:
2531 case QAM_256:
2532 qamSlSigPower = DRXK_QAM_SL_SIG_POWER_QAM256 << 2;
2533 break;
2534 }
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002535
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002536 if (qamSlErrPower > 0) {
2537 qamSlMer = Log10Times100(qamSlSigPower) -
2538 Log10Times100((u32) qamSlErrPower);
2539 }
2540 *pSignalToNoise = qamSlMer;
2541
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002542 return status;
2543}
2544
Oliver Endrissebc7de22011-07-03 13:49:44 -03002545static int GetDVBTSignalToNoise(struct drxk_state *state,
2546 s32 *pSignalToNoise)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002547{
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002548 int status;
Oliver Endrissebc7de22011-07-03 13:49:44 -03002549 u16 regData = 0;
2550 u32 EqRegTdSqrErrI = 0;
2551 u32 EqRegTdSqrErrQ = 0;
2552 u16 EqRegTdSqrErrExp = 0;
2553 u16 EqRegTdTpsPwrOfs = 0;
2554 u16 EqRegTdReqSmbCnt = 0;
2555 u32 tpsCnt = 0;
2556 u32 SqrErrIQ = 0;
2557 u32 a = 0;
2558 u32 b = 0;
2559 u32 c = 0;
2560 u32 iMER = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002561 u16 transmissionParams = 0;
2562
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03002563 dprintk(1, "\n");
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002564
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002565 status = read16(state, OFDM_EQ_TOP_TD_TPS_PWR_OFS__A, &EqRegTdTpsPwrOfs);
2566 if (status < 0)
2567 goto error;
2568 status = read16(state, OFDM_EQ_TOP_TD_REQ_SMB_CNT__A, &EqRegTdReqSmbCnt);
2569 if (status < 0)
2570 goto error;
2571 status = read16(state, OFDM_EQ_TOP_TD_SQR_ERR_EXP__A, &EqRegTdSqrErrExp);
2572 if (status < 0)
2573 goto error;
2574 status = read16(state, OFDM_EQ_TOP_TD_SQR_ERR_I__A, &regData);
2575 if (status < 0)
2576 goto error;
2577 /* Extend SQR_ERR_I operational range */
2578 EqRegTdSqrErrI = (u32) regData;
2579 if ((EqRegTdSqrErrExp > 11) &&
2580 (EqRegTdSqrErrI < 0x00000FFFUL)) {
2581 EqRegTdSqrErrI += 0x00010000UL;
2582 }
2583 status = read16(state, OFDM_EQ_TOP_TD_SQR_ERR_Q__A, &regData);
2584 if (status < 0)
2585 goto error;
2586 /* Extend SQR_ERR_Q operational range */
2587 EqRegTdSqrErrQ = (u32) regData;
2588 if ((EqRegTdSqrErrExp > 11) &&
2589 (EqRegTdSqrErrQ < 0x00000FFFUL))
2590 EqRegTdSqrErrQ += 0x00010000UL;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002591
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002592 status = read16(state, OFDM_SC_RA_RAM_OP_PARAM__A, &transmissionParams);
2593 if (status < 0)
2594 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002595
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002596 /* Check input data for MER */
2597
2598 /* MER calculation (in 0.1 dB) without math.h */
2599 if ((EqRegTdTpsPwrOfs == 0) || (EqRegTdReqSmbCnt == 0))
2600 iMER = 0;
2601 else if ((EqRegTdSqrErrI + EqRegTdSqrErrQ) == 0) {
2602 /* No error at all, this must be the HW reset value
2603 * Apparently no first measurement yet
2604 * Set MER to 0.0 */
2605 iMER = 0;
2606 } else {
2607 SqrErrIQ = (EqRegTdSqrErrI + EqRegTdSqrErrQ) <<
2608 EqRegTdSqrErrExp;
2609 if ((transmissionParams &
2610 OFDM_SC_RA_RAM_OP_PARAM_MODE__M)
2611 == OFDM_SC_RA_RAM_OP_PARAM_MODE_2K)
2612 tpsCnt = 17;
2613 else
2614 tpsCnt = 68;
2615
2616 /* IMER = 100 * log10 (x)
2617 where x = (EqRegTdTpsPwrOfs^2 *
2618 EqRegTdReqSmbCnt * tpsCnt)/SqrErrIQ
2619
2620 => IMER = a + b -c
2621 where a = 100 * log10 (EqRegTdTpsPwrOfs^2)
2622 b = 100 * log10 (EqRegTdReqSmbCnt * tpsCnt)
2623 c = 100 * log10 (SqrErrIQ)
2624 */
2625
2626 /* log(x) x = 9bits * 9bits->18 bits */
2627 a = Log10Times100(EqRegTdTpsPwrOfs *
2628 EqRegTdTpsPwrOfs);
2629 /* log(x) x = 16bits * 7bits->23 bits */
2630 b = Log10Times100(EqRegTdReqSmbCnt * tpsCnt);
2631 /* log(x) x = (16bits + 16bits) << 15 ->32 bits */
2632 c = Log10Times100(SqrErrIQ);
2633
2634 iMER = a + b;
2635 /* No negative MER, clip to zero */
2636 if (iMER > c)
2637 iMER -= c;
2638 else
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002639 iMER = 0;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002640 }
2641 *pSignalToNoise = iMER;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002642
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002643error:
2644 if (status < 0)
2645 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002646 return status;
2647}
2648
2649static int GetSignalToNoise(struct drxk_state *state, s32 *pSignalToNoise)
2650{
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03002651 dprintk(1, "\n");
2652
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002653 *pSignalToNoise = 0;
Oliver Endrissebc7de22011-07-03 13:49:44 -03002654 switch (state->m_OperationMode) {
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002655 case OM_DVBT:
2656 return GetDVBTSignalToNoise(state, pSignalToNoise);
2657 case OM_QAM_ITU_A:
2658 case OM_QAM_ITU_C:
2659 return GetQAMSignalToNoise(state, pSignalToNoise);
2660 default:
2661 break;
2662 }
2663 return 0;
2664}
2665
2666#if 0
2667static int GetDVBTQuality(struct drxk_state *state, s32 *pQuality)
2668{
2669 /* SNR Values for quasi errorfree reception rom Nordig 2.2 */
2670 int status = 0;
2671
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03002672 dprintk(1, "\n");
2673
Oliver Endrissebc7de22011-07-03 13:49:44 -03002674 static s32 QE_SN[] = {
2675 51, /* QPSK 1/2 */
2676 69, /* QPSK 2/3 */
2677 79, /* QPSK 3/4 */
2678 89, /* QPSK 5/6 */
2679 97, /* QPSK 7/8 */
2680 108, /* 16-QAM 1/2 */
2681 131, /* 16-QAM 2/3 */
2682 146, /* 16-QAM 3/4 */
2683 156, /* 16-QAM 5/6 */
2684 160, /* 16-QAM 7/8 */
2685 165, /* 64-QAM 1/2 */
2686 187, /* 64-QAM 2/3 */
2687 202, /* 64-QAM 3/4 */
2688 216, /* 64-QAM 5/6 */
2689 225, /* 64-QAM 7/8 */
2690 };
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002691
2692 *pQuality = 0;
2693
2694 do {
2695 s32 SignalToNoise = 0;
2696 u16 Constellation = 0;
2697 u16 CodeRate = 0;
2698 u32 SignalToNoiseRel;
2699 u32 BERQuality;
2700
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03002701 status = GetDVBTSignalToNoise(state, &SignalToNoise);
2702 if (status < 0)
2703 break;
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -03002704 status = read16(state, OFDM_EQ_TOP_TD_TPS_CONST__A, &Constellation);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03002705 if (status < 0)
2706 break;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002707 Constellation &= OFDM_EQ_TOP_TD_TPS_CONST__M;
2708
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -03002709 status = read16(state, OFDM_EQ_TOP_TD_TPS_CODE_HP__A, &CodeRate);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03002710 if (status < 0)
2711 break;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002712 CodeRate &= OFDM_EQ_TOP_TD_TPS_CODE_HP__M;
2713
2714 if (Constellation > OFDM_EQ_TOP_TD_TPS_CONST_64QAM ||
2715 CodeRate > OFDM_EQ_TOP_TD_TPS_CODE_LP_7_8)
2716 break;
2717 SignalToNoiseRel = SignalToNoise -
Oliver Endrissebc7de22011-07-03 13:49:44 -03002718 QE_SN[Constellation * 5 + CodeRate];
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002719 BERQuality = 100;
2720
Oliver Endrissebc7de22011-07-03 13:49:44 -03002721 if (SignalToNoiseRel < -70)
2722 *pQuality = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002723 else if (SignalToNoiseRel < 30)
2724 *pQuality = ((SignalToNoiseRel + 70) *
2725 BERQuality) / 100;
2726 else
2727 *pQuality = BERQuality;
Oliver Endrissebc7de22011-07-03 13:49:44 -03002728 } while (0);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002729 return 0;
2730};
2731
Oliver Endrissebc7de22011-07-03 13:49:44 -03002732static int GetDVBCQuality(struct drxk_state *state, s32 *pQuality)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002733{
2734 int status = 0;
2735 *pQuality = 0;
2736
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03002737 dprintk(1, "\n");
2738
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002739 do {
2740 u32 SignalToNoise = 0;
2741 u32 BERQuality = 100;
2742 u32 SignalToNoiseRel = 0;
2743
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03002744 status = GetQAMSignalToNoise(state, &SignalToNoise);
2745 if (status < 0)
2746 break;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002747
Oliver Endrissebc7de22011-07-03 13:49:44 -03002748 switch (state->param.u.qam.modulation) {
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002749 case QAM_16:
2750 SignalToNoiseRel = SignalToNoise - 200;
2751 break;
2752 case QAM_32:
2753 SignalToNoiseRel = SignalToNoise - 230;
Oliver Endrissebc7de22011-07-03 13:49:44 -03002754 break; /* Not in NorDig */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002755 case QAM_64:
2756 SignalToNoiseRel = SignalToNoise - 260;
2757 break;
2758 case QAM_128:
2759 SignalToNoiseRel = SignalToNoise - 290;
2760 break;
2761 default:
2762 case QAM_256:
2763 SignalToNoiseRel = SignalToNoise - 320;
2764 break;
2765 }
2766
2767 if (SignalToNoiseRel < -70)
2768 *pQuality = 0;
2769 else if (SignalToNoiseRel < 30)
2770 *pQuality = ((SignalToNoiseRel + 70) *
2771 BERQuality) / 100;
2772 else
2773 *pQuality = BERQuality;
Oliver Endrissebc7de22011-07-03 13:49:44 -03002774 } while (0);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002775
2776 return status;
2777}
2778
2779static int GetQuality(struct drxk_state *state, s32 *pQuality)
2780{
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03002781 dprintk(1, "\n");
2782
Oliver Endrissebc7de22011-07-03 13:49:44 -03002783 switch (state->m_OperationMode) {
2784 case OM_DVBT:
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002785 return GetDVBTQuality(state, pQuality);
Oliver Endrissebc7de22011-07-03 13:49:44 -03002786 case OM_QAM_ITU_A:
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002787 return GetDVBCQuality(state, pQuality);
2788 default:
2789 break;
2790 }
2791
2792 return 0;
2793}
2794#endif
2795
2796/* Free data ram in SIO HI */
2797#define SIO_HI_RA_RAM_USR_BEGIN__A 0x420040
2798#define SIO_HI_RA_RAM_USR_END__A 0x420060
2799
2800#define DRXK_HI_ATOMIC_BUF_START (SIO_HI_RA_RAM_USR_BEGIN__A)
2801#define DRXK_HI_ATOMIC_BUF_END (SIO_HI_RA_RAM_USR_BEGIN__A + 7)
2802#define DRXK_HI_ATOMIC_READ SIO_HI_RA_RAM_PAR_3_ACP_RW_READ
2803#define DRXK_HI_ATOMIC_WRITE SIO_HI_RA_RAM_PAR_3_ACP_RW_WRITE
2804
2805#define DRXDAP_FASI_ADDR2BLOCK(addr) (((addr) >> 22) & 0x3F)
2806#define DRXDAP_FASI_ADDR2BANK(addr) (((addr) >> 16) & 0x3F)
2807#define DRXDAP_FASI_ADDR2OFFSET(addr) ((addr) & 0x7FFF)
2808
2809static int ConfigureI2CBridge(struct drxk_state *state, bool bEnableBridge)
2810{
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002811 int status = -EINVAL;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002812
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03002813 dprintk(1, "\n");
2814
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002815 if (state->m_DrxkState == DRXK_UNINITIALIZED)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002816 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002817 if (state->m_DrxkState == DRXK_POWERED_DOWN)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002818 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002819
Mauro Carvalho Chehabf1fe1b72011-07-09 21:59:33 -03002820 if (state->no_i2c_bridge)
2821 return 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002822
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002823 status = write16(state, SIO_HI_RA_RAM_PAR_1__A, SIO_HI_RA_RAM_PAR_1_PAR1_SEC_KEY);
2824 if (status < 0)
2825 goto error;
2826 if (bEnableBridge) {
2827 status = write16(state, SIO_HI_RA_RAM_PAR_2__A, SIO_HI_RA_RAM_PAR_2_BRD_CFG_CLOSED);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03002828 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002829 goto error;
2830 } else {
2831 status = write16(state, SIO_HI_RA_RAM_PAR_2__A, SIO_HI_RA_RAM_PAR_2_BRD_CFG_OPEN);
2832 if (status < 0)
2833 goto error;
2834 }
2835
2836 status = HI_Command(state, SIO_HI_RA_RAM_CMD_BRDCTRL, 0);
2837
2838error:
2839 if (status < 0)
2840 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002841 return status;
2842}
2843
Oliver Endrissebc7de22011-07-03 13:49:44 -03002844static int SetPreSaw(struct drxk_state *state,
2845 struct SCfgPreSaw *pPreSawCfg)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002846{
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002847 int status = -EINVAL;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002848
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03002849 dprintk(1, "\n");
2850
Oliver Endrissebc7de22011-07-03 13:49:44 -03002851 if ((pPreSawCfg == NULL)
2852 || (pPreSawCfg->reference > IQM_AF_PDREF__M))
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002853 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002854
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -03002855 status = write16(state, IQM_AF_PDREF__A, pPreSawCfg->reference);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002856error:
2857 if (status < 0)
2858 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002859 return status;
2860}
2861
2862static int BLDirectCmd(struct drxk_state *state, u32 targetAddr,
Oliver Endrissebc7de22011-07-03 13:49:44 -03002863 u16 romOffset, u16 nrOfElements, u32 timeOut)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002864{
Oliver Endrissebc7de22011-07-03 13:49:44 -03002865 u16 blStatus = 0;
2866 u16 offset = (u16) ((targetAddr >> 0) & 0x00FFFF);
2867 u16 blockbank = (u16) ((targetAddr >> 16) & 0x000FFF);
2868 int status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002869 unsigned long end;
2870
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03002871 dprintk(1, "\n");
2872
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002873 mutex_lock(&state->mutex);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002874 status = write16(state, SIO_BL_MODE__A, SIO_BL_MODE_DIRECT);
2875 if (status < 0)
2876 goto error;
2877 status = write16(state, SIO_BL_TGT_HDR__A, blockbank);
2878 if (status < 0)
2879 goto error;
2880 status = write16(state, SIO_BL_TGT_ADDR__A, offset);
2881 if (status < 0)
2882 goto error;
2883 status = write16(state, SIO_BL_SRC_ADDR__A, romOffset);
2884 if (status < 0)
2885 goto error;
2886 status = write16(state, SIO_BL_SRC_LEN__A, nrOfElements);
2887 if (status < 0)
2888 goto error;
2889 status = write16(state, SIO_BL_ENABLE__A, SIO_BL_ENABLE_ON);
2890 if (status < 0)
2891 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002892
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002893 end = jiffies + msecs_to_jiffies(timeOut);
2894 do {
2895 status = read16(state, SIO_BL_STATUS__A, &blStatus);
2896 if (status < 0)
2897 goto error;
2898 } while ((blStatus == 0x1) && time_is_after_jiffies(end));
2899 if (blStatus == 0x1) {
2900 printk(KERN_ERR "drxk: SIO not ready\n");
2901 status = -EINVAL;
2902 goto error2;
2903 }
2904error:
2905 if (status < 0)
2906 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
2907error2:
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002908 mutex_unlock(&state->mutex);
2909 return status;
2910
2911}
2912
Oliver Endrissebc7de22011-07-03 13:49:44 -03002913static int ADCSyncMeasurement(struct drxk_state *state, u16 *count)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002914{
2915 u16 data = 0;
2916 int status;
2917
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03002918 dprintk(1, "\n");
2919
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002920 /* Start measurement */
2921 status = write16(state, IQM_AF_COMM_EXEC__A, IQM_AF_COMM_EXEC_ACTIVE);
2922 if (status < 0)
2923 goto error;
2924 status = write16(state, IQM_AF_START_LOCK__A, 1);
2925 if (status < 0)
2926 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002927
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002928 *count = 0;
2929 status = read16(state, IQM_AF_PHASE0__A, &data);
2930 if (status < 0)
2931 goto error;
2932 if (data == 127)
2933 *count = *count + 1;
2934 status = read16(state, IQM_AF_PHASE1__A, &data);
2935 if (status < 0)
2936 goto error;
2937 if (data == 127)
2938 *count = *count + 1;
2939 status = read16(state, IQM_AF_PHASE2__A, &data);
2940 if (status < 0)
2941 goto error;
2942 if (data == 127)
2943 *count = *count + 1;
2944
2945error:
2946 if (status < 0)
2947 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002948 return status;
2949}
2950
2951static int ADCSynchronization(struct drxk_state *state)
2952{
2953 u16 count = 0;
2954 int status;
2955
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03002956 dprintk(1, "\n");
2957
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002958 status = ADCSyncMeasurement(state, &count);
2959 if (status < 0)
2960 goto error;
2961
2962 if (count == 1) {
2963 /* Try sampling on a diffrent edge */
2964 u16 clkNeg = 0;
2965
2966 status = read16(state, IQM_AF_CLKNEG__A, &clkNeg);
2967 if (status < 0)
2968 goto error;
2969 if ((clkNeg | IQM_AF_CLKNEG_CLKNEGDATA__M) ==
2970 IQM_AF_CLKNEG_CLKNEGDATA_CLK_ADC_DATA_POS) {
2971 clkNeg &= (~(IQM_AF_CLKNEG_CLKNEGDATA__M));
2972 clkNeg |=
2973 IQM_AF_CLKNEG_CLKNEGDATA_CLK_ADC_DATA_NEG;
2974 } else {
2975 clkNeg &= (~(IQM_AF_CLKNEG_CLKNEGDATA__M));
2976 clkNeg |=
2977 IQM_AF_CLKNEG_CLKNEGDATA_CLK_ADC_DATA_POS;
2978 }
2979 status = write16(state, IQM_AF_CLKNEG__A, clkNeg);
2980 if (status < 0)
2981 goto error;
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03002982 status = ADCSyncMeasurement(state, &count);
2983 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002984 goto error;
2985 }
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002986
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002987 if (count < 2)
2988 status = -EINVAL;
2989error:
2990 if (status < 0)
2991 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002992 return status;
2993}
2994
2995static int SetFrequencyShifter(struct drxk_state *state,
2996 u16 intermediateFreqkHz,
Oliver Endrissebc7de22011-07-03 13:49:44 -03002997 s32 tunerFreqOffset, bool isDTV)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002998{
2999 bool selectPosImage = false;
Oliver Endrissebc7de22011-07-03 13:49:44 -03003000 u32 rfFreqResidual = tunerFreqOffset;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003001 u32 fmFrequencyShift = 0;
3002 bool tunerMirror = !state->m_bMirrorFreqSpect;
3003 u32 adcFreq;
3004 bool adcFlip;
3005 int status;
3006 u32 ifFreqActual;
Oliver Endrissebc7de22011-07-03 13:49:44 -03003007 u32 samplingFrequency = (u32) (state->m_sysClockFreq / 3);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003008 u32 frequencyShift;
3009 bool imageToSelect;
3010
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03003011 dprintk(1, "\n");
3012
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003013 /*
Oliver Endrissebc7de22011-07-03 13:49:44 -03003014 Program frequency shifter
3015 No need to account for mirroring on RF
3016 */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003017 if (isDTV) {
3018 if ((state->m_OperationMode == OM_QAM_ITU_A) ||
3019 (state->m_OperationMode == OM_QAM_ITU_C) ||
3020 (state->m_OperationMode == OM_DVBT))
Oliver Endrissebc7de22011-07-03 13:49:44 -03003021 selectPosImage = true;
3022 else
3023 selectPosImage = false;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003024 }
3025 if (tunerMirror)
3026 /* tuner doesn't mirror */
3027 ifFreqActual = intermediateFreqkHz +
Oliver Endrissebc7de22011-07-03 13:49:44 -03003028 rfFreqResidual + fmFrequencyShift;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003029 else
3030 /* tuner mirrors */
3031 ifFreqActual = intermediateFreqkHz -
Oliver Endrissebc7de22011-07-03 13:49:44 -03003032 rfFreqResidual - fmFrequencyShift;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003033 if (ifFreqActual > samplingFrequency / 2) {
3034 /* adc mirrors */
3035 adcFreq = samplingFrequency - ifFreqActual;
3036 adcFlip = true;
3037 } else {
3038 /* adc doesn't mirror */
3039 adcFreq = ifFreqActual;
3040 adcFlip = false;
3041 }
3042
3043 frequencyShift = adcFreq;
3044 imageToSelect = state->m_rfmirror ^ tunerMirror ^
Oliver Endrissebc7de22011-07-03 13:49:44 -03003045 adcFlip ^ selectPosImage;
3046 state->m_IqmFsRateOfs =
3047 Frac28a((frequencyShift), samplingFrequency);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003048
3049 if (imageToSelect)
3050 state->m_IqmFsRateOfs = ~state->m_IqmFsRateOfs + 1;
3051
3052 /* Program frequency shifter with tuner offset compensation */
3053 /* frequencyShift += tunerFreqOffset; TODO */
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -03003054 status = write32(state, IQM_FS_RATE_OFS_LO__A,
3055 state->m_IqmFsRateOfs);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003056 if (status < 0)
3057 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003058 return status;
3059}
3060
3061static int InitAGC(struct drxk_state *state, bool isDTV)
3062{
Oliver Endrissebc7de22011-07-03 13:49:44 -03003063 u16 ingainTgt = 0;
3064 u16 ingainTgtMin = 0;
3065 u16 ingainTgtMax = 0;
3066 u16 clpCyclen = 0;
3067 u16 clpSumMin = 0;
3068 u16 clpDirTo = 0;
3069 u16 snsSumMin = 0;
3070 u16 snsSumMax = 0;
3071 u16 clpSumMax = 0;
3072 u16 snsDirTo = 0;
3073 u16 kiInnergainMin = 0;
3074 u16 ifIaccuHiTgt = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003075 u16 ifIaccuHiTgtMin = 0;
3076 u16 ifIaccuHiTgtMax = 0;
Oliver Endrissebc7de22011-07-03 13:49:44 -03003077 u16 data = 0;
3078 u16 fastClpCtrlDelay = 0;
3079 u16 clpCtrlMode = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003080 int status = 0;
3081
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03003082 dprintk(1, "\n");
3083
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003084 /* Common settings */
3085 snsSumMax = 1023;
3086 ifIaccuHiTgtMin = 2047;
3087 clpCyclen = 500;
3088 clpSumMax = 1023;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003089
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003090 if (IsQAM(state)) {
3091 /* Standard specific settings */
3092 clpSumMin = 8;
3093 clpDirTo = (u16) -9;
3094 clpCtrlMode = 0;
3095 snsSumMin = 8;
3096 snsDirTo = (u16) -9;
3097 kiInnergainMin = (u16) -1030;
3098 } else {
3099 status = -EINVAL;
3100 goto error;
3101 }
3102 if (IsQAM(state)) {
3103 ifIaccuHiTgtMax = 0x2380;
3104 ifIaccuHiTgt = 0x2380;
3105 ingainTgtMin = 0x0511;
3106 ingainTgt = 0x0511;
3107 ingainTgtMax = 5119;
3108 fastClpCtrlDelay =
3109 state->m_qamIfAgcCfg.FastClipCtrlDelay;
3110 } else {
3111 ifIaccuHiTgtMax = 0x1200;
3112 ifIaccuHiTgt = 0x1200;
3113 ingainTgtMin = 13424;
3114 ingainTgt = 13424;
3115 ingainTgtMax = 30000;
3116 fastClpCtrlDelay =
3117 state->m_dvbtIfAgcCfg.FastClipCtrlDelay;
3118 }
3119 status = write16(state, SCU_RAM_AGC_FAST_CLP_CTRL_DELAY__A, fastClpCtrlDelay);
3120 if (status < 0)
3121 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003122
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003123 status = write16(state, SCU_RAM_AGC_CLP_CTRL_MODE__A, clpCtrlMode);
3124 if (status < 0)
3125 goto error;
3126 status = write16(state, SCU_RAM_AGC_INGAIN_TGT__A, ingainTgt);
3127 if (status < 0)
3128 goto error;
3129 status = write16(state, SCU_RAM_AGC_INGAIN_TGT_MIN__A, ingainTgtMin);
3130 if (status < 0)
3131 goto error;
3132 status = write16(state, SCU_RAM_AGC_INGAIN_TGT_MAX__A, ingainTgtMax);
3133 if (status < 0)
3134 goto error;
3135 status = write16(state, SCU_RAM_AGC_IF_IACCU_HI_TGT_MIN__A, ifIaccuHiTgtMin);
3136 if (status < 0)
3137 goto error;
3138 status = write16(state, SCU_RAM_AGC_IF_IACCU_HI_TGT_MAX__A, ifIaccuHiTgtMax);
3139 if (status < 0)
3140 goto error;
3141 status = write16(state, SCU_RAM_AGC_IF_IACCU_HI__A, 0);
3142 if (status < 0)
3143 goto error;
3144 status = write16(state, SCU_RAM_AGC_IF_IACCU_LO__A, 0);
3145 if (status < 0)
3146 goto error;
3147 status = write16(state, SCU_RAM_AGC_RF_IACCU_HI__A, 0);
3148 if (status < 0)
3149 goto error;
3150 status = write16(state, SCU_RAM_AGC_RF_IACCU_LO__A, 0);
3151 if (status < 0)
3152 goto error;
3153 status = write16(state, SCU_RAM_AGC_CLP_SUM_MAX__A, clpSumMax);
3154 if (status < 0)
3155 goto error;
3156 status = write16(state, SCU_RAM_AGC_SNS_SUM_MAX__A, snsSumMax);
3157 if (status < 0)
3158 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003159
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003160 status = write16(state, SCU_RAM_AGC_KI_INNERGAIN_MIN__A, kiInnergainMin);
3161 if (status < 0)
3162 goto error;
3163 status = write16(state, SCU_RAM_AGC_IF_IACCU_HI_TGT__A, ifIaccuHiTgt);
3164 if (status < 0)
3165 goto error;
3166 status = write16(state, SCU_RAM_AGC_CLP_CYCLEN__A, clpCyclen);
3167 if (status < 0)
3168 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003169
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003170 status = write16(state, SCU_RAM_AGC_RF_SNS_DEV_MAX__A, 1023);
3171 if (status < 0)
3172 goto error;
3173 status = write16(state, SCU_RAM_AGC_RF_SNS_DEV_MIN__A, (u16) -1023);
3174 if (status < 0)
3175 goto error;
3176 status = write16(state, SCU_RAM_AGC_FAST_SNS_CTRL_DELAY__A, 50);
3177 if (status < 0)
3178 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003179
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003180 status = write16(state, SCU_RAM_AGC_KI_MAXMINGAIN_TH__A, 20);
3181 if (status < 0)
3182 goto error;
3183 status = write16(state, SCU_RAM_AGC_CLP_SUM_MIN__A, clpSumMin);
3184 if (status < 0)
3185 goto error;
3186 status = write16(state, SCU_RAM_AGC_SNS_SUM_MIN__A, snsSumMin);
3187 if (status < 0)
3188 goto error;
3189 status = write16(state, SCU_RAM_AGC_CLP_DIR_TO__A, clpDirTo);
3190 if (status < 0)
3191 goto error;
3192 status = write16(state, SCU_RAM_AGC_SNS_DIR_TO__A, snsDirTo);
3193 if (status < 0)
3194 goto error;
3195 status = write16(state, SCU_RAM_AGC_KI_MINGAIN__A, 0x7fff);
3196 if (status < 0)
3197 goto error;
3198 status = write16(state, SCU_RAM_AGC_KI_MAXGAIN__A, 0x0);
3199 if (status < 0)
3200 goto error;
3201 status = write16(state, SCU_RAM_AGC_KI_MIN__A, 0x0117);
3202 if (status < 0)
3203 goto error;
3204 status = write16(state, SCU_RAM_AGC_KI_MAX__A, 0x0657);
3205 if (status < 0)
3206 goto error;
3207 status = write16(state, SCU_RAM_AGC_CLP_SUM__A, 0);
3208 if (status < 0)
3209 goto error;
3210 status = write16(state, SCU_RAM_AGC_CLP_CYCCNT__A, 0);
3211 if (status < 0)
3212 goto error;
3213 status = write16(state, SCU_RAM_AGC_CLP_DIR_WD__A, 0);
3214 if (status < 0)
3215 goto error;
3216 status = write16(state, SCU_RAM_AGC_CLP_DIR_STP__A, 1);
3217 if (status < 0)
3218 goto error;
3219 status = write16(state, SCU_RAM_AGC_SNS_SUM__A, 0);
3220 if (status < 0)
3221 goto error;
3222 status = write16(state, SCU_RAM_AGC_SNS_CYCCNT__A, 0);
3223 if (status < 0)
3224 goto error;
3225 status = write16(state, SCU_RAM_AGC_SNS_DIR_WD__A, 0);
3226 if (status < 0)
3227 goto error;
3228 status = write16(state, SCU_RAM_AGC_SNS_DIR_STP__A, 1);
3229 if (status < 0)
3230 goto error;
3231 status = write16(state, SCU_RAM_AGC_SNS_CYCLEN__A, 500);
3232 if (status < 0)
3233 goto error;
3234 status = write16(state, SCU_RAM_AGC_KI_CYCLEN__A, 500);
3235 if (status < 0)
3236 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003237
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003238 /* Initialize inner-loop KI gain factors */
3239 status = read16(state, SCU_RAM_AGC_KI__A, &data);
3240 if (status < 0)
3241 goto error;
3242 if (IsQAM(state)) {
3243 data = 0x0657;
3244 data &= ~SCU_RAM_AGC_KI_RF__M;
3245 data |= (DRXK_KI_RAGC_QAM << SCU_RAM_AGC_KI_RF__B);
3246 data &= ~SCU_RAM_AGC_KI_IF__M;
3247 data |= (DRXK_KI_IAGC_QAM << SCU_RAM_AGC_KI_IF__B);
3248 }
3249 status = write16(state, SCU_RAM_AGC_KI__A, data);
3250error:
3251 if (status < 0)
3252 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003253 return status;
3254}
3255
Oliver Endrissebc7de22011-07-03 13:49:44 -03003256static int DVBTQAMGetAccPktErr(struct drxk_state *state, u16 *packetErr)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003257{
3258 int status;
3259
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03003260 dprintk(1, "\n");
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003261 if (packetErr == NULL)
3262 status = write16(state, SCU_RAM_FEC_ACCUM_PKT_FAILURES__A, 0);
3263 else
3264 status = read16(state, SCU_RAM_FEC_ACCUM_PKT_FAILURES__A, packetErr);
3265 if (status < 0)
3266 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003267 return status;
3268}
3269
3270static int DVBTScCommand(struct drxk_state *state,
3271 u16 cmd, u16 subcmd,
3272 u16 param0, u16 param1, u16 param2,
3273 u16 param3, u16 param4)
3274{
Oliver Endrissebc7de22011-07-03 13:49:44 -03003275 u16 curCmd = 0;
3276 u16 errCode = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003277 u16 retryCnt = 0;
Oliver Endrissebc7de22011-07-03 13:49:44 -03003278 u16 scExec = 0;
3279 int status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003280
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03003281 dprintk(1, "\n");
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -03003282 status = read16(state, OFDM_SC_COMM_EXEC__A, &scExec);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003283 if (scExec != 1) {
3284 /* SC is not running */
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003285 status = -EINVAL;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003286 }
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003287 if (status < 0)
3288 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003289
3290 /* Wait until sc is ready to receive command */
Oliver Endrissebc7de22011-07-03 13:49:44 -03003291 retryCnt = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003292 do {
3293 msleep(1);
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -03003294 status = read16(state, OFDM_SC_RA_RAM_CMD__A, &curCmd);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003295 retryCnt++;
3296 } while ((curCmd != 0) && (retryCnt < DRXK_MAX_RETRIES));
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003297 if (retryCnt >= DRXK_MAX_RETRIES && (status < 0))
3298 goto error;
3299
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003300 /* Write sub-command */
3301 switch (cmd) {
3302 /* All commands using sub-cmd */
3303 case OFDM_SC_RA_RAM_CMD_PROC_START:
3304 case OFDM_SC_RA_RAM_CMD_SET_PREF_PARAM:
3305 case OFDM_SC_RA_RAM_CMD_PROGRAM_PARAM:
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003306 status = write16(state, OFDM_SC_RA_RAM_CMD_ADDR__A, subcmd);
3307 if (status < 0)
3308 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003309 break;
3310 default:
3311 /* Do nothing */
3312 break;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003313 }
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003314
3315 /* Write needed parameters and the command */
3316 switch (cmd) {
3317 /* All commands using 5 parameters */
3318 /* All commands using 4 parameters */
3319 /* All commands using 3 parameters */
3320 /* All commands using 2 parameters */
3321 case OFDM_SC_RA_RAM_CMD_PROC_START:
3322 case OFDM_SC_RA_RAM_CMD_SET_PREF_PARAM:
3323 case OFDM_SC_RA_RAM_CMD_PROGRAM_PARAM:
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003324 status = write16(state, OFDM_SC_RA_RAM_PARAM1__A, param1);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003325 /* All commands using 1 parameters */
3326 case OFDM_SC_RA_RAM_CMD_SET_ECHO_TIMING:
3327 case OFDM_SC_RA_RAM_CMD_USER_IO:
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003328 status = write16(state, OFDM_SC_RA_RAM_PARAM0__A, param0);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003329 /* All commands using 0 parameters */
3330 case OFDM_SC_RA_RAM_CMD_GET_OP_PARAM:
3331 case OFDM_SC_RA_RAM_CMD_NULL:
3332 /* Write command */
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -03003333 status = write16(state, OFDM_SC_RA_RAM_CMD__A, cmd);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003334 break;
3335 default:
3336 /* Unknown command */
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003337 status = -EINVAL;
3338 }
3339 if (status < 0)
3340 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003341
3342 /* Wait until sc is ready processing command */
3343 retryCnt = 0;
Oliver Endrissebc7de22011-07-03 13:49:44 -03003344 do {
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003345 msleep(1);
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -03003346 status = read16(state, OFDM_SC_RA_RAM_CMD__A, &curCmd);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003347 retryCnt++;
Oliver Endrissebc7de22011-07-03 13:49:44 -03003348 } while ((curCmd != 0) && (retryCnt < DRXK_MAX_RETRIES));
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003349 if (retryCnt >= DRXK_MAX_RETRIES && (status < 0))
3350 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003351
3352 /* Check for illegal cmd */
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -03003353 status = read16(state, OFDM_SC_RA_RAM_CMD_ADDR__A, &errCode);
Oliver Endrissebc7de22011-07-03 13:49:44 -03003354 if (errCode == 0xFFFF) {
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003355 /* illegal command */
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003356 status = -EINVAL;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003357 }
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003358 if (status < 0)
3359 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003360
3361 /* Retreive results parameters from SC */
3362 switch (cmd) {
3363 /* All commands yielding 5 results */
3364 /* All commands yielding 4 results */
3365 /* All commands yielding 3 results */
3366 /* All commands yielding 2 results */
3367 /* All commands yielding 1 result */
3368 case OFDM_SC_RA_RAM_CMD_USER_IO:
3369 case OFDM_SC_RA_RAM_CMD_GET_OP_PARAM:
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003370 status = read16(state, OFDM_SC_RA_RAM_PARAM0__A, &(param0));
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003371 /* All commands yielding 0 results */
3372 case OFDM_SC_RA_RAM_CMD_SET_ECHO_TIMING:
3373 case OFDM_SC_RA_RAM_CMD_SET_TIMER:
3374 case OFDM_SC_RA_RAM_CMD_PROC_START:
3375 case OFDM_SC_RA_RAM_CMD_SET_PREF_PARAM:
3376 case OFDM_SC_RA_RAM_CMD_PROGRAM_PARAM:
3377 case OFDM_SC_RA_RAM_CMD_NULL:
3378 break;
3379 default:
3380 /* Unknown command */
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003381 status = -EINVAL;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003382 break;
Oliver Endrissebc7de22011-07-03 13:49:44 -03003383 } /* switch (cmd->cmd) */
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003384error:
3385 if (status < 0)
3386 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003387 return status;
3388}
3389
Oliver Endrissebc7de22011-07-03 13:49:44 -03003390static int PowerUpDVBT(struct drxk_state *state)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003391{
Oliver Endrissebc7de22011-07-03 13:49:44 -03003392 enum DRXPowerMode powerMode = DRX_POWER_UP;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003393 int status;
3394
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03003395 dprintk(1, "\n");
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003396 status = CtrlPowerMode(state, &powerMode);
3397 if (status < 0)
3398 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003399 return status;
3400}
3401
Oliver Endrissebc7de22011-07-03 13:49:44 -03003402static int DVBTCtrlSetIncEnable(struct drxk_state *state, bool *enabled)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003403{
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003404 int status;
Oliver Endrissebc7de22011-07-03 13:49:44 -03003405
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03003406 dprintk(1, "\n");
Oliver Endrissebc7de22011-07-03 13:49:44 -03003407 if (*enabled == true)
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -03003408 status = write16(state, IQM_CF_BYPASSDET__A, 0);
Oliver Endrissebc7de22011-07-03 13:49:44 -03003409 else
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -03003410 status = write16(state, IQM_CF_BYPASSDET__A, 1);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003411 if (status < 0)
3412 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
Oliver Endrissebc7de22011-07-03 13:49:44 -03003413 return status;
3414}
3415
3416#define DEFAULT_FR_THRES_8K 4000
3417static int DVBTCtrlSetFrEnable(struct drxk_state *state, bool *enabled)
3418{
3419
3420 int status;
3421
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03003422 dprintk(1, "\n");
Oliver Endrissebc7de22011-07-03 13:49:44 -03003423 if (*enabled == true) {
3424 /* write mask to 1 */
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -03003425 status = write16(state, OFDM_SC_RA_RAM_FR_THRES_8K__A,
Oliver Endrissebc7de22011-07-03 13:49:44 -03003426 DEFAULT_FR_THRES_8K);
3427 } else {
3428 /* write mask to 0 */
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -03003429 status = write16(state, OFDM_SC_RA_RAM_FR_THRES_8K__A, 0);
Oliver Endrissebc7de22011-07-03 13:49:44 -03003430 }
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003431 if (status < 0)
3432 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
Oliver Endrissebc7de22011-07-03 13:49:44 -03003433
3434 return status;
3435}
3436
3437static int DVBTCtrlSetEchoThreshold(struct drxk_state *state,
3438 struct DRXKCfgDvbtEchoThres_t *echoThres)
3439{
3440 u16 data = 0;
3441 int status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003442
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03003443 dprintk(1, "\n");
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003444 status = read16(state, OFDM_SC_RA_RAM_ECHO_THRES__A, &data);
3445 if (status < 0)
3446 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003447
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003448 switch (echoThres->fftMode) {
3449 case DRX_FFTMODE_2K:
3450 data &= ~OFDM_SC_RA_RAM_ECHO_THRES_2K__M;
3451 data |= ((echoThres->threshold <<
3452 OFDM_SC_RA_RAM_ECHO_THRES_2K__B)
3453 & (OFDM_SC_RA_RAM_ECHO_THRES_2K__M));
3454 goto error;
3455 case DRX_FFTMODE_8K:
3456 data &= ~OFDM_SC_RA_RAM_ECHO_THRES_8K__M;
3457 data |= ((echoThres->threshold <<
3458 OFDM_SC_RA_RAM_ECHO_THRES_8K__B)
3459 & (OFDM_SC_RA_RAM_ECHO_THRES_8K__M));
3460 goto error;
3461 default:
3462 return -EINVAL;
3463 goto error;
3464 }
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003465
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003466 status = write16(state, OFDM_SC_RA_RAM_ECHO_THRES__A, data);
3467error:
3468 if (status < 0)
3469 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
Oliver Endrissebc7de22011-07-03 13:49:44 -03003470 return status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003471}
3472
3473static int DVBTCtrlSetSqiSpeed(struct drxk_state *state,
Oliver Endrissebc7de22011-07-03 13:49:44 -03003474 enum DRXKCfgDvbtSqiSpeed *speed)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003475{
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003476 int status = -EINVAL;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003477
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03003478 dprintk(1, "\n");
3479
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003480 switch (*speed) {
3481 case DRXK_DVBT_SQI_SPEED_FAST:
3482 case DRXK_DVBT_SQI_SPEED_MEDIUM:
3483 case DRXK_DVBT_SQI_SPEED_SLOW:
3484 break;
3485 default:
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003486 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003487 }
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -03003488 status = write16(state, SCU_RAM_FEC_PRE_RS_BER_FILTER_SH__A,
Oliver Endrissebc7de22011-07-03 13:49:44 -03003489 (u16) *speed);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003490error:
3491 if (status < 0)
3492 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003493 return status;
3494}
3495
3496/*============================================================================*/
3497
3498/**
3499* \brief Activate DVBT specific presets
3500* \param demod instance of demodulator.
3501* \return DRXStatus_t.
3502*
3503* Called in DVBTSetStandard
3504*
3505*/
Oliver Endrissebc7de22011-07-03 13:49:44 -03003506static int DVBTActivatePresets(struct drxk_state *state)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003507{
Oliver Endrissebc7de22011-07-03 13:49:44 -03003508 int status;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003509 bool setincenable = false;
3510 bool setfrenable = true;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003511
Oliver Endrissebc7de22011-07-03 13:49:44 -03003512 struct DRXKCfgDvbtEchoThres_t echoThres2k = { 0, DRX_FFTMODE_2K };
3513 struct DRXKCfgDvbtEchoThres_t echoThres8k = { 0, DRX_FFTMODE_8K };
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003514
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03003515 dprintk(1, "\n");
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003516 status = DVBTCtrlSetIncEnable(state, &setincenable);
3517 if (status < 0)
3518 goto error;
3519 status = DVBTCtrlSetFrEnable(state, &setfrenable);
3520 if (status < 0)
3521 goto error;
3522 status = DVBTCtrlSetEchoThreshold(state, &echoThres2k);
3523 if (status < 0)
3524 goto error;
3525 status = DVBTCtrlSetEchoThreshold(state, &echoThres8k);
3526 if (status < 0)
3527 goto error;
3528 status = write16(state, SCU_RAM_AGC_INGAIN_TGT_MAX__A, state->m_dvbtIfAgcCfg.IngainTgtMax);
3529error:
3530 if (status < 0)
3531 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
Oliver Endrissebc7de22011-07-03 13:49:44 -03003532 return status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003533}
Oliver Endrissebc7de22011-07-03 13:49:44 -03003534
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003535/*============================================================================*/
3536
3537/**
3538* \brief Initialize channelswitch-independent settings for DVBT.
3539* \param demod instance of demodulator.
3540* \return DRXStatus_t.
3541*
3542* For ROM code channel filter taps are loaded from the bootloader. For microcode
3543* the DVB-T taps from the drxk_filters.h are used.
3544*/
Oliver Endrissebc7de22011-07-03 13:49:44 -03003545static int SetDVBTStandard(struct drxk_state *state,
3546 enum OperationMode oMode)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003547{
Oliver Endrissebc7de22011-07-03 13:49:44 -03003548 u16 cmdResult = 0;
3549 u16 data = 0;
3550 int status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003551
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03003552 dprintk(1, "\n");
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003553
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03003554 PowerUpDVBT(state);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003555 /* added antenna switch */
3556 SwitchAntennaToDVBT(state);
3557 /* send OFDM reset command */
3558 status = scu_command(state, SCU_RAM_COMMAND_STANDARD_OFDM | SCU_RAM_COMMAND_CMD_DEMOD_RESET, 0, NULL, 1, &cmdResult);
Oliver Endrissebc7de22011-07-03 13:49:44 -03003559 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003560 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003561
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003562 /* send OFDM setenv command */
3563 status = scu_command(state, SCU_RAM_COMMAND_STANDARD_OFDM | SCU_RAM_COMMAND_CMD_DEMOD_SET_ENV, 0, NULL, 1, &cmdResult);
3564 if (status < 0)
3565 goto error;
3566
3567 /* reset datapath for OFDM, processors first */
3568 status = write16(state, OFDM_SC_COMM_EXEC__A, OFDM_SC_COMM_EXEC_STOP);
3569 if (status < 0)
3570 goto error;
3571 status = write16(state, OFDM_LC_COMM_EXEC__A, OFDM_LC_COMM_EXEC_STOP);
3572 if (status < 0)
3573 goto error;
3574 status = write16(state, IQM_COMM_EXEC__A, IQM_COMM_EXEC_B_STOP);
3575 if (status < 0)
3576 goto error;
3577
3578 /* IQM setup */
3579 /* synchronize on ofdstate->m_festart */
3580 status = write16(state, IQM_AF_UPD_SEL__A, 1);
3581 if (status < 0)
3582 goto error;
3583 /* window size for clipping ADC detection */
3584 status = write16(state, IQM_AF_CLP_LEN__A, 0);
3585 if (status < 0)
3586 goto error;
3587 /* window size for for sense pre-SAW detection */
3588 status = write16(state, IQM_AF_SNS_LEN__A, 0);
3589 if (status < 0)
3590 goto error;
3591 /* sense threshold for sense pre-SAW detection */
3592 status = write16(state, IQM_AF_AMUX__A, IQM_AF_AMUX_SIGNAL2ADC);
3593 if (status < 0)
3594 goto error;
3595 status = SetIqmAf(state, true);
3596 if (status < 0)
3597 goto error;
3598
3599 status = write16(state, IQM_AF_AGC_RF__A, 0);
3600 if (status < 0)
3601 goto error;
3602
3603 /* Impulse noise cruncher setup */
3604 status = write16(state, IQM_AF_INC_LCT__A, 0); /* crunch in IQM_CF */
3605 if (status < 0)
3606 goto error;
3607 status = write16(state, IQM_CF_DET_LCT__A, 0); /* detect in IQM_CF */
3608 if (status < 0)
3609 goto error;
3610 status = write16(state, IQM_CF_WND_LEN__A, 3); /* peak detector window length */
3611 if (status < 0)
3612 goto error;
3613
3614 status = write16(state, IQM_RC_STRETCH__A, 16);
3615 if (status < 0)
3616 goto error;
3617 status = write16(state, IQM_CF_OUT_ENA__A, 0x4); /* enable output 2 */
3618 if (status < 0)
3619 goto error;
3620 status = write16(state, IQM_CF_DS_ENA__A, 0x4); /* decimate output 2 */
3621 if (status < 0)
3622 goto error;
3623 status = write16(state, IQM_CF_SCALE__A, 1600);
3624 if (status < 0)
3625 goto error;
3626 status = write16(state, IQM_CF_SCALE_SH__A, 0);
3627 if (status < 0)
3628 goto error;
3629
3630 /* virtual clipping threshold for clipping ADC detection */
3631 status = write16(state, IQM_AF_CLP_TH__A, 448);
3632 if (status < 0)
3633 goto error;
3634 status = write16(state, IQM_CF_DATATH__A, 495); /* crunching threshold */
3635 if (status < 0)
3636 goto error;
3637
3638 status = BLChainCmd(state, DRXK_BL_ROM_OFFSET_TAPS_DVBT, DRXK_BLCC_NR_ELEMENTS_TAPS, DRXK_BLC_TIMEOUT);
3639 if (status < 0)
3640 goto error;
3641
3642 status = write16(state, IQM_CF_PKDTH__A, 2); /* peak detector threshold */
3643 if (status < 0)
3644 goto error;
3645 status = write16(state, IQM_CF_POW_MEAS_LEN__A, 2);
3646 if (status < 0)
3647 goto error;
3648 /* enable power measurement interrupt */
3649 status = write16(state, IQM_CF_COMM_INT_MSK__A, 1);
3650 if (status < 0)
3651 goto error;
3652 status = write16(state, IQM_COMM_EXEC__A, IQM_COMM_EXEC_B_ACTIVE);
3653 if (status < 0)
3654 goto error;
3655
3656 /* IQM will not be reset from here, sync ADC and update/init AGC */
3657 status = ADCSynchronization(state);
3658 if (status < 0)
3659 goto error;
3660 status = SetPreSaw(state, &state->m_dvbtPreSawCfg);
3661 if (status < 0)
3662 goto error;
3663
3664 /* Halt SCU to enable safe non-atomic accesses */
3665 status = write16(state, SCU_COMM_EXEC__A, SCU_COMM_EXEC_HOLD);
3666 if (status < 0)
3667 goto error;
3668
3669 status = SetAgcRf(state, &state->m_dvbtRfAgcCfg, true);
3670 if (status < 0)
3671 goto error;
3672 status = SetAgcIf(state, &state->m_dvbtIfAgcCfg, true);
3673 if (status < 0)
3674 goto error;
3675
3676 /* Set Noise Estimation notch width and enable DC fix */
3677 status = read16(state, OFDM_SC_RA_RAM_CONFIG__A, &data);
3678 if (status < 0)
3679 goto error;
3680 data |= OFDM_SC_RA_RAM_CONFIG_NE_FIX_ENABLE__M;
3681 status = write16(state, OFDM_SC_RA_RAM_CONFIG__A, data);
3682 if (status < 0)
3683 goto error;
3684
3685 /* Activate SCU to enable SCU commands */
3686 status = write16(state, SCU_COMM_EXEC__A, SCU_COMM_EXEC_ACTIVE);
3687 if (status < 0)
3688 goto error;
3689
3690 if (!state->m_DRXK_A3_ROM_CODE) {
3691 /* AGCInit() is not done for DVBT, so set agcFastClipCtrlDelay */
3692 status = write16(state, SCU_RAM_AGC_FAST_CLP_CTRL_DELAY__A, state->m_dvbtIfAgcCfg.FastClipCtrlDelay);
3693 if (status < 0)
3694 goto error;
3695 }
3696
3697 /* OFDM_SC setup */
3698#ifdef COMPILE_FOR_NONRT
3699 status = write16(state, OFDM_SC_RA_RAM_BE_OPT_DELAY__A, 1);
3700 if (status < 0)
3701 goto error;
3702 status = write16(state, OFDM_SC_RA_RAM_BE_OPT_INIT_DELAY__A, 2);
3703 if (status < 0)
3704 goto error;
3705#endif
3706
3707 /* FEC setup */
3708 status = write16(state, FEC_DI_INPUT_CTL__A, 1); /* OFDM input */
3709 if (status < 0)
3710 goto error;
3711
3712
3713#ifdef COMPILE_FOR_NONRT
3714 status = write16(state, FEC_RS_MEASUREMENT_PERIOD__A, 0x400);
3715 if (status < 0)
3716 goto error;
3717#else
3718 status = write16(state, FEC_RS_MEASUREMENT_PERIOD__A, 0x1000);
3719 if (status < 0)
3720 goto error;
3721#endif
3722 status = write16(state, FEC_RS_MEASUREMENT_PRESCALE__A, 0x0001);
3723 if (status < 0)
3724 goto error;
3725
3726 /* Setup MPEG bus */
3727 status = MPEGTSDtoSetup(state, OM_DVBT);
3728 if (status < 0)
3729 goto error;
3730 /* Set DVBT Presets */
3731 status = DVBTActivatePresets(state);
3732 if (status < 0)
3733 goto error;
3734
3735error:
3736 if (status < 0)
3737 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003738 return status;
3739}
3740
3741/*============================================================================*/
3742/**
3743* \brief Start dvbt demodulating for channel.
3744* \param demod instance of demodulator.
3745* \return DRXStatus_t.
3746*/
3747static int DVBTStart(struct drxk_state *state)
3748{
Oliver Endrissebc7de22011-07-03 13:49:44 -03003749 u16 param1;
3750 int status;
3751 /* DRXKOfdmScCmd_t scCmd; */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003752
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03003753 dprintk(1, "\n");
Oliver Endrissebc7de22011-07-03 13:49:44 -03003754 /* Start correct processes to get in lock */
3755 /* DRXK: OFDM_SC_RA_RAM_PROC_LOCKTRACK is no longer in mapfile! */
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003756 param1 = OFDM_SC_RA_RAM_LOCKTRACK_MIN;
3757 status = DVBTScCommand(state, OFDM_SC_RA_RAM_CMD_PROC_START, 0, OFDM_SC_RA_RAM_SW_EVENT_RUN_NMASK__M, param1, 0, 0, 0);
3758 if (status < 0)
3759 goto error;
3760 /* Start FEC OC */
3761 status = MPEGTSStart(state);
3762 if (status < 0)
3763 goto error;
3764 status = write16(state, FEC_COMM_EXEC__A, FEC_COMM_EXEC_ACTIVE);
3765 if (status < 0)
3766 goto error;
3767error:
3768 if (status < 0)
3769 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
Oliver Endrissebc7de22011-07-03 13:49:44 -03003770 return status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003771}
3772
3773
3774/*============================================================================*/
3775
3776/**
3777* \brief Set up dvbt demodulator for channel.
3778* \param demod instance of demodulator.
3779* \return DRXStatus_t.
3780* // original DVBTSetChannel()
3781*/
Oliver Endrissebc7de22011-07-03 13:49:44 -03003782static int SetDVBT(struct drxk_state *state, u16 IntermediateFreqkHz,
3783 s32 tunerFreqOffset)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003784{
Oliver Endrissebc7de22011-07-03 13:49:44 -03003785 u16 cmdResult = 0;
3786 u16 transmissionParams = 0;
3787 u16 operationMode = 0;
3788 u32 iqmRcRateOfs = 0;
3789 u32 bandwidth = 0;
3790 u16 param1;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003791 int status;
3792
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003793 dprintk(1, "IF =%d, TFO = %d\n", IntermediateFreqkHz, tunerFreqOffset);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003794
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003795 status = scu_command(state, SCU_RAM_COMMAND_STANDARD_OFDM | SCU_RAM_COMMAND_CMD_DEMOD_STOP, 0, NULL, 1, &cmdResult);
3796 if (status < 0)
3797 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003798
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003799 /* Halt SCU to enable safe non-atomic accesses */
3800 status = write16(state, SCU_COMM_EXEC__A, SCU_COMM_EXEC_HOLD);
3801 if (status < 0)
3802 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003803
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003804 /* Stop processors */
3805 status = write16(state, OFDM_SC_COMM_EXEC__A, OFDM_SC_COMM_EXEC_STOP);
3806 if (status < 0)
3807 goto error;
3808 status = write16(state, OFDM_LC_COMM_EXEC__A, OFDM_LC_COMM_EXEC_STOP);
3809 if (status < 0)
3810 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003811
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003812 /* Mandatory fix, always stop CP, required to set spl offset back to
3813 hardware default (is set to 0 by ucode during pilot detection */
3814 status = write16(state, OFDM_CP_COMM_EXEC__A, OFDM_CP_COMM_EXEC_STOP);
3815 if (status < 0)
3816 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003817
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003818 /*== Write channel settings to device =====================================*/
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003819
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003820 /* mode */
3821 switch (state->param.u.ofdm.transmission_mode) {
3822 case TRANSMISSION_MODE_AUTO:
3823 default:
3824 operationMode |= OFDM_SC_RA_RAM_OP_AUTO_MODE__M;
3825 /* fall through , try first guess DRX_FFTMODE_8K */
3826 case TRANSMISSION_MODE_8K:
3827 transmissionParams |= OFDM_SC_RA_RAM_OP_PARAM_MODE_8K;
3828 goto error;
3829 case TRANSMISSION_MODE_2K:
3830 transmissionParams |= OFDM_SC_RA_RAM_OP_PARAM_MODE_2K;
3831 goto error;
3832 }
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003833
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003834 /* guard */
3835 switch (state->param.u.ofdm.guard_interval) {
3836 default:
3837 case GUARD_INTERVAL_AUTO:
3838 operationMode |= OFDM_SC_RA_RAM_OP_AUTO_GUARD__M;
3839 /* fall through , try first guess DRX_GUARD_1DIV4 */
3840 case GUARD_INTERVAL_1_4:
3841 transmissionParams |= OFDM_SC_RA_RAM_OP_PARAM_GUARD_4;
3842 goto error;
3843 case GUARD_INTERVAL_1_32:
3844 transmissionParams |= OFDM_SC_RA_RAM_OP_PARAM_GUARD_32;
3845 goto error;
3846 case GUARD_INTERVAL_1_16:
3847 transmissionParams |= OFDM_SC_RA_RAM_OP_PARAM_GUARD_16;
3848 goto error;
3849 case GUARD_INTERVAL_1_8:
3850 transmissionParams |= OFDM_SC_RA_RAM_OP_PARAM_GUARD_8;
3851 goto error;
3852 }
3853
3854 /* hierarchy */
3855 switch (state->param.u.ofdm.hierarchy_information) {
3856 case HIERARCHY_AUTO:
3857 case HIERARCHY_NONE:
3858 default:
3859 operationMode |= OFDM_SC_RA_RAM_OP_AUTO_HIER__M;
3860 /* fall through , try first guess SC_RA_RAM_OP_PARAM_HIER_NO */
3861 /* transmissionParams |= OFDM_SC_RA_RAM_OP_PARAM_HIER_NO; */
3862 /* break; */
3863 case HIERARCHY_1:
3864 transmissionParams |= OFDM_SC_RA_RAM_OP_PARAM_HIER_A1;
3865 break;
3866 case HIERARCHY_2:
3867 transmissionParams |= OFDM_SC_RA_RAM_OP_PARAM_HIER_A2;
3868 break;
3869 case HIERARCHY_4:
3870 transmissionParams |= OFDM_SC_RA_RAM_OP_PARAM_HIER_A4;
3871 break;
3872 }
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003873
3874
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003875 /* constellation */
3876 switch (state->param.u.ofdm.constellation) {
3877 case QAM_AUTO:
3878 default:
3879 operationMode |= OFDM_SC_RA_RAM_OP_AUTO_CONST__M;
3880 /* fall through , try first guess DRX_CONSTELLATION_QAM64 */
3881 case QAM_64:
3882 transmissionParams |= OFDM_SC_RA_RAM_OP_PARAM_CONST_QAM64;
3883 break;
3884 case QPSK:
3885 transmissionParams |= OFDM_SC_RA_RAM_OP_PARAM_CONST_QPSK;
3886 break;
3887 case QAM_16:
3888 transmissionParams |= OFDM_SC_RA_RAM_OP_PARAM_CONST_QAM16;
3889 break;
3890 }
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003891#if 0
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003892 /* No hierachical channels support in BDA */
3893 /* Priority (only for hierarchical channels) */
3894 switch (channel->priority) {
3895 case DRX_PRIORITY_LOW:
3896 transmissionParams |= OFDM_SC_RA_RAM_OP_PARAM_PRIO_LO;
3897 WR16(devAddr, OFDM_EC_SB_PRIOR__A,
3898 OFDM_EC_SB_PRIOR_LO);
3899 break;
3900 case DRX_PRIORITY_HIGH:
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003901 transmissionParams |= OFDM_SC_RA_RAM_OP_PARAM_PRIO_HI;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003902 WR16(devAddr, OFDM_EC_SB_PRIOR__A,
3903 OFDM_EC_SB_PRIOR_HI));
3904 break;
3905 case DRX_PRIORITY_UNKNOWN: /* fall through */
3906 default:
3907 status = -EINVAL;
3908 goto error;
3909 }
3910#else
3911 /* Set Priorty high */
3912 transmissionParams |= OFDM_SC_RA_RAM_OP_PARAM_PRIO_HI;
3913 status = write16(state, OFDM_EC_SB_PRIOR__A, OFDM_EC_SB_PRIOR_HI);
3914 if (status < 0)
3915 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003916#endif
3917
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003918 /* coderate */
3919 switch (state->param.u.ofdm.code_rate_HP) {
3920 case FEC_AUTO:
3921 default:
3922 operationMode |= OFDM_SC_RA_RAM_OP_AUTO_RATE__M;
3923 /* fall through , try first guess DRX_CODERATE_2DIV3 */
3924 case FEC_2_3:
3925 transmissionParams |= OFDM_SC_RA_RAM_OP_PARAM_RATE_2_3;
3926 break;
3927 case FEC_1_2:
3928 transmissionParams |= OFDM_SC_RA_RAM_OP_PARAM_RATE_1_2;
3929 break;
3930 case FEC_3_4:
3931 transmissionParams |= OFDM_SC_RA_RAM_OP_PARAM_RATE_3_4;
3932 break;
3933 case FEC_5_6:
3934 transmissionParams |= OFDM_SC_RA_RAM_OP_PARAM_RATE_5_6;
3935 break;
3936 case FEC_7_8:
3937 transmissionParams |= OFDM_SC_RA_RAM_OP_PARAM_RATE_7_8;
3938 break;
3939 }
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003940
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003941 /* SAW filter selection: normaly not necesarry, but if wanted
3942 the application can select a SAW filter via the driver by using UIOs */
3943 /* First determine real bandwidth (Hz) */
3944 /* Also set delay for impulse noise cruncher */
3945 /* Also set parameters for EC_OC fix, note EC_OC_REG_TMD_HIL_MAR is changed
3946 by SC for fix for some 8K,1/8 guard but is restored by InitEC and ResetEC
3947 functions */
3948 switch (state->param.u.ofdm.bandwidth) {
3949 case BANDWIDTH_AUTO:
3950 case BANDWIDTH_8_MHZ:
3951 bandwidth = DRXK_BANDWIDTH_8MHZ_IN_HZ;
3952 status = write16(state, OFDM_SC_RA_RAM_SRMM_FIX_FACT_8K__A, 3052);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03003953 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003954 goto error;
3955 /* cochannel protection for PAL 8 MHz */
3956 status = write16(state, OFDM_SC_RA_RAM_NI_INIT_8K_PER_LEFT__A, 7);
3957 if (status < 0)
3958 goto error;
3959 status = write16(state, OFDM_SC_RA_RAM_NI_INIT_8K_PER_RIGHT__A, 7);
3960 if (status < 0)
3961 goto error;
3962 status = write16(state, OFDM_SC_RA_RAM_NI_INIT_2K_PER_LEFT__A, 7);
3963 if (status < 0)
3964 goto error;
3965 status = write16(state, OFDM_SC_RA_RAM_NI_INIT_2K_PER_RIGHT__A, 1);
3966 if (status < 0)
3967 goto error;
3968 break;
3969 case BANDWIDTH_7_MHZ:
3970 bandwidth = DRXK_BANDWIDTH_7MHZ_IN_HZ;
3971 status = write16(state, OFDM_SC_RA_RAM_SRMM_FIX_FACT_8K__A, 3491);
3972 if (status < 0)
3973 goto error;
3974 /* cochannel protection for PAL 7 MHz */
3975 status = write16(state, OFDM_SC_RA_RAM_NI_INIT_8K_PER_LEFT__A, 8);
3976 if (status < 0)
3977 goto error;
3978 status = write16(state, OFDM_SC_RA_RAM_NI_INIT_8K_PER_RIGHT__A, 8);
3979 if (status < 0)
3980 goto error;
3981 status = write16(state, OFDM_SC_RA_RAM_NI_INIT_2K_PER_LEFT__A, 4);
3982 if (status < 0)
3983 goto error;
3984 status = write16(state, OFDM_SC_RA_RAM_NI_INIT_2K_PER_RIGHT__A, 1);
3985 if (status < 0)
3986 goto error;
3987 break;
3988 case BANDWIDTH_6_MHZ:
3989 bandwidth = DRXK_BANDWIDTH_6MHZ_IN_HZ;
3990 status = write16(state, OFDM_SC_RA_RAM_SRMM_FIX_FACT_8K__A, 4073);
3991 if (status < 0)
3992 goto error;
3993 /* cochannel protection for NTSC 6 MHz */
3994 status = write16(state, OFDM_SC_RA_RAM_NI_INIT_8K_PER_LEFT__A, 19);
3995 if (status < 0)
3996 goto error;
3997 status = write16(state, OFDM_SC_RA_RAM_NI_INIT_8K_PER_RIGHT__A, 19);
3998 if (status < 0)
3999 goto error;
4000 status = write16(state, OFDM_SC_RA_RAM_NI_INIT_2K_PER_LEFT__A, 14);
4001 if (status < 0)
4002 goto error;
4003 status = write16(state, OFDM_SC_RA_RAM_NI_INIT_2K_PER_RIGHT__A, 1);
4004 if (status < 0)
4005 goto error;
4006 break;
4007 default:
4008 status = -EINVAL;
4009 goto error;
4010 }
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004011
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004012 if (iqmRcRateOfs == 0) {
4013 /* Now compute IQM_RC_RATE_OFS
4014 (((SysFreq/BandWidth)/2)/2) -1) * 2^23)
4015 =>
4016 ((SysFreq / BandWidth) * (2^21)) - (2^23)
4017 */
4018 /* (SysFreq / BandWidth) * (2^28) */
4019 /* assert (MAX(sysClk)/MIN(bandwidth) < 16)
4020 => assert(MAX(sysClk) < 16*MIN(bandwidth))
4021 => assert(109714272 > 48000000) = true so Frac 28 can be used */
4022 iqmRcRateOfs = Frac28a((u32)
4023 ((state->m_sysClockFreq *
4024 1000) / 3), bandwidth);
4025 /* (SysFreq / BandWidth) * (2^21), rounding before truncating */
4026 if ((iqmRcRateOfs & 0x7fL) >= 0x40)
4027 iqmRcRateOfs += 0x80L;
4028 iqmRcRateOfs = iqmRcRateOfs >> 7;
4029 /* ((SysFreq / BandWidth) * (2^21)) - (2^23) */
4030 iqmRcRateOfs = iqmRcRateOfs - (1 << 23);
4031 }
4032
4033 iqmRcRateOfs &=
4034 ((((u32) IQM_RC_RATE_OFS_HI__M) <<
4035 IQM_RC_RATE_OFS_LO__W) | IQM_RC_RATE_OFS_LO__M);
4036 status = write32(state, IQM_RC_RATE_OFS_LO__A, iqmRcRateOfs);
4037 if (status < 0)
4038 goto error;
4039
4040 /* Bandwidth setting done */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004041
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03004042#if 0
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004043 status = DVBTSetFrequencyShift(demod, channel, tunerOffset);
4044 if (status < 0)
4045 goto error;
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03004046#endif
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004047 status = SetFrequencyShifter(state, IntermediateFreqkHz, tunerFreqOffset, true);
4048 if (status < 0)
4049 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004050
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004051 /*== Start SC, write channel settings to SC ===============================*/
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004052
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004053 /* Activate SCU to enable SCU commands */
4054 status = write16(state, SCU_COMM_EXEC__A, SCU_COMM_EXEC_ACTIVE);
4055 if (status < 0)
4056 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004057
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004058 /* Enable SC after setting all other parameters */
4059 status = write16(state, OFDM_SC_COMM_STATE__A, 0);
4060 if (status < 0)
4061 goto error;
4062 status = write16(state, OFDM_SC_COMM_EXEC__A, 1);
4063 if (status < 0)
4064 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004065
4066
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004067 status = scu_command(state, SCU_RAM_COMMAND_STANDARD_OFDM | SCU_RAM_COMMAND_CMD_DEMOD_START, 0, NULL, 1, &cmdResult);
4068 if (status < 0)
4069 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004070
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004071 /* Write SC parameter registers, set all AUTO flags in operation mode */
4072 param1 = (OFDM_SC_RA_RAM_OP_AUTO_MODE__M |
4073 OFDM_SC_RA_RAM_OP_AUTO_GUARD__M |
4074 OFDM_SC_RA_RAM_OP_AUTO_CONST__M |
4075 OFDM_SC_RA_RAM_OP_AUTO_HIER__M |
4076 OFDM_SC_RA_RAM_OP_AUTO_RATE__M);
4077 status = DVBTScCommand(state, OFDM_SC_RA_RAM_CMD_SET_PREF_PARAM,
4078 0, transmissionParams, param1, 0, 0, 0);
4079 if (status < 0)
4080 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004081
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004082 if (!state->m_DRXK_A3_ROM_CODE)
4083 status = DVBTCtrlSetSqiSpeed(state, &state->m_sqiSpeed);
4084error:
4085 if (status < 0)
4086 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004087
4088 return status;
4089}
4090
4091
4092/*============================================================================*/
4093
4094/**
4095* \brief Retreive lock status .
4096* \param demod Pointer to demodulator instance.
4097* \param lockStat Pointer to lock status structure.
4098* \return DRXStatus_t.
4099*
4100*/
4101static int GetDVBTLockStatus(struct drxk_state *state, u32 *pLockStatus)
4102{
Oliver Endrissebc7de22011-07-03 13:49:44 -03004103 int status;
4104 const u16 mpeg_lock_mask = (OFDM_SC_RA_RAM_LOCK_MPEG__M |
4105 OFDM_SC_RA_RAM_LOCK_FEC__M);
4106 const u16 fec_lock_mask = (OFDM_SC_RA_RAM_LOCK_FEC__M);
4107 const u16 demod_lock_mask = OFDM_SC_RA_RAM_LOCK_DEMOD__M;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004108
Oliver Endrissebc7de22011-07-03 13:49:44 -03004109 u16 ScRaRamLock = 0;
4110 u16 ScCommExec = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004111
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03004112 dprintk(1, "\n");
4113
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004114 *pLockStatus = NOT_LOCKED;
Oliver Endrissebc7de22011-07-03 13:49:44 -03004115 /* driver 0.9.0 */
4116 /* Check if SC is running */
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -03004117 status = read16(state, OFDM_SC_COMM_EXEC__A, &ScCommExec);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004118 if (status < 0)
4119 goto end;
4120 if (ScCommExec == OFDM_SC_COMM_EXEC_STOP)
4121 goto end;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004122
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -03004123 status = read16(state, OFDM_SC_RA_RAM_LOCK__A, &ScRaRamLock);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004124 if (status < 0)
4125 goto end;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004126
Oliver Endrissebc7de22011-07-03 13:49:44 -03004127 if ((ScRaRamLock & mpeg_lock_mask) == mpeg_lock_mask)
4128 *pLockStatus = MPEG_LOCK;
4129 else if ((ScRaRamLock & fec_lock_mask) == fec_lock_mask)
4130 *pLockStatus = FEC_LOCK;
4131 else if ((ScRaRamLock & demod_lock_mask) == demod_lock_mask)
4132 *pLockStatus = DEMOD_LOCK;
4133 else if (ScRaRamLock & OFDM_SC_RA_RAM_LOCK_NODVBT__M)
4134 *pLockStatus = NEVER_LOCK;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004135end:
4136 if (status < 0)
4137 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004138
Oliver Endrissebc7de22011-07-03 13:49:44 -03004139 return status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004140}
4141
Oliver Endrissebc7de22011-07-03 13:49:44 -03004142static int PowerUpQAM(struct drxk_state *state)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004143{
Oliver Endrissebc7de22011-07-03 13:49:44 -03004144 enum DRXPowerMode powerMode = DRXK_POWER_DOWN_OFDM;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004145 int status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004146
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03004147 dprintk(1, "\n");
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004148 status = CtrlPowerMode(state, &powerMode);
4149 if (status < 0)
4150 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004151
Oliver Endrissebc7de22011-07-03 13:49:44 -03004152 return status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004153}
4154
4155
Oliver Endrissebc7de22011-07-03 13:49:44 -03004156/** Power Down QAM */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004157static int PowerDownQAM(struct drxk_state *state)
4158{
Oliver Endrissebc7de22011-07-03 13:49:44 -03004159 u16 data = 0;
4160 u16 cmdResult;
4161 int status = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004162
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03004163 dprintk(1, "\n");
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004164 status = read16(state, SCU_COMM_EXEC__A, &data);
4165 if (status < 0)
4166 goto error;
4167 if (data == SCU_COMM_EXEC_ACTIVE) {
4168 /*
4169 STOP demodulator
4170 QAM and HW blocks
4171 */
4172 /* stop all comstate->m_exec */
4173 status = write16(state, QAM_COMM_EXEC__A, QAM_COMM_EXEC_STOP);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03004174 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004175 goto error;
4176 status = scu_command(state, SCU_RAM_COMMAND_STANDARD_QAM | SCU_RAM_COMMAND_CMD_DEMOD_STOP, 0, NULL, 1, &cmdResult);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03004177 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004178 goto error;
4179 }
4180 /* powerdown AFE */
4181 status = SetIqmAf(state, false);
4182
4183error:
4184 if (status < 0)
4185 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004186
Oliver Endrissebc7de22011-07-03 13:49:44 -03004187 return status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004188}
Oliver Endrissebc7de22011-07-03 13:49:44 -03004189
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004190/*============================================================================*/
4191
4192/**
4193* \brief Setup of the QAM Measurement intervals for signal quality
4194* \param demod instance of demod.
4195* \param constellation current constellation.
4196* \return DRXStatus_t.
4197*
4198* NOTE:
4199* Take into account that for certain settings the errorcounters can overflow.
4200* The implementation does not check this.
4201*
4202*/
4203static int SetQAMMeasurement(struct drxk_state *state,
4204 enum EDrxkConstellation constellation,
4205 u32 symbolRate)
4206{
Oliver Endrissebc7de22011-07-03 13:49:44 -03004207 u32 fecBitsDesired = 0; /* BER accounting period */
4208 u32 fecRsPeriodTotal = 0; /* Total period */
4209 u16 fecRsPrescale = 0; /* ReedSolomon Measurement Prescale */
4210 u16 fecRsPeriod = 0; /* Value for corresponding I2C register */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004211 int status = 0;
4212
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03004213 dprintk(1, "\n");
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004214
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03004215 fecRsPrescale = 1;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004216 /* fecBitsDesired = symbolRate [kHz] *
4217 FrameLenght [ms] *
4218 (constellation + 1) *
4219 SyncLoss (== 1) *
4220 ViterbiLoss (==1)
4221 */
4222 switch (constellation) {
4223 case DRX_CONSTELLATION_QAM16:
4224 fecBitsDesired = 4 * symbolRate;
4225 break;
4226 case DRX_CONSTELLATION_QAM32:
4227 fecBitsDesired = 5 * symbolRate;
4228 break;
4229 case DRX_CONSTELLATION_QAM64:
4230 fecBitsDesired = 6 * symbolRate;
4231 break;
4232 case DRX_CONSTELLATION_QAM128:
4233 fecBitsDesired = 7 * symbolRate;
4234 break;
4235 case DRX_CONSTELLATION_QAM256:
4236 fecBitsDesired = 8 * symbolRate;
4237 break;
4238 default:
4239 status = -EINVAL;
4240 }
Oliver Endrissebc7de22011-07-03 13:49:44 -03004241 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004242 goto error;
Oliver Endrissebc7de22011-07-03 13:49:44 -03004243
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004244 fecBitsDesired /= 1000; /* symbolRate [Hz] -> symbolRate [kHz] */
4245 fecBitsDesired *= 500; /* meas. period [ms] */
4246
4247 /* Annex A/C: bits/RsPeriod = 204 * 8 = 1632 */
4248 /* fecRsPeriodTotal = fecBitsDesired / 1632 */
4249 fecRsPeriodTotal = (fecBitsDesired / 1632UL) + 1; /* roughly ceil */
4250
4251 /* fecRsPeriodTotal = fecRsPrescale * fecRsPeriod */
4252 fecRsPrescale = 1 + (u16) (fecRsPeriodTotal >> 16);
4253 if (fecRsPrescale == 0) {
4254 /* Divide by zero (though impossible) */
4255 status = -EINVAL;
4256 if (status < 0)
4257 goto error;
4258 }
4259 fecRsPeriod =
4260 ((u16) fecRsPeriodTotal +
4261 (fecRsPrescale >> 1)) / fecRsPrescale;
4262
4263 /* write corresponding registers */
4264 status = write16(state, FEC_RS_MEASUREMENT_PERIOD__A, fecRsPeriod);
4265 if (status < 0)
4266 goto error;
4267 status = write16(state, FEC_RS_MEASUREMENT_PRESCALE__A, fecRsPrescale);
4268 if (status < 0)
4269 goto error;
4270 status = write16(state, FEC_OC_SNC_FAIL_PERIOD__A, fecRsPeriod);
4271error:
4272 if (status < 0)
4273 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004274 return status;
4275}
4276
Oliver Endrissebc7de22011-07-03 13:49:44 -03004277static int SetQAM16(struct drxk_state *state)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004278{
Oliver Endrissebc7de22011-07-03 13:49:44 -03004279 int status = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004280
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03004281 dprintk(1, "\n");
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004282 /* QAM Equalizer Setup */
4283 /* Equalizer */
4284 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD0__A, 13517);
4285 if (status < 0)
4286 goto error;
4287 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD1__A, 13517);
4288 if (status < 0)
4289 goto error;
4290 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD2__A, 13517);
4291 if (status < 0)
4292 goto error;
4293 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD3__A, 13517);
4294 if (status < 0)
4295 goto error;
4296 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD4__A, 13517);
4297 if (status < 0)
4298 goto error;
4299 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD5__A, 13517);
4300 if (status < 0)
4301 goto error;
4302 /* Decision Feedback Equalizer */
4303 status = write16(state, QAM_DQ_QUAL_FUN0__A, 2);
4304 if (status < 0)
4305 goto error;
4306 status = write16(state, QAM_DQ_QUAL_FUN1__A, 2);
4307 if (status < 0)
4308 goto error;
4309 status = write16(state, QAM_DQ_QUAL_FUN2__A, 2);
4310 if (status < 0)
4311 goto error;
4312 status = write16(state, QAM_DQ_QUAL_FUN3__A, 2);
4313 if (status < 0)
4314 goto error;
4315 status = write16(state, QAM_DQ_QUAL_FUN4__A, 2);
4316 if (status < 0)
4317 goto error;
4318 status = write16(state, QAM_DQ_QUAL_FUN5__A, 0);
4319 if (status < 0)
4320 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004321
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004322 status = write16(state, QAM_SY_SYNC_HWM__A, 5);
4323 if (status < 0)
4324 goto error;
4325 status = write16(state, QAM_SY_SYNC_AWM__A, 4);
4326 if (status < 0)
4327 goto error;
4328 status = write16(state, QAM_SY_SYNC_LWM__A, 3);
4329 if (status < 0)
4330 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004331
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004332 /* QAM Slicer Settings */
4333 status = write16(state, SCU_RAM_QAM_SL_SIG_POWER__A, DRXK_QAM_SL_SIG_POWER_QAM16);
4334 if (status < 0)
4335 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004336
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004337 /* QAM Loop Controller Coeficients */
4338 status = write16(state, SCU_RAM_QAM_LC_CA_FINE__A, 15);
4339 if (status < 0)
4340 goto error;
4341 status = write16(state, SCU_RAM_QAM_LC_CA_COARSE__A, 40);
4342 if (status < 0)
4343 goto error;
4344 status = write16(state, SCU_RAM_QAM_LC_EP_FINE__A, 12);
4345 if (status < 0)
4346 goto error;
4347 status = write16(state, SCU_RAM_QAM_LC_EP_MEDIUM__A, 24);
4348 if (status < 0)
4349 goto error;
4350 status = write16(state, SCU_RAM_QAM_LC_EP_COARSE__A, 24);
4351 if (status < 0)
4352 goto error;
4353 status = write16(state, SCU_RAM_QAM_LC_EI_FINE__A, 12);
4354 if (status < 0)
4355 goto error;
4356 status = write16(state, SCU_RAM_QAM_LC_EI_MEDIUM__A, 16);
4357 if (status < 0)
4358 goto error;
4359 status = write16(state, SCU_RAM_QAM_LC_EI_COARSE__A, 16);
4360 if (status < 0)
4361 goto error;
Oliver Endrissebc7de22011-07-03 13:49:44 -03004362
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004363 status = write16(state, SCU_RAM_QAM_LC_CP_FINE__A, 5);
4364 if (status < 0)
4365 goto error;
4366 status = write16(state, SCU_RAM_QAM_LC_CP_MEDIUM__A, 20);
4367 if (status < 0)
4368 goto error;
4369 status = write16(state, SCU_RAM_QAM_LC_CP_COARSE__A, 80);
4370 if (status < 0)
4371 goto error;
4372 status = write16(state, SCU_RAM_QAM_LC_CI_FINE__A, 5);
4373 if (status < 0)
4374 goto error;
4375 status = write16(state, SCU_RAM_QAM_LC_CI_MEDIUM__A, 20);
4376 if (status < 0)
4377 goto error;
4378 status = write16(state, SCU_RAM_QAM_LC_CI_COARSE__A, 50);
4379 if (status < 0)
4380 goto error;
4381 status = write16(state, SCU_RAM_QAM_LC_CF_FINE__A, 16);
4382 if (status < 0)
4383 goto error;
4384 status = write16(state, SCU_RAM_QAM_LC_CF_MEDIUM__A, 16);
4385 if (status < 0)
4386 goto error;
4387 status = write16(state, SCU_RAM_QAM_LC_CF_COARSE__A, 32);
4388 if (status < 0)
4389 goto error;
4390 status = write16(state, SCU_RAM_QAM_LC_CF1_FINE__A, 5);
4391 if (status < 0)
4392 goto error;
4393 status = write16(state, SCU_RAM_QAM_LC_CF1_MEDIUM__A, 10);
4394 if (status < 0)
4395 goto error;
4396 status = write16(state, SCU_RAM_QAM_LC_CF1_COARSE__A, 10);
4397 if (status < 0)
4398 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004399
4400
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004401 /* QAM State Machine (FSM) Thresholds */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004402
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004403 status = write16(state, SCU_RAM_QAM_FSM_RTH__A, 140);
4404 if (status < 0)
4405 goto error;
4406 status = write16(state, SCU_RAM_QAM_FSM_FTH__A, 50);
4407 if (status < 0)
4408 goto error;
4409 status = write16(state, SCU_RAM_QAM_FSM_CTH__A, 95);
4410 if (status < 0)
4411 goto error;
4412 status = write16(state, SCU_RAM_QAM_FSM_PTH__A, 120);
4413 if (status < 0)
4414 goto error;
4415 status = write16(state, SCU_RAM_QAM_FSM_QTH__A, 230);
4416 if (status < 0)
4417 goto error;
4418 status = write16(state, SCU_RAM_QAM_FSM_MTH__A, 105);
4419 if (status < 0)
4420 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004421
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004422 status = write16(state, SCU_RAM_QAM_FSM_RATE_LIM__A, 40);
4423 if (status < 0)
4424 goto error;
4425 status = write16(state, SCU_RAM_QAM_FSM_COUNT_LIM__A, 4);
4426 if (status < 0)
4427 goto error;
4428 status = write16(state, SCU_RAM_QAM_FSM_FREQ_LIM__A, 24);
4429 if (status < 0)
4430 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004431
4432
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004433 /* QAM FSM Tracking Parameters */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004434
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004435 status = write16(state, SCU_RAM_QAM_FSM_MEDIAN_AV_MULT__A, (u16) 16);
4436 if (status < 0)
4437 goto error;
4438 status = write16(state, SCU_RAM_QAM_FSM_RADIUS_AV_LIMIT__A, (u16) 220);
4439 if (status < 0)
4440 goto error;
4441 status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET1__A, (u16) 25);
4442 if (status < 0)
4443 goto error;
4444 status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET2__A, (u16) 6);
4445 if (status < 0)
4446 goto error;
4447 status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET3__A, (u16) -24);
4448 if (status < 0)
4449 goto error;
4450 status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET4__A, (u16) -65);
4451 if (status < 0)
4452 goto error;
4453 status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET5__A, (u16) -127);
4454 if (status < 0)
4455 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004456
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004457error:
4458 if (status < 0)
4459 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
Oliver Endrissebc7de22011-07-03 13:49:44 -03004460 return status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004461}
4462
4463/*============================================================================*/
4464
4465/**
4466* \brief QAM32 specific setup
4467* \param demod instance of demod.
4468* \return DRXStatus_t.
4469*/
Oliver Endrissebc7de22011-07-03 13:49:44 -03004470static int SetQAM32(struct drxk_state *state)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004471{
Oliver Endrissebc7de22011-07-03 13:49:44 -03004472 int status = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004473
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03004474 dprintk(1, "\n");
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004475
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004476 /* QAM Equalizer Setup */
4477 /* Equalizer */
4478 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD0__A, 6707);
4479 if (status < 0)
4480 goto error;
4481 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD1__A, 6707);
4482 if (status < 0)
4483 goto error;
4484 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD2__A, 6707);
4485 if (status < 0)
4486 goto error;
4487 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD3__A, 6707);
4488 if (status < 0)
4489 goto error;
4490 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD4__A, 6707);
4491 if (status < 0)
4492 goto error;
4493 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD5__A, 6707);
4494 if (status < 0)
4495 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004496
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004497 /* Decision Feedback Equalizer */
4498 status = write16(state, QAM_DQ_QUAL_FUN0__A, 3);
4499 if (status < 0)
4500 goto error;
4501 status = write16(state, QAM_DQ_QUAL_FUN1__A, 3);
4502 if (status < 0)
4503 goto error;
4504 status = write16(state, QAM_DQ_QUAL_FUN2__A, 3);
4505 if (status < 0)
4506 goto error;
4507 status = write16(state, QAM_DQ_QUAL_FUN3__A, 3);
4508 if (status < 0)
4509 goto error;
4510 status = write16(state, QAM_DQ_QUAL_FUN4__A, 3);
4511 if (status < 0)
4512 goto error;
4513 status = write16(state, QAM_DQ_QUAL_FUN5__A, 0);
4514 if (status < 0)
4515 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004516
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004517 status = write16(state, QAM_SY_SYNC_HWM__A, 6);
4518 if (status < 0)
4519 goto error;
4520 status = write16(state, QAM_SY_SYNC_AWM__A, 5);
4521 if (status < 0)
4522 goto error;
4523 status = write16(state, QAM_SY_SYNC_LWM__A, 3);
4524 if (status < 0)
4525 goto error;
Oliver Endrissebc7de22011-07-03 13:49:44 -03004526
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004527 /* QAM Slicer Settings */
4528
4529 status = write16(state, SCU_RAM_QAM_SL_SIG_POWER__A, DRXK_QAM_SL_SIG_POWER_QAM32);
4530 if (status < 0)
4531 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004532
4533
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004534 /* QAM Loop Controller Coeficients */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004535
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004536 status = write16(state, SCU_RAM_QAM_LC_CA_FINE__A, 15);
4537 if (status < 0)
4538 goto error;
4539 status = write16(state, SCU_RAM_QAM_LC_CA_COARSE__A, 40);
4540 if (status < 0)
4541 goto error;
4542 status = write16(state, SCU_RAM_QAM_LC_EP_FINE__A, 12);
4543 if (status < 0)
4544 goto error;
4545 status = write16(state, SCU_RAM_QAM_LC_EP_MEDIUM__A, 24);
4546 if (status < 0)
4547 goto error;
4548 status = write16(state, SCU_RAM_QAM_LC_EP_COARSE__A, 24);
4549 if (status < 0)
4550 goto error;
4551 status = write16(state, SCU_RAM_QAM_LC_EI_FINE__A, 12);
4552 if (status < 0)
4553 goto error;
4554 status = write16(state, SCU_RAM_QAM_LC_EI_MEDIUM__A, 16);
4555 if (status < 0)
4556 goto error;
4557 status = write16(state, SCU_RAM_QAM_LC_EI_COARSE__A, 16);
4558 if (status < 0)
4559 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004560
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004561 status = write16(state, SCU_RAM_QAM_LC_CP_FINE__A, 5);
4562 if (status < 0)
4563 goto error;
4564 status = write16(state, SCU_RAM_QAM_LC_CP_MEDIUM__A, 20);
4565 if (status < 0)
4566 goto error;
4567 status = write16(state, SCU_RAM_QAM_LC_CP_COARSE__A, 80);
4568 if (status < 0)
4569 goto error;
4570 status = write16(state, SCU_RAM_QAM_LC_CI_FINE__A, 5);
4571 if (status < 0)
4572 goto error;
4573 status = write16(state, SCU_RAM_QAM_LC_CI_MEDIUM__A, 20);
4574 if (status < 0)
4575 goto error;
4576 status = write16(state, SCU_RAM_QAM_LC_CI_COARSE__A, 50);
4577 if (status < 0)
4578 goto error;
4579 status = write16(state, SCU_RAM_QAM_LC_CF_FINE__A, 16);
4580 if (status < 0)
4581 goto error;
4582 status = write16(state, SCU_RAM_QAM_LC_CF_MEDIUM__A, 16);
4583 if (status < 0)
4584 goto error;
4585 status = write16(state, SCU_RAM_QAM_LC_CF_COARSE__A, 16);
4586 if (status < 0)
4587 goto error;
4588 status = write16(state, SCU_RAM_QAM_LC_CF1_FINE__A, 5);
4589 if (status < 0)
4590 goto error;
4591 status = write16(state, SCU_RAM_QAM_LC_CF1_MEDIUM__A, 10);
4592 if (status < 0)
4593 goto error;
4594 status = write16(state, SCU_RAM_QAM_LC_CF1_COARSE__A, 0);
4595 if (status < 0)
4596 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004597
4598
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004599 /* QAM State Machine (FSM) Thresholds */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004600
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004601 status = write16(state, SCU_RAM_QAM_FSM_RTH__A, 90);
4602 if (status < 0)
4603 goto error;
4604 status = write16(state, SCU_RAM_QAM_FSM_FTH__A, 50);
4605 if (status < 0)
4606 goto error;
4607 status = write16(state, SCU_RAM_QAM_FSM_CTH__A, 80);
4608 if (status < 0)
4609 goto error;
4610 status = write16(state, SCU_RAM_QAM_FSM_PTH__A, 100);
4611 if (status < 0)
4612 goto error;
4613 status = write16(state, SCU_RAM_QAM_FSM_QTH__A, 170);
4614 if (status < 0)
4615 goto error;
4616 status = write16(state, SCU_RAM_QAM_FSM_MTH__A, 100);
4617 if (status < 0)
4618 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004619
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004620 status = write16(state, SCU_RAM_QAM_FSM_RATE_LIM__A, 40);
4621 if (status < 0)
4622 goto error;
4623 status = write16(state, SCU_RAM_QAM_FSM_COUNT_LIM__A, 4);
4624 if (status < 0)
4625 goto error;
4626 status = write16(state, SCU_RAM_QAM_FSM_FREQ_LIM__A, 10);
4627 if (status < 0)
4628 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004629
4630
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004631 /* QAM FSM Tracking Parameters */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004632
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004633 status = write16(state, SCU_RAM_QAM_FSM_MEDIAN_AV_MULT__A, (u16) 12);
4634 if (status < 0)
4635 goto error;
4636 status = write16(state, SCU_RAM_QAM_FSM_RADIUS_AV_LIMIT__A, (u16) 140);
4637 if (status < 0)
4638 goto error;
4639 status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET1__A, (u16) -8);
4640 if (status < 0)
4641 goto error;
4642 status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET2__A, (u16) -16);
4643 if (status < 0)
4644 goto error;
4645 status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET3__A, (u16) -26);
4646 if (status < 0)
4647 goto error;
4648 status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET4__A, (u16) -56);
4649 if (status < 0)
4650 goto error;
4651 status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET5__A, (u16) -86);
4652error:
4653 if (status < 0)
4654 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
Oliver Endrissebc7de22011-07-03 13:49:44 -03004655 return status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004656}
4657
4658/*============================================================================*/
4659
4660/**
4661* \brief QAM64 specific setup
4662* \param demod instance of demod.
4663* \return DRXStatus_t.
4664*/
Oliver Endrissebc7de22011-07-03 13:49:44 -03004665static int SetQAM64(struct drxk_state *state)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004666{
Oliver Endrissebc7de22011-07-03 13:49:44 -03004667 int status = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004668
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03004669 dprintk(1, "\n");
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004670 /* QAM Equalizer Setup */
4671 /* Equalizer */
4672 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD0__A, 13336);
4673 if (status < 0)
4674 goto error;
4675 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD1__A, 12618);
4676 if (status < 0)
4677 goto error;
4678 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD2__A, 11988);
4679 if (status < 0)
4680 goto error;
4681 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD3__A, 13809);
4682 if (status < 0)
4683 goto error;
4684 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD4__A, 13809);
4685 if (status < 0)
4686 goto error;
4687 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD5__A, 15609);
4688 if (status < 0)
4689 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004690
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004691 /* Decision Feedback Equalizer */
4692 status = write16(state, QAM_DQ_QUAL_FUN0__A, 4);
4693 if (status < 0)
4694 goto error;
4695 status = write16(state, QAM_DQ_QUAL_FUN1__A, 4);
4696 if (status < 0)
4697 goto error;
4698 status = write16(state, QAM_DQ_QUAL_FUN2__A, 4);
4699 if (status < 0)
4700 goto error;
4701 status = write16(state, QAM_DQ_QUAL_FUN3__A, 4);
4702 if (status < 0)
4703 goto error;
4704 status = write16(state, QAM_DQ_QUAL_FUN4__A, 3);
4705 if (status < 0)
4706 goto error;
4707 status = write16(state, QAM_DQ_QUAL_FUN5__A, 0);
4708 if (status < 0)
4709 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004710
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004711 status = write16(state, QAM_SY_SYNC_HWM__A, 5);
4712 if (status < 0)
4713 goto error;
4714 status = write16(state, QAM_SY_SYNC_AWM__A, 4);
4715 if (status < 0)
4716 goto error;
4717 status = write16(state, QAM_SY_SYNC_LWM__A, 3);
4718 if (status < 0)
4719 goto error;
Oliver Endrissebc7de22011-07-03 13:49:44 -03004720
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004721 /* QAM Slicer Settings */
4722 status = write16(state, SCU_RAM_QAM_SL_SIG_POWER__A, DRXK_QAM_SL_SIG_POWER_QAM64);
4723 if (status < 0)
4724 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004725
4726
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004727 /* QAM Loop Controller Coeficients */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004728
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004729 status = write16(state, SCU_RAM_QAM_LC_CA_FINE__A, 15);
4730 if (status < 0)
4731 goto error;
4732 status = write16(state, SCU_RAM_QAM_LC_CA_COARSE__A, 40);
4733 if (status < 0)
4734 goto error;
4735 status = write16(state, SCU_RAM_QAM_LC_EP_FINE__A, 12);
4736 if (status < 0)
4737 goto error;
4738 status = write16(state, SCU_RAM_QAM_LC_EP_MEDIUM__A, 24);
4739 if (status < 0)
4740 goto error;
4741 status = write16(state, SCU_RAM_QAM_LC_EP_COARSE__A, 24);
4742 if (status < 0)
4743 goto error;
4744 status = write16(state, SCU_RAM_QAM_LC_EI_FINE__A, 12);
4745 if (status < 0)
4746 goto error;
4747 status = write16(state, SCU_RAM_QAM_LC_EI_MEDIUM__A, 16);
4748 if (status < 0)
4749 goto error;
4750 status = write16(state, SCU_RAM_QAM_LC_EI_COARSE__A, 16);
4751 if (status < 0)
4752 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004753
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004754 status = write16(state, SCU_RAM_QAM_LC_CP_FINE__A, 5);
4755 if (status < 0)
4756 goto error;
4757 status = write16(state, SCU_RAM_QAM_LC_CP_MEDIUM__A, 30);
4758 if (status < 0)
4759 goto error;
4760 status = write16(state, SCU_RAM_QAM_LC_CP_COARSE__A, 100);
4761 if (status < 0)
4762 goto error;
4763 status = write16(state, SCU_RAM_QAM_LC_CI_FINE__A, 5);
4764 if (status < 0)
4765 goto error;
4766 status = write16(state, SCU_RAM_QAM_LC_CI_MEDIUM__A, 30);
4767 if (status < 0)
4768 goto error;
4769 status = write16(state, SCU_RAM_QAM_LC_CI_COARSE__A, 50);
4770 if (status < 0)
4771 goto error;
4772 status = write16(state, SCU_RAM_QAM_LC_CF_FINE__A, 16);
4773 if (status < 0)
4774 goto error;
4775 status = write16(state, SCU_RAM_QAM_LC_CF_MEDIUM__A, 25);
4776 if (status < 0)
4777 goto error;
4778 status = write16(state, SCU_RAM_QAM_LC_CF_COARSE__A, 48);
4779 if (status < 0)
4780 goto error;
4781 status = write16(state, SCU_RAM_QAM_LC_CF1_FINE__A, 5);
4782 if (status < 0)
4783 goto error;
4784 status = write16(state, SCU_RAM_QAM_LC_CF1_MEDIUM__A, 10);
4785 if (status < 0)
4786 goto error;
4787 status = write16(state, SCU_RAM_QAM_LC_CF1_COARSE__A, 10);
4788 if (status < 0)
4789 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004790
4791
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004792 /* QAM State Machine (FSM) Thresholds */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004793
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004794 status = write16(state, SCU_RAM_QAM_FSM_RTH__A, 100);
4795 if (status < 0)
4796 goto error;
4797 status = write16(state, SCU_RAM_QAM_FSM_FTH__A, 60);
4798 if (status < 0)
4799 goto error;
4800 status = write16(state, SCU_RAM_QAM_FSM_CTH__A, 80);
4801 if (status < 0)
4802 goto error;
4803 status = write16(state, SCU_RAM_QAM_FSM_PTH__A, 110);
4804 if (status < 0)
4805 goto error;
4806 status = write16(state, SCU_RAM_QAM_FSM_QTH__A, 200);
4807 if (status < 0)
4808 goto error;
4809 status = write16(state, SCU_RAM_QAM_FSM_MTH__A, 95);
4810 if (status < 0)
4811 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004812
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004813 status = write16(state, SCU_RAM_QAM_FSM_RATE_LIM__A, 40);
4814 if (status < 0)
4815 goto error;
4816 status = write16(state, SCU_RAM_QAM_FSM_COUNT_LIM__A, 4);
4817 if (status < 0)
4818 goto error;
4819 status = write16(state, SCU_RAM_QAM_FSM_FREQ_LIM__A, 15);
4820 if (status < 0)
4821 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004822
4823
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004824 /* QAM FSM Tracking Parameters */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004825
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004826 status = write16(state, SCU_RAM_QAM_FSM_MEDIAN_AV_MULT__A, (u16) 12);
4827 if (status < 0)
4828 goto error;
4829 status = write16(state, SCU_RAM_QAM_FSM_RADIUS_AV_LIMIT__A, (u16) 141);
4830 if (status < 0)
4831 goto error;
4832 status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET1__A, (u16) 7);
4833 if (status < 0)
4834 goto error;
4835 status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET2__A, (u16) 0);
4836 if (status < 0)
4837 goto error;
4838 status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET3__A, (u16) -15);
4839 if (status < 0)
4840 goto error;
4841 status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET4__A, (u16) -45);
4842 if (status < 0)
4843 goto error;
4844 status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET5__A, (u16) -80);
4845error:
4846 if (status < 0)
4847 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004848
Oliver Endrissebc7de22011-07-03 13:49:44 -03004849 return status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004850}
4851
4852/*============================================================================*/
4853
4854/**
4855* \brief QAM128 specific setup
4856* \param demod: instance of demod.
4857* \return DRXStatus_t.
4858*/
4859static int SetQAM128(struct drxk_state *state)
4860{
Oliver Endrissebc7de22011-07-03 13:49:44 -03004861 int status = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004862
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03004863 dprintk(1, "\n");
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004864 /* QAM Equalizer Setup */
4865 /* Equalizer */
4866 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD0__A, 6564);
4867 if (status < 0)
4868 goto error;
4869 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD1__A, 6598);
4870 if (status < 0)
4871 goto error;
4872 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD2__A, 6394);
4873 if (status < 0)
4874 goto error;
4875 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD3__A, 6409);
4876 if (status < 0)
4877 goto error;
4878 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD4__A, 6656);
4879 if (status < 0)
4880 goto error;
4881 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD5__A, 7238);
4882 if (status < 0)
4883 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004884
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004885 /* Decision Feedback Equalizer */
4886 status = write16(state, QAM_DQ_QUAL_FUN0__A, 6);
4887 if (status < 0)
4888 goto error;
4889 status = write16(state, QAM_DQ_QUAL_FUN1__A, 6);
4890 if (status < 0)
4891 goto error;
4892 status = write16(state, QAM_DQ_QUAL_FUN2__A, 6);
4893 if (status < 0)
4894 goto error;
4895 status = write16(state, QAM_DQ_QUAL_FUN3__A, 6);
4896 if (status < 0)
4897 goto error;
4898 status = write16(state, QAM_DQ_QUAL_FUN4__A, 5);
4899 if (status < 0)
4900 goto error;
4901 status = write16(state, QAM_DQ_QUAL_FUN5__A, 0);
4902 if (status < 0)
4903 goto error;
Oliver Endrissebc7de22011-07-03 13:49:44 -03004904
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004905 status = write16(state, QAM_SY_SYNC_HWM__A, 6);
4906 if (status < 0)
4907 goto error;
4908 status = write16(state, QAM_SY_SYNC_AWM__A, 5);
4909 if (status < 0)
4910 goto error;
4911 status = write16(state, QAM_SY_SYNC_LWM__A, 3);
4912 if (status < 0)
4913 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004914
4915
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004916 /* QAM Slicer Settings */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004917
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004918 status = write16(state, SCU_RAM_QAM_SL_SIG_POWER__A, DRXK_QAM_SL_SIG_POWER_QAM128);
4919 if (status < 0)
4920 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004921
4922
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004923 /* QAM Loop Controller Coeficients */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004924
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004925 status = write16(state, SCU_RAM_QAM_LC_CA_FINE__A, 15);
4926 if (status < 0)
4927 goto error;
4928 status = write16(state, SCU_RAM_QAM_LC_CA_COARSE__A, 40);
4929 if (status < 0)
4930 goto error;
4931 status = write16(state, SCU_RAM_QAM_LC_EP_FINE__A, 12);
4932 if (status < 0)
4933 goto error;
4934 status = write16(state, SCU_RAM_QAM_LC_EP_MEDIUM__A, 24);
4935 if (status < 0)
4936 goto error;
4937 status = write16(state, SCU_RAM_QAM_LC_EP_COARSE__A, 24);
4938 if (status < 0)
4939 goto error;
4940 status = write16(state, SCU_RAM_QAM_LC_EI_FINE__A, 12);
4941 if (status < 0)
4942 goto error;
4943 status = write16(state, SCU_RAM_QAM_LC_EI_MEDIUM__A, 16);
4944 if (status < 0)
4945 goto error;
4946 status = write16(state, SCU_RAM_QAM_LC_EI_COARSE__A, 16);
4947 if (status < 0)
4948 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004949
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004950 status = write16(state, SCU_RAM_QAM_LC_CP_FINE__A, 5);
4951 if (status < 0)
4952 goto error;
4953 status = write16(state, SCU_RAM_QAM_LC_CP_MEDIUM__A, 40);
4954 if (status < 0)
4955 goto error;
4956 status = write16(state, SCU_RAM_QAM_LC_CP_COARSE__A, 120);
4957 if (status < 0)
4958 goto error;
4959 status = write16(state, SCU_RAM_QAM_LC_CI_FINE__A, 5);
4960 if (status < 0)
4961 goto error;
4962 status = write16(state, SCU_RAM_QAM_LC_CI_MEDIUM__A, 40);
4963 if (status < 0)
4964 goto error;
4965 status = write16(state, SCU_RAM_QAM_LC_CI_COARSE__A, 60);
4966 if (status < 0)
4967 goto error;
4968 status = write16(state, SCU_RAM_QAM_LC_CF_FINE__A, 16);
4969 if (status < 0)
4970 goto error;
4971 status = write16(state, SCU_RAM_QAM_LC_CF_MEDIUM__A, 25);
4972 if (status < 0)
4973 goto error;
4974 status = write16(state, SCU_RAM_QAM_LC_CF_COARSE__A, 64);
4975 if (status < 0)
4976 goto error;
4977 status = write16(state, SCU_RAM_QAM_LC_CF1_FINE__A, 5);
4978 if (status < 0)
4979 goto error;
4980 status = write16(state, SCU_RAM_QAM_LC_CF1_MEDIUM__A, 10);
4981 if (status < 0)
4982 goto error;
4983 status = write16(state, SCU_RAM_QAM_LC_CF1_COARSE__A, 0);
4984 if (status < 0)
4985 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004986
4987
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004988 /* QAM State Machine (FSM) Thresholds */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004989
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004990 status = write16(state, SCU_RAM_QAM_FSM_RTH__A, 50);
4991 if (status < 0)
4992 goto error;
4993 status = write16(state, SCU_RAM_QAM_FSM_FTH__A, 60);
4994 if (status < 0)
4995 goto error;
4996 status = write16(state, SCU_RAM_QAM_FSM_CTH__A, 80);
4997 if (status < 0)
4998 goto error;
4999 status = write16(state, SCU_RAM_QAM_FSM_PTH__A, 100);
5000 if (status < 0)
5001 goto error;
5002 status = write16(state, SCU_RAM_QAM_FSM_QTH__A, 140);
5003 if (status < 0)
5004 goto error;
5005 status = write16(state, SCU_RAM_QAM_FSM_MTH__A, 100);
5006 if (status < 0)
5007 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005008
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005009 status = write16(state, SCU_RAM_QAM_FSM_RATE_LIM__A, 40);
5010 if (status < 0)
5011 goto error;
5012 status = write16(state, SCU_RAM_QAM_FSM_COUNT_LIM__A, 5);
5013 if (status < 0)
5014 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005015
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005016 status = write16(state, SCU_RAM_QAM_FSM_FREQ_LIM__A, 12);
5017 if (status < 0)
5018 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005019
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005020 /* QAM FSM Tracking Parameters */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005021
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005022 status = write16(state, SCU_RAM_QAM_FSM_MEDIAN_AV_MULT__A, (u16) 8);
5023 if (status < 0)
5024 goto error;
5025 status = write16(state, SCU_RAM_QAM_FSM_RADIUS_AV_LIMIT__A, (u16) 65);
5026 if (status < 0)
5027 goto error;
5028 status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET1__A, (u16) 5);
5029 if (status < 0)
5030 goto error;
5031 status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET2__A, (u16) 3);
5032 if (status < 0)
5033 goto error;
5034 status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET3__A, (u16) -1);
5035 if (status < 0)
5036 goto error;
5037 status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET4__A, (u16) -12);
5038 if (status < 0)
5039 goto error;
5040 status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET5__A, (u16) -23);
5041error:
5042 if (status < 0)
5043 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005044
Oliver Endrissebc7de22011-07-03 13:49:44 -03005045 return status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005046}
5047
5048/*============================================================================*/
5049
5050/**
5051* \brief QAM256 specific setup
5052* \param demod: instance of demod.
5053* \return DRXStatus_t.
5054*/
5055static int SetQAM256(struct drxk_state *state)
5056{
Oliver Endrissebc7de22011-07-03 13:49:44 -03005057 int status = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005058
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03005059 dprintk(1, "\n");
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005060 /* QAM Equalizer Setup */
5061 /* Equalizer */
5062 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD0__A, 11502);
5063 if (status < 0)
5064 goto error;
5065 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD1__A, 12084);
5066 if (status < 0)
5067 goto error;
5068 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD2__A, 12543);
5069 if (status < 0)
5070 goto error;
5071 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD3__A, 12931);
5072 if (status < 0)
5073 goto error;
5074 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD4__A, 13629);
5075 if (status < 0)
5076 goto error;
5077 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD5__A, 15385);
5078 if (status < 0)
5079 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005080
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005081 /* Decision Feedback Equalizer */
5082 status = write16(state, QAM_DQ_QUAL_FUN0__A, 8);
5083 if (status < 0)
5084 goto error;
5085 status = write16(state, QAM_DQ_QUAL_FUN1__A, 8);
5086 if (status < 0)
5087 goto error;
5088 status = write16(state, QAM_DQ_QUAL_FUN2__A, 8);
5089 if (status < 0)
5090 goto error;
5091 status = write16(state, QAM_DQ_QUAL_FUN3__A, 8);
5092 if (status < 0)
5093 goto error;
5094 status = write16(state, QAM_DQ_QUAL_FUN4__A, 6);
5095 if (status < 0)
5096 goto error;
5097 status = write16(state, QAM_DQ_QUAL_FUN5__A, 0);
5098 if (status < 0)
5099 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005100
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005101 status = write16(state, QAM_SY_SYNC_HWM__A, 5);
5102 if (status < 0)
5103 goto error;
5104 status = write16(state, QAM_SY_SYNC_AWM__A, 4);
5105 if (status < 0)
5106 goto error;
5107 status = write16(state, QAM_SY_SYNC_LWM__A, 3);
5108 if (status < 0)
5109 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005110
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005111 /* QAM Slicer Settings */
Oliver Endrissebc7de22011-07-03 13:49:44 -03005112
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005113 status = write16(state, SCU_RAM_QAM_SL_SIG_POWER__A, DRXK_QAM_SL_SIG_POWER_QAM256);
5114 if (status < 0)
5115 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005116
5117
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005118 /* QAM Loop Controller Coeficients */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005119
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005120 status = write16(state, SCU_RAM_QAM_LC_CA_FINE__A, 15);
5121 if (status < 0)
5122 goto error;
5123 status = write16(state, SCU_RAM_QAM_LC_CA_COARSE__A, 40);
5124 if (status < 0)
5125 goto error;
5126 status = write16(state, SCU_RAM_QAM_LC_EP_FINE__A, 12);
5127 if (status < 0)
5128 goto error;
5129 status = write16(state, SCU_RAM_QAM_LC_EP_MEDIUM__A, 24);
5130 if (status < 0)
5131 goto error;
5132 status = write16(state, SCU_RAM_QAM_LC_EP_COARSE__A, 24);
5133 if (status < 0)
5134 goto error;
5135 status = write16(state, SCU_RAM_QAM_LC_EI_FINE__A, 12);
5136 if (status < 0)
5137 goto error;
5138 status = write16(state, SCU_RAM_QAM_LC_EI_MEDIUM__A, 16);
5139 if (status < 0)
5140 goto error;
5141 status = write16(state, SCU_RAM_QAM_LC_EI_COARSE__A, 16);
5142 if (status < 0)
5143 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005144
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005145 status = write16(state, SCU_RAM_QAM_LC_CP_FINE__A, 5);
5146 if (status < 0)
5147 goto error;
5148 status = write16(state, SCU_RAM_QAM_LC_CP_MEDIUM__A, 50);
5149 if (status < 0)
5150 goto error;
5151 status = write16(state, SCU_RAM_QAM_LC_CP_COARSE__A, 250);
5152 if (status < 0)
5153 goto error;
5154 status = write16(state, SCU_RAM_QAM_LC_CI_FINE__A, 5);
5155 if (status < 0)
5156 goto error;
5157 status = write16(state, SCU_RAM_QAM_LC_CI_MEDIUM__A, 50);
5158 if (status < 0)
5159 goto error;
5160 status = write16(state, SCU_RAM_QAM_LC_CI_COARSE__A, 125);
5161 if (status < 0)
5162 goto error;
5163 status = write16(state, SCU_RAM_QAM_LC_CF_FINE__A, 16);
5164 if (status < 0)
5165 goto error;
5166 status = write16(state, SCU_RAM_QAM_LC_CF_MEDIUM__A, 25);
5167 if (status < 0)
5168 goto error;
5169 status = write16(state, SCU_RAM_QAM_LC_CF_COARSE__A, 48);
5170 if (status < 0)
5171 goto error;
5172 status = write16(state, SCU_RAM_QAM_LC_CF1_FINE__A, 5);
5173 if (status < 0)
5174 goto error;
5175 status = write16(state, SCU_RAM_QAM_LC_CF1_MEDIUM__A, 10);
5176 if (status < 0)
5177 goto error;
5178 status = write16(state, SCU_RAM_QAM_LC_CF1_COARSE__A, 10);
5179 if (status < 0)
5180 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005181
5182
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005183 /* QAM State Machine (FSM) Thresholds */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005184
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005185 status = write16(state, SCU_RAM_QAM_FSM_RTH__A, 50);
5186 if (status < 0)
5187 goto error;
5188 status = write16(state, SCU_RAM_QAM_FSM_FTH__A, 60);
5189 if (status < 0)
5190 goto error;
5191 status = write16(state, SCU_RAM_QAM_FSM_CTH__A, 80);
5192 if (status < 0)
5193 goto error;
5194 status = write16(state, SCU_RAM_QAM_FSM_PTH__A, 100);
5195 if (status < 0)
5196 goto error;
5197 status = write16(state, SCU_RAM_QAM_FSM_QTH__A, 150);
5198 if (status < 0)
5199 goto error;
5200 status = write16(state, SCU_RAM_QAM_FSM_MTH__A, 110);
5201 if (status < 0)
5202 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005203
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005204 status = write16(state, SCU_RAM_QAM_FSM_RATE_LIM__A, 40);
5205 if (status < 0)
5206 goto error;
5207 status = write16(state, SCU_RAM_QAM_FSM_COUNT_LIM__A, 4);
5208 if (status < 0)
5209 goto error;
5210 status = write16(state, SCU_RAM_QAM_FSM_FREQ_LIM__A, 12);
5211 if (status < 0)
5212 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005213
5214
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005215 /* QAM FSM Tracking Parameters */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005216
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005217 status = write16(state, SCU_RAM_QAM_FSM_MEDIAN_AV_MULT__A, (u16) 8);
5218 if (status < 0)
5219 goto error;
5220 status = write16(state, SCU_RAM_QAM_FSM_RADIUS_AV_LIMIT__A, (u16) 74);
5221 if (status < 0)
5222 goto error;
5223 status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET1__A, (u16) 18);
5224 if (status < 0)
5225 goto error;
5226 status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET2__A, (u16) 13);
5227 if (status < 0)
5228 goto error;
5229 status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET3__A, (u16) 7);
5230 if (status < 0)
5231 goto error;
5232 status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET4__A, (u16) 0);
5233 if (status < 0)
5234 goto error;
5235 status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET5__A, (u16) -8);
5236error:
5237 if (status < 0)
5238 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
Oliver Endrissebc7de22011-07-03 13:49:44 -03005239 return status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005240}
5241
5242
5243/*============================================================================*/
5244/**
5245* \brief Reset QAM block.
5246* \param demod: instance of demod.
5247* \param channel: pointer to channel data.
5248* \return DRXStatus_t.
5249*/
5250static int QAMResetQAM(struct drxk_state *state)
5251{
Oliver Endrissebc7de22011-07-03 13:49:44 -03005252 int status;
5253 u16 cmdResult;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005254
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03005255 dprintk(1, "\n");
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005256 /* Stop QAM comstate->m_exec */
5257 status = write16(state, QAM_COMM_EXEC__A, QAM_COMM_EXEC_STOP);
5258 if (status < 0)
5259 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005260
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005261 status = scu_command(state, SCU_RAM_COMMAND_STANDARD_QAM | SCU_RAM_COMMAND_CMD_DEMOD_RESET, 0, NULL, 1, &cmdResult);
5262error:
5263 if (status < 0)
5264 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
Oliver Endrissebc7de22011-07-03 13:49:44 -03005265 return status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005266}
5267
5268/*============================================================================*/
5269
5270/**
5271* \brief Set QAM symbolrate.
5272* \param demod: instance of demod.
5273* \param channel: pointer to channel data.
5274* \return DRXStatus_t.
5275*/
5276static int QAMSetSymbolrate(struct drxk_state *state)
5277{
Oliver Endrissebc7de22011-07-03 13:49:44 -03005278 u32 adcFrequency = 0;
5279 u32 symbFreq = 0;
5280 u32 iqmRcRate = 0;
5281 u16 ratesel = 0;
5282 u32 lcSymbRate = 0;
5283 int status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005284
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03005285 dprintk(1, "\n");
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005286 /* Select & calculate correct IQM rate */
5287 adcFrequency = (state->m_sysClockFreq * 1000) / 3;
5288 ratesel = 0;
5289 /* printk(KERN_DEBUG "drxk: SR %d\n", state->param.u.qam.symbol_rate); */
5290 if (state->param.u.qam.symbol_rate <= 1188750)
5291 ratesel = 3;
5292 else if (state->param.u.qam.symbol_rate <= 2377500)
5293 ratesel = 2;
5294 else if (state->param.u.qam.symbol_rate <= 4755000)
5295 ratesel = 1;
5296 status = write16(state, IQM_FD_RATESEL__A, ratesel);
5297 if (status < 0)
5298 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005299
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005300 /*
5301 IqmRcRate = ((Fadc / (symbolrate * (4<<ratesel))) - 1) * (1<<23)
5302 */
5303 symbFreq = state->param.u.qam.symbol_rate * (1 << ratesel);
5304 if (symbFreq == 0) {
5305 /* Divide by zero */
5306 status = -EINVAL;
5307 goto error;
5308 }
5309 iqmRcRate = (adcFrequency / symbFreq) * (1 << 21) +
5310 (Frac28a((adcFrequency % symbFreq), symbFreq) >> 7) -
5311 (1 << 23);
5312 status = write32(state, IQM_RC_RATE_OFS_LO__A, iqmRcRate);
5313 if (status < 0)
5314 goto error;
5315 state->m_iqmRcRate = iqmRcRate;
5316 /*
5317 LcSymbFreq = round (.125 * symbolrate / adcFreq * (1<<15))
5318 */
5319 symbFreq = state->param.u.qam.symbol_rate;
5320 if (adcFrequency == 0) {
5321 /* Divide by zero */
5322 status = -EINVAL;
5323 goto error;
5324 }
5325 lcSymbRate = (symbFreq / adcFrequency) * (1 << 12) +
5326 (Frac28a((symbFreq % adcFrequency), adcFrequency) >>
5327 16);
5328 if (lcSymbRate > 511)
5329 lcSymbRate = 511;
5330 status = write16(state, QAM_LC_SYMBOL_FREQ__A, (u16) lcSymbRate);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005331
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005332error:
5333 if (status < 0)
5334 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
Oliver Endrissebc7de22011-07-03 13:49:44 -03005335 return status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005336}
5337
5338/*============================================================================*/
5339
5340/**
5341* \brief Get QAM lock status.
5342* \param demod: instance of demod.
5343* \param channel: pointer to channel data.
5344* \return DRXStatus_t.
5345*/
5346
5347static int GetQAMLockStatus(struct drxk_state *state, u32 *pLockStatus)
5348{
5349 int status;
Oliver Endrissebc7de22011-07-03 13:49:44 -03005350 u16 Result[2] = { 0, 0 };
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005351
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03005352 dprintk(1, "\n");
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005353 *pLockStatus = NOT_LOCKED;
5354 status = scu_command(state,
Oliver Endrissebc7de22011-07-03 13:49:44 -03005355 SCU_RAM_COMMAND_STANDARD_QAM |
5356 SCU_RAM_COMMAND_CMD_DEMOD_GET_LOCK, 0, NULL, 2,
5357 Result);
5358 if (status < 0)
Mauro Carvalho Chehabe0e6eca2011-07-04 08:27:47 -03005359 printk(KERN_ERR "drxk: %s status = %08x\n", __func__, status);
Oliver Endrissebc7de22011-07-03 13:49:44 -03005360
5361 if (Result[1] < SCU_RAM_QAM_LOCKED_LOCKED_DEMOD_LOCKED) {
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005362 /* 0x0000 NOT LOCKED */
Oliver Endrissebc7de22011-07-03 13:49:44 -03005363 } else if (Result[1] < SCU_RAM_QAM_LOCKED_LOCKED_LOCKED) {
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005364 /* 0x4000 DEMOD LOCKED */
5365 *pLockStatus = DEMOD_LOCK;
Oliver Endrissebc7de22011-07-03 13:49:44 -03005366 } else if (Result[1] < SCU_RAM_QAM_LOCKED_LOCKED_NEVER_LOCK) {
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005367 /* 0x8000 DEMOD + FEC LOCKED (system lock) */
5368 *pLockStatus = MPEG_LOCK;
Oliver Endrissebc7de22011-07-03 13:49:44 -03005369 } else {
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005370 /* 0xC000 NEVER LOCKED */
5371 /* (system will never be able to lock to the signal) */
5372 /* TODO: check this, intermediate & standard specific lock states are not
5373 taken into account here */
5374 *pLockStatus = NEVER_LOCK;
5375 }
5376 return status;
5377}
5378
5379#define QAM_MIRROR__M 0x03
5380#define QAM_MIRROR_NORMAL 0x00
5381#define QAM_MIRRORED 0x01
5382#define QAM_MIRROR_AUTO_ON 0x02
5383#define QAM_LOCKRANGE__M 0x10
5384#define QAM_LOCKRANGE_NORMAL 0x10
5385
Oliver Endrissebc7de22011-07-03 13:49:44 -03005386static int SetQAM(struct drxk_state *state, u16 IntermediateFreqkHz,
5387 s32 tunerFreqOffset)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005388{
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005389 int status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005390 u8 parameterLen;
Oliver Endrissebc7de22011-07-03 13:49:44 -03005391 u16 setEnvParameters[5];
5392 u16 setParamParameters[4] = { 0, 0, 0, 0 };
5393 u16 cmdResult;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005394
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03005395 dprintk(1, "\n");
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005396 /*
5397 STEP 1: reset demodulator
5398 resets FEC DI and FEC RS
5399 resets QAM block
5400 resets SCU variables
5401 */
5402 status = write16(state, FEC_DI_COMM_EXEC__A, FEC_DI_COMM_EXEC_STOP);
Oliver Endrissebc7de22011-07-03 13:49:44 -03005403 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005404 goto error;
5405 status = write16(state, FEC_RS_COMM_EXEC__A, FEC_RS_COMM_EXEC_STOP);
5406 if (status < 0)
5407 goto error;
5408 status = QAMResetQAM(state);
5409 if (status < 0)
5410 goto error;
Oliver Endrissebc7de22011-07-03 13:49:44 -03005411
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005412 /*
5413 STEP 2: configure demodulator
5414 -set env
5415 -set params; resets IQM,QAM,FEC HW; initializes some SCU variables
5416 */
5417 status = QAMSetSymbolrate(state);
5418 if (status < 0)
5419 goto error;
5420
5421 /* Env parameters */
5422 setEnvParameters[2] = QAM_TOP_ANNEX_A; /* Annex */
5423 if (state->m_OperationMode == OM_QAM_ITU_C)
5424 setEnvParameters[2] = QAM_TOP_ANNEX_C; /* Annex */
5425 setParamParameters[3] |= (QAM_MIRROR_AUTO_ON);
5426 /* check for LOCKRANGE Extented */
5427 /* setParamParameters[3] |= QAM_LOCKRANGE_NORMAL; */
5428 parameterLen = 4;
5429
5430 /* Set params */
5431 switch (state->param.u.qam.modulation) {
5432 case QAM_256:
5433 state->m_Constellation = DRX_CONSTELLATION_QAM256;
5434 break;
5435 case QAM_AUTO:
5436 case QAM_64:
5437 state->m_Constellation = DRX_CONSTELLATION_QAM64;
5438 break;
5439 case QAM_16:
5440 state->m_Constellation = DRX_CONSTELLATION_QAM16;
5441 break;
5442 case QAM_32:
5443 state->m_Constellation = DRX_CONSTELLATION_QAM32;
5444 break;
5445 case QAM_128:
5446 state->m_Constellation = DRX_CONSTELLATION_QAM128;
5447 break;
5448 default:
5449 status = -EINVAL;
5450 break;
5451 }
5452 if (status < 0)
5453 goto error;
5454 setParamParameters[0] = state->m_Constellation; /* constellation */
5455 setParamParameters[1] = DRXK_QAM_I12_J17; /* interleave mode */
5456
5457 status = scu_command(state, SCU_RAM_COMMAND_STANDARD_QAM | SCU_RAM_COMMAND_CMD_DEMOD_SET_PARAM, 4, setParamParameters, 1, &cmdResult);
5458 if (status < 0)
5459 goto error;
5460
5461
5462 /* STEP 3: enable the system in a mode where the ADC provides valid signal
5463 setup constellation independent registers */
5464#if 0
5465 status = SetFrequency(channel, tunerFreqOffset));
5466 if (status < 0)
5467 goto error;
5468#endif
5469 status = SetFrequencyShifter(state, IntermediateFreqkHz, tunerFreqOffset, true);
5470 if (status < 0)
5471 goto error;
5472
5473 /* Setup BER measurement */
5474 status = SetQAMMeasurement(state, state->m_Constellation, state->param.u. qam.symbol_rate);
5475 if (status < 0)
5476 goto error;
5477
5478 /* Reset default values */
5479 status = write16(state, IQM_CF_SCALE_SH__A, IQM_CF_SCALE_SH__PRE);
5480 if (status < 0)
5481 goto error;
5482 status = write16(state, QAM_SY_TIMEOUT__A, QAM_SY_TIMEOUT__PRE);
5483 if (status < 0)
5484 goto error;
5485
5486 /* Reset default LC values */
5487 status = write16(state, QAM_LC_RATE_LIMIT__A, 3);
5488 if (status < 0)
5489 goto error;
5490 status = write16(state, QAM_LC_LPF_FACTORP__A, 4);
5491 if (status < 0)
5492 goto error;
5493 status = write16(state, QAM_LC_LPF_FACTORI__A, 4);
5494 if (status < 0)
5495 goto error;
5496 status = write16(state, QAM_LC_MODE__A, 7);
5497 if (status < 0)
5498 goto error;
5499
5500 status = write16(state, QAM_LC_QUAL_TAB0__A, 1);
5501 if (status < 0)
5502 goto error;
5503 status = write16(state, QAM_LC_QUAL_TAB1__A, 1);
5504 if (status < 0)
5505 goto error;
5506 status = write16(state, QAM_LC_QUAL_TAB2__A, 1);
5507 if (status < 0)
5508 goto error;
5509 status = write16(state, QAM_LC_QUAL_TAB3__A, 1);
5510 if (status < 0)
5511 goto error;
5512 status = write16(state, QAM_LC_QUAL_TAB4__A, 2);
5513 if (status < 0)
5514 goto error;
5515 status = write16(state, QAM_LC_QUAL_TAB5__A, 2);
5516 if (status < 0)
5517 goto error;
5518 status = write16(state, QAM_LC_QUAL_TAB6__A, 2);
5519 if (status < 0)
5520 goto error;
5521 status = write16(state, QAM_LC_QUAL_TAB8__A, 2);
5522 if (status < 0)
5523 goto error;
5524 status = write16(state, QAM_LC_QUAL_TAB9__A, 2);
5525 if (status < 0)
5526 goto error;
5527 status = write16(state, QAM_LC_QUAL_TAB10__A, 2);
5528 if (status < 0)
5529 goto error;
5530 status = write16(state, QAM_LC_QUAL_TAB12__A, 2);
5531 if (status < 0)
5532 goto error;
5533 status = write16(state, QAM_LC_QUAL_TAB15__A, 3);
5534 if (status < 0)
5535 goto error;
5536 status = write16(state, QAM_LC_QUAL_TAB16__A, 3);
5537 if (status < 0)
5538 goto error;
5539 status = write16(state, QAM_LC_QUAL_TAB20__A, 4);
5540 if (status < 0)
5541 goto error;
5542 status = write16(state, QAM_LC_QUAL_TAB25__A, 4);
5543 if (status < 0)
5544 goto error;
5545
5546 /* Mirroring, QAM-block starting point not inverted */
5547 status = write16(state, QAM_SY_SP_INV__A, QAM_SY_SP_INV_SPECTRUM_INV_DIS);
5548 if (status < 0)
5549 goto error;
5550
5551 /* Halt SCU to enable safe non-atomic accesses */
5552 status = write16(state, SCU_COMM_EXEC__A, SCU_COMM_EXEC_HOLD);
5553 if (status < 0)
5554 goto error;
5555
5556 /* STEP 4: constellation specific setup */
5557 switch (state->param.u.qam.modulation) {
5558 case QAM_16:
5559 status = SetQAM16(state);
5560 break;
5561 case QAM_32:
5562 status = SetQAM32(state);
5563 break;
5564 case QAM_AUTO:
5565 case QAM_64:
5566 status = SetQAM64(state);
5567 break;
5568 case QAM_128:
5569 status = SetQAM128(state);
5570 break;
5571 case QAM_256:
5572 status = SetQAM256(state);
5573 break;
5574 default:
5575 status = -EINVAL;
5576 break;
5577 }
5578 if (status < 0)
5579 goto error;
5580
5581 /* Activate SCU to enable SCU commands */
5582 status = write16(state, SCU_COMM_EXEC__A, SCU_COMM_EXEC_ACTIVE);
5583 if (status < 0)
5584 goto error;
5585
5586 /* Re-configure MPEG output, requires knowledge of channel bitrate */
5587 /* extAttr->currentChannel.constellation = channel->constellation; */
5588 /* extAttr->currentChannel.symbolrate = channel->symbolrate; */
5589 status = MPEGTSDtoSetup(state, state->m_OperationMode);
5590 if (status < 0)
5591 goto error;
5592
5593 /* Start processes */
5594 status = MPEGTSStart(state);
5595 if (status < 0)
5596 goto error;
5597 status = write16(state, FEC_COMM_EXEC__A, FEC_COMM_EXEC_ACTIVE);
5598 if (status < 0)
5599 goto error;
5600 status = write16(state, QAM_COMM_EXEC__A, QAM_COMM_EXEC_ACTIVE);
5601 if (status < 0)
5602 goto error;
5603 status = write16(state, IQM_COMM_EXEC__A, IQM_COMM_EXEC_B_ACTIVE);
5604 if (status < 0)
5605 goto error;
5606
5607 /* STEP 5: start QAM demodulator (starts FEC, QAM and IQM HW) */
5608 status = scu_command(state, SCU_RAM_COMMAND_STANDARD_QAM | SCU_RAM_COMMAND_CMD_DEMOD_START, 0, NULL, 1, &cmdResult);
5609 if (status < 0)
5610 goto error;
5611
5612 /* update global DRXK data container */
5613/*? extAttr->qamInterleaveMode = DRXK_QAM_I12_J17; */
5614
5615error:
5616 if (status < 0)
5617 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
Oliver Endrissebc7de22011-07-03 13:49:44 -03005618 return status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005619}
5620
Oliver Endrissebc7de22011-07-03 13:49:44 -03005621static int SetQAMStandard(struct drxk_state *state,
5622 enum OperationMode oMode)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005623{
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005624 int status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005625#ifdef DRXK_QAM_TAPS
5626#define DRXK_QAMA_TAPS_SELECT
5627#include "drxk_filters.h"
5628#undef DRXK_QAMA_TAPS_SELECT
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005629#endif
5630
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005631 /* added antenna switch */
5632 SwitchAntennaToQAM(state);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005633
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005634 /* Ensure correct power-up mode */
5635 status = PowerUpQAM(state);
5636 if (status < 0)
5637 goto error;
5638 /* Reset QAM block */
5639 status = QAMResetQAM(state);
5640 if (status < 0)
5641 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005642
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005643 /* Setup IQM */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005644
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005645 status = write16(state, IQM_COMM_EXEC__A, IQM_COMM_EXEC_B_STOP);
5646 if (status < 0)
5647 goto error;
5648 status = write16(state, IQM_AF_AMUX__A, IQM_AF_AMUX_SIGNAL2ADC);
5649 if (status < 0)
5650 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005651
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005652 /* Upload IQM Channel Filter settings by
5653 boot loader from ROM table */
5654 switch (oMode) {
5655 case OM_QAM_ITU_A:
5656 status = BLChainCmd(state, DRXK_BL_ROM_OFFSET_TAPS_ITU_A, DRXK_BLCC_NR_ELEMENTS_TAPS, DRXK_BLC_TIMEOUT);
5657 break;
5658 case OM_QAM_ITU_C:
5659 status = BLDirectCmd(state, IQM_CF_TAP_RE0__A, DRXK_BL_ROM_OFFSET_TAPS_ITU_C, DRXK_BLDC_NR_ELEMENTS_TAPS, DRXK_BLC_TIMEOUT);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03005660 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005661 goto error;
5662 status = BLDirectCmd(state, IQM_CF_TAP_IM0__A, DRXK_BL_ROM_OFFSET_TAPS_ITU_C, DRXK_BLDC_NR_ELEMENTS_TAPS, DRXK_BLC_TIMEOUT);
5663 break;
5664 default:
5665 status = -EINVAL;
5666 }
5667 if (status < 0)
5668 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005669
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005670 status = write16(state, IQM_CF_OUT_ENA__A, (1 << IQM_CF_OUT_ENA_QAM__B));
5671 if (status < 0)
5672 goto error;
5673 status = write16(state, IQM_CF_SYMMETRIC__A, 0);
5674 if (status < 0)
5675 goto error;
5676 status = write16(state, IQM_CF_MIDTAP__A, ((1 << IQM_CF_MIDTAP_RE__B) | (1 << IQM_CF_MIDTAP_IM__B)));
5677 if (status < 0)
5678 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005679
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005680 status = write16(state, IQM_RC_STRETCH__A, 21);
5681 if (status < 0)
5682 goto error;
5683 status = write16(state, IQM_AF_CLP_LEN__A, 0);
5684 if (status < 0)
5685 goto error;
5686 status = write16(state, IQM_AF_CLP_TH__A, 448);
5687 if (status < 0)
5688 goto error;
5689 status = write16(state, IQM_AF_SNS_LEN__A, 0);
5690 if (status < 0)
5691 goto error;
5692 status = write16(state, IQM_CF_POW_MEAS_LEN__A, 0);
5693 if (status < 0)
5694 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005695
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005696 status = write16(state, IQM_FS_ADJ_SEL__A, 1);
5697 if (status < 0)
5698 goto error;
5699 status = write16(state, IQM_RC_ADJ_SEL__A, 1);
5700 if (status < 0)
5701 goto error;
5702 status = write16(state, IQM_CF_ADJ_SEL__A, 1);
5703 if (status < 0)
5704 goto error;
5705 status = write16(state, IQM_AF_UPD_SEL__A, 0);
5706 if (status < 0)
5707 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005708
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005709 /* IQM Impulse Noise Processing Unit */
5710 status = write16(state, IQM_CF_CLP_VAL__A, 500);
5711 if (status < 0)
5712 goto error;
5713 status = write16(state, IQM_CF_DATATH__A, 1000);
5714 if (status < 0)
5715 goto error;
5716 status = write16(state, IQM_CF_BYPASSDET__A, 1);
5717 if (status < 0)
5718 goto error;
5719 status = write16(state, IQM_CF_DET_LCT__A, 0);
5720 if (status < 0)
5721 goto error;
5722 status = write16(state, IQM_CF_WND_LEN__A, 1);
5723 if (status < 0)
5724 goto error;
5725 status = write16(state, IQM_CF_PKDTH__A, 1);
5726 if (status < 0)
5727 goto error;
5728 status = write16(state, IQM_AF_INC_BYPASS__A, 1);
5729 if (status < 0)
5730 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005731
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005732 /* turn on IQMAF. Must be done before setAgc**() */
5733 status = SetIqmAf(state, true);
5734 if (status < 0)
5735 goto error;
5736 status = write16(state, IQM_AF_START_LOCK__A, 0x01);
5737 if (status < 0)
5738 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005739
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005740 /* IQM will not be reset from here, sync ADC and update/init AGC */
5741 status = ADCSynchronization(state);
5742 if (status < 0)
5743 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005744
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005745 /* Set the FSM step period */
5746 status = write16(state, SCU_RAM_QAM_FSM_STEP_PERIOD__A, 2000);
5747 if (status < 0)
5748 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005749
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005750 /* Halt SCU to enable safe non-atomic accesses */
5751 status = write16(state, SCU_COMM_EXEC__A, SCU_COMM_EXEC_HOLD);
5752 if (status < 0)
5753 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005754
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005755 /* No more resets of the IQM, current standard correctly set =>
5756 now AGCs can be configured. */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005757
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005758 status = InitAGC(state, true);
5759 if (status < 0)
5760 goto error;
5761 status = SetPreSaw(state, &(state->m_qamPreSawCfg));
5762 if (status < 0)
5763 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005764
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005765 /* Configure AGC's */
5766 status = SetAgcRf(state, &(state->m_qamRfAgcCfg), true);
5767 if (status < 0)
5768 goto error;
5769 status = SetAgcIf(state, &(state->m_qamIfAgcCfg), true);
5770 if (status < 0)
5771 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005772
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005773 /* Activate SCU to enable SCU commands */
5774 status = write16(state, SCU_COMM_EXEC__A, SCU_COMM_EXEC_ACTIVE);
5775error:
5776 if (status < 0)
5777 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
Oliver Endrissebc7de22011-07-03 13:49:44 -03005778 return status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005779}
5780
5781static int WriteGPIO(struct drxk_state *state)
5782{
Oliver Endrissebc7de22011-07-03 13:49:44 -03005783 int status;
5784 u16 value = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005785
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03005786 dprintk(1, "\n");
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005787 /* stop lock indicator process */
5788 status = write16(state, SCU_RAM_GPIO__A, SCU_RAM_GPIO_HW_LOCK_IND_DISABLE);
5789 if (status < 0)
5790 goto error;
5791
5792 /* Write magic word to enable pdr reg write */
5793 status = write16(state, SIO_TOP_COMM_KEY__A, SIO_TOP_COMM_KEY_KEY);
5794 if (status < 0)
5795 goto error;
5796
5797 if (state->m_hasSAWSW) {
5798 /* write to io pad configuration register - output mode */
5799 status = write16(state, SIO_PDR_SMA_TX_CFG__A, state->m_GPIOCfg);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03005800 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005801 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005802
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005803 /* use corresponding bit in io data output registar */
5804 status = read16(state, SIO_PDR_UIO_OUT_LO__A, &value);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03005805 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005806 goto error;
5807 if (state->m_GPIO == 0)
5808 value &= 0x7FFF; /* write zero to 15th bit - 1st UIO */
5809 else
5810 value |= 0x8000; /* write one to 15th bit - 1st UIO */
5811 /* write back to io data output register */
5812 status = write16(state, SIO_PDR_UIO_OUT_LO__A, value);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03005813 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005814 goto error;
5815
5816 }
5817 /* Write magic word to disable pdr reg write */
5818 status = write16(state, SIO_TOP_COMM_KEY__A, 0x0000);
5819error:
5820 if (status < 0)
5821 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
Oliver Endrissebc7de22011-07-03 13:49:44 -03005822 return status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005823}
5824
5825static int SwitchAntennaToQAM(struct drxk_state *state)
5826{
Mauro Carvalho Chehab147e1102011-07-10 08:24:26 -03005827 int status = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005828
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03005829 dprintk(1, "\n");
Mauro Carvalho Chehab147e1102011-07-10 08:24:26 -03005830
Oliver Endrissebc7de22011-07-03 13:49:44 -03005831 if (state->m_AntennaSwitchDVBTDVBC != 0) {
5832 if (state->m_GPIO != state->m_AntennaDVBC) {
5833 state->m_GPIO = state->m_AntennaDVBC;
5834 status = WriteGPIO(state);
5835 }
5836 }
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005837 if (status < 0)
5838 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
Oliver Endrissebc7de22011-07-03 13:49:44 -03005839 return status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005840}
5841
5842static int SwitchAntennaToDVBT(struct drxk_state *state)
5843{
Mauro Carvalho Chehab147e1102011-07-10 08:24:26 -03005844 int status = 0;
Oliver Endrissebc7de22011-07-03 13:49:44 -03005845
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03005846 dprintk(1, "\n");
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005847 if (state->m_AntennaSwitchDVBTDVBC != 0) {
5848 if (state->m_GPIO != state->m_AntennaDVBT) {
5849 state->m_GPIO = state->m_AntennaDVBT;
5850 status = WriteGPIO(state);
5851 }
5852 }
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005853 if (status < 0)
5854 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005855 return status;
5856}
5857
5858
5859static int PowerDownDevice(struct drxk_state *state)
5860{
5861 /* Power down to requested mode */
5862 /* Backup some register settings */
5863 /* Set pins with possible pull-ups connected to them in input mode */
5864 /* Analog power down */
5865 /* ADC power down */
5866 /* Power down device */
5867 int status;
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03005868
5869 dprintk(1, "\n");
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005870 if (state->m_bPDownOpenBridge) {
5871 /* Open I2C bridge before power down of DRXK */
5872 status = ConfigureI2CBridge(state, true);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03005873 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005874 goto error;
5875 }
5876 /* driver 0.9.0 */
5877 status = DVBTEnableOFDMTokenRing(state, false);
Oliver Endrissebc7de22011-07-03 13:49:44 -03005878 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005879 goto error;
Oliver Endrissebc7de22011-07-03 13:49:44 -03005880
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005881 status = write16(state, SIO_CC_PWD_MODE__A, SIO_CC_PWD_MODE_LEVEL_CLOCK);
5882 if (status < 0)
5883 goto error;
5884 status = write16(state, SIO_CC_UPDATE__A, SIO_CC_UPDATE_KEY);
5885 if (status < 0)
5886 goto error;
5887 state->m_HICfgCtrl |= SIO_HI_RA_RAM_PAR_5_CFG_SLEEP_ZZZ;
5888 status = HI_CfgCommand(state);
5889error:
5890 if (status < 0)
5891 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
5892
5893 return status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005894}
5895
Mauro Carvalho Chehabe4f4f872011-07-09 17:35:26 -03005896static int load_microcode(struct drxk_state *state, const char *mc_name)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005897{
5898 const struct firmware *fw = NULL;
Oliver Endrissebc7de22011-07-03 13:49:44 -03005899 int err = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005900
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03005901 dprintk(1, "\n");
5902
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005903 err = request_firmware(&fw, mc_name, state->i2c->dev.parent);
5904 if (err < 0) {
5905 printk(KERN_ERR
Mauro Carvalho Chehabe0e6eca2011-07-04 08:27:47 -03005906 "drxk: Could not load firmware file %s.\n", mc_name);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005907 printk(KERN_INFO
Mauro Carvalho Chehabe0e6eca2011-07-04 08:27:47 -03005908 "drxk: Copy %s to your hotplug directory!\n", mc_name);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005909 return err;
5910 }
Oliver Endrissebc7de22011-07-03 13:49:44 -03005911 err = DownloadMicrocode(state, fw->data, fw->size);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005912 release_firmware(fw);
5913 return err;
5914}
5915
5916static int init_drxk(struct drxk_state *state)
5917{
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005918 int status = 0;
Oliver Endrissebc7de22011-07-03 13:49:44 -03005919 enum DRXPowerMode powerMode = DRXK_POWER_DOWN_OFDM;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005920 u16 driverVersion;
5921
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03005922 dprintk(1, "\n");
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005923 if ((state->m_DrxkState == DRXK_UNINITIALIZED)) {
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005924 status = PowerUpDevice(state);
5925 if (status < 0)
5926 goto error;
5927 status = DRXX_Open(state);
5928 if (status < 0)
5929 goto error;
5930 /* Soft reset of OFDM-, sys- and osc-clockdomain */
5931 status = write16(state, SIO_CC_SOFT_RST__A, SIO_CC_SOFT_RST_OFDM__M | SIO_CC_SOFT_RST_SYS__M | SIO_CC_SOFT_RST_OSC__M);
5932 if (status < 0)
5933 goto error;
5934 status = write16(state, SIO_CC_UPDATE__A, SIO_CC_UPDATE_KEY);
5935 if (status < 0)
5936 goto error;
5937 /* TODO is this needed, if yes how much delay in worst case scenario */
5938 msleep(1);
5939 state->m_DRXK_A3_PATCH_CODE = true;
5940 status = GetDeviceCapabilities(state);
5941 if (status < 0)
5942 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005943
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005944 /* Bridge delay, uses oscilator clock */
5945 /* Delay = (delay (nano seconds) * oscclk (kHz))/ 1000 */
5946 /* SDA brdige delay */
5947 state->m_HICfgBridgeDelay =
5948 (u16) ((state->m_oscClockFreq / 1000) *
5949 HI_I2C_BRIDGE_DELAY) / 1000;
5950 /* Clipping */
5951 if (state->m_HICfgBridgeDelay >
5952 SIO_HI_RA_RAM_PAR_3_CFG_DBL_SDA__M) {
Oliver Endrissebc7de22011-07-03 13:49:44 -03005953 state->m_HICfgBridgeDelay =
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005954 SIO_HI_RA_RAM_PAR_3_CFG_DBL_SDA__M;
5955 }
5956 /* SCL bridge delay, same as SDA for now */
5957 state->m_HICfgBridgeDelay +=
5958 state->m_HICfgBridgeDelay <<
5959 SIO_HI_RA_RAM_PAR_3_CFG_DBL_SCL__B;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005960
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005961 status = InitHI(state);
5962 if (status < 0)
5963 goto error;
5964 /* disable various processes */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005965#if NOA1ROM
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005966 if (!(state->m_DRXK_A1_ROM_CODE)
5967 && !(state->m_DRXK_A2_ROM_CODE))
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005968#endif
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005969 {
5970 status = write16(state, SCU_RAM_GPIO__A, SCU_RAM_GPIO_HW_LOCK_IND_DISABLE);
5971 if (status < 0)
5972 goto error;
5973 }
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005974
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005975 /* disable MPEG port */
5976 status = MPEGTSDisable(state);
5977 if (status < 0)
5978 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005979
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005980 /* Stop AUD and SCU */
5981 status = write16(state, AUD_COMM_EXEC__A, AUD_COMM_EXEC_STOP);
5982 if (status < 0)
5983 goto error;
5984 status = write16(state, SCU_COMM_EXEC__A, SCU_COMM_EXEC_STOP);
5985 if (status < 0)
5986 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005987
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005988 /* enable token-ring bus through OFDM block for possible ucode upload */
5989 status = write16(state, SIO_OFDM_SH_OFDM_RING_ENABLE__A, SIO_OFDM_SH_OFDM_RING_ENABLE_ON);
5990 if (status < 0)
5991 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005992
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005993 /* include boot loader section */
5994 status = write16(state, SIO_BL_COMM_EXEC__A, SIO_BL_COMM_EXEC_ACTIVE);
5995 if (status < 0)
5996 goto error;
5997 status = BLChainCmd(state, 0, 6, 100);
5998 if (status < 0)
5999 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006000
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006001 if (!state->microcode_name)
6002 load_microcode(state, "drxk_a3.mc");
6003 else
6004 load_microcode(state, state->microcode_name);
Mauro Carvalho Chehabe4f4f872011-07-09 17:35:26 -03006005
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006006 /* disable token-ring bus through OFDM block for possible ucode upload */
6007 status = write16(state, SIO_OFDM_SH_OFDM_RING_ENABLE__A, SIO_OFDM_SH_OFDM_RING_ENABLE_OFF);
6008 if (status < 0)
6009 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006010
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006011 /* Run SCU for a little while to initialize microcode version numbers */
6012 status = write16(state, SCU_COMM_EXEC__A, SCU_COMM_EXEC_ACTIVE);
6013 if (status < 0)
6014 goto error;
6015 status = DRXX_Open(state);
6016 if (status < 0)
6017 goto error;
6018 /* added for test */
6019 msleep(30);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006020
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006021 powerMode = DRXK_POWER_DOWN_OFDM;
6022 status = CtrlPowerMode(state, &powerMode);
6023 if (status < 0)
6024 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006025
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006026 /* Stamp driver version number in SCU data RAM in BCD code
6027 Done to enable field application engineers to retreive drxdriver version
6028 via I2C from SCU RAM.
6029 Not using SCU command interface for SCU register access since no
6030 microcode may be present.
6031 */
6032 driverVersion =
6033 (((DRXK_VERSION_MAJOR / 100) % 10) << 12) +
6034 (((DRXK_VERSION_MAJOR / 10) % 10) << 8) +
6035 ((DRXK_VERSION_MAJOR % 10) << 4) +
6036 (DRXK_VERSION_MINOR % 10);
6037 status = write16(state, SCU_RAM_DRIVER_VER_HI__A, driverVersion);
6038 if (status < 0)
6039 goto error;
6040 driverVersion =
6041 (((DRXK_VERSION_PATCH / 1000) % 10) << 12) +
6042 (((DRXK_VERSION_PATCH / 100) % 10) << 8) +
6043 (((DRXK_VERSION_PATCH / 10) % 10) << 4) +
6044 (DRXK_VERSION_PATCH % 10);
6045 status = write16(state, SCU_RAM_DRIVER_VER_LO__A, driverVersion);
6046 if (status < 0)
6047 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006048
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006049 printk(KERN_INFO "DRXK driver version %d.%d.%d\n",
6050 DRXK_VERSION_MAJOR, DRXK_VERSION_MINOR,
6051 DRXK_VERSION_PATCH);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006052
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006053 /* Dirty fix of default values for ROM/PATCH microcode
6054 Dirty because this fix makes it impossible to setup suitable values
6055 before calling DRX_Open. This solution requires changes to RF AGC speed
6056 to be done via the CTRL function after calling DRX_Open */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006057
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006058 /* m_dvbtRfAgcCfg.speed = 3; */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006059
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006060 /* Reset driver debug flags to 0 */
6061 status = write16(state, SCU_RAM_DRIVER_DEBUG__A, 0);
6062 if (status < 0)
6063 goto error;
6064 /* driver 0.9.0 */
6065 /* Setup FEC OC:
6066 NOTE: No more full FEC resets allowed afterwards!! */
6067 status = write16(state, FEC_COMM_EXEC__A, FEC_COMM_EXEC_STOP);
6068 if (status < 0)
6069 goto error;
6070 /* MPEGTS functions are still the same */
6071 status = MPEGTSDtoInit(state);
6072 if (status < 0)
6073 goto error;
6074 status = MPEGTSStop(state);
6075 if (status < 0)
6076 goto error;
6077 status = MPEGTSConfigurePolarity(state);
6078 if (status < 0)
6079 goto error;
6080 status = MPEGTSConfigurePins(state, state->m_enableMPEGOutput);
6081 if (status < 0)
6082 goto error;
6083 /* added: configure GPIO */
6084 status = WriteGPIO(state);
6085 if (status < 0)
6086 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006087
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006088 state->m_DrxkState = DRXK_STOPPED;
6089
6090 if (state->m_bPowerDown) {
6091 status = PowerDownDevice(state);
6092 if (status < 0)
6093 goto error;
6094 state->m_DrxkState = DRXK_POWERED_DOWN;
6095 } else
Oliver Endrissebc7de22011-07-03 13:49:44 -03006096 state->m_DrxkState = DRXK_STOPPED;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006097 }
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006098error:
6099 if (status < 0)
6100 printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006101
6102 return 0;
6103}
6104
Oliver Endrissebc7de22011-07-03 13:49:44 -03006105static void drxk_c_release(struct dvb_frontend *fe)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006106{
Oliver Endrissebc7de22011-07-03 13:49:44 -03006107 struct drxk_state *state = fe->demodulator_priv;
6108
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03006109 dprintk(1, "\n");
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006110 kfree(state);
6111}
6112
Oliver Endrissebc7de22011-07-03 13:49:44 -03006113static int drxk_c_init(struct dvb_frontend *fe)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006114{
Oliver Endrissebc7de22011-07-03 13:49:44 -03006115 struct drxk_state *state = fe->demodulator_priv;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006116
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03006117 dprintk(1, "\n");
Oliver Endrissebc7de22011-07-03 13:49:44 -03006118 if (mutex_trylock(&state->ctlock) == 0)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006119 return -EBUSY;
6120 SetOperationMode(state, OM_QAM_ITU_A);
6121 return 0;
6122}
6123
Oliver Endrissebc7de22011-07-03 13:49:44 -03006124static int drxk_c_sleep(struct dvb_frontend *fe)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006125{
Oliver Endrissebc7de22011-07-03 13:49:44 -03006126 struct drxk_state *state = fe->demodulator_priv;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006127
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03006128 dprintk(1, "\n");
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006129 ShutDown(state);
6130 mutex_unlock(&state->ctlock);
6131 return 0;
6132}
6133
Oliver Endrissebc7de22011-07-03 13:49:44 -03006134static int drxk_gate_ctrl(struct dvb_frontend *fe, int enable)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006135{
6136 struct drxk_state *state = fe->demodulator_priv;
6137
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03006138 dprintk(1, "%s\n", enable ? "enable" : "disable");
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006139 return ConfigureI2CBridge(state, enable ? true : false);
6140}
6141
Oliver Endrissebc7de22011-07-03 13:49:44 -03006142static int drxk_set_parameters(struct dvb_frontend *fe,
6143 struct dvb_frontend_parameters *p)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006144{
6145 struct drxk_state *state = fe->demodulator_priv;
6146 u32 IF;
6147
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03006148 dprintk(1, "\n");
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006149 if (fe->ops.i2c_gate_ctrl)
6150 fe->ops.i2c_gate_ctrl(fe, 1);
6151 if (fe->ops.tuner_ops.set_params)
6152 fe->ops.tuner_ops.set_params(fe, p);
6153 if (fe->ops.i2c_gate_ctrl)
6154 fe->ops.i2c_gate_ctrl(fe, 0);
Oliver Endrissebc7de22011-07-03 13:49:44 -03006155 state->param = *p;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006156 fe->ops.tuner_ops.get_frequency(fe, &IF);
6157 Start(state, 0, IF);
6158
Mauro Carvalho Chehabe0e6eca2011-07-04 08:27:47 -03006159 /* printk(KERN_DEBUG "drxk: %s IF=%d done\n", __func__, IF); */
Oliver Endrissebc7de22011-07-03 13:49:44 -03006160
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006161 return 0;
6162}
6163
Oliver Endrissebc7de22011-07-03 13:49:44 -03006164static int drxk_c_get_frontend(struct dvb_frontend *fe,
6165 struct dvb_frontend_parameters *p)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006166{
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03006167 dprintk(1, "\n");
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006168 return 0;
6169}
6170
6171static int drxk_read_status(struct dvb_frontend *fe, fe_status_t *status)
6172{
6173 struct drxk_state *state = fe->demodulator_priv;
6174 u32 stat;
6175
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03006176 dprintk(1, "\n");
Oliver Endrissebc7de22011-07-03 13:49:44 -03006177 *status = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006178 GetLockStatus(state, &stat, 0);
Oliver Endrissebc7de22011-07-03 13:49:44 -03006179 if (stat == MPEG_LOCK)
6180 *status |= 0x1f;
6181 if (stat == FEC_LOCK)
6182 *status |= 0x0f;
6183 if (stat == DEMOD_LOCK)
6184 *status |= 0x07;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006185 return 0;
6186}
6187
6188static int drxk_read_ber(struct dvb_frontend *fe, u32 *ber)
6189{
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03006190 dprintk(1, "\n");
6191
Oliver Endrissebc7de22011-07-03 13:49:44 -03006192 *ber = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006193 return 0;
6194}
6195
Oliver Endrissebc7de22011-07-03 13:49:44 -03006196static int drxk_read_signal_strength(struct dvb_frontend *fe,
6197 u16 *strength)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006198{
6199 struct drxk_state *state = fe->demodulator_priv;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006200 u32 val = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006201
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03006202 dprintk(1, "\n");
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006203 ReadIFAgc(state, &val);
Oliver Endrissebc7de22011-07-03 13:49:44 -03006204 *strength = val & 0xffff;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006205 return 0;
6206}
6207
6208static int drxk_read_snr(struct dvb_frontend *fe, u16 *snr)
6209{
6210 struct drxk_state *state = fe->demodulator_priv;
6211 s32 snr2;
6212
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03006213 dprintk(1, "\n");
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006214 GetSignalToNoise(state, &snr2);
Oliver Endrissebc7de22011-07-03 13:49:44 -03006215 *snr = snr2 & 0xffff;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006216 return 0;
6217}
6218
6219static int drxk_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
6220{
6221 struct drxk_state *state = fe->demodulator_priv;
6222 u16 err;
6223
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03006224 dprintk(1, "\n");
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006225 DVBTQAMGetAccPktErr(state, &err);
6226 *ucblocks = (u32) err;
6227 return 0;
6228}
6229
Oliver Endrissebc7de22011-07-03 13:49:44 -03006230static int drxk_c_get_tune_settings(struct dvb_frontend *fe, struct dvb_frontend_tune_settings
6231 *sets)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006232{
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03006233 dprintk(1, "\n");
Oliver Endrissebc7de22011-07-03 13:49:44 -03006234 sets->min_delay_ms = 3000;
6235 sets->max_drift = 0;
6236 sets->step_size = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006237 return 0;
6238}
6239
Oliver Endrissebc7de22011-07-03 13:49:44 -03006240static void drxk_t_release(struct dvb_frontend *fe)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006241{
Oliver Endrissebc7de22011-07-03 13:49:44 -03006242#if 0
6243 struct drxk_state *state = fe->demodulator_priv;
6244
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03006245 dprintk(1, "\n");
Oliver Endrissebc7de22011-07-03 13:49:44 -03006246 kfree(state);
6247#endif
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006248}
6249
Oliver Endrissebc7de22011-07-03 13:49:44 -03006250static int drxk_t_init(struct dvb_frontend *fe)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006251{
Oliver Endrissebc7de22011-07-03 13:49:44 -03006252 struct drxk_state *state = fe->demodulator_priv;
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03006253
6254 dprintk(1, "\n");
Oliver Endrissebc7de22011-07-03 13:49:44 -03006255 if (mutex_trylock(&state->ctlock) == 0)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006256 return -EBUSY;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006257 SetOperationMode(state, OM_DVBT);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006258 return 0;
6259}
6260
Oliver Endrissebc7de22011-07-03 13:49:44 -03006261static int drxk_t_sleep(struct dvb_frontend *fe)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006262{
Oliver Endrissebc7de22011-07-03 13:49:44 -03006263 struct drxk_state *state = fe->demodulator_priv;
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03006264
6265 dprintk(1, "\n");
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006266 mutex_unlock(&state->ctlock);
6267 return 0;
6268}
6269
Oliver Endrissebc7de22011-07-03 13:49:44 -03006270static int drxk_t_get_frontend(struct dvb_frontend *fe,
6271 struct dvb_frontend_parameters *p)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006272{
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03006273 dprintk(1, "\n");
6274
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006275 return 0;
6276}
6277
6278static struct dvb_frontend_ops drxk_c_ops = {
6279 .info = {
Oliver Endrissebc7de22011-07-03 13:49:44 -03006280 .name = "DRXK DVB-C",
6281 .type = FE_QAM,
6282 .frequency_stepsize = 62500,
6283 .frequency_min = 47000000,
6284 .frequency_max = 862000000,
6285 .symbol_rate_min = 870000,
6286 .symbol_rate_max = 11700000,
6287 .caps = FE_CAN_QAM_16 | FE_CAN_QAM_32 | FE_CAN_QAM_64 |
6288 FE_CAN_QAM_128 | FE_CAN_QAM_256 | FE_CAN_FEC_AUTO},
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006289 .release = drxk_c_release,
6290 .init = drxk_c_init,
6291 .sleep = drxk_c_sleep,
6292 .i2c_gate_ctrl = drxk_gate_ctrl,
6293
6294 .set_frontend = drxk_set_parameters,
6295 .get_frontend = drxk_c_get_frontend,
6296 .get_tune_settings = drxk_c_get_tune_settings,
6297
6298 .read_status = drxk_read_status,
6299 .read_ber = drxk_read_ber,
6300 .read_signal_strength = drxk_read_signal_strength,
6301 .read_snr = drxk_read_snr,
6302 .read_ucblocks = drxk_read_ucblocks,
6303};
6304
6305static struct dvb_frontend_ops drxk_t_ops = {
6306 .info = {
Oliver Endrissebc7de22011-07-03 13:49:44 -03006307 .name = "DRXK DVB-T",
6308 .type = FE_OFDM,
6309 .frequency_min = 47125000,
6310 .frequency_max = 865000000,
6311 .frequency_stepsize = 166667,
6312 .frequency_tolerance = 0,
6313 .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 |
6314 FE_CAN_FEC_3_4 | FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 |
6315 FE_CAN_FEC_AUTO |
6316 FE_CAN_QAM_16 | FE_CAN_QAM_64 |
6317 FE_CAN_QAM_AUTO |
6318 FE_CAN_TRANSMISSION_MODE_AUTO |
6319 FE_CAN_GUARD_INTERVAL_AUTO |
6320 FE_CAN_HIERARCHY_AUTO | FE_CAN_RECOVER | FE_CAN_MUTE_TS},
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006321 .release = drxk_t_release,
6322 .init = drxk_t_init,
6323 .sleep = drxk_t_sleep,
6324 .i2c_gate_ctrl = drxk_gate_ctrl,
6325
6326 .set_frontend = drxk_set_parameters,
6327 .get_frontend = drxk_t_get_frontend,
6328
6329 .read_status = drxk_read_status,
6330 .read_ber = drxk_read_ber,
6331 .read_signal_strength = drxk_read_signal_strength,
6332 .read_snr = drxk_read_snr,
6333 .read_ucblocks = drxk_read_ucblocks,
6334};
6335
Mauro Carvalho Chehab0fc55e82011-07-09 12:36:58 -03006336struct dvb_frontend *drxk_attach(const struct drxk_config *config,
6337 struct i2c_adapter *i2c,
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006338 struct dvb_frontend **fe_t)
6339{
6340 struct drxk_state *state = NULL;
Mauro Carvalho Chehab0fc55e82011-07-09 12:36:58 -03006341 u8 adr = config->adr;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006342
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03006343 dprintk(1, "\n");
Oliver Endrissebc7de22011-07-03 13:49:44 -03006344 state = kzalloc(sizeof(struct drxk_state), GFP_KERNEL);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006345 if (!state)
6346 return NULL;
6347
Oliver Endrissebc7de22011-07-03 13:49:44 -03006348 state->i2c = i2c;
6349 state->demod_address = adr;
Mauro Carvalho Chehabe076c922011-07-09 13:06:12 -03006350 state->single_master = config->single_master;
Mauro Carvalho Chehabe4f4f872011-07-09 17:35:26 -03006351 state->microcode_name = config->microcode_name;
Mauro Carvalho Chehabf1fe1b72011-07-09 21:59:33 -03006352 state->no_i2c_bridge = config->no_i2c_bridge;
Mauro Carvalho Chehab147e1102011-07-10 08:24:26 -03006353 state->m_AntennaSwitchDVBTDVBC = config->antenna_uses_gpio;
6354 state->m_AntennaDVBC = config->antenna_dvbc;
6355 state->m_AntennaDVBT = config->antenna_dvbt;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006356
6357 mutex_init(&state->mutex);
6358 mutex_init(&state->ctlock);
6359
Oliver Endrissebc7de22011-07-03 13:49:44 -03006360 memcpy(&state->c_frontend.ops, &drxk_c_ops,
6361 sizeof(struct dvb_frontend_ops));
6362 memcpy(&state->t_frontend.ops, &drxk_t_ops,
6363 sizeof(struct dvb_frontend_ops));
6364 state->c_frontend.demodulator_priv = state;
6365 state->t_frontend.demodulator_priv = state;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006366
6367 init_state(state);
Oliver Endrissebc7de22011-07-03 13:49:44 -03006368 if (init_drxk(state) < 0)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006369 goto error;
6370 *fe_t = &state->t_frontend;
6371 return &state->c_frontend;
6372
6373error:
Oliver Endrissebc7de22011-07-03 13:49:44 -03006374 printk(KERN_ERR "drxk: not found\n");
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006375 kfree(state);
6376 return NULL;
6377}
Oliver Endrissebc7de22011-07-03 13:49:44 -03006378EXPORT_SYMBOL(drxk_attach);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006379
6380MODULE_DESCRIPTION("DRX-K driver");
6381MODULE_AUTHOR("Ralph Metzler");
6382MODULE_LICENSE("GPL");