blob: 30a78af424cbb27a0ad41def9baabfaf41aaf027 [file] [log] [blame]
Ralph Metzler126f1e62011-03-12 23:44:33 -05001/*
2 * drxd_hard.c: DVB-T Demodulator Micronas DRX3975D-A2,DRX397xD-B1
3 *
4 * Copyright (C) 2003-2007 Micronas
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 "drxd.h"
36#include "drxd_firm.h"
37
Devin Heitmueller8f19f272011-03-13 02:11:07 -030038#define DRX_FW_FILENAME_A2 "drxd-a2-1.1.fw"
39#define DRX_FW_FILENAME_B1 "drxd-b1-1.1.fw"
40
Ralph Metzler126f1e62011-03-12 23:44:33 -050041#define CHUNK_SIZE 48
42
43#define DRX_I2C_RMW 0x10
44#define DRX_I2C_BROADCAST 0x20
45#define DRX_I2C_CLEARCRC 0x80
46#define DRX_I2C_SINGLE_MASTER 0xC0
47#define DRX_I2C_MODEFLAGS 0xC0
48#define DRX_I2C_FLAGS 0xF0
49
50#ifndef SIZEOF_ARRAY
51#define SIZEOF_ARRAY(array) (sizeof((array))/sizeof((array)[0]))
52#endif
53
54#define DEFAULT_LOCK_TIMEOUT 1100
55
56#define DRX_CHANNEL_AUTO 0
57#define DRX_CHANNEL_HIGH 1
58#define DRX_CHANNEL_LOW 2
59
60#define DRX_LOCK_MPEG 1
61#define DRX_LOCK_FEC 2
62#define DRX_LOCK_DEMOD 4
63
Ralph Metzler126f1e62011-03-12 23:44:33 -050064/****************************************************************************/
65
66enum CSCDState {
67 CSCD_INIT = 0,
68 CSCD_SET,
69 CSCD_SAVED
70};
71
72enum CDrxdState {
73 DRXD_UNINITIALIZED = 0,
74 DRXD_STOPPED,
75 DRXD_STARTED
76};
77
78enum AGC_CTRL_MODE {
79 AGC_CTRL_AUTO = 0,
80 AGC_CTRL_USER,
81 AGC_CTRL_OFF
82};
83
84enum OperationMode {
85 OM_Default,
86 OM_DVBT_Diversity_Front,
87 OM_DVBT_Diversity_End
88};
89
90struct SCfgAgc {
91 enum AGC_CTRL_MODE ctrlMode;
Devin Heitmueller6cacdd42011-03-24 13:44:01 -030092 u16 outputLevel; /* range [0, ... , 1023], 1/n of fullscale range */
93 u16 settleLevel; /* range [0, ... , 1023], 1/n of fullscale range */
94 u16 minOutputLevel; /* range [0, ... , 1023], 1/n of fullscale range */
95 u16 maxOutputLevel; /* range [0, ... , 1023], 1/n of fullscale range */
96 u16 speed; /* range [0, ... , 1023], 1/n of fullscale range */
Ralph Metzler126f1e62011-03-12 23:44:33 -050097
98 u16 R1;
99 u16 R2;
100 u16 R3;
101};
102
103struct SNoiseCal {
104 int cpOpt;
105 u16 cpNexpOfs;
106 u16 tdCal2k;
107 u16 tdCal8k;
108};
109
110enum app_env {
111 APPENV_STATIC = 0,
112 APPENV_PORTABLE = 1,
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300113 APPENV_MOBILE = 2
Ralph Metzler126f1e62011-03-12 23:44:33 -0500114};
115
116enum EIFFilter {
117 IFFILTER_SAW = 0,
118 IFFILTER_DISCRETE = 1
119};
120
121struct drxd_state {
122 struct dvb_frontend frontend;
123 struct dvb_frontend_ops ops;
124 struct dvb_frontend_parameters param;
125
126 const struct firmware *fw;
127 struct device *dev;
128
129 struct i2c_adapter *i2c;
130 void *priv;
131 struct drxd_config config;
132
133 int i2c_access;
134 int init_done;
Mauro Carvalho Chehab834751d2011-03-25 12:46:05 -0300135 struct mutex mutex;
Ralph Metzler126f1e62011-03-12 23:44:33 -0500136
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300137 u8 chip_adr;
Ralph Metzler126f1e62011-03-12 23:44:33 -0500138 u16 hi_cfg_timing_div;
139 u16 hi_cfg_bridge_delay;
140 u16 hi_cfg_wakeup_key;
141 u16 hi_cfg_ctrl;
142
143 u16 intermediate_freq;
144 u16 osc_clock_freq;
145
146 enum CSCDState cscd_state;
147 enum CDrxdState drxd_state;
148
149 u16 sys_clock_freq;
150 s16 osc_clock_deviation;
151 u16 expected_sys_clock_freq;
152
153 u16 insert_rs_byte;
154 u16 enable_parallel;
155
156 int operation_mode;
157
158 struct SCfgAgc if_agc_cfg;
159 struct SCfgAgc rf_agc_cfg;
160
161 struct SNoiseCal noise_cal;
162
163 u32 fe_fs_add_incr;
164 u32 org_fe_fs_add_incr;
165 u16 current_fe_if_incr;
166
167 u16 m_FeAgRegAgPwd;
168 u16 m_FeAgRegAgAgcSio;
169
170 u16 m_EcOcRegOcModeLop;
171 u16 m_EcOcRegSncSncLvl;
172 u8 *m_InitAtomicRead;
173 u8 *m_HiI2cPatch;
174
175 u8 *m_ResetCEFR;
176 u8 *m_InitFE_1;
177 u8 *m_InitFE_2;
178 u8 *m_InitCP;
179 u8 *m_InitCE;
180 u8 *m_InitEQ;
181 u8 *m_InitSC;
182 u8 *m_InitEC;
183 u8 *m_ResetECRAM;
184 u8 *m_InitDiversityFront;
185 u8 *m_InitDiversityEnd;
186 u8 *m_DisableDiversity;
187 u8 *m_StartDiversityFront;
188 u8 *m_StartDiversityEnd;
189
190 u8 *m_DiversityDelay8MHZ;
191 u8 *m_DiversityDelay6MHZ;
192
193 u8 *microcode;
194 u32 microcode_length;
195
196 int type_A;
197 int PGA;
198 int diversity;
199 int tuner_mirrors;
200
201 enum app_env app_env_default;
202 enum app_env app_env_diversity;
203
204};
205
Ralph Metzler126f1e62011-03-12 23:44:33 -0500206/****************************************************************************/
207/* I2C **********************************************************************/
208/****************************************************************************/
209
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300210static int i2c_write(struct i2c_adapter *adap, u8 adr, u8 * data, int len)
Ralph Metzler126f1e62011-03-12 23:44:33 -0500211{
Mauro Carvalho Chehab9999daf2011-03-25 12:10:05 -0300212 struct i2c_msg msg = {.addr = adr, .flags = 0, .buf = data, .len = len };
Ralph Metzler126f1e62011-03-12 23:44:33 -0500213
214 if (i2c_transfer(adap, &msg, 1) != 1)
215 return -1;
216 return 0;
217}
218
219static int i2c_read(struct i2c_adapter *adap,
Mauro Carvalho Chehab9999daf2011-03-25 12:10:05 -0300220 u8 adr, u8 *msg, int len, u8 *answ, int alen)
Ralph Metzler126f1e62011-03-12 23:44:33 -0500221{
Mauro Carvalho Chehab9999daf2011-03-25 12:10:05 -0300222 struct i2c_msg msgs[2] = {
223 {
224 .addr = adr, .flags = 0,
225 .buf = msg, .len = len
226 }, {
227 .addr = adr, .flags = I2C_M_RD,
228 .buf = answ, .len = alen
229 }
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300230 };
Ralph Metzler126f1e62011-03-12 23:44:33 -0500231 if (i2c_transfer(adap, msgs, 2) != 2)
232 return -1;
233 return 0;
234}
235
236inline u32 MulDiv32(u32 a, u32 b, u32 c)
237{
238 u64 tmp64;
239
Mauro Carvalho Chehab9999daf2011-03-25 12:10:05 -0300240 tmp64 = (u64)a * (u64)b;
Ralph Metzler126f1e62011-03-12 23:44:33 -0500241 do_div(tmp64, c);
242
243 return (u32) tmp64;
244}
245
Mauro Carvalho Chehab9999daf2011-03-25 12:10:05 -0300246static int Read16(struct drxd_state *state, u32 reg, u16 *data, u8 flags)
Ralph Metzler126f1e62011-03-12 23:44:33 -0500247{
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300248 u8 adr = state->config.demod_address;
249 u8 mm1[4] = { reg & 0xff, (reg >> 16) & 0xff,
250 flags | ((reg >> 24) & 0xff), (reg >> 8) & 0xff
251 };
Ralph Metzler126f1e62011-03-12 23:44:33 -0500252 u8 mm2[2];
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300253 if (i2c_read(state->i2c, adr, mm1, 4, mm2, 2) < 0)
Ralph Metzler126f1e62011-03-12 23:44:33 -0500254 return -1;
255 if (data)
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300256 *data = mm2[0] | (mm2[1] << 8);
257 return mm2[0] | (mm2[1] << 8);
Ralph Metzler126f1e62011-03-12 23:44:33 -0500258}
259
Mauro Carvalho Chehab9999daf2011-03-25 12:10:05 -0300260static int Read32(struct drxd_state *state, u32 reg, u32 *data, u8 flags)
Ralph Metzler126f1e62011-03-12 23:44:33 -0500261{
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300262 u8 adr = state->config.demod_address;
263 u8 mm1[4] = { reg & 0xff, (reg >> 16) & 0xff,
264 flags | ((reg >> 24) & 0xff), (reg >> 8) & 0xff
265 };
Ralph Metzler126f1e62011-03-12 23:44:33 -0500266 u8 mm2[4];
267
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300268 if (i2c_read(state->i2c, adr, mm1, 4, mm2, 4) < 0)
Ralph Metzler126f1e62011-03-12 23:44:33 -0500269 return -1;
270 if (data)
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300271 *data =
272 mm2[0] | (mm2[1] << 8) | (mm2[2] << 16) | (mm2[3] << 24);
Ralph Metzler126f1e62011-03-12 23:44:33 -0500273 return 0;
274}
275
276static int Write16(struct drxd_state *state, u32 reg, u16 data, u8 flags)
277{
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300278 u8 adr = state->config.demod_address;
279 u8 mm[6] = { reg & 0xff, (reg >> 16) & 0xff,
280 flags | ((reg >> 24) & 0xff), (reg >> 8) & 0xff,
281 data & 0xff, (data >> 8) & 0xff
282 };
Ralph Metzler126f1e62011-03-12 23:44:33 -0500283
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300284 if (i2c_write(state->i2c, adr, mm, 6) < 0)
Ralph Metzler126f1e62011-03-12 23:44:33 -0500285 return -1;
286 return 0;
287}
288
289static int Write32(struct drxd_state *state, u32 reg, u32 data, u8 flags)
290{
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300291 u8 adr = state->config.demod_address;
292 u8 mm[8] = { reg & 0xff, (reg >> 16) & 0xff,
293 flags | ((reg >> 24) & 0xff), (reg >> 8) & 0xff,
294 data & 0xff, (data >> 8) & 0xff,
295 (data >> 16) & 0xff, (data >> 24) & 0xff
296 };
Ralph Metzler126f1e62011-03-12 23:44:33 -0500297
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300298 if (i2c_write(state->i2c, adr, mm, 8) < 0)
Ralph Metzler126f1e62011-03-12 23:44:33 -0500299 return -1;
300 return 0;
301}
302
303static int write_chunk(struct drxd_state *state,
Mauro Carvalho Chehab9999daf2011-03-25 12:10:05 -0300304 u32 reg, u8 *data, u32 len, u8 flags)
Ralph Metzler126f1e62011-03-12 23:44:33 -0500305{
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300306 u8 adr = state->config.demod_address;
307 u8 mm[CHUNK_SIZE + 4] = { reg & 0xff, (reg >> 16) & 0xff,
308 flags | ((reg >> 24) & 0xff), (reg >> 8) & 0xff
309 };
Ralph Metzler126f1e62011-03-12 23:44:33 -0500310 int i;
311
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300312 for (i = 0; i < len; i++)
313 mm[4 + i] = data[i];
314 if (i2c_write(state->i2c, adr, mm, 4 + len) < 0) {
Mauro Carvalho Chehab9999daf2011-03-25 12:10:05 -0300315 printk(KERN_ERR "error in write_chunk\n");
Ralph Metzler126f1e62011-03-12 23:44:33 -0500316 return -1;
317 }
318 return 0;
319}
320
321static int WriteBlock(struct drxd_state *state,
Mauro Carvalho Chehab9999daf2011-03-25 12:10:05 -0300322 u32 Address, u16 BlockSize, u8 *pBlock, u8 Flags)
Ralph Metzler126f1e62011-03-12 23:44:33 -0500323{
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300324 while (BlockSize > 0) {
Ralph Metzler126f1e62011-03-12 23:44:33 -0500325 u16 Chunk = BlockSize > CHUNK_SIZE ? CHUNK_SIZE : BlockSize;
326
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300327 if (write_chunk(state, Address, pBlock, Chunk, Flags) < 0)
Ralph Metzler126f1e62011-03-12 23:44:33 -0500328 return -1;
329 pBlock += Chunk;
330 Address += (Chunk >> 1);
331 BlockSize -= Chunk;
332 }
333 return 0;
334}
335
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300336static int WriteTable(struct drxd_state *state, u8 * pTable)
Ralph Metzler126f1e62011-03-12 23:44:33 -0500337{
338 int status = 0;
339
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300340 if (pTable == NULL)
Ralph Metzler126f1e62011-03-12 23:44:33 -0500341 return 0;
342
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300343 while (!status) {
Ralph Metzler126f1e62011-03-12 23:44:33 -0500344 u16 Length;
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300345 u32 Address = pTable[0] | (pTable[1] << 8) |
346 (pTable[2] << 16) | (pTable[3] << 24);
Ralph Metzler126f1e62011-03-12 23:44:33 -0500347
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300348 if (Address == 0xFFFFFFFF)
Ralph Metzler126f1e62011-03-12 23:44:33 -0500349 break;
350 pTable += sizeof(u32);
351
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300352 Length = pTable[0] | (pTable[1] << 8);
Ralph Metzler126f1e62011-03-12 23:44:33 -0500353 pTable += sizeof(u16);
354 if (!Length)
355 break;
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300356 status = WriteBlock(state, Address, Length * 2, pTable, 0);
357 pTable += (Length * 2);
Ralph Metzler126f1e62011-03-12 23:44:33 -0500358 }
359 return status;
360}
361
Ralph Metzler126f1e62011-03-12 23:44:33 -0500362/****************************************************************************/
363/****************************************************************************/
364/****************************************************************************/
365
366static int ResetCEFR(struct drxd_state *state)
367{
368 return WriteTable(state, state->m_ResetCEFR);
369}
370
371static int InitCP(struct drxd_state *state)
372{
373 return WriteTable(state, state->m_InitCP);
374}
375
376static int InitCE(struct drxd_state *state)
377{
378 int status;
379 enum app_env AppEnv = state->app_env_default;
380
381 do {
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -0300382 status = WriteTable(state, state->m_InitCE);
383 if (status < 0)
384 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -0500385
386 if (state->operation_mode == OM_DVBT_Diversity_Front ||
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300387 state->operation_mode == OM_DVBT_Diversity_End) {
Ralph Metzler126f1e62011-03-12 23:44:33 -0500388 AppEnv = state->app_env_diversity;
389 }
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300390 if (AppEnv == APPENV_STATIC) {
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -0300391 status = Write16(state, CE_REG_TAPSET__A, 0x0000, 0);
392 if (status < 0)
393 break;
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300394 } else if (AppEnv == APPENV_PORTABLE) {
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -0300395 status = Write16(state, CE_REG_TAPSET__A, 0x0001, 0);
396 if (status < 0)
397 break;
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300398 } else if (AppEnv == APPENV_MOBILE && state->type_A) {
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -0300399 status = Write16(state, CE_REG_TAPSET__A, 0x0002, 0);
400 if (status < 0)
401 break;
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300402 } else if (AppEnv == APPENV_MOBILE && !state->type_A) {
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -0300403 status = Write16(state, CE_REG_TAPSET__A, 0x0006, 0);
404 if (status < 0)
405 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -0500406 }
407
408 /* start ce */
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -0300409 status = Write16(state, B_CE_REG_COMM_EXEC__A, 0x0001, 0);
410 if (status < 0)
411 break;
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300412 } while (0);
Ralph Metzler126f1e62011-03-12 23:44:33 -0500413 return status;
414}
415
416static int StopOC(struct drxd_state *state)
417{
418 int status = 0;
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300419 u16 ocSyncLvl = 0;
420 u16 ocModeLop = state->m_EcOcRegOcModeLop;
421 u16 dtoIncLop = 0;
422 u16 dtoIncHip = 0;
Ralph Metzler126f1e62011-03-12 23:44:33 -0500423
424 do {
425 /* Store output configuration */
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -0300426 status = Read16(state, EC_OC_REG_SNC_ISC_LVL__A, &ocSyncLvl, 0);
427 if (status < 0)
Mauro Carvalho Chehab9999daf2011-03-25 12:10:05 -0300428 break;
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -0300429 /* CHK_ERROR(Read16(EC_OC_REG_OC_MODE_LOP__A, &ocModeLop)); */
Ralph Metzler126f1e62011-03-12 23:44:33 -0500430 state->m_EcOcRegSncSncLvl = ocSyncLvl;
431 /* m_EcOcRegOcModeLop = ocModeLop; */
432
433 /* Flush FIFO (byte-boundary) at fixed rate */
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -0300434 status = Read16(state, EC_OC_REG_RCN_MAP_LOP__A, &dtoIncLop, 0);
435 if (status < 0)
436 break;
437 status = Read16(state, EC_OC_REG_RCN_MAP_HIP__A, &dtoIncHip, 0);
438 if (status < 0)
439 break;
440 status = Write16(state, EC_OC_REG_DTO_INC_LOP__A, dtoIncLop, 0);
441 if (status < 0)
442 break;
443 status = Write16(state, EC_OC_REG_DTO_INC_HIP__A, dtoIncHip, 0);
444 if (status < 0)
445 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -0500446 ocModeLop &= ~(EC_OC_REG_OC_MODE_LOP_DTO_CTR_SRC__M);
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300447 ocModeLop |= EC_OC_REG_OC_MODE_LOP_DTO_CTR_SRC_STATIC;
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -0300448 status = Write16(state, EC_OC_REG_OC_MODE_LOP__A, ocModeLop, 0);
449 if (status < 0)
450 break;
451 status = Write16(state, EC_OC_REG_COMM_EXEC__A, EC_OC_REG_COMM_EXEC_CTL_HOLD, 0);
452 if (status < 0)
453 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -0500454
455 msleep(1);
456 /* Output pins to '0' */
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -0300457 status = Write16(state, EC_OC_REG_OCR_MPG_UOS__A, EC_OC_REG_OCR_MPG_UOS__M, 0);
458 if (status < 0)
459 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -0500460
461 /* Force the OC out of sync */
462 ocSyncLvl &= ~(EC_OC_REG_SNC_ISC_LVL_OSC__M);
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -0300463 status = Write16(state, EC_OC_REG_SNC_ISC_LVL__A, ocSyncLvl, 0);
464 if (status < 0)
465 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -0500466 ocModeLop &= ~(EC_OC_REG_OC_MODE_LOP_PAR_ENA__M);
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300467 ocModeLop |= EC_OC_REG_OC_MODE_LOP_PAR_ENA_ENABLE;
468 ocModeLop |= 0x2; /* Magically-out-of-sync */
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -0300469 status = Write16(state, EC_OC_REG_OC_MODE_LOP__A, ocModeLop, 0);
470 if (status < 0)
471 break;
472 status = Write16(state, EC_OC_REG_COMM_INT_STA__A, 0x0, 0);
473 if (status < 0)
474 break;
475 status = Write16(state, EC_OC_REG_COMM_EXEC__A, EC_OC_REG_COMM_EXEC_CTL_ACTIVE, 0);
476 if (status < 0)
477 break;
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300478 } while (0);
Ralph Metzler126f1e62011-03-12 23:44:33 -0500479
480 return status;
481}
482
483static int StartOC(struct drxd_state *state)
484{
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300485 int status = 0;
Ralph Metzler126f1e62011-03-12 23:44:33 -0500486
487 do {
488 /* Stop OC */
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -0300489 status = Write16(state, EC_OC_REG_COMM_EXEC__A, EC_OC_REG_COMM_EXEC_CTL_HOLD, 0);
490 if (status < 0)
491 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -0500492
493 /* Restore output configuration */
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -0300494 status = Write16(state, EC_OC_REG_SNC_ISC_LVL__A, state->m_EcOcRegSncSncLvl, 0);
495 if (status < 0)
496 break;
497 status = Write16(state, EC_OC_REG_OC_MODE_LOP__A, state->m_EcOcRegOcModeLop, 0);
498 if (status < 0)
499 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -0500500
501 /* Output pins active again */
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -0300502 status = Write16(state, EC_OC_REG_OCR_MPG_UOS__A, EC_OC_REG_OCR_MPG_UOS_INIT, 0);
503 if (status < 0)
504 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -0500505
506 /* Start OC */
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -0300507 status = Write16(state, EC_OC_REG_COMM_EXEC__A, EC_OC_REG_COMM_EXEC_CTL_ACTIVE, 0);
508 if (status < 0)
509 break;
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300510 } while (0);
Ralph Metzler126f1e62011-03-12 23:44:33 -0500511 return status;
512}
513
514static int InitEQ(struct drxd_state *state)
515{
516 return WriteTable(state, state->m_InitEQ);
517}
518
519static int InitEC(struct drxd_state *state)
520{
521 return WriteTable(state, state->m_InitEC);
522}
523
524static int InitSC(struct drxd_state *state)
525{
526 return WriteTable(state, state->m_InitSC);
527}
528
529static int InitAtomicRead(struct drxd_state *state)
530{
531 return WriteTable(state, state->m_InitAtomicRead);
532}
533
534static int CorrectSysClockDeviation(struct drxd_state *state);
535
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300536static int DRX_GetLockStatus(struct drxd_state *state, u32 * pLockStatus)
Ralph Metzler126f1e62011-03-12 23:44:33 -0500537{
538 u16 ScRaRamLock = 0;
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300539 const u16 mpeg_lock_mask = (SC_RA_RAM_LOCK_MPEG__M |
540 SC_RA_RAM_LOCK_FEC__M |
541 SC_RA_RAM_LOCK_DEMOD__M);
542 const u16 fec_lock_mask = (SC_RA_RAM_LOCK_FEC__M |
543 SC_RA_RAM_LOCK_DEMOD__M);
544 const u16 demod_lock_mask = SC_RA_RAM_LOCK_DEMOD__M;
Ralph Metzler126f1e62011-03-12 23:44:33 -0500545
546 int status;
547
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300548 *pLockStatus = 0;
Ralph Metzler126f1e62011-03-12 23:44:33 -0500549
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300550 status = Read16(state, SC_RA_RAM_LOCK__A, &ScRaRamLock, 0x0000);
551 if (status < 0) {
Mauro Carvalho Chehab9999daf2011-03-25 12:10:05 -0300552 printk(KERN_ERR "Can't read SC_RA_RAM_LOCK__A status = %08x\n", status);
Ralph Metzler126f1e62011-03-12 23:44:33 -0500553 return status;
554 }
555
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300556 if (state->drxd_state != DRXD_STARTED)
Ralph Metzler126f1e62011-03-12 23:44:33 -0500557 return 0;
558
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300559 if ((ScRaRamLock & mpeg_lock_mask) == mpeg_lock_mask) {
560 *pLockStatus |= DRX_LOCK_MPEG;
Ralph Metzler126f1e62011-03-12 23:44:33 -0500561 CorrectSysClockDeviation(state);
562 }
563
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300564 if ((ScRaRamLock & fec_lock_mask) == fec_lock_mask)
565 *pLockStatus |= DRX_LOCK_FEC;
Ralph Metzler126f1e62011-03-12 23:44:33 -0500566
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300567 if ((ScRaRamLock & demod_lock_mask) == demod_lock_mask)
568 *pLockStatus |= DRX_LOCK_DEMOD;
Ralph Metzler126f1e62011-03-12 23:44:33 -0500569 return 0;
570}
571
572/****************************************************************************/
573
574static int SetCfgIfAgc(struct drxd_state *state, struct SCfgAgc *cfg)
575{
576 int status;
577
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300578 if (cfg->outputLevel > DRXD_FE_CTRL_MAX)
579 return -1;
Ralph Metzler126f1e62011-03-12 23:44:33 -0500580
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300581 if (cfg->ctrlMode == AGC_CTRL_USER) {
Ralph Metzler126f1e62011-03-12 23:44:33 -0500582 do {
583 u16 FeAgRegPm1AgcWri;
584 u16 FeAgRegAgModeLop;
585
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -0300586 status = Read16(state, FE_AG_REG_AG_MODE_LOP__A, &FeAgRegAgModeLop, 0);
587 if (status < 0)
588 break;
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300589 FeAgRegAgModeLop &= (~FE_AG_REG_AG_MODE_LOP_MODE_4__M);
590 FeAgRegAgModeLop |= FE_AG_REG_AG_MODE_LOP_MODE_4_STATIC;
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -0300591 status = Write16(state, FE_AG_REG_AG_MODE_LOP__A, FeAgRegAgModeLop, 0);
592 if (status < 0)
593 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -0500594
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300595 FeAgRegPm1AgcWri = (u16) (cfg->outputLevel &
596 FE_AG_REG_PM1_AGC_WRI__M);
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -0300597 status = Write16(state, FE_AG_REG_PM1_AGC_WRI__A, FeAgRegPm1AgcWri, 0);
598 if (status < 0)
599 break;
Mauro Carvalho Chehab9999daf2011-03-25 12:10:05 -0300600 } while (0);
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300601 } else if (cfg->ctrlMode == AGC_CTRL_AUTO) {
602 if (((cfg->maxOutputLevel) < (cfg->minOutputLevel)) ||
603 ((cfg->maxOutputLevel) > DRXD_FE_CTRL_MAX) ||
604 ((cfg->speed) > DRXD_FE_CTRL_MAX) ||
605 ((cfg->settleLevel) > DRXD_FE_CTRL_MAX)
606 )
Mauro Carvalho Chehab9999daf2011-03-25 12:10:05 -0300607 return -1;
Ralph Metzler126f1e62011-03-12 23:44:33 -0500608 do {
609 u16 FeAgRegAgModeLop;
610 u16 FeAgRegEgcSetLvl;
611 u16 slope, offset;
612
613 /* == Mode == */
614
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -0300615 status = Read16(state, FE_AG_REG_AG_MODE_LOP__A, &FeAgRegAgModeLop, 0);
616 if (status < 0)
617 break;
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300618 FeAgRegAgModeLop &= (~FE_AG_REG_AG_MODE_LOP_MODE_4__M);
Ralph Metzler126f1e62011-03-12 23:44:33 -0500619 FeAgRegAgModeLop |=
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300620 FE_AG_REG_AG_MODE_LOP_MODE_4_DYNAMIC;
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -0300621 status = Write16(state, FE_AG_REG_AG_MODE_LOP__A, FeAgRegAgModeLop, 0);
622 if (status < 0)
623 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -0500624
625 /* == Settle level == */
626
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300627 FeAgRegEgcSetLvl = (u16) ((cfg->settleLevel >> 1) &
628 FE_AG_REG_EGC_SET_LVL__M);
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -0300629 status = Write16(state, FE_AG_REG_EGC_SET_LVL__A, FeAgRegEgcSetLvl, 0);
630 if (status < 0)
631 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -0500632
633 /* == Min/Max == */
634
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300635 slope = (u16) ((cfg->maxOutputLevel -
636 cfg->minOutputLevel) / 2);
637 offset = (u16) ((cfg->maxOutputLevel +
638 cfg->minOutputLevel) / 2 - 511);
Ralph Metzler126f1e62011-03-12 23:44:33 -0500639
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -0300640 status = Write16(state, FE_AG_REG_GC1_AGC_RIC__A, slope, 0);
641 if (status < 0)
642 break;
643 status = Write16(state, FE_AG_REG_GC1_AGC_OFF__A, offset, 0);
644 if (status < 0)
645 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -0500646
647 /* == Speed == */
648 {
649 const u16 maxRur = 8;
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300650 const u16 slowIncrDecLUT[] = { 3, 4, 4, 5, 6 };
651 const u16 fastIncrDecLUT[] = { 14, 15, 15, 16,
652 17, 18, 18, 19,
653 20, 21, 22, 23,
654 24, 26, 27, 28,
655 29, 31
656 };
Ralph Metzler126f1e62011-03-12 23:44:33 -0500657
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300658 u16 fineSteps = (DRXD_FE_CTRL_MAX + 1) /
659 (maxRur + 1);
660 u16 fineSpeed = (u16) (cfg->speed -
661 ((cfg->speed /
662 fineSteps) *
Ralph Metzler126f1e62011-03-12 23:44:33 -0500663 fineSteps));
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300664 u16 invRurCount = (u16) (cfg->speed /
665 fineSteps);
Ralph Metzler126f1e62011-03-12 23:44:33 -0500666 u16 rurCount;
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300667 if (invRurCount > maxRur) {
668 rurCount = 0;
Ralph Metzler126f1e62011-03-12 23:44:33 -0500669 fineSpeed += fineSteps;
670 } else {
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300671 rurCount = maxRur - invRurCount;
Ralph Metzler126f1e62011-03-12 23:44:33 -0500672 }
673
674 /*
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300675 fastInc = default *
676 (2^(fineSpeed/fineSteps))
677 => range[default...2*default>
678 slowInc = default *
679 (2^(fineSpeed/fineSteps))
680 */
Ralph Metzler126f1e62011-03-12 23:44:33 -0500681 {
682 u16 fastIncrDec =
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300683 fastIncrDecLUT[fineSpeed /
684 ((fineSteps /
685 (14 + 1)) + 1)];
686 u16 slowIncrDec =
687 slowIncrDecLUT[fineSpeed /
688 (fineSteps /
689 (3 + 1))];
Ralph Metzler126f1e62011-03-12 23:44:33 -0500690
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -0300691 status = Write16(state, FE_AG_REG_EGC_RUR_CNT__A, rurCount, 0);
692 if (status < 0)
693 break;
694 status = Write16(state, FE_AG_REG_EGC_FAS_INC__A, fastIncrDec, 0);
695 if (status < 0)
696 break;
697 status = Write16(state, FE_AG_REG_EGC_FAS_DEC__A, fastIncrDec, 0);
698 if (status < 0)
699 break;
700 status = Write16(state, FE_AG_REG_EGC_SLO_INC__A, slowIncrDec, 0);
701 if (status < 0)
702 break;
703 status = Write16(state, FE_AG_REG_EGC_SLO_DEC__A, slowIncrDec, 0);
704 if (status < 0)
705 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -0500706 }
707 }
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300708 } while (0);
Ralph Metzler126f1e62011-03-12 23:44:33 -0500709
710 } else {
711 /* No OFF mode for IF control */
Mauro Carvalho Chehab9999daf2011-03-25 12:10:05 -0300712 return -1;
Ralph Metzler126f1e62011-03-12 23:44:33 -0500713 }
714 return status;
715}
716
Ralph Metzler126f1e62011-03-12 23:44:33 -0500717static int SetCfgRfAgc(struct drxd_state *state, struct SCfgAgc *cfg)
718{
719 int status = 0;
720
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300721 if (cfg->outputLevel > DRXD_FE_CTRL_MAX)
Ralph Metzler126f1e62011-03-12 23:44:33 -0500722 return -1;
723
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300724 if (cfg->ctrlMode == AGC_CTRL_USER) {
Ralph Metzler126f1e62011-03-12 23:44:33 -0500725 do {
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300726 u16 AgModeLop = 0;
727 u16 level = (cfg->outputLevel);
Ralph Metzler126f1e62011-03-12 23:44:33 -0500728
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300729 if (level == DRXD_FE_CTRL_MAX)
Ralph Metzler126f1e62011-03-12 23:44:33 -0500730 level++;
731
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -0300732 status = Write16(state, FE_AG_REG_PM2_AGC_WRI__A, level, 0x0000);
733 if (status < 0)
734 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -0500735
736 /*==== Mode ====*/
737
738 /* Powerdown PD2, WRI source */
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300739 state->m_FeAgRegAgPwd &= ~(FE_AG_REG_AG_PWD_PWD_PD2__M);
Ralph Metzler126f1e62011-03-12 23:44:33 -0500740 state->m_FeAgRegAgPwd |=
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300741 FE_AG_REG_AG_PWD_PWD_PD2_DISABLE;
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -0300742 status = Write16(state, FE_AG_REG_AG_PWD__A, state->m_FeAgRegAgPwd, 0x0000);
743 if (status < 0)
744 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -0500745
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -0300746 status = Read16(state, FE_AG_REG_AG_MODE_LOP__A, &AgModeLop, 0x0000);
747 if (status < 0)
748 break;
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300749 AgModeLop &= (~(FE_AG_REG_AG_MODE_LOP_MODE_5__M |
750 FE_AG_REG_AG_MODE_LOP_MODE_E__M));
751 AgModeLop |= (FE_AG_REG_AG_MODE_LOP_MODE_5_STATIC |
752 FE_AG_REG_AG_MODE_LOP_MODE_E_STATIC);
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -0300753 status = Write16(state, FE_AG_REG_AG_MODE_LOP__A, AgModeLop, 0x0000);
754 if (status < 0)
755 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -0500756
757 /* enable AGC2 pin */
758 {
759 u16 FeAgRegAgAgcSio = 0;
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -0300760 status = Read16(state, FE_AG_REG_AG_AGC_SIO__A, &FeAgRegAgAgcSio, 0x0000);
761 if (status < 0)
762 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -0500763 FeAgRegAgAgcSio &=
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300764 ~(FE_AG_REG_AG_AGC_SIO_AGC_SIO_2__M);
Ralph Metzler126f1e62011-03-12 23:44:33 -0500765 FeAgRegAgAgcSio |=
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300766 FE_AG_REG_AG_AGC_SIO_AGC_SIO_2_OUTPUT;
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -0300767 status = Write16(state, FE_AG_REG_AG_AGC_SIO__A, FeAgRegAgAgcSio, 0x0000);
768 if (status < 0)
769 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -0500770 }
771
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300772 } while (0);
773 } else if (cfg->ctrlMode == AGC_CTRL_AUTO) {
774 u16 AgModeLop = 0;
Ralph Metzler126f1e62011-03-12 23:44:33 -0500775
776 do {
777 u16 level;
778 /* Automatic control */
779 /* Powerup PD2, AGC2 as output, TGC source */
780 (state->m_FeAgRegAgPwd) &=
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300781 ~(FE_AG_REG_AG_PWD_PWD_PD2__M);
Ralph Metzler126f1e62011-03-12 23:44:33 -0500782 (state->m_FeAgRegAgPwd) |=
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300783 FE_AG_REG_AG_PWD_PWD_PD2_DISABLE;
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -0300784 status = Write16(state, FE_AG_REG_AG_PWD__A, (state->m_FeAgRegAgPwd), 0x0000);
785 if (status < 0)
786 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -0500787
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -0300788 status = Read16(state, FE_AG_REG_AG_MODE_LOP__A, &AgModeLop, 0x0000);
789 if (status < 0)
790 break;
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300791 AgModeLop &= (~(FE_AG_REG_AG_MODE_LOP_MODE_5__M |
792 FE_AG_REG_AG_MODE_LOP_MODE_E__M));
793 AgModeLop |= (FE_AG_REG_AG_MODE_LOP_MODE_5_STATIC |
794 FE_AG_REG_AG_MODE_LOP_MODE_E_DYNAMIC);
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -0300795 status = Write16(state, FE_AG_REG_AG_MODE_LOP__A, AgModeLop, 0x0000);
796 if (status < 0)
797 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -0500798 /* Settle level */
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300799 level = (((cfg->settleLevel) >> 4) &
800 FE_AG_REG_TGC_SET_LVL__M);
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -0300801 status = Write16(state, FE_AG_REG_TGC_SET_LVL__A, level, 0x0000);
802 if (status < 0)
803 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -0500804
805 /* Min/max: don't care */
806
807 /* Speed: TODO */
808
809 /* enable AGC2 pin */
810 {
811 u16 FeAgRegAgAgcSio = 0;
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -0300812 status = Read16(state, FE_AG_REG_AG_AGC_SIO__A, &FeAgRegAgAgcSio, 0x0000);
813 if (status < 0)
814 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -0500815 FeAgRegAgAgcSio &=
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300816 ~(FE_AG_REG_AG_AGC_SIO_AGC_SIO_2__M);
Ralph Metzler126f1e62011-03-12 23:44:33 -0500817 FeAgRegAgAgcSio |=
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300818 FE_AG_REG_AG_AGC_SIO_AGC_SIO_2_OUTPUT;
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -0300819 status = Write16(state, FE_AG_REG_AG_AGC_SIO__A, FeAgRegAgAgcSio, 0x0000);
820 if (status < 0)
821 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -0500822 }
823
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300824 } while (0);
Ralph Metzler126f1e62011-03-12 23:44:33 -0500825 } else {
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300826 u16 AgModeLop = 0;
Ralph Metzler126f1e62011-03-12 23:44:33 -0500827
828 do {
829 /* No RF AGC control */
830 /* Powerdown PD2, AGC2 as output, WRI source */
831 (state->m_FeAgRegAgPwd) &=
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300832 ~(FE_AG_REG_AG_PWD_PWD_PD2__M);
Ralph Metzler126f1e62011-03-12 23:44:33 -0500833 (state->m_FeAgRegAgPwd) |=
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300834 FE_AG_REG_AG_PWD_PWD_PD2_ENABLE;
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -0300835 status = Write16(state, FE_AG_REG_AG_PWD__A, (state->m_FeAgRegAgPwd), 0x0000);
836 if (status < 0)
837 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -0500838
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -0300839 status = Read16(state, FE_AG_REG_AG_MODE_LOP__A, &AgModeLop, 0x0000);
840 if (status < 0)
841 break;
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300842 AgModeLop &= (~(FE_AG_REG_AG_MODE_LOP_MODE_5__M |
843 FE_AG_REG_AG_MODE_LOP_MODE_E__M));
844 AgModeLop |= (FE_AG_REG_AG_MODE_LOP_MODE_5_STATIC |
845 FE_AG_REG_AG_MODE_LOP_MODE_E_STATIC);
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -0300846 status = Write16(state, FE_AG_REG_AG_MODE_LOP__A, AgModeLop, 0x0000);
847 if (status < 0)
848 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -0500849
850 /* set FeAgRegAgAgcSio AGC2 (RF) as input */
851 {
852 u16 FeAgRegAgAgcSio = 0;
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -0300853 status = Read16(state, FE_AG_REG_AG_AGC_SIO__A, &FeAgRegAgAgcSio, 0x0000);
854 if (status < 0)
855 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -0500856 FeAgRegAgAgcSio &=
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300857 ~(FE_AG_REG_AG_AGC_SIO_AGC_SIO_2__M);
Ralph Metzler126f1e62011-03-12 23:44:33 -0500858 FeAgRegAgAgcSio |=
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300859 FE_AG_REG_AG_AGC_SIO_AGC_SIO_2_INPUT;
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -0300860 status = Write16(state, FE_AG_REG_AG_AGC_SIO__A, FeAgRegAgAgcSio, 0x0000);
861 if (status < 0)
862 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -0500863 }
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300864 } while (0);
Ralph Metzler126f1e62011-03-12 23:44:33 -0500865 }
866 return status;
867}
868
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300869static int ReadIFAgc(struct drxd_state *state, u32 * pValue)
Ralph Metzler126f1e62011-03-12 23:44:33 -0500870{
871 int status = 0;
872
873 *pValue = 0;
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300874 if (state->if_agc_cfg.ctrlMode != AGC_CTRL_OFF) {
Ralph Metzler126f1e62011-03-12 23:44:33 -0500875 u16 Value;
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300876 status = Read16(state, FE_AG_REG_GC1_AGC_DAT__A, &Value, 0);
Ralph Metzler126f1e62011-03-12 23:44:33 -0500877 Value &= FE_AG_REG_GC1_AGC_DAT__M;
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300878 if (status >= 0) {
Ralph Metzler126f1e62011-03-12 23:44:33 -0500879 /* 3.3V
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300880 |
881 R1
882 |
Ralph Metzler126f1e62011-03-12 23:44:33 -0500883 Vin - R3 - * -- Vout
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300884 |
885 R2
886 |
887 GND
888 */
Ralph Metzler126f1e62011-03-12 23:44:33 -0500889 u32 R1 = state->if_agc_cfg.R1;
890 u32 R2 = state->if_agc_cfg.R2;
891 u32 R3 = state->if_agc_cfg.R3;
892
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300893 u32 Vmax = (3300 * R2) / (R1 + R2);
894 u32 Rpar = (R2 * R3) / (R3 + R2);
895 u32 Vmin = (3300 * Rpar) / (R1 + Rpar);
896 u32 Vout = Vmin + ((Vmax - Vmin) * Value) / 1024;
Ralph Metzler126f1e62011-03-12 23:44:33 -0500897
898 *pValue = Vout;
899 }
900 }
901 return status;
902}
903
Devin Heitmueller8f19f272011-03-13 02:11:07 -0300904static int load_firmware(struct drxd_state *state, const char *fw_name)
905{
906 const struct firmware *fw;
907
908 if (request_firmware(&fw, fw_name, state->dev) < 0) {
909 printk(KERN_ERR "drxd: firmware load failure [%s]\n", fw_name);
910 return -EIO;
911 }
912
913 state->microcode = kzalloc(fw->size, GFP_KERNEL);
914 if (state->microcode == NULL) {
915 printk(KERN_ERR "drxd: firmware load failure: nomemory\n");
916 return -ENOMEM;
917 }
918
919 memcpy(state->microcode, fw->data, fw->size);
920 state->microcode_length = fw->size;
921 return 0;
922}
923
Ralph Metzler126f1e62011-03-12 23:44:33 -0500924static int DownloadMicrocode(struct drxd_state *state,
Mauro Carvalho Chehab9999daf2011-03-25 12:10:05 -0300925 const u8 *pMCImage, u32 Length)
Ralph Metzler126f1e62011-03-12 23:44:33 -0500926{
927 u8 *pSrc;
928 u16 Flags;
929 u32 Address;
930 u16 nBlocks;
931 u16 BlockSize;
932 u16 BlockCRC;
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300933 u32 offset = 0;
934 int i, status = 0;
Ralph Metzler126f1e62011-03-12 23:44:33 -0500935
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300936 pSrc = (u8 *) pMCImage;
Ralph Metzler126f1e62011-03-12 23:44:33 -0500937 Flags = (pSrc[0] << 8) | pSrc[1];
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300938 pSrc += sizeof(u16);
939 offset += sizeof(u16);
Ralph Metzler126f1e62011-03-12 23:44:33 -0500940 nBlocks = (pSrc[0] << 8) | pSrc[1];
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300941 pSrc += sizeof(u16);
942 offset += sizeof(u16);
Ralph Metzler126f1e62011-03-12 23:44:33 -0500943
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300944 for (i = 0; i < nBlocks; i++) {
945 Address = (pSrc[0] << 24) | (pSrc[1] << 16) |
946 (pSrc[2] << 8) | pSrc[3];
947 pSrc += sizeof(u32);
948 offset += sizeof(u32);
Ralph Metzler126f1e62011-03-12 23:44:33 -0500949
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300950 BlockSize = ((pSrc[0] << 8) | pSrc[1]) * sizeof(u16);
951 pSrc += sizeof(u16);
952 offset += sizeof(u16);
Ralph Metzler126f1e62011-03-12 23:44:33 -0500953
954 Flags = (pSrc[0] << 8) | pSrc[1];
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300955 pSrc += sizeof(u16);
956 offset += sizeof(u16);
Ralph Metzler126f1e62011-03-12 23:44:33 -0500957
958 BlockCRC = (pSrc[0] << 8) | pSrc[1];
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300959 pSrc += sizeof(u16);
960 offset += sizeof(u16);
Ralph Metzler126f1e62011-03-12 23:44:33 -0500961
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300962 status = WriteBlock(state, Address, BlockSize,
963 pSrc, DRX_I2C_CLEARCRC);
964 if (status < 0)
Ralph Metzler126f1e62011-03-12 23:44:33 -0500965 break;
966 pSrc += BlockSize;
967 offset += BlockSize;
968 }
969
970 return status;
971}
972
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300973static int HI_Command(struct drxd_state *state, u16 cmd, u16 * pResult)
Ralph Metzler126f1e62011-03-12 23:44:33 -0500974{
975 u32 nrRetries = 0;
976 u16 waitCmd;
977 int status;
978
Mauro Carvalho Chehab9999daf2011-03-25 12:10:05 -0300979 status = Write16(state, HI_RA_RAM_SRV_CMD__A, cmd, 0);
980 if (status < 0)
Ralph Metzler126f1e62011-03-12 23:44:33 -0500981 return status;
982
983 do {
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300984 nrRetries += 1;
985 if (nrRetries > DRXD_MAX_RETRIES) {
986 status = -1;
Ralph Metzler126f1e62011-03-12 23:44:33 -0500987 break;
988 };
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300989 status = Read16(state, HI_RA_RAM_SRV_CMD__A, &waitCmd, 0);
990 } while (waitCmd != 0);
Ralph Metzler126f1e62011-03-12 23:44:33 -0500991
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300992 if (status >= 0)
993 status = Read16(state, HI_RA_RAM_SRV_RES__A, pResult, 0);
Ralph Metzler126f1e62011-03-12 23:44:33 -0500994 return status;
995}
996
997static int HI_CfgCommand(struct drxd_state *state)
998{
Devin Heitmueller6cacdd42011-03-24 13:44:01 -0300999 int status = 0;
Ralph Metzler126f1e62011-03-12 23:44:33 -05001000
Mauro Carvalho Chehab834751d2011-03-25 12:46:05 -03001001 mutex_lock(&state->mutex);
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001002 Write16(state, HI_RA_RAM_SRV_CFG_KEY__A, HI_RA_RAM_SRV_RST_KEY_ACT, 0);
Ralph Metzler126f1e62011-03-12 23:44:33 -05001003 Write16(state, HI_RA_RAM_SRV_CFG_DIV__A, state->hi_cfg_timing_div, 0);
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001004 Write16(state, HI_RA_RAM_SRV_CFG_BDL__A, state->hi_cfg_bridge_delay, 0);
Ralph Metzler126f1e62011-03-12 23:44:33 -05001005 Write16(state, HI_RA_RAM_SRV_CFG_WUP__A, state->hi_cfg_wakeup_key, 0);
1006 Write16(state, HI_RA_RAM_SRV_CFG_ACT__A, state->hi_cfg_ctrl, 0);
1007
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001008 Write16(state, HI_RA_RAM_SRV_CFG_KEY__A, HI_RA_RAM_SRV_RST_KEY_ACT, 0);
Ralph Metzler126f1e62011-03-12 23:44:33 -05001009
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001010 if ((state->hi_cfg_ctrl & HI_RA_RAM_SRV_CFG_ACT_PWD_EXE) ==
Ralph Metzler126f1e62011-03-12 23:44:33 -05001011 HI_RA_RAM_SRV_CFG_ACT_PWD_EXE)
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001012 status = Write16(state, HI_RA_RAM_SRV_CMD__A,
1013 HI_RA_RAM_SRV_CMD_CONFIG, 0);
Ralph Metzler126f1e62011-03-12 23:44:33 -05001014 else
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001015 status = HI_Command(state, HI_RA_RAM_SRV_CMD_CONFIG, 0);
Mauro Carvalho Chehab834751d2011-03-25 12:46:05 -03001016 mutex_unlock(&state->mutex);
Ralph Metzler126f1e62011-03-12 23:44:33 -05001017 return status;
1018}
1019
1020static int InitHI(struct drxd_state *state)
1021{
1022 state->hi_cfg_wakeup_key = (state->chip_adr);
1023 /* port/bridge/power down ctrl */
1024 state->hi_cfg_ctrl = HI_RA_RAM_SRV_CFG_ACT_SLV0_ON;
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001025 return HI_CfgCommand(state);
Ralph Metzler126f1e62011-03-12 23:44:33 -05001026}
1027
1028static int HI_ResetCommand(struct drxd_state *state)
1029{
1030 int status;
1031
Mauro Carvalho Chehab834751d2011-03-25 12:46:05 -03001032 mutex_lock(&state->mutex);
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001033 status = Write16(state, HI_RA_RAM_SRV_RST_KEY__A,
1034 HI_RA_RAM_SRV_RST_KEY_ACT, 0);
1035 if (status == 0)
1036 status = HI_Command(state, HI_RA_RAM_SRV_CMD_RESET, 0);
Mauro Carvalho Chehab834751d2011-03-25 12:46:05 -03001037 mutex_unlock(&state->mutex);
Ralph Metzler126f1e62011-03-12 23:44:33 -05001038 msleep(1);
1039 return status;
1040}
1041
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001042static int DRX_ConfigureI2CBridge(struct drxd_state *state, int bEnableBridge)
Ralph Metzler126f1e62011-03-12 23:44:33 -05001043{
1044 state->hi_cfg_ctrl &= (~HI_RA_RAM_SRV_CFG_ACT_BRD__M);
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001045 if (bEnableBridge)
Ralph Metzler126f1e62011-03-12 23:44:33 -05001046 state->hi_cfg_ctrl |= HI_RA_RAM_SRV_CFG_ACT_BRD_ON;
1047 else
1048 state->hi_cfg_ctrl |= HI_RA_RAM_SRV_CFG_ACT_BRD_OFF;
1049
1050 return HI_CfgCommand(state);
1051}
1052
1053#define HI_TR_WRITE 0x9
1054#define HI_TR_READ 0xA
1055#define HI_TR_READ_WRITE 0xB
1056#define HI_TR_BROADCAST 0x4
1057
1058#if 0
1059static int AtomicReadBlock(struct drxd_state *state,
Mauro Carvalho Chehab9999daf2011-03-25 12:10:05 -03001060 u32 Addr, u16 DataSize, u8 *pData, u8 Flags)
Ralph Metzler126f1e62011-03-12 23:44:33 -05001061{
1062 int status;
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001063 int i = 0;
Ralph Metzler126f1e62011-03-12 23:44:33 -05001064
1065 /* Parameter check */
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001066 if ((!pData) || ((DataSize & 1) != 0))
Ralph Metzler126f1e62011-03-12 23:44:33 -05001067 return -1;
1068
Mauro Carvalho Chehab834751d2011-03-25 12:46:05 -03001069 mutex_lock(&state->mutex);
Ralph Metzler126f1e62011-03-12 23:44:33 -05001070
1071 do {
1072 /* Instruct HI to read n bytes */
1073 /* TODO use proper names forthese egisters */
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03001074 status = Write16(state, HI_RA_RAM_SRV_CFG_KEY__A, (HI_TR_FUNC_ADDR & 0xFFFF), 0);
1075 if (status < 0)
1076 break;
1077 status = Write16(state, HI_RA_RAM_SRV_CFG_DIV__A, (u16) (Addr >> 16), 0);
1078 if (status < 0)
1079 break;
1080 status = Write16(state, HI_RA_RAM_SRV_CFG_BDL__A, (u16) (Addr & 0xFFFF), 0);
1081 if (status < 0)
1082 break;
1083 status = Write16(state, HI_RA_RAM_SRV_CFG_WUP__A, (u16) ((DataSize / 2) - 1), 0);
1084 if (status < 0)
1085 break;
1086 status = Write16(state, HI_RA_RAM_SRV_CFG_ACT__A, HI_TR_READ, 0);
1087 if (status < 0)
1088 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -05001089
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03001090 status = HI_Command(state, HI_RA_RAM_SRV_CMD_EXECUTE, 0);
1091 if (status < 0)
1092 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -05001093
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001094 } while (0);
Ralph Metzler126f1e62011-03-12 23:44:33 -05001095
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001096 if (status >= 0) {
1097 for (i = 0; i < (DataSize / 2); i += 1) {
Ralph Metzler126f1e62011-03-12 23:44:33 -05001098 u16 word;
1099
1100 status = Read16(state, (HI_RA_RAM_USR_BEGIN__A + i),
1101 &word, 0);
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001102 if (status < 0)
Ralph Metzler126f1e62011-03-12 23:44:33 -05001103 break;
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001104 pData[2 * i] = (u8) (word & 0xFF);
1105 pData[(2 * i) + 1] = (u8) (word >> 8);
Ralph Metzler126f1e62011-03-12 23:44:33 -05001106 }
1107 }
Mauro Carvalho Chehab834751d2011-03-25 12:46:05 -03001108 mutex_unlock(&state->mutex);
Ralph Metzler126f1e62011-03-12 23:44:33 -05001109 return status;
1110}
1111
1112static int AtomicReadReg32(struct drxd_state *state,
Mauro Carvalho Chehab9999daf2011-03-25 12:10:05 -03001113 u32 Addr, u32 *pData, u8 Flags)
Ralph Metzler126f1e62011-03-12 23:44:33 -05001114{
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001115 u8 buf[sizeof(u32)];
Ralph Metzler126f1e62011-03-12 23:44:33 -05001116 int status;
1117
1118 if (!pData)
1119 return -1;
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001120 status = AtomicReadBlock(state, Addr, sizeof(u32), buf, Flags);
1121 *pData = (((u32) buf[0]) << 0) +
1122 (((u32) buf[1]) << 8) +
1123 (((u32) buf[2]) << 16) + (((u32) buf[3]) << 24);
Ralph Metzler126f1e62011-03-12 23:44:33 -05001124 return status;
1125}
1126#endif
1127
1128static int StopAllProcessors(struct drxd_state *state)
1129{
1130 return Write16(state, HI_COMM_EXEC__A,
1131 SC_COMM_EXEC_CTL_STOP, DRX_I2C_BROADCAST);
1132}
1133
1134static int EnableAndResetMB(struct drxd_state *state)
1135{
1136 if (state->type_A) {
1137 /* disable? monitor bus observe @ EC_OC */
1138 Write16(state, EC_OC_REG_OC_MON_SIO__A, 0x0000, 0x0000);
1139 }
1140
1141 /* do inverse broadcast, followed by explicit write to HI */
1142 Write16(state, HI_COMM_MB__A, 0x0000, DRX_I2C_BROADCAST);
1143 Write16(state, HI_COMM_MB__A, 0x0000, 0x0000);
1144 return 0;
1145}
1146
1147static int InitCC(struct drxd_state *state)
1148{
1149 if (state->osc_clock_freq == 0 ||
1150 state->osc_clock_freq > 20000 ||
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001151 (state->osc_clock_freq % 4000) != 0) {
Mauro Carvalho Chehab9999daf2011-03-25 12:10:05 -03001152 printk(KERN_ERR "invalid osc frequency %d\n", state->osc_clock_freq);
Ralph Metzler126f1e62011-03-12 23:44:33 -05001153 return -1;
1154 }
1155
1156 Write16(state, CC_REG_OSC_MODE__A, CC_REG_OSC_MODE_M20, 0);
1157 Write16(state, CC_REG_PLL_MODE__A, CC_REG_PLL_MODE_BYPASS_PLL |
1158 CC_REG_PLL_MODE_PUMP_CUR_12, 0);
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001159 Write16(state, CC_REG_REF_DIVIDE__A, state->osc_clock_freq / 4000, 0);
Ralph Metzler126f1e62011-03-12 23:44:33 -05001160 Write16(state, CC_REG_PWD_MODE__A, CC_REG_PWD_MODE_DOWN_PLL, 0);
1161 Write16(state, CC_REG_UPDATE__A, CC_REG_UPDATE_KEY, 0);
1162
1163 return 0;
1164}
1165
1166static int ResetECOD(struct drxd_state *state)
1167{
1168 int status = 0;
1169
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001170 if (state->type_A)
Ralph Metzler126f1e62011-03-12 23:44:33 -05001171 status = Write16(state, EC_OD_REG_SYNC__A, 0x0664, 0);
1172 else
1173 status = Write16(state, B_EC_OD_REG_SYNC__A, 0x0664, 0);
1174
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001175 if (!(status < 0))
Ralph Metzler126f1e62011-03-12 23:44:33 -05001176 status = WriteTable(state, state->m_ResetECRAM);
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001177 if (!(status < 0))
Ralph Metzler126f1e62011-03-12 23:44:33 -05001178 status = Write16(state, EC_OD_REG_COMM_EXEC__A, 0x0001, 0);
1179 return status;
1180}
1181
Ralph Metzler126f1e62011-03-12 23:44:33 -05001182/* Configure PGA switch */
1183
1184static int SetCfgPga(struct drxd_state *state, int pgaSwitch)
1185{
1186 int status;
1187 u16 AgModeLop = 0;
1188 u16 AgModeHip = 0;
1189 do {
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001190 if (pgaSwitch) {
Ralph Metzler126f1e62011-03-12 23:44:33 -05001191 /* PGA on */
1192 /* fine gain */
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03001193 status = Read16(state, B_FE_AG_REG_AG_MODE_LOP__A, &AgModeLop, 0x0000);
1194 if (status < 0)
1195 break;
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001196 AgModeLop &= (~(B_FE_AG_REG_AG_MODE_LOP_MODE_C__M));
1197 AgModeLop |= B_FE_AG_REG_AG_MODE_LOP_MODE_C_DYNAMIC;
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03001198 status = Write16(state, B_FE_AG_REG_AG_MODE_LOP__A, AgModeLop, 0x0000);
1199 if (status < 0)
1200 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -05001201
1202 /* coarse gain */
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03001203 status = Read16(state, B_FE_AG_REG_AG_MODE_HIP__A, &AgModeHip, 0x0000);
1204 if (status < 0)
1205 break;
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001206 AgModeHip &= (~(B_FE_AG_REG_AG_MODE_HIP_MODE_J__M));
1207 AgModeHip |= B_FE_AG_REG_AG_MODE_HIP_MODE_J_DYNAMIC;
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03001208 status = Write16(state, B_FE_AG_REG_AG_MODE_HIP__A, AgModeHip, 0x0000);
1209 if (status < 0)
1210 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -05001211
1212 /* enable fine and coarse gain, enable AAF,
1213 no ext resistor */
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03001214 status = Write16(state, B_FE_AG_REG_AG_PGA_MODE__A, B_FE_AG_REG_AG_PGA_MODE_PFY_PCY_AFY_REN, 0x0000);
1215 if (status < 0)
1216 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -05001217 } else {
1218 /* PGA off, bypass */
1219
1220 /* fine gain */
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03001221 status = Read16(state, B_FE_AG_REG_AG_MODE_LOP__A, &AgModeLop, 0x0000);
1222 if (status < 0)
1223 break;
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001224 AgModeLop &= (~(B_FE_AG_REG_AG_MODE_LOP_MODE_C__M));
1225 AgModeLop |= B_FE_AG_REG_AG_MODE_LOP_MODE_C_STATIC;
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03001226 status = Write16(state, B_FE_AG_REG_AG_MODE_LOP__A, AgModeLop, 0x0000);
1227 if (status < 0)
1228 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -05001229
1230 /* coarse gain */
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03001231 status = Read16(state, B_FE_AG_REG_AG_MODE_HIP__A, &AgModeHip, 0x0000);
1232 if (status < 0)
1233 break;
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001234 AgModeHip &= (~(B_FE_AG_REG_AG_MODE_HIP_MODE_J__M));
1235 AgModeHip |= B_FE_AG_REG_AG_MODE_HIP_MODE_J_STATIC;
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03001236 status = Write16(state, B_FE_AG_REG_AG_MODE_HIP__A, AgModeHip, 0x0000);
1237 if (status < 0)
1238 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -05001239
1240 /* disable fine and coarse gain, enable AAF,
1241 no ext resistor */
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03001242 status = Write16(state, B_FE_AG_REG_AG_PGA_MODE__A, B_FE_AG_REG_AG_PGA_MODE_PFN_PCN_AFY_REN, 0x0000);
1243 if (status < 0)
1244 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -05001245 }
Mauro Carvalho Chehab9999daf2011-03-25 12:10:05 -03001246 } while (0);
Ralph Metzler126f1e62011-03-12 23:44:33 -05001247 return status;
1248}
1249
1250static int InitFE(struct drxd_state *state)
1251{
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001252 int status;
Ralph Metzler126f1e62011-03-12 23:44:33 -05001253
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001254 do {
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03001255 status = WriteTable(state, state->m_InitFE_1);
1256 if (status < 0)
1257 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -05001258
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001259 if (state->type_A) {
1260 status = Write16(state, FE_AG_REG_AG_PGA_MODE__A,
1261 FE_AG_REG_AG_PGA_MODE_PFN_PCN_AFY_REN,
1262 0);
1263 } else {
1264 if (state->PGA)
1265 status = SetCfgPga(state, 0);
1266 else
1267 status =
1268 Write16(state, B_FE_AG_REG_AG_PGA_MODE__A,
1269 B_FE_AG_REG_AG_PGA_MODE_PFN_PCN_AFY_REN,
1270 0);
1271 }
Ralph Metzler126f1e62011-03-12 23:44:33 -05001272
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001273 if (status < 0)
1274 break;
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03001275 status = Write16(state, FE_AG_REG_AG_AGC_SIO__A, state->m_FeAgRegAgAgcSio, 0x0000);
1276 if (status < 0)
1277 break;
1278 status = Write16(state, FE_AG_REG_AG_PWD__A, state->m_FeAgRegAgPwd, 0x0000);
1279 if (status < 0)
1280 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -05001281
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03001282 status = WriteTable(state, state->m_InitFE_2);
1283 if (status < 0)
1284 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -05001285
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001286 } while (0);
Ralph Metzler126f1e62011-03-12 23:44:33 -05001287
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001288 return status;
Ralph Metzler126f1e62011-03-12 23:44:33 -05001289}
1290
1291static int InitFT(struct drxd_state *state)
1292{
1293 /*
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001294 norm OFFSET, MB says =2 voor 8K en =3 voor 2K waarschijnlijk
1295 SC stuff
1296 */
1297 return Write16(state, FT_REG_COMM_EXEC__A, 0x0001, 0x0000);
Ralph Metzler126f1e62011-03-12 23:44:33 -05001298}
1299
1300static int SC_WaitForReady(struct drxd_state *state)
1301{
1302 u16 curCmd;
1303 int i;
1304
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001305 for (i = 0; i < DRXD_MAX_RETRIES; i += 1) {
1306 int status = Read16(state, SC_RA_RAM_CMD__A, &curCmd, 0);
1307 if (status == 0 || curCmd == 0)
Ralph Metzler126f1e62011-03-12 23:44:33 -05001308 return status;
1309 }
1310 return -1;
1311}
1312
1313static int SC_SendCommand(struct drxd_state *state, u16 cmd)
1314{
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001315 int status = 0;
Ralph Metzler126f1e62011-03-12 23:44:33 -05001316 u16 errCode;
1317
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001318 Write16(state, SC_RA_RAM_CMD__A, cmd, 0);
Ralph Metzler126f1e62011-03-12 23:44:33 -05001319 SC_WaitForReady(state);
1320
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001321 Read16(state, SC_RA_RAM_CMD_ADDR__A, &errCode, 0);
Ralph Metzler126f1e62011-03-12 23:44:33 -05001322
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001323 if (errCode == 0xFFFF) {
Mauro Carvalho Chehab9999daf2011-03-25 12:10:05 -03001324 printk(KERN_ERR "Command Error\n");
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001325 status = -1;
Ralph Metzler126f1e62011-03-12 23:44:33 -05001326 }
1327
1328 return status;
1329}
1330
1331static int SC_ProcStartCommand(struct drxd_state *state,
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001332 u16 subCmd, u16 param0, u16 param1)
Ralph Metzler126f1e62011-03-12 23:44:33 -05001333{
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001334 int status = 0;
Ralph Metzler126f1e62011-03-12 23:44:33 -05001335 u16 scExec;
1336
Mauro Carvalho Chehab834751d2011-03-25 12:46:05 -03001337 mutex_lock(&state->mutex);
Ralph Metzler126f1e62011-03-12 23:44:33 -05001338 do {
1339 Read16(state, SC_COMM_EXEC__A, &scExec, 0);
1340 if (scExec != 1) {
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001341 status = -1;
Ralph Metzler126f1e62011-03-12 23:44:33 -05001342 break;
1343 }
1344 SC_WaitForReady(state);
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001345 Write16(state, SC_RA_RAM_CMD_ADDR__A, subCmd, 0);
1346 Write16(state, SC_RA_RAM_PARAM1__A, param1, 0);
1347 Write16(state, SC_RA_RAM_PARAM0__A, param0, 0);
Ralph Metzler126f1e62011-03-12 23:44:33 -05001348
1349 SC_SendCommand(state, SC_RA_RAM_CMD_PROC_START);
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001350 } while (0);
Mauro Carvalho Chehab834751d2011-03-25 12:46:05 -03001351 mutex_unlock(&state->mutex);
Ralph Metzler126f1e62011-03-12 23:44:33 -05001352 return status;
1353}
1354
Ralph Metzler126f1e62011-03-12 23:44:33 -05001355static int SC_SetPrefParamCommand(struct drxd_state *state,
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001356 u16 subCmd, u16 param0, u16 param1)
Ralph Metzler126f1e62011-03-12 23:44:33 -05001357{
1358 int status;
1359
Mauro Carvalho Chehab834751d2011-03-25 12:46:05 -03001360 mutex_lock(&state->mutex);
Ralph Metzler126f1e62011-03-12 23:44:33 -05001361 do {
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03001362 status = SC_WaitForReady(state);
1363 if (status < 0)
1364 break;
1365 status = Write16(state, SC_RA_RAM_CMD_ADDR__A, subCmd, 0);
1366 if (status < 0)
1367 break;
1368 status = Write16(state, SC_RA_RAM_PARAM1__A, param1, 0);
1369 if (status < 0)
1370 break;
1371 status = Write16(state, SC_RA_RAM_PARAM0__A, param0, 0);
1372 if (status < 0)
1373 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -05001374
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03001375 status = SC_SendCommand(state, SC_RA_RAM_CMD_SET_PREF_PARAM);
1376 if (status < 0)
1377 break;
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001378 } while (0);
Mauro Carvalho Chehab834751d2011-03-25 12:46:05 -03001379 mutex_unlock(&state->mutex);
Ralph Metzler126f1e62011-03-12 23:44:33 -05001380 return status;
1381}
1382
1383#if 0
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001384static int SC_GetOpParamCommand(struct drxd_state *state, u16 * result)
Ralph Metzler126f1e62011-03-12 23:44:33 -05001385{
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001386 int status = 0;
Ralph Metzler126f1e62011-03-12 23:44:33 -05001387
Mauro Carvalho Chehab834751d2011-03-25 12:46:05 -03001388 mutex_lock(&state->mutex);
Ralph Metzler126f1e62011-03-12 23:44:33 -05001389 do {
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03001390 status = SC_WaitForReady(state);
1391 if (status < 0)
1392 break;
1393 status = SC_SendCommand(state, SC_RA_RAM_CMD_GET_OP_PARAM);
1394 if (status < 0)
1395 break;
1396 status = Read16(state, SC_RA_RAM_PARAM0__A, result, 0);
1397 if (status < 0)
1398 break;
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001399 } while (0);
Mauro Carvalho Chehab834751d2011-03-25 12:46:05 -03001400 mutex_unlock(&state->mutex);
Ralph Metzler126f1e62011-03-12 23:44:33 -05001401 return status;
1402}
1403#endif
1404
1405static int ConfigureMPEGOutput(struct drxd_state *state, int bEnableOutput)
1406{
1407 int status;
1408
1409 do {
1410 u16 EcOcRegIprInvMpg = 0;
1411 u16 EcOcRegOcModeLop = 0;
1412 u16 EcOcRegOcModeHip = 0;
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001413 u16 EcOcRegOcMpgSio = 0;
Ralph Metzler126f1e62011-03-12 23:44:33 -05001414
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03001415 /*CHK_ERROR(Read16(state, EC_OC_REG_OC_MODE_LOP__A, &EcOcRegOcModeLop, 0)); */
Ralph Metzler126f1e62011-03-12 23:44:33 -05001416
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001417 if (state->operation_mode == OM_DVBT_Diversity_Front) {
1418 if (bEnableOutput) {
Ralph Metzler126f1e62011-03-12 23:44:33 -05001419 EcOcRegOcModeHip |=
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001420 B_EC_OC_REG_OC_MODE_HIP_MPG_BUS_SRC_MONITOR;
1421 } else
Ralph Metzler126f1e62011-03-12 23:44:33 -05001422 EcOcRegOcMpgSio |= EC_OC_REG_OC_MPG_SIO__M;
1423 EcOcRegOcModeLop |=
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001424 EC_OC_REG_OC_MODE_LOP_PAR_ENA_DISABLE;
1425 } else {
Ralph Metzler126f1e62011-03-12 23:44:33 -05001426 EcOcRegOcModeLop = state->m_EcOcRegOcModeLop;
1427
1428 if (bEnableOutput)
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001429 EcOcRegOcMpgSio &= (~(EC_OC_REG_OC_MPG_SIO__M));
Ralph Metzler126f1e62011-03-12 23:44:33 -05001430 else
1431 EcOcRegOcMpgSio |= EC_OC_REG_OC_MPG_SIO__M;
1432
1433 /* Don't Insert RS Byte */
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001434 if (state->insert_rs_byte) {
Ralph Metzler126f1e62011-03-12 23:44:33 -05001435 EcOcRegOcModeLop &=
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001436 (~(EC_OC_REG_OC_MODE_LOP_PAR_ENA__M));
Ralph Metzler126f1e62011-03-12 23:44:33 -05001437 EcOcRegOcModeHip &=
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001438 (~EC_OC_REG_OC_MODE_HIP_MPG_PAR_VAL__M);
Ralph Metzler126f1e62011-03-12 23:44:33 -05001439 EcOcRegOcModeHip |=
1440 EC_OC_REG_OC_MODE_HIP_MPG_PAR_VAL_ENABLE;
1441 } else {
1442 EcOcRegOcModeLop |=
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001443 EC_OC_REG_OC_MODE_LOP_PAR_ENA_DISABLE;
Ralph Metzler126f1e62011-03-12 23:44:33 -05001444 EcOcRegOcModeHip &=
1445 (~EC_OC_REG_OC_MODE_HIP_MPG_PAR_VAL__M);
1446 EcOcRegOcModeHip |=
1447 EC_OC_REG_OC_MODE_HIP_MPG_PAR_VAL_DISABLE;
1448 }
1449
1450 /* Mode = Parallel */
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001451 if (state->enable_parallel)
Ralph Metzler126f1e62011-03-12 23:44:33 -05001452 EcOcRegOcModeLop &=
1453 (~(EC_OC_REG_OC_MODE_LOP_MPG_TRM_MDE__M));
1454 else
1455 EcOcRegOcModeLop |=
1456 EC_OC_REG_OC_MODE_LOP_MPG_TRM_MDE_SERIAL;
1457 }
1458 /* Invert Data */
1459 /* EcOcRegIprInvMpg |= 0x00FF; */
1460 EcOcRegIprInvMpg &= (~(0x00FF));
1461
1462 /* Invert Error ( we don't use the pin ) */
1463 /* EcOcRegIprInvMpg |= 0x0100; */
1464 EcOcRegIprInvMpg &= (~(0x0100));
1465
1466 /* Invert Start ( we don't use the pin ) */
1467 /* EcOcRegIprInvMpg |= 0x0200; */
1468 EcOcRegIprInvMpg &= (~(0x0200));
1469
1470 /* Invert Valid ( we don't use the pin ) */
1471 /* EcOcRegIprInvMpg |= 0x0400; */
1472 EcOcRegIprInvMpg &= (~(0x0400));
1473
1474 /* Invert Clock */
1475 /* EcOcRegIprInvMpg |= 0x0800; */
1476 EcOcRegIprInvMpg &= (~(0x0800));
1477
1478 /* EcOcRegOcModeLop =0x05; */
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03001479 status = Write16(state, EC_OC_REG_IPR_INV_MPG__A, EcOcRegIprInvMpg, 0);
1480 if (status < 0)
1481 break;
1482 status = Write16(state, EC_OC_REG_OC_MODE_LOP__A, EcOcRegOcModeLop, 0);
1483 if (status < 0)
1484 break;
1485 status = Write16(state, EC_OC_REG_OC_MODE_HIP__A, EcOcRegOcModeHip, 0x0000);
1486 if (status < 0)
1487 break;
1488 status = Write16(state, EC_OC_REG_OC_MPG_SIO__A, EcOcRegOcMpgSio, 0);
1489 if (status < 0)
1490 break;
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001491 } while (0);
Ralph Metzler126f1e62011-03-12 23:44:33 -05001492 return status;
1493}
1494
1495static int SetDeviceTypeId(struct drxd_state *state)
1496{
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001497 int status = 0;
1498 u16 deviceId = 0;
Ralph Metzler126f1e62011-03-12 23:44:33 -05001499
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001500 do {
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03001501 status = Read16(state, CC_REG_JTAGID_L__A, &deviceId, 0);
1502 if (status < 0)
1503 break;
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001504 /* TODO: why twice? */
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03001505 status = Read16(state, CC_REG_JTAGID_L__A, &deviceId, 0);
1506 if (status < 0)
1507 break;
Mauro Carvalho Chehab9999daf2011-03-25 12:10:05 -03001508 printk(KERN_INFO "drxd: deviceId = %04x\n", deviceId);
Ralph Metzler126f1e62011-03-12 23:44:33 -05001509
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001510 state->type_A = 0;
1511 state->PGA = 0;
1512 state->diversity = 0;
1513 if (deviceId == 0) { /* on A2 only 3975 available */
1514 state->type_A = 1;
Mauro Carvalho Chehab9999daf2011-03-25 12:10:05 -03001515 printk(KERN_INFO "DRX3975D-A2\n");
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001516 } else {
1517 deviceId >>= 12;
Mauro Carvalho Chehab9999daf2011-03-25 12:10:05 -03001518 printk(KERN_INFO "DRX397%dD-B1\n", deviceId);
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001519 switch (deviceId) {
1520 case 4:
1521 state->diversity = 1;
1522 case 3:
1523 case 7:
1524 state->PGA = 1;
1525 break;
1526 case 6:
1527 state->diversity = 1;
1528 case 5:
1529 case 8:
1530 break;
1531 default:
1532 status = -1;
1533 break;
1534 }
1535 }
1536 } while (0);
Ralph Metzler126f1e62011-03-12 23:44:33 -05001537
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001538 if (status < 0)
1539 return status;
Ralph Metzler126f1e62011-03-12 23:44:33 -05001540
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001541 /* Init Table selection */
1542 state->m_InitAtomicRead = DRXD_InitAtomicRead;
1543 state->m_InitSC = DRXD_InitSC;
1544 state->m_ResetECRAM = DRXD_ResetECRAM;
1545 if (state->type_A) {
1546 state->m_ResetCEFR = DRXD_ResetCEFR;
1547 state->m_InitFE_1 = DRXD_InitFEA2_1;
1548 state->m_InitFE_2 = DRXD_InitFEA2_2;
1549 state->m_InitCP = DRXD_InitCPA2;
1550 state->m_InitCE = DRXD_InitCEA2;
1551 state->m_InitEQ = DRXD_InitEQA2;
1552 state->m_InitEC = DRXD_InitECA2;
1553 if (load_firmware(state, DRX_FW_FILENAME_A2))
1554 return -EIO;
1555 } else {
1556 state->m_ResetCEFR = NULL;
1557 state->m_InitFE_1 = DRXD_InitFEB1_1;
1558 state->m_InitFE_2 = DRXD_InitFEB1_2;
1559 state->m_InitCP = DRXD_InitCPB1;
1560 state->m_InitCE = DRXD_InitCEB1;
1561 state->m_InitEQ = DRXD_InitEQB1;
1562 state->m_InitEC = DRXD_InitECB1;
1563 if (load_firmware(state, DRX_FW_FILENAME_B1))
1564 return -EIO;
1565 }
1566 if (state->diversity) {
1567 state->m_InitDiversityFront = DRXD_InitDiversityFront;
1568 state->m_InitDiversityEnd = DRXD_InitDiversityEnd;
1569 state->m_DisableDiversity = DRXD_DisableDiversity;
1570 state->m_StartDiversityFront = DRXD_StartDiversityFront;
1571 state->m_StartDiversityEnd = DRXD_StartDiversityEnd;
1572 state->m_DiversityDelay8MHZ = DRXD_DiversityDelay8MHZ;
1573 state->m_DiversityDelay6MHZ = DRXD_DiversityDelay6MHZ;
1574 } else {
1575 state->m_InitDiversityFront = NULL;
1576 state->m_InitDiversityEnd = NULL;
1577 state->m_DisableDiversity = NULL;
1578 state->m_StartDiversityFront = NULL;
1579 state->m_StartDiversityEnd = NULL;
1580 state->m_DiversityDelay8MHZ = NULL;
1581 state->m_DiversityDelay6MHZ = NULL;
1582 }
Ralph Metzler126f1e62011-03-12 23:44:33 -05001583
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001584 return status;
Ralph Metzler126f1e62011-03-12 23:44:33 -05001585}
1586
1587static int CorrectSysClockDeviation(struct drxd_state *state)
1588{
1589 int status;
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001590 s32 incr = 0;
1591 s32 nomincr = 0;
1592 u32 bandwidth = 0;
1593 u32 sysClockInHz = 0;
1594 u32 sysClockFreq = 0; /* in kHz */
Ralph Metzler126f1e62011-03-12 23:44:33 -05001595 s16 oscClockDeviation;
1596 s16 Diff;
1597
1598 do {
1599 /* Retrieve bandwidth and incr, sanity check */
1600
1601 /* These accesses should be AtomicReadReg32, but that
1602 causes trouble (at least for diversity */
Mauro Carvalho Chehab9999daf2011-03-25 12:10:05 -03001603 status = Read32(state, LC_RA_RAM_IFINCR_NOM_L__A, ((u32 *) &nomincr), 0);
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03001604 if (status < 0)
1605 break;
Mauro Carvalho Chehab9999daf2011-03-25 12:10:05 -03001606 status = Read32(state, FE_IF_REG_INCR0__A, (u32 *) &incr, 0);
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03001607 if (status < 0)
1608 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -05001609
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001610 if (state->type_A) {
1611 if ((nomincr - incr < -500) || (nomincr - incr > 500))
Ralph Metzler126f1e62011-03-12 23:44:33 -05001612 break;
1613 } else {
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001614 if ((nomincr - incr < -2000) || (nomincr - incr > 2000))
Ralph Metzler126f1e62011-03-12 23:44:33 -05001615 break;
1616 }
1617
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001618 switch (state->param.u.ofdm.bandwidth) {
1619 case BANDWIDTH_8_MHZ:
Ralph Metzler126f1e62011-03-12 23:44:33 -05001620 bandwidth = DRXD_BANDWIDTH_8MHZ_IN_HZ;
1621 break;
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001622 case BANDWIDTH_7_MHZ:
Ralph Metzler126f1e62011-03-12 23:44:33 -05001623 bandwidth = DRXD_BANDWIDTH_7MHZ_IN_HZ;
1624 break;
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001625 case BANDWIDTH_6_MHZ:
Ralph Metzler126f1e62011-03-12 23:44:33 -05001626 bandwidth = DRXD_BANDWIDTH_6MHZ_IN_HZ;
1627 break;
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001628 default:
Ralph Metzler126f1e62011-03-12 23:44:33 -05001629 return -1;
1630 break;
1631 }
1632
1633 /* Compute new sysclock value
1634 sysClockFreq = (((incr + 2^23)*bandwidth)/2^21)/1000 */
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001635 incr += (1 << 23);
1636 sysClockInHz = MulDiv32(incr, bandwidth, 1 << 21);
1637 sysClockFreq = (u32) (sysClockInHz / 1000);
Ralph Metzler126f1e62011-03-12 23:44:33 -05001638 /* rounding */
Mauro Carvalho Chehab9999daf2011-03-25 12:10:05 -03001639 if ((sysClockInHz % 1000) > 500)
Ralph Metzler126f1e62011-03-12 23:44:33 -05001640 sysClockFreq++;
Ralph Metzler126f1e62011-03-12 23:44:33 -05001641
1642 /* Compute clock deviation in ppm */
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001643 oscClockDeviation = (u16) ((((s32) (sysClockFreq) -
1644 (s32)
1645 (state->expected_sys_clock_freq)) *
1646 1000000L) /
1647 (s32)
1648 (state->expected_sys_clock_freq));
Ralph Metzler126f1e62011-03-12 23:44:33 -05001649
1650 Diff = oscClockDeviation - state->osc_clock_deviation;
Mauro Carvalho Chehab9999daf2011-03-25 12:10:05 -03001651 /*printk(KERN_INFO "sysclockdiff=%d\n", Diff); */
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001652 if (Diff >= -200 && Diff <= 200) {
Ralph Metzler126f1e62011-03-12 23:44:33 -05001653 state->sys_clock_freq = (u16) sysClockFreq;
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001654 if (oscClockDeviation != state->osc_clock_deviation) {
Ralph Metzler126f1e62011-03-12 23:44:33 -05001655 if (state->config.osc_deviation) {
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001656 state->config.osc_deviation(state->priv,
1657 oscClockDeviation,
1658 1);
1659 state->osc_clock_deviation =
1660 oscClockDeviation;
Ralph Metzler126f1e62011-03-12 23:44:33 -05001661 }
1662 }
1663 /* switch OFF SRMM scan in SC */
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03001664 status = Write16(state, SC_RA_RAM_SAMPLE_RATE_COUNT__A, DRXD_OSCDEV_DONT_SCAN, 0);
1665 if (status < 0)
1666 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -05001667 /* overrule FE_IF internal value for
1668 proper re-locking */
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03001669 status = Write16(state, SC_RA_RAM_IF_SAVE__AX, state->current_fe_if_incr, 0);
1670 if (status < 0)
1671 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -05001672 state->cscd_state = CSCD_SAVED;
1673 }
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001674 } while (0);
Ralph Metzler126f1e62011-03-12 23:44:33 -05001675
Mauro Carvalho Chehab9999daf2011-03-25 12:10:05 -03001676 return status;
Ralph Metzler126f1e62011-03-12 23:44:33 -05001677}
1678
1679static int DRX_Stop(struct drxd_state *state)
1680{
1681 int status;
1682
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001683 if (state->drxd_state != DRXD_STARTED)
Ralph Metzler126f1e62011-03-12 23:44:33 -05001684 return 0;
1685
1686 do {
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001687 if (state->cscd_state != CSCD_SAVED) {
Ralph Metzler126f1e62011-03-12 23:44:33 -05001688 u32 lock;
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03001689 status = DRX_GetLockStatus(state, &lock);
1690 if (status < 0)
1691 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -05001692 }
1693
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03001694 status = StopOC(state);
1695 if (status < 0)
1696 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -05001697
1698 state->drxd_state = DRXD_STOPPED;
1699
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03001700 status = ConfigureMPEGOutput(state, 0);
1701 if (status < 0)
1702 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -05001703
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001704 if (state->type_A) {
Ralph Metzler126f1e62011-03-12 23:44:33 -05001705 /* Stop relevant processors off the device */
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03001706 status = Write16(state, EC_OD_REG_COMM_EXEC__A, 0x0000, 0x0000);
1707 if (status < 0)
1708 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -05001709
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03001710 status = Write16(state, SC_COMM_EXEC__A, SC_COMM_EXEC_CTL_STOP, 0);
1711 if (status < 0)
1712 break;
1713 status = Write16(state, LC_COMM_EXEC__A, SC_COMM_EXEC_CTL_STOP, 0);
1714 if (status < 0)
1715 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -05001716 } else {
1717 /* Stop all processors except HI & CC & FE */
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03001718 status = Write16(state, B_SC_COMM_EXEC__A, SC_COMM_EXEC_CTL_STOP, 0);
1719 if (status < 0)
1720 break;
1721 status = Write16(state, B_LC_COMM_EXEC__A, SC_COMM_EXEC_CTL_STOP, 0);
1722 if (status < 0)
1723 break;
1724 status = Write16(state, B_FT_COMM_EXEC__A, SC_COMM_EXEC_CTL_STOP, 0);
1725 if (status < 0)
1726 break;
1727 status = Write16(state, B_CP_COMM_EXEC__A, SC_COMM_EXEC_CTL_STOP, 0);
1728 if (status < 0)
1729 break;
1730 status = Write16(state, B_CE_COMM_EXEC__A, SC_COMM_EXEC_CTL_STOP, 0);
1731 if (status < 0)
1732 break;
1733 status = Write16(state, B_EQ_COMM_EXEC__A, SC_COMM_EXEC_CTL_STOP, 0);
1734 if (status < 0)
1735 break;
1736 status = Write16(state, EC_OD_REG_COMM_EXEC__A, 0x0000, 0);
1737 if (status < 0)
1738 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -05001739 }
1740
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001741 } while (0);
Ralph Metzler126f1e62011-03-12 23:44:33 -05001742 return status;
1743}
1744
Ralph Metzler126f1e62011-03-12 23:44:33 -05001745int SetOperationMode(struct drxd_state *state, int oMode)
1746{
1747 int status;
1748
1749 do {
1750 if (state->drxd_state != DRXD_STOPPED) {
1751 status = -1;
1752 break;
1753 }
1754
1755 if (oMode == state->operation_mode) {
1756 status = 0;
1757 break;
1758 }
1759
1760 if (oMode != OM_Default && !state->diversity) {
1761 status = -1;
1762 break;
1763 }
1764
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001765 switch (oMode) {
Ralph Metzler126f1e62011-03-12 23:44:33 -05001766 case OM_DVBT_Diversity_Front:
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001767 status = WriteTable(state, state->m_InitDiversityFront);
Ralph Metzler126f1e62011-03-12 23:44:33 -05001768 break;
1769 case OM_DVBT_Diversity_End:
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001770 status = WriteTable(state, state->m_InitDiversityEnd);
Ralph Metzler126f1e62011-03-12 23:44:33 -05001771 break;
1772 case OM_Default:
1773 /* We need to check how to
1774 get DRXD out of diversity */
1775 default:
1776 status = WriteTable(state, state->m_DisableDiversity);
1777 break;
1778 }
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001779 } while (0);
Ralph Metzler126f1e62011-03-12 23:44:33 -05001780
1781 if (!status)
1782 state->operation_mode = oMode;
1783 return status;
1784}
1785
Ralph Metzler126f1e62011-03-12 23:44:33 -05001786static int StartDiversity(struct drxd_state *state)
1787{
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001788 int status = 0;
Ralph Metzler126f1e62011-03-12 23:44:33 -05001789 u16 rcControl;
1790
1791 do {
1792 if (state->operation_mode == OM_DVBT_Diversity_Front) {
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03001793 status = WriteTable(state, state->m_StartDiversityFront);
1794 if (status < 0)
1795 break;
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001796 } else if (state->operation_mode == OM_DVBT_Diversity_End) {
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03001797 status = WriteTable(state, state->m_StartDiversityEnd);
1798 if (status < 0)
1799 break;
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001800 if (state->param.u.ofdm.bandwidth == BANDWIDTH_8_MHZ) {
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03001801 status = WriteTable(state, state->m_DiversityDelay8MHZ);
1802 if (status < 0)
1803 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -05001804 } else {
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03001805 status = WriteTable(state, state->m_DiversityDelay6MHZ);
1806 if (status < 0)
1807 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -05001808 }
1809
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03001810 status = Read16(state, B_EQ_REG_RC_SEL_CAR__A, &rcControl, 0);
1811 if (status < 0)
1812 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -05001813 rcControl &= ~(B_EQ_REG_RC_SEL_CAR_FFTMODE__M);
1814 rcControl |= B_EQ_REG_RC_SEL_CAR_DIV_ON |
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001815 /* combining enabled */
1816 B_EQ_REG_RC_SEL_CAR_MEAS_A_CC |
1817 B_EQ_REG_RC_SEL_CAR_PASS_A_CC |
1818 B_EQ_REG_RC_SEL_CAR_LOCAL_A_CC;
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03001819 status = Write16(state, B_EQ_REG_RC_SEL_CAR__A, rcControl, 0);
1820 if (status < 0)
1821 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -05001822 }
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001823 } while (0);
Ralph Metzler126f1e62011-03-12 23:44:33 -05001824 return status;
1825}
1826
Ralph Metzler126f1e62011-03-12 23:44:33 -05001827static int SetFrequencyShift(struct drxd_state *state,
1828 u32 offsetFreq, int channelMirrored)
1829{
1830 int negativeShift = (state->tuner_mirrors == channelMirrored);
1831
1832 /* Handle all mirroring
1833 *
1834 * Note: ADC mirroring (aliasing) is implictly handled by limiting
1835 * feFsRegAddInc to 28 bits below
1836 * (if the result before masking is more than 28 bits, this means
1837 * that the ADC is mirroring.
1838 * The masking is in fact the aliasing of the ADC)
1839 *
1840 */
1841
1842 /* Compute register value, unsigned computation */
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001843 state->fe_fs_add_incr = MulDiv32(state->intermediate_freq +
Ralph Metzler126f1e62011-03-12 23:44:33 -05001844 offsetFreq,
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001845 1 << 28, state->sys_clock_freq);
Ralph Metzler126f1e62011-03-12 23:44:33 -05001846 /* Remove integer part */
1847 state->fe_fs_add_incr &= 0x0FFFFFFFL;
Mauro Carvalho Chehab9999daf2011-03-25 12:10:05 -03001848 if (negativeShift)
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001849 state->fe_fs_add_incr = ((1 << 28) - state->fe_fs_add_incr);
Ralph Metzler126f1e62011-03-12 23:44:33 -05001850
1851 /* Save the frequency shift without tunerOffset compensation
1852 for CtrlGetChannel. */
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001853 state->org_fe_fs_add_incr = MulDiv32(state->intermediate_freq,
1854 1 << 28, state->sys_clock_freq);
Ralph Metzler126f1e62011-03-12 23:44:33 -05001855 /* Remove integer part */
1856 state->org_fe_fs_add_incr &= 0x0FFFFFFFL;
1857 if (negativeShift)
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001858 state->org_fe_fs_add_incr = ((1L << 28) -
Ralph Metzler126f1e62011-03-12 23:44:33 -05001859 state->org_fe_fs_add_incr);
1860
1861 return Write32(state, FE_FS_REG_ADD_INC_LOP__A,
1862 state->fe_fs_add_incr, 0);
1863}
1864
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001865static int SetCfgNoiseCalibration(struct drxd_state *state,
1866 struct SNoiseCal *noiseCal)
Ralph Metzler126f1e62011-03-12 23:44:33 -05001867{
1868 u16 beOptEna;
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001869 int status = 0;
Ralph Metzler126f1e62011-03-12 23:44:33 -05001870
1871 do {
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03001872 status = Read16(state, SC_RA_RAM_BE_OPT_ENA__A, &beOptEna, 0);
1873 if (status < 0)
1874 break;
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001875 if (noiseCal->cpOpt) {
Ralph Metzler126f1e62011-03-12 23:44:33 -05001876 beOptEna |= (1 << SC_RA_RAM_BE_OPT_ENA_CP_OPT);
1877 } else {
1878 beOptEna &= ~(1 << SC_RA_RAM_BE_OPT_ENA_CP_OPT);
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03001879 status = Write16(state, CP_REG_AC_NEXP_OFFS__A, noiseCal->cpNexpOfs, 0);
1880 if (status < 0)
1881 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -05001882 }
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03001883 status = Write16(state, SC_RA_RAM_BE_OPT_ENA__A, beOptEna, 0);
1884 if (status < 0)
1885 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -05001886
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001887 if (!state->type_A) {
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03001888 status = Write16(state, B_SC_RA_RAM_CO_TD_CAL_2K__A, noiseCal->tdCal2k, 0);
1889 if (status < 0)
1890 break;
1891 status = Write16(state, B_SC_RA_RAM_CO_TD_CAL_8K__A, noiseCal->tdCal8k, 0);
1892 if (status < 0)
1893 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -05001894 }
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001895 } while (0);
Ralph Metzler126f1e62011-03-12 23:44:33 -05001896
1897 return status;
1898}
1899
1900static int DRX_Start(struct drxd_state *state, s32 off)
1901{
1902 struct dvb_ofdm_parameters *p = &state->param.u.ofdm;
1903 int status;
1904
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001905 u16 transmissionParams = 0;
1906 u16 operationMode = 0;
1907 u16 qpskTdTpsPwr = 0;
1908 u16 qam16TdTpsPwr = 0;
1909 u16 qam64TdTpsPwr = 0;
1910 u32 feIfIncr = 0;
1911 u32 bandwidth = 0;
Ralph Metzler126f1e62011-03-12 23:44:33 -05001912 int mirrorFreqSpect;
1913
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001914 u16 qpskSnCeGain = 0;
1915 u16 qam16SnCeGain = 0;
1916 u16 qam64SnCeGain = 0;
1917 u16 qpskIsGainMan = 0;
1918 u16 qam16IsGainMan = 0;
1919 u16 qam64IsGainMan = 0;
1920 u16 qpskIsGainExp = 0;
1921 u16 qam16IsGainExp = 0;
1922 u16 qam64IsGainExp = 0;
1923 u16 bandwidthParam = 0;
Ralph Metzler126f1e62011-03-12 23:44:33 -05001924
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001925 if (off < 0)
1926 off = (off - 500) / 1000;
Ralph Metzler126f1e62011-03-12 23:44:33 -05001927 else
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001928 off = (off + 500) / 1000;
Ralph Metzler126f1e62011-03-12 23:44:33 -05001929
1930 do {
1931 if (state->drxd_state != DRXD_STOPPED)
1932 return -1;
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03001933 status = ResetECOD(state);
1934 if (status < 0)
1935 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -05001936 if (state->type_A) {
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03001937 status = InitSC(state);
1938 if (status < 0)
1939 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -05001940 } else {
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03001941 status = InitFT(state);
1942 if (status < 0)
1943 break;
1944 status = InitCP(state);
1945 if (status < 0)
1946 break;
1947 status = InitCE(state);
1948 if (status < 0)
1949 break;
1950 status = InitEQ(state);
1951 if (status < 0)
1952 break;
1953 status = InitSC(state);
1954 if (status < 0)
1955 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -05001956 }
1957
1958 /* Restore current IF & RF AGC settings */
1959
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03001960 status = SetCfgIfAgc(state, &state->if_agc_cfg);
1961 if (status < 0)
1962 break;
1963 status = SetCfgRfAgc(state, &state->rf_agc_cfg);
1964 if (status < 0)
1965 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -05001966
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001967 mirrorFreqSpect = (state->param.inversion == INVERSION_ON);
Ralph Metzler126f1e62011-03-12 23:44:33 -05001968
1969 switch (p->transmission_mode) {
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001970 default: /* Not set, detect it automatically */
Ralph Metzler126f1e62011-03-12 23:44:33 -05001971 operationMode |= SC_RA_RAM_OP_AUTO_MODE__M;
1972 /* fall through , try first guess DRX_FFTMODE_8K */
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001973 case TRANSMISSION_MODE_8K:
Ralph Metzler126f1e62011-03-12 23:44:33 -05001974 transmissionParams |= SC_RA_RAM_OP_PARAM_MODE_8K;
1975 if (state->type_A) {
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03001976 status = Write16(state, EC_SB_REG_TR_MODE__A, EC_SB_REG_TR_MODE_8K, 0x0000);
1977 if (status < 0)
1978 break;
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001979 qpskSnCeGain = 99;
Ralph Metzler126f1e62011-03-12 23:44:33 -05001980 qam16SnCeGain = 83;
1981 qam64SnCeGain = 67;
1982 }
1983 break;
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001984 case TRANSMISSION_MODE_2K:
Ralph Metzler126f1e62011-03-12 23:44:33 -05001985 transmissionParams |= SC_RA_RAM_OP_PARAM_MODE_2K;
1986 if (state->type_A) {
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03001987 status = Write16(state, EC_SB_REG_TR_MODE__A, EC_SB_REG_TR_MODE_2K, 0x0000);
1988 if (status < 0)
1989 break;
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001990 qpskSnCeGain = 97;
Ralph Metzler126f1e62011-03-12 23:44:33 -05001991 qam16SnCeGain = 71;
1992 qam64SnCeGain = 65;
1993 }
1994 break;
1995 }
1996
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03001997 switch (p->guard_interval) {
Ralph Metzler126f1e62011-03-12 23:44:33 -05001998 case GUARD_INTERVAL_1_4:
1999 transmissionParams |= SC_RA_RAM_OP_PARAM_GUARD_4;
2000 break;
2001 case GUARD_INTERVAL_1_8:
2002 transmissionParams |= SC_RA_RAM_OP_PARAM_GUARD_8;
2003 break;
2004 case GUARD_INTERVAL_1_16:
2005 transmissionParams |= SC_RA_RAM_OP_PARAM_GUARD_16;
2006 break;
2007 case GUARD_INTERVAL_1_32:
2008 transmissionParams |= SC_RA_RAM_OP_PARAM_GUARD_32;
2009 break;
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002010 default: /* Not set, detect it automatically */
Ralph Metzler126f1e62011-03-12 23:44:33 -05002011 operationMode |= SC_RA_RAM_OP_AUTO_GUARD__M;
2012 /* try first guess 1/4 */
2013 transmissionParams |= SC_RA_RAM_OP_PARAM_GUARD_4;
2014 break;
2015 }
2016
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002017 switch (p->hierarchy_information) {
Ralph Metzler126f1e62011-03-12 23:44:33 -05002018 case HIERARCHY_1:
2019 transmissionParams |= SC_RA_RAM_OP_PARAM_HIER_A1;
2020 if (state->type_A) {
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03002021 status = Write16(state, EQ_REG_OT_ALPHA__A, 0x0001, 0x0000);
2022 if (status < 0)
2023 break;
2024 status = Write16(state, EC_SB_REG_ALPHA__A, 0x0001, 0x0000);
2025 if (status < 0)
2026 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002027
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002028 qpskTdTpsPwr = EQ_TD_TPS_PWR_UNKNOWN;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002029 qam16TdTpsPwr = EQ_TD_TPS_PWR_QAM16_ALPHA1;
2030 qam64TdTpsPwr = EQ_TD_TPS_PWR_QAM64_ALPHA1;
2031
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002032 qpskIsGainMan =
2033 SC_RA_RAM_EQ_IS_GAIN_UNKNOWN_MAN__PRE;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002034 qam16IsGainMan =
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002035 SC_RA_RAM_EQ_IS_GAIN_16QAM_MAN__PRE;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002036 qam64IsGainMan =
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002037 SC_RA_RAM_EQ_IS_GAIN_64QAM_MAN__PRE;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002038
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002039 qpskIsGainExp =
2040 SC_RA_RAM_EQ_IS_GAIN_UNKNOWN_EXP__PRE;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002041 qam16IsGainExp =
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002042 SC_RA_RAM_EQ_IS_GAIN_16QAM_EXP__PRE;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002043 qam64IsGainExp =
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002044 SC_RA_RAM_EQ_IS_GAIN_64QAM_EXP__PRE;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002045 }
2046 break;
2047
2048 case HIERARCHY_2:
2049 transmissionParams |= SC_RA_RAM_OP_PARAM_HIER_A2;
2050 if (state->type_A) {
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03002051 status = Write16(state, EQ_REG_OT_ALPHA__A, 0x0002, 0x0000);
2052 if (status < 0)
2053 break;
2054 status = Write16(state, EC_SB_REG_ALPHA__A, 0x0002, 0x0000);
2055 if (status < 0)
2056 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002057
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002058 qpskTdTpsPwr = EQ_TD_TPS_PWR_UNKNOWN;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002059 qam16TdTpsPwr = EQ_TD_TPS_PWR_QAM16_ALPHA2;
2060 qam64TdTpsPwr = EQ_TD_TPS_PWR_QAM64_ALPHA2;
2061
2062 qpskIsGainMan =
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002063 SC_RA_RAM_EQ_IS_GAIN_UNKNOWN_MAN__PRE;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002064 qam16IsGainMan =
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002065 SC_RA_RAM_EQ_IS_GAIN_16QAM_A2_MAN__PRE;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002066 qam64IsGainMan =
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002067 SC_RA_RAM_EQ_IS_GAIN_64QAM_A2_MAN__PRE;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002068
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002069 qpskIsGainExp =
2070 SC_RA_RAM_EQ_IS_GAIN_UNKNOWN_EXP__PRE;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002071 qam16IsGainExp =
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002072 SC_RA_RAM_EQ_IS_GAIN_16QAM_A2_EXP__PRE;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002073 qam64IsGainExp =
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002074 SC_RA_RAM_EQ_IS_GAIN_64QAM_A2_EXP__PRE;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002075 }
2076 break;
2077 case HIERARCHY_4:
2078 transmissionParams |= SC_RA_RAM_OP_PARAM_HIER_A4;
2079 if (state->type_A) {
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03002080 status = Write16(state, EQ_REG_OT_ALPHA__A, 0x0003, 0x0000);
2081 if (status < 0)
2082 break;
2083 status = Write16(state, EC_SB_REG_ALPHA__A, 0x0003, 0x0000);
2084 if (status < 0)
2085 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002086
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002087 qpskTdTpsPwr = EQ_TD_TPS_PWR_UNKNOWN;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002088 qam16TdTpsPwr = EQ_TD_TPS_PWR_QAM16_ALPHA4;
2089 qam64TdTpsPwr = EQ_TD_TPS_PWR_QAM64_ALPHA4;
2090
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002091 qpskIsGainMan =
2092 SC_RA_RAM_EQ_IS_GAIN_UNKNOWN_MAN__PRE;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002093 qam16IsGainMan =
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002094 SC_RA_RAM_EQ_IS_GAIN_16QAM_A4_MAN__PRE;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002095 qam64IsGainMan =
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002096 SC_RA_RAM_EQ_IS_GAIN_64QAM_A4_MAN__PRE;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002097
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002098 qpskIsGainExp =
2099 SC_RA_RAM_EQ_IS_GAIN_UNKNOWN_EXP__PRE;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002100 qam16IsGainExp =
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002101 SC_RA_RAM_EQ_IS_GAIN_16QAM_A4_EXP__PRE;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002102 qam64IsGainExp =
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002103 SC_RA_RAM_EQ_IS_GAIN_64QAM_A4_EXP__PRE;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002104 }
2105 break;
2106 case HIERARCHY_AUTO:
2107 default:
2108 /* Not set, detect it automatically, start with none */
2109 operationMode |= SC_RA_RAM_OP_AUTO_HIER__M;
2110 transmissionParams |= SC_RA_RAM_OP_PARAM_HIER_NO;
2111 if (state->type_A) {
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03002112 status = Write16(state, EQ_REG_OT_ALPHA__A, 0x0000, 0x0000);
2113 if (status < 0)
2114 break;
2115 status = Write16(state, EC_SB_REG_ALPHA__A, 0x0000, 0x0000);
2116 if (status < 0)
2117 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002118
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002119 qpskTdTpsPwr = EQ_TD_TPS_PWR_QPSK;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002120 qam16TdTpsPwr = EQ_TD_TPS_PWR_QAM16_ALPHAN;
2121 qam64TdTpsPwr = EQ_TD_TPS_PWR_QAM64_ALPHAN;
2122
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002123 qpskIsGainMan =
2124 SC_RA_RAM_EQ_IS_GAIN_QPSK_MAN__PRE;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002125 qam16IsGainMan =
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002126 SC_RA_RAM_EQ_IS_GAIN_16QAM_MAN__PRE;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002127 qam64IsGainMan =
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002128 SC_RA_RAM_EQ_IS_GAIN_64QAM_MAN__PRE;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002129
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002130 qpskIsGainExp =
2131 SC_RA_RAM_EQ_IS_GAIN_QPSK_EXP__PRE;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002132 qam16IsGainExp =
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002133 SC_RA_RAM_EQ_IS_GAIN_16QAM_EXP__PRE;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002134 qam64IsGainExp =
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002135 SC_RA_RAM_EQ_IS_GAIN_64QAM_EXP__PRE;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002136 }
2137 break;
2138 }
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03002139 status = status;
2140 if (status < 0)
2141 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002142
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002143 switch (p->constellation) {
Ralph Metzler126f1e62011-03-12 23:44:33 -05002144 default:
2145 operationMode |= SC_RA_RAM_OP_AUTO_CONST__M;
2146 /* fall through , try first guess
2147 DRX_CONSTELLATION_QAM64 */
2148 case QAM_64:
2149 transmissionParams |= SC_RA_RAM_OP_PARAM_CONST_QAM64;
2150 if (state->type_A) {
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03002151 status = Write16(state, EQ_REG_OT_CONST__A, 0x0002, 0x0000);
2152 if (status < 0)
2153 break;
2154 status = Write16(state, EC_SB_REG_CONST__A, EC_SB_REG_CONST_64QAM, 0x0000);
2155 if (status < 0)
2156 break;
2157 status = Write16(state, EC_SB_REG_SCALE_MSB__A, 0x0020, 0x0000);
2158 if (status < 0)
2159 break;
2160 status = Write16(state, EC_SB_REG_SCALE_BIT2__A, 0x0008, 0x0000);
2161 if (status < 0)
2162 break;
2163 status = Write16(state, EC_SB_REG_SCALE_LSB__A, 0x0002, 0x0000);
2164 if (status < 0)
2165 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002166
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03002167 status = Write16(state, EQ_REG_TD_TPS_PWR_OFS__A, qam64TdTpsPwr, 0x0000);
2168 if (status < 0)
2169 break;
2170 status = Write16(state, EQ_REG_SN_CEGAIN__A, qam64SnCeGain, 0x0000);
2171 if (status < 0)
2172 break;
2173 status = Write16(state, EQ_REG_IS_GAIN_MAN__A, qam64IsGainMan, 0x0000);
2174 if (status < 0)
2175 break;
2176 status = Write16(state, EQ_REG_IS_GAIN_EXP__A, qam64IsGainExp, 0x0000);
2177 if (status < 0)
2178 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002179 }
2180 break;
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002181 case QPSK:
Ralph Metzler126f1e62011-03-12 23:44:33 -05002182 transmissionParams |= SC_RA_RAM_OP_PARAM_CONST_QPSK;
2183 if (state->type_A) {
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03002184 status = Write16(state, EQ_REG_OT_CONST__A, 0x0000, 0x0000);
2185 if (status < 0)
2186 break;
2187 status = Write16(state, EC_SB_REG_CONST__A, EC_SB_REG_CONST_QPSK, 0x0000);
2188 if (status < 0)
2189 break;
2190 status = Write16(state, EC_SB_REG_SCALE_MSB__A, 0x0010, 0x0000);
2191 if (status < 0)
2192 break;
2193 status = Write16(state, EC_SB_REG_SCALE_BIT2__A, 0x0000, 0x0000);
2194 if (status < 0)
2195 break;
2196 status = Write16(state, EC_SB_REG_SCALE_LSB__A, 0x0000, 0x0000);
2197 if (status < 0)
2198 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002199
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03002200 status = Write16(state, EQ_REG_TD_TPS_PWR_OFS__A, qpskTdTpsPwr, 0x0000);
2201 if (status < 0)
2202 break;
2203 status = Write16(state, EQ_REG_SN_CEGAIN__A, qpskSnCeGain, 0x0000);
2204 if (status < 0)
2205 break;
2206 status = Write16(state, EQ_REG_IS_GAIN_MAN__A, qpskIsGainMan, 0x0000);
2207 if (status < 0)
2208 break;
2209 status = Write16(state, EQ_REG_IS_GAIN_EXP__A, qpskIsGainExp, 0x0000);
2210 if (status < 0)
2211 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002212 }
2213 break;
2214
2215 case QAM_16:
2216 transmissionParams |= SC_RA_RAM_OP_PARAM_CONST_QAM16;
2217 if (state->type_A) {
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03002218 status = Write16(state, EQ_REG_OT_CONST__A, 0x0001, 0x0000);
2219 if (status < 0)
2220 break;
2221 status = Write16(state, EC_SB_REG_CONST__A, EC_SB_REG_CONST_16QAM, 0x0000);
2222 if (status < 0)
2223 break;
2224 status = Write16(state, EC_SB_REG_SCALE_MSB__A, 0x0010, 0x0000);
2225 if (status < 0)
2226 break;
2227 status = Write16(state, EC_SB_REG_SCALE_BIT2__A, 0x0004, 0x0000);
2228 if (status < 0)
2229 break;
2230 status = Write16(state, EC_SB_REG_SCALE_LSB__A, 0x0000, 0x0000);
2231 if (status < 0)
2232 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002233
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03002234 status = Write16(state, EQ_REG_TD_TPS_PWR_OFS__A, qam16TdTpsPwr, 0x0000);
2235 if (status < 0)
2236 break;
2237 status = Write16(state, EQ_REG_SN_CEGAIN__A, qam16SnCeGain, 0x0000);
2238 if (status < 0)
2239 break;
2240 status = Write16(state, EQ_REG_IS_GAIN_MAN__A, qam16IsGainMan, 0x0000);
2241 if (status < 0)
2242 break;
2243 status = Write16(state, EQ_REG_IS_GAIN_EXP__A, qam16IsGainExp, 0x0000);
2244 if (status < 0)
2245 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002246 }
2247 break;
2248
2249 }
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03002250 status = status;
2251 if (status < 0)
2252 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002253
2254 switch (DRX_CHANNEL_HIGH) {
2255 default:
2256 case DRX_CHANNEL_AUTO:
2257 case DRX_CHANNEL_LOW:
2258 transmissionParams |= SC_RA_RAM_OP_PARAM_PRIO_LO;
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03002259 status = Write16(state, EC_SB_REG_PRIOR__A, EC_SB_REG_PRIOR_LO, 0x0000);
2260 if (status < 0)
2261 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002262 break;
2263 case DRX_CHANNEL_HIGH:
2264 transmissionParams |= SC_RA_RAM_OP_PARAM_PRIO_HI;
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03002265 status = Write16(state, EC_SB_REG_PRIOR__A, EC_SB_REG_PRIOR_HI, 0x0000);
2266 if (status < 0)
2267 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002268 break;
2269
2270 }
2271
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002272 switch (p->code_rate_HP) {
Ralph Metzler126f1e62011-03-12 23:44:33 -05002273 case FEC_1_2:
2274 transmissionParams |= SC_RA_RAM_OP_PARAM_RATE_1_2;
2275 if (state->type_A) {
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03002276 status = Write16(state, EC_VD_REG_SET_CODERATE__A, EC_VD_REG_SET_CODERATE_C1_2, 0x0000);
2277 if (status < 0)
2278 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002279 }
2280 break;
2281 default:
2282 operationMode |= SC_RA_RAM_OP_AUTO_RATE__M;
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002283 case FEC_2_3:
Ralph Metzler126f1e62011-03-12 23:44:33 -05002284 transmissionParams |= SC_RA_RAM_OP_PARAM_RATE_2_3;
2285 if (state->type_A) {
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03002286 status = Write16(state, EC_VD_REG_SET_CODERATE__A, EC_VD_REG_SET_CODERATE_C2_3, 0x0000);
2287 if (status < 0)
2288 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002289 }
2290 break;
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002291 case FEC_3_4:
Ralph Metzler126f1e62011-03-12 23:44:33 -05002292 transmissionParams |= SC_RA_RAM_OP_PARAM_RATE_3_4;
2293 if (state->type_A) {
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03002294 status = Write16(state, EC_VD_REG_SET_CODERATE__A, EC_VD_REG_SET_CODERATE_C3_4, 0x0000);
2295 if (status < 0)
2296 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002297 }
2298 break;
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002299 case FEC_5_6:
Ralph Metzler126f1e62011-03-12 23:44:33 -05002300 transmissionParams |= SC_RA_RAM_OP_PARAM_RATE_5_6;
2301 if (state->type_A) {
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03002302 status = Write16(state, EC_VD_REG_SET_CODERATE__A, EC_VD_REG_SET_CODERATE_C5_6, 0x0000);
2303 if (status < 0)
2304 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002305 }
2306 break;
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002307 case FEC_7_8:
Ralph Metzler126f1e62011-03-12 23:44:33 -05002308 transmissionParams |= SC_RA_RAM_OP_PARAM_RATE_7_8;
2309 if (state->type_A) {
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03002310 status = Write16(state, EC_VD_REG_SET_CODERATE__A, EC_VD_REG_SET_CODERATE_C7_8, 0x0000);
2311 if (status < 0)
2312 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002313 }
2314 break;
2315 }
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03002316 status = status;
2317 if (status < 0)
2318 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002319
2320 /* First determine real bandwidth (Hz) */
2321 /* Also set delay for impulse noise cruncher (only A2) */
2322 /* Also set parameters for EC_OC fix, note
2323 EC_OC_REG_TMD_HIL_MAR is changed
2324 by SC for fix for some 8K,1/8 guard but is restored by
2325 InitEC and ResetEC
2326 functions */
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002327 switch (p->bandwidth) {
Ralph Metzler126f1e62011-03-12 23:44:33 -05002328 case BANDWIDTH_AUTO:
2329 case BANDWIDTH_8_MHZ:
2330 /* (64/7)*(8/8)*1000000 */
2331 bandwidth = DRXD_BANDWIDTH_8MHZ_IN_HZ;
2332
2333 bandwidthParam = 0;
2334 status = Write16(state,
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002335 FE_AG_REG_IND_DEL__A, 50, 0x0000);
Ralph Metzler126f1e62011-03-12 23:44:33 -05002336 break;
2337 case BANDWIDTH_7_MHZ:
2338 /* (64/7)*(7/8)*1000000 */
2339 bandwidth = DRXD_BANDWIDTH_7MHZ_IN_HZ;
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002340 bandwidthParam = 0x4807; /*binary:0100 1000 0000 0111 */
Ralph Metzler126f1e62011-03-12 23:44:33 -05002341 status = Write16(state,
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002342 FE_AG_REG_IND_DEL__A, 59, 0x0000);
Ralph Metzler126f1e62011-03-12 23:44:33 -05002343 break;
2344 case BANDWIDTH_6_MHZ:
2345 /* (64/7)*(6/8)*1000000 */
2346 bandwidth = DRXD_BANDWIDTH_6MHZ_IN_HZ;
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002347 bandwidthParam = 0x0F07; /*binary: 0000 1111 0000 0111 */
Ralph Metzler126f1e62011-03-12 23:44:33 -05002348 status = Write16(state,
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002349 FE_AG_REG_IND_DEL__A, 71, 0x0000);
Ralph Metzler126f1e62011-03-12 23:44:33 -05002350 break;
2351 }
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03002352 status = status;
2353 if (status < 0)
2354 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002355
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03002356 status = Write16(state, SC_RA_RAM_BAND__A, bandwidthParam, 0x0000);
2357 if (status < 0)
2358 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002359
2360 {
2361 u16 sc_config;
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03002362 status = Read16(state, SC_RA_RAM_CONFIG__A, &sc_config, 0);
2363 if (status < 0)
2364 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002365
2366 /* enable SLAVE mode in 2k 1/32 to
2367 prevent timing change glitches */
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002368 if ((p->transmission_mode == TRANSMISSION_MODE_2K) &&
2369 (p->guard_interval == GUARD_INTERVAL_1_32)) {
Ralph Metzler126f1e62011-03-12 23:44:33 -05002370 /* enable slave */
2371 sc_config |= SC_RA_RAM_CONFIG_SLAVE__M;
2372 } else {
2373 /* disable slave */
2374 sc_config &= ~SC_RA_RAM_CONFIG_SLAVE__M;
2375 }
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03002376 status = Write16(state, SC_RA_RAM_CONFIG__A, sc_config, 0);
2377 if (status < 0)
2378 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002379 }
2380
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03002381 status = SetCfgNoiseCalibration(state, &state->noise_cal);
2382 if (status < 0)
2383 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002384
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002385 if (state->cscd_state == CSCD_INIT) {
Ralph Metzler126f1e62011-03-12 23:44:33 -05002386 /* switch on SRMM scan in SC */
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03002387 status = Write16(state, SC_RA_RAM_SAMPLE_RATE_COUNT__A, DRXD_OSCDEV_DO_SCAN, 0x0000);
2388 if (status < 0)
2389 break;
2390/* CHK_ERROR(Write16(SC_RA_RAM_SAMPLE_RATE_STEP__A, DRXD_OSCDEV_STEP, 0x0000));*/
Ralph Metzler126f1e62011-03-12 23:44:33 -05002391 state->cscd_state = CSCD_SET;
2392 }
2393
Ralph Metzler126f1e62011-03-12 23:44:33 -05002394 /* Now compute FE_IF_REG_INCR */
2395 /*((( SysFreq/BandWidth)/2)/2) -1) * 2^23) =>
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002396 ((SysFreq / BandWidth) * (2^21) ) - (2^23) */
2397 feIfIncr = MulDiv32(state->sys_clock_freq * 1000,
2398 (1ULL << 21), bandwidth) - (1 << 23);
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03002399 status = Write16(state, FE_IF_REG_INCR0__A, (u16) (feIfIncr & FE_IF_REG_INCR0__M), 0x0000);
2400 if (status < 0)
2401 break;
2402 status = Write16(state, FE_IF_REG_INCR1__A, (u16) ((feIfIncr >> FE_IF_REG_INCR0__W) & FE_IF_REG_INCR1__M), 0x0000);
2403 if (status < 0)
2404 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002405 /* Bandwidth setting done */
2406
2407 /* Mirror & frequency offset */
2408 SetFrequencyShift(state, off, mirrorFreqSpect);
2409
2410 /* Start SC, write channel settings to SC */
2411
2412 /* Enable SC after setting all other parameters */
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03002413 status = Write16(state, SC_COMM_STATE__A, 0, 0x0000);
2414 if (status < 0)
2415 break;
2416 status = Write16(state, SC_COMM_EXEC__A, 1, 0x0000);
2417 if (status < 0)
2418 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002419
2420 /* Write SC parameter registers, operation mode */
2421#if 1
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002422 operationMode = (SC_RA_RAM_OP_AUTO_MODE__M |
2423 SC_RA_RAM_OP_AUTO_GUARD__M |
2424 SC_RA_RAM_OP_AUTO_CONST__M |
2425 SC_RA_RAM_OP_AUTO_HIER__M |
2426 SC_RA_RAM_OP_AUTO_RATE__M);
Ralph Metzler126f1e62011-03-12 23:44:33 -05002427#endif
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03002428 status = SC_SetPrefParamCommand(state, 0x0000, transmissionParams, operationMode);
2429 if (status < 0)
2430 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002431
2432 /* Start correct processes to get in lock */
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03002433 status = SC_ProcStartCommand(state, SC_RA_RAM_PROC_LOCKTRACK, SC_RA_RAM_SW_EVENT_RUN_NMASK__M, SC_RA_RAM_LOCKTRACK_MIN);
2434 if (status < 0)
2435 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002436
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03002437 status = StartOC(state);
2438 if (status < 0)
2439 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002440
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002441 if (state->operation_mode != OM_Default) {
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03002442 status = StartDiversity(state);
2443 if (status < 0)
2444 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002445 }
2446
2447 state->drxd_state = DRXD_STARTED;
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002448 } while (0);
Ralph Metzler126f1e62011-03-12 23:44:33 -05002449
2450 return status;
2451}
2452
2453static int CDRXD(struct drxd_state *state, u32 IntermediateFrequency)
2454{
2455 u32 ulRfAgcOutputLevel = 0xffffffff;
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002456 u32 ulRfAgcSettleLevel = 528; /* Optimum value for MT2060 */
2457 u32 ulRfAgcMinLevel = 0; /* Currently unused */
2458 u32 ulRfAgcMaxLevel = DRXD_FE_CTRL_MAX; /* Currently unused */
2459 u32 ulRfAgcSpeed = 0; /* Currently unused */
2460 u32 ulRfAgcMode = 0; /*2; Off */
2461 u32 ulRfAgcR1 = 820;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002462 u32 ulRfAgcR2 = 2200;
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002463 u32 ulRfAgcR3 = 150;
2464 u32 ulIfAgcMode = 0; /* Auto */
Ralph Metzler126f1e62011-03-12 23:44:33 -05002465 u32 ulIfAgcOutputLevel = 0xffffffff;
2466 u32 ulIfAgcSettleLevel = 0xffffffff;
2467 u32 ulIfAgcMinLevel = 0xffffffff;
2468 u32 ulIfAgcMaxLevel = 0xffffffff;
2469 u32 ulIfAgcSpeed = 0xffffffff;
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002470 u32 ulIfAgcR1 = 820;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002471 u32 ulIfAgcR2 = 2200;
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002472 u32 ulIfAgcR3 = 150;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002473 u32 ulClock = state->config.clock;
2474 u32 ulSerialMode = 0;
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002475 u32 ulEcOcRegOcModeLop = 4; /* Dynamic DTO source */
Ralph Metzler126f1e62011-03-12 23:44:33 -05002476 u32 ulHiI2cDelay = HI_I2C_DELAY;
2477 u32 ulHiI2cBridgeDelay = HI_I2C_BRIDGE_DELAY;
2478 u32 ulHiI2cPatch = 0;
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002479 u32 ulEnvironment = APPENV_PORTABLE;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002480 u32 ulEnvironmentDiversity = APPENV_MOBILE;
2481 u32 ulIFFilter = IFFILTER_SAW;
2482
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002483 state->if_agc_cfg.ctrlMode = AGC_CTRL_AUTO;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002484 state->if_agc_cfg.outputLevel = 0;
2485 state->if_agc_cfg.settleLevel = 140;
2486 state->if_agc_cfg.minOutputLevel = 0;
2487 state->if_agc_cfg.maxOutputLevel = 1023;
2488 state->if_agc_cfg.speed = 904;
2489
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002490 if (ulIfAgcMode == 1 && ulIfAgcOutputLevel <= DRXD_FE_CTRL_MAX) {
2491 state->if_agc_cfg.ctrlMode = AGC_CTRL_USER;
2492 state->if_agc_cfg.outputLevel = (u16) (ulIfAgcOutputLevel);
Ralph Metzler126f1e62011-03-12 23:44:33 -05002493 }
2494
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002495 if (ulIfAgcMode == 0 &&
Ralph Metzler126f1e62011-03-12 23:44:33 -05002496 ulIfAgcSettleLevel <= DRXD_FE_CTRL_MAX &&
2497 ulIfAgcMinLevel <= DRXD_FE_CTRL_MAX &&
2498 ulIfAgcMaxLevel <= DRXD_FE_CTRL_MAX &&
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002499 ulIfAgcSpeed <= DRXD_FE_CTRL_MAX) {
2500 state->if_agc_cfg.ctrlMode = AGC_CTRL_AUTO;
2501 state->if_agc_cfg.settleLevel = (u16) (ulIfAgcSettleLevel);
2502 state->if_agc_cfg.minOutputLevel = (u16) (ulIfAgcMinLevel);
2503 state->if_agc_cfg.maxOutputLevel = (u16) (ulIfAgcMaxLevel);
2504 state->if_agc_cfg.speed = (u16) (ulIfAgcSpeed);
Ralph Metzler126f1e62011-03-12 23:44:33 -05002505 }
2506
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002507 state->if_agc_cfg.R1 = (u16) (ulIfAgcR1);
2508 state->if_agc_cfg.R2 = (u16) (ulIfAgcR2);
2509 state->if_agc_cfg.R3 = (u16) (ulIfAgcR3);
Ralph Metzler126f1e62011-03-12 23:44:33 -05002510
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002511 state->rf_agc_cfg.R1 = (u16) (ulRfAgcR1);
2512 state->rf_agc_cfg.R2 = (u16) (ulRfAgcR2);
2513 state->rf_agc_cfg.R3 = (u16) (ulRfAgcR3);
Ralph Metzler126f1e62011-03-12 23:44:33 -05002514
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002515 state->rf_agc_cfg.ctrlMode = AGC_CTRL_AUTO;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002516 /* rest of the RFAgcCfg structure currently unused */
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002517 if (ulRfAgcMode == 1 && ulRfAgcOutputLevel <= DRXD_FE_CTRL_MAX) {
2518 state->rf_agc_cfg.ctrlMode = AGC_CTRL_USER;
2519 state->rf_agc_cfg.outputLevel = (u16) (ulRfAgcOutputLevel);
Ralph Metzler126f1e62011-03-12 23:44:33 -05002520 }
2521
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002522 if (ulRfAgcMode == 0 &&
Ralph Metzler126f1e62011-03-12 23:44:33 -05002523 ulRfAgcSettleLevel <= DRXD_FE_CTRL_MAX &&
2524 ulRfAgcMinLevel <= DRXD_FE_CTRL_MAX &&
2525 ulRfAgcMaxLevel <= DRXD_FE_CTRL_MAX &&
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002526 ulRfAgcSpeed <= DRXD_FE_CTRL_MAX) {
2527 state->rf_agc_cfg.ctrlMode = AGC_CTRL_AUTO;
2528 state->rf_agc_cfg.settleLevel = (u16) (ulRfAgcSettleLevel);
2529 state->rf_agc_cfg.minOutputLevel = (u16) (ulRfAgcMinLevel);
2530 state->rf_agc_cfg.maxOutputLevel = (u16) (ulRfAgcMaxLevel);
2531 state->rf_agc_cfg.speed = (u16) (ulRfAgcSpeed);
Ralph Metzler126f1e62011-03-12 23:44:33 -05002532 }
2533
Mauro Carvalho Chehab9999daf2011-03-25 12:10:05 -03002534 if (ulRfAgcMode == 2)
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002535 state->rf_agc_cfg.ctrlMode = AGC_CTRL_OFF;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002536
2537 if (ulEnvironment <= 2)
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002538 state->app_env_default = (enum app_env)
2539 (ulEnvironment);
Ralph Metzler126f1e62011-03-12 23:44:33 -05002540 if (ulEnvironmentDiversity <= 2)
2541 state->app_env_diversity = (enum app_env)
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002542 (ulEnvironmentDiversity);
Ralph Metzler126f1e62011-03-12 23:44:33 -05002543
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002544 if (ulIFFilter == IFFILTER_DISCRETE) {
Ralph Metzler126f1e62011-03-12 23:44:33 -05002545 /* discrete filter */
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002546 state->noise_cal.cpOpt = 0;
2547 state->noise_cal.cpNexpOfs = 40;
2548 state->noise_cal.tdCal2k = -40;
2549 state->noise_cal.tdCal8k = -24;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002550 } else {
2551 /* SAW filter */
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002552 state->noise_cal.cpOpt = 1;
2553 state->noise_cal.cpNexpOfs = 0;
2554 state->noise_cal.tdCal2k = -21;
2555 state->noise_cal.tdCal8k = -24;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002556 }
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002557 state->m_EcOcRegOcModeLop = (u16) (ulEcOcRegOcModeLop);
Ralph Metzler126f1e62011-03-12 23:44:33 -05002558
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002559 state->chip_adr = (state->config.demod_address << 1) | 1;
2560 switch (ulHiI2cPatch) {
2561 case 1:
2562 state->m_HiI2cPatch = DRXD_HiI2cPatch_1;
2563 break;
2564 case 3:
2565 state->m_HiI2cPatch = DRXD_HiI2cPatch_3;
2566 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002567 default:
2568 state->m_HiI2cPatch = NULL;
2569 }
2570
2571 /* modify tuner and clock attributes */
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002572 state->intermediate_freq = (u16) (IntermediateFrequency / 1000);
Ralph Metzler126f1e62011-03-12 23:44:33 -05002573 /* expected system clock frequency in kHz */
2574 state->expected_sys_clock_freq = 48000;
2575 /* real system clock frequency in kHz */
2576 state->sys_clock_freq = 48000;
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002577 state->osc_clock_freq = (u16) ulClock;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002578 state->osc_clock_deviation = 0;
2579 state->cscd_state = CSCD_INIT;
2580 state->drxd_state = DRXD_UNINITIALIZED;
2581
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002582 state->PGA = 0;
2583 state->type_A = 0;
2584 state->tuner_mirrors = 0;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002585
2586 /* modify MPEG output attributes */
Devin Heitmuellerba967962011-03-13 01:54:02 -03002587 state->insert_rs_byte = state->config.insert_rs_byte;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002588 state->enable_parallel = (ulSerialMode != 1);
2589
2590 /* Timing div, 250ns/Psys */
2591 /* Timing div, = ( delay (nano seconds) * sysclk (kHz) )/ 1000 */
2592
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002593 state->hi_cfg_timing_div = (u16) ((state->sys_clock_freq / 1000) *
2594 ulHiI2cDelay) / 1000;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002595 /* Bridge delay, uses oscilator clock */
2596 /* Delay = ( delay (nano seconds) * oscclk (kHz) )/ 1000 */
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002597 state->hi_cfg_bridge_delay = (u16) ((state->osc_clock_freq / 1000) *
2598 ulHiI2cBridgeDelay) / 1000;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002599
2600 state->m_FeAgRegAgPwd = DRXD_DEF_AG_PWD_CONSUMER;
2601 /* state->m_FeAgRegAgPwd = DRXD_DEF_AG_PWD_PRO; */
2602 state->m_FeAgRegAgAgcSio = DRXD_DEF_AG_AGC_SIO;
2603 return 0;
2604}
2605
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002606int DRXD_init(struct drxd_state *state, const u8 * fw, u32 fw_size)
Ralph Metzler126f1e62011-03-12 23:44:33 -05002607{
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002608 int status = 0;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002609 u32 driverVersion;
2610
2611 if (state->init_done)
2612 return 0;
2613
2614 CDRXD(state, state->config.IF ? state->config.IF : 36000000);
2615
2616 do {
2617 state->operation_mode = OM_Default;
2618
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03002619 status = SetDeviceTypeId(state);
2620 if (status < 0)
2621 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002622
2623 /* Apply I2c address patch to B1 */
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002624 if (!state->type_A && state->m_HiI2cPatch != NULL)
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03002625 status = WriteTable(state, state->m_HiI2cPatch);
2626 if (status < 0)
2627 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002628
2629 if (state->type_A) {
2630 /* HI firmware patch for UIO readout,
2631 avoid clearing of result register */
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03002632 status = Write16(state, 0x43012D, 0x047f, 0);
2633 if (status < 0)
2634 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002635 }
2636
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03002637 status = HI_ResetCommand(state);
2638 if (status < 0)
2639 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002640
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03002641 status = StopAllProcessors(state);
2642 if (status < 0)
2643 break;
2644 status = InitCC(state);
2645 if (status < 0)
2646 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002647
2648 state->osc_clock_deviation = 0;
2649
2650 if (state->config.osc_deviation)
2651 state->osc_clock_deviation =
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002652 state->config.osc_deviation(state->priv, 0, 0);
Ralph Metzler126f1e62011-03-12 23:44:33 -05002653 {
2654 /* Handle clock deviation */
2655 s32 devB;
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002656 s32 devA = (s32) (state->osc_clock_deviation) *
2657 (s32) (state->expected_sys_clock_freq);
Ralph Metzler126f1e62011-03-12 23:44:33 -05002658 /* deviation in kHz */
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002659 s32 deviation = (devA / (1000000L));
Ralph Metzler126f1e62011-03-12 23:44:33 -05002660 /* rounding, signed */
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002661 if (devA > 0)
2662 devB = (2);
Ralph Metzler126f1e62011-03-12 23:44:33 -05002663 else
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002664 devB = (-2);
2665 if ((devB * (devA % 1000000L) > 1000000L)) {
Ralph Metzler126f1e62011-03-12 23:44:33 -05002666 /* add +1 or -1 */
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002667 deviation += (devB / 2);
Ralph Metzler126f1e62011-03-12 23:44:33 -05002668 }
2669
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002670 state->sys_clock_freq =
2671 (u16) ((state->expected_sys_clock_freq) +
2672 deviation);
Ralph Metzler126f1e62011-03-12 23:44:33 -05002673 }
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03002674 status = InitHI(state);
2675 if (status < 0)
2676 break;
2677 status = InitAtomicRead(state);
2678 if (status < 0)
2679 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002680
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03002681 status = EnableAndResetMB(state);
2682 if (status < 0)
2683 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002684 if (state->type_A)
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03002685 status = ResetCEFR(state);
2686 if (status < 0)
2687 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002688
2689 if (fw) {
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03002690 status = DownloadMicrocode(state, fw, fw_size);
2691 if (status < 0)
2692 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002693 } else {
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03002694 status = DownloadMicrocode(state, state->microcode, state->microcode_length);
2695 if (status < 0)
2696 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002697 }
2698
2699 if (state->PGA) {
2700 state->m_FeAgRegAgPwd = DRXD_DEF_AG_PWD_PRO;
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002701 SetCfgPga(state, 0); /* PGA = 0 dB */
Ralph Metzler126f1e62011-03-12 23:44:33 -05002702 } else {
2703 state->m_FeAgRegAgPwd = DRXD_DEF_AG_PWD_CONSUMER;
2704 }
2705
2706 state->m_FeAgRegAgAgcSio = DRXD_DEF_AG_AGC_SIO;
2707
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03002708 status = InitFE(state);
2709 if (status < 0)
2710 break;
2711 status = InitFT(state);
2712 if (status < 0)
2713 break;
2714 status = InitCP(state);
2715 if (status < 0)
2716 break;
2717 status = InitCE(state);
2718 if (status < 0)
2719 break;
2720 status = InitEQ(state);
2721 if (status < 0)
2722 break;
2723 status = InitEC(state);
2724 if (status < 0)
2725 break;
2726 status = InitSC(state);
2727 if (status < 0)
2728 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002729
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03002730 status = SetCfgIfAgc(state, &state->if_agc_cfg);
2731 if (status < 0)
2732 break;
2733 status = SetCfgRfAgc(state, &state->rf_agc_cfg);
2734 if (status < 0)
2735 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002736
2737 state->cscd_state = CSCD_INIT;
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03002738 status = Write16(state, SC_COMM_EXEC__A, SC_COMM_EXEC_CTL_STOP, 0);
2739 if (status < 0)
2740 break;
2741 status = Write16(state, LC_COMM_EXEC__A, SC_COMM_EXEC_CTL_STOP, 0);
2742 if (status < 0)
2743 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002744
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002745 driverVersion = (((VERSION_MAJOR / 10) << 4) +
2746 (VERSION_MAJOR % 10)) << 24;
2747 driverVersion += (((VERSION_MINOR / 10) << 4) +
2748 (VERSION_MINOR % 10)) << 16;
2749 driverVersion += ((VERSION_PATCH / 1000) << 12) +
2750 ((VERSION_PATCH / 100) << 8) +
2751 ((VERSION_PATCH / 10) << 4) + (VERSION_PATCH % 10);
Ralph Metzler126f1e62011-03-12 23:44:33 -05002752
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03002753 status = Write32(state, SC_RA_RAM_DRIVER_VERSION__AX, driverVersion, 0);
2754 if (status < 0)
2755 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002756
Mauro Carvalho Chehab58d5eae2011-03-25 11:45:29 -03002757 status = StopOC(state);
2758 if (status < 0)
2759 break;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002760
2761 state->drxd_state = DRXD_STOPPED;
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002762 state->init_done = 1;
2763 status = 0;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002764 } while (0);
2765 return status;
2766}
2767
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002768int DRXD_status(struct drxd_state *state, u32 * pLockStatus)
Ralph Metzler126f1e62011-03-12 23:44:33 -05002769{
2770 DRX_GetLockStatus(state, pLockStatus);
2771
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002772 /*if (*pLockStatus&DRX_LOCK_MPEG) */
2773 if (*pLockStatus & DRX_LOCK_FEC) {
Ralph Metzler126f1e62011-03-12 23:44:33 -05002774 ConfigureMPEGOutput(state, 1);
2775 /* Get status again, in case we have MPEG lock now */
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002776 /*DRX_GetLockStatus(state, pLockStatus); */
Ralph Metzler126f1e62011-03-12 23:44:33 -05002777 }
2778
2779 return 0;
2780}
2781
2782/****************************************************************************/
2783/****************************************************************************/
2784/****************************************************************************/
2785
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002786static int drxd_read_signal_strength(struct dvb_frontend *fe, u16 * strength)
Ralph Metzler126f1e62011-03-12 23:44:33 -05002787{
2788 struct drxd_state *state = fe->demodulator_priv;
2789 u32 value;
2790 int res;
2791
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002792 res = ReadIFAgc(state, &value);
2793 if (res < 0)
2794 *strength = 0;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002795 else
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002796 *strength = 0xffff - (value << 4);
Ralph Metzler126f1e62011-03-12 23:44:33 -05002797 return 0;
2798}
2799
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002800static int drxd_read_status(struct dvb_frontend *fe, fe_status_t * status)
Ralph Metzler126f1e62011-03-12 23:44:33 -05002801{
2802 struct drxd_state *state = fe->demodulator_priv;
2803 u32 lock;
2804
2805 DRXD_status(state, &lock);
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002806 *status = 0;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002807 /* No MPEG lock in V255 firmware, bug ? */
2808#if 1
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002809 if (lock & DRX_LOCK_MPEG)
2810 *status |= FE_HAS_LOCK;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002811#else
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002812 if (lock & DRX_LOCK_FEC)
2813 *status |= FE_HAS_LOCK;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002814#endif
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002815 if (lock & DRX_LOCK_FEC)
2816 *status |= FE_HAS_VITERBI | FE_HAS_SYNC;
2817 if (lock & DRX_LOCK_DEMOD)
2818 *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002819
2820 return 0;
2821}
2822
2823static int drxd_init(struct dvb_frontend *fe)
2824{
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002825 struct drxd_state *state = fe->demodulator_priv;
2826 int err = 0;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002827
2828/* if (request_firmware(&state->fw, "drxd.fw", state->dev)<0) */
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002829 return DRXD_init(state, 0, 0);
Ralph Metzler126f1e62011-03-12 23:44:33 -05002830
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002831 err = DRXD_init(state, state->fw->data, state->fw->size);
Ralph Metzler126f1e62011-03-12 23:44:33 -05002832 release_firmware(state->fw);
2833 return err;
2834}
2835
2836int drxd_config_i2c(struct dvb_frontend *fe, int onoff)
2837{
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002838 struct drxd_state *state = fe->demodulator_priv;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002839
Devin Heitmueller6b142b32011-03-13 02:02:01 -03002840 if (state->config.disable_i2c_gate_ctrl == 1)
2841 return 0;
2842
Ralph Metzler126f1e62011-03-12 23:44:33 -05002843 return DRX_ConfigureI2CBridge(state, onoff);
2844}
Mauro Carvalho Chehab9999daf2011-03-25 12:10:05 -03002845EXPORT_SYMBOL(drxd_config_i2c);
Ralph Metzler126f1e62011-03-12 23:44:33 -05002846
2847static int drxd_get_tune_settings(struct dvb_frontend *fe,
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002848 struct dvb_frontend_tune_settings *sets)
Ralph Metzler126f1e62011-03-12 23:44:33 -05002849{
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002850 sets->min_delay_ms = 10000;
2851 sets->max_drift = 0;
2852 sets->step_size = 0;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002853 return 0;
2854}
2855
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002856static int drxd_read_ber(struct dvb_frontend *fe, u32 * ber)
Ralph Metzler126f1e62011-03-12 23:44:33 -05002857{
2858 *ber = 0;
2859 return 0;
2860}
2861
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002862static int drxd_read_snr(struct dvb_frontend *fe, u16 * snr)
Ralph Metzler126f1e62011-03-12 23:44:33 -05002863{
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002864 *snr = 0;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002865 return 0;
2866}
2867
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002868static int drxd_read_ucblocks(struct dvb_frontend *fe, u32 * ucblocks)
Ralph Metzler126f1e62011-03-12 23:44:33 -05002869{
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002870 *ucblocks = 0;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002871 return 0;
2872}
2873
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002874static int drxd_sleep(struct dvb_frontend *fe)
Ralph Metzler126f1e62011-03-12 23:44:33 -05002875{
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002876 struct drxd_state *state = fe->demodulator_priv;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002877
2878 ConfigureMPEGOutput(state, 0);
2879 return 0;
2880}
2881
2882static int drxd_get_frontend(struct dvb_frontend *fe,
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002883 struct dvb_frontend_parameters *param)
Ralph Metzler126f1e62011-03-12 23:44:33 -05002884{
2885 return 0;
2886}
2887
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002888static int drxd_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
Ralph Metzler126f1e62011-03-12 23:44:33 -05002889{
2890 return drxd_config_i2c(fe, enable);
2891}
2892
2893static int drxd_set_frontend(struct dvb_frontend *fe,
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002894 struct dvb_frontend_parameters *param)
Ralph Metzler126f1e62011-03-12 23:44:33 -05002895{
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002896 struct drxd_state *state = fe->demodulator_priv;
2897 s32 off = 0;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002898
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002899 state->param = *param;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002900 DRX_Stop(state);
2901
2902 if (fe->ops.tuner_ops.set_params) {
2903 fe->ops.tuner_ops.set_params(fe, param);
2904 if (fe->ops.i2c_gate_ctrl)
2905 fe->ops.i2c_gate_ctrl(fe, 0);
2906 }
2907
2908 /* FIXME: move PLL drivers */
2909 if (state->config.pll_set &&
2910 state->config.pll_set(state->priv, param,
2911 state->config.pll_address,
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002912 state->config.demoda_address, &off) < 0) {
Mauro Carvalho Chehab9999daf2011-03-25 12:10:05 -03002913 printk(KERN_ERR "Error in pll_set\n");
Ralph Metzler126f1e62011-03-12 23:44:33 -05002914 return -1;
2915 }
2916
2917 msleep(200);
2918
2919 return DRX_Start(state, off);
2920}
2921
Ralph Metzler126f1e62011-03-12 23:44:33 -05002922static void drxd_release(struct dvb_frontend *fe)
2923{
2924 struct drxd_state *state = fe->demodulator_priv;
2925
2926 kfree(state);
2927}
2928
2929static struct dvb_frontend_ops drxd_ops = {
2930
2931 .info = {
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002932 .name = "Micronas DRXD DVB-T",
2933 .type = FE_OFDM,
2934 .frequency_min = 47125000,
2935 .frequency_max = 855250000,
2936 .frequency_stepsize = 166667,
2937 .frequency_tolerance = 0,
2938 .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 |
2939 FE_CAN_FEC_3_4 | FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 |
2940 FE_CAN_FEC_AUTO |
2941 FE_CAN_QAM_16 | FE_CAN_QAM_64 |
2942 FE_CAN_QAM_AUTO |
2943 FE_CAN_TRANSMISSION_MODE_AUTO |
2944 FE_CAN_GUARD_INTERVAL_AUTO |
2945 FE_CAN_HIERARCHY_AUTO | FE_CAN_RECOVER | FE_CAN_MUTE_TS},
Ralph Metzler126f1e62011-03-12 23:44:33 -05002946
2947 .release = drxd_release,
2948 .init = drxd_init,
2949 .sleep = drxd_sleep,
2950 .i2c_gate_ctrl = drxd_i2c_gate_ctrl,
2951
2952 .set_frontend = drxd_set_frontend,
2953 .get_frontend = drxd_get_frontend,
2954 .get_tune_settings = drxd_get_tune_settings,
2955
2956 .read_status = drxd_read_status,
2957 .read_ber = drxd_read_ber,
2958 .read_signal_strength = drxd_read_signal_strength,
2959 .read_snr = drxd_read_snr,
2960 .read_ucblocks = drxd_read_ucblocks,
2961};
2962
2963struct dvb_frontend *drxd_attach(const struct drxd_config *config,
2964 void *priv, struct i2c_adapter *i2c,
2965 struct device *dev)
2966{
2967 struct drxd_state *state = NULL;
2968
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002969 state = kmalloc(sizeof(struct drxd_state), GFP_KERNEL);
Ralph Metzler126f1e62011-03-12 23:44:33 -05002970 if (!state)
2971 return NULL;
2972 memset(state, 0, sizeof(*state));
2973
2974 memcpy(&state->ops, &drxd_ops, sizeof(struct dvb_frontend_ops));
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002975 state->dev = dev;
2976 state->config = *config;
2977 state->i2c = i2c;
2978 state->priv = priv;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002979
Mauro Carvalho Chehab834751d2011-03-25 12:46:05 -03002980 mutex_init(&state->mutex);
Ralph Metzler126f1e62011-03-12 23:44:33 -05002981
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002982 if (Read16(state, 0, 0, 0) < 0)
Ralph Metzler126f1e62011-03-12 23:44:33 -05002983 goto error;
2984
Ralph Metzler126f1e62011-03-12 23:44:33 -05002985 memcpy(&state->frontend.ops, &drxd_ops,
2986 sizeof(struct dvb_frontend_ops));
Devin Heitmueller6cacdd42011-03-24 13:44:01 -03002987 state->frontend.demodulator_priv = state;
Ralph Metzler126f1e62011-03-12 23:44:33 -05002988 ConfigureMPEGOutput(state, 0);
2989 return &state->frontend;
2990
2991error:
Mauro Carvalho Chehab9999daf2011-03-25 12:10:05 -03002992 printk(KERN_ERR "drxd: not found\n");
Ralph Metzler126f1e62011-03-12 23:44:33 -05002993 kfree(state);
2994 return NULL;
2995}
Mauro Carvalho Chehab9999daf2011-03-25 12:10:05 -03002996EXPORT_SYMBOL(drxd_attach);
Ralph Metzler126f1e62011-03-12 23:44:33 -05002997
2998MODULE_DESCRIPTION("DRXD driver");
2999MODULE_AUTHOR("Micronas");
3000MODULE_LICENSE("GPL");