blob: b975da099929e0b00afe791861b3dd556e3a8106 [file] [log] [blame]
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001/*
2 * drxk_hard: DRX-K DVB-C/T demodulator driver
3 *
4 * Copyright (C) 2010-2011 Digital Devices GmbH
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * version 2 only, as published by the Free Software Foundation.
9 *
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 * 02110-1301, USA
21 * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
22 */
23
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -030024#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
25
Ralph Metzler43dd07f2011-07-03 13:42:18 -030026#include <linux/kernel.h>
27#include <linux/module.h>
28#include <linux/moduleparam.h>
29#include <linux/init.h>
30#include <linux/delay.h>
31#include <linux/firmware.h>
32#include <linux/i2c.h>
Mauro Carvalho Chehab20bfe7a2012-06-29 14:43:32 -030033#include <linux/hardirq.h>
Ralph Metzler43dd07f2011-07-03 13:42:18 -030034#include <asm/div64.h>
35
36#include "dvb_frontend.h"
37#include "drxk.h"
38#include "drxk_hard.h"
Mauro Carvalho Chehabb5e9eb62013-04-28 11:47:43 -030039#include "dvb_math.h"
Ralph Metzler43dd07f2011-07-03 13:42:18 -030040
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -030041static int power_down_dvbt(struct drxk_state *state, bool set_power_mode);
42static int power_down_qam(struct drxk_state *state);
43static int set_dvbt_standard(struct drxk_state *state,
44 enum operation_mode o_mode);
45static int set_qam_standard(struct drxk_state *state,
46 enum operation_mode o_mode);
47static int set_qam(struct drxk_state *state, u16 intermediate_freqk_hz,
48 s32 tuner_freq_offset);
49static int set_dvbt_standard(struct drxk_state *state,
50 enum operation_mode o_mode);
51static int dvbt_start(struct drxk_state *state);
52static int set_dvbt(struct drxk_state *state, u16 intermediate_freqk_hz,
53 s32 tuner_freq_offset);
54static int get_qam_lock_status(struct drxk_state *state, u32 *p_lock_status);
55static int get_dvbt_lock_status(struct drxk_state *state, u32 *p_lock_status);
56static int switch_antenna_to_qam(struct drxk_state *state);
57static int switch_antenna_to_dvbt(struct drxk_state *state);
Ralph Metzler43dd07f2011-07-03 13:42:18 -030058
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -030059static bool is_dvbt(struct drxk_state *state)
Ralph Metzler43dd07f2011-07-03 13:42:18 -030060{
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -030061 return state->m_operation_mode == OM_DVBT;
Ralph Metzler43dd07f2011-07-03 13:42:18 -030062}
63
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -030064static bool is_qam(struct drxk_state *state)
Ralph Metzler43dd07f2011-07-03 13:42:18 -030065{
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -030066 return state->m_operation_mode == OM_QAM_ITU_A ||
67 state->m_operation_mode == OM_QAM_ITU_B ||
68 state->m_operation_mode == OM_QAM_ITU_C;
Ralph Metzler43dd07f2011-07-03 13:42:18 -030069}
70
Ralph Metzler43dd07f2011-07-03 13:42:18 -030071#define NOA1ROM 0
72
Ralph Metzler43dd07f2011-07-03 13:42:18 -030073#define DRXDAP_FASI_SHORT_FORMAT(addr) (((addr) & 0xFC30FF80) == 0)
74#define DRXDAP_FASI_LONG_FORMAT(addr) (((addr) & 0xFC30FF80) != 0)
75
76#define DEFAULT_MER_83 165
77#define DEFAULT_MER_93 250
78
79#ifndef DRXK_MPEG_SERIAL_OUTPUT_PIN_DRIVE_STRENGTH
80#define DRXK_MPEG_SERIAL_OUTPUT_PIN_DRIVE_STRENGTH (0x02)
81#endif
82
83#ifndef DRXK_MPEG_PARALLEL_OUTPUT_PIN_DRIVE_STRENGTH
84#define DRXK_MPEG_PARALLEL_OUTPUT_PIN_DRIVE_STRENGTH (0x03)
85#endif
86
Ralph Metzler43dd07f2011-07-03 13:42:18 -030087#define DEFAULT_DRXK_MPEG_LOCK_TIMEOUT 700
88#define DEFAULT_DRXK_DEMOD_LOCK_TIMEOUT 500
89
90#ifndef DRXK_KI_RAGC_ATV
91#define DRXK_KI_RAGC_ATV 4
92#endif
93#ifndef DRXK_KI_IAGC_ATV
94#define DRXK_KI_IAGC_ATV 6
95#endif
96#ifndef DRXK_KI_DAGC_ATV
97#define DRXK_KI_DAGC_ATV 7
98#endif
99
100#ifndef DRXK_KI_RAGC_QAM
101#define DRXK_KI_RAGC_QAM 3
102#endif
103#ifndef DRXK_KI_IAGC_QAM
104#define DRXK_KI_IAGC_QAM 4
105#endif
106#ifndef DRXK_KI_DAGC_QAM
107#define DRXK_KI_DAGC_QAM 7
108#endif
109#ifndef DRXK_KI_RAGC_DVBT
110#define DRXK_KI_RAGC_DVBT (IsA1WithPatchCode(state) ? 3 : 2)
111#endif
112#ifndef DRXK_KI_IAGC_DVBT
113#define DRXK_KI_IAGC_DVBT (IsA1WithPatchCode(state) ? 4 : 2)
114#endif
115#ifndef DRXK_KI_DAGC_DVBT
116#define DRXK_KI_DAGC_DVBT (IsA1WithPatchCode(state) ? 10 : 7)
117#endif
118
119#ifndef DRXK_AGC_DAC_OFFSET
120#define DRXK_AGC_DAC_OFFSET (0x800)
121#endif
122
123#ifndef DRXK_BANDWIDTH_8MHZ_IN_HZ
124#define DRXK_BANDWIDTH_8MHZ_IN_HZ (0x8B8249L)
125#endif
126
127#ifndef DRXK_BANDWIDTH_7MHZ_IN_HZ
128#define DRXK_BANDWIDTH_7MHZ_IN_HZ (0x7A1200L)
129#endif
130
131#ifndef DRXK_BANDWIDTH_6MHZ_IN_HZ
132#define DRXK_BANDWIDTH_6MHZ_IN_HZ (0x68A1B6L)
133#endif
134
135#ifndef DRXK_QAM_SYMBOLRATE_MAX
136#define DRXK_QAM_SYMBOLRATE_MAX (7233000)
137#endif
138
139#define DRXK_BL_ROM_OFFSET_TAPS_DVBT 56
140#define DRXK_BL_ROM_OFFSET_TAPS_ITU_A 64
141#define DRXK_BL_ROM_OFFSET_TAPS_ITU_C 0x5FE0
142#define DRXK_BL_ROM_OFFSET_TAPS_BG 24
143#define DRXK_BL_ROM_OFFSET_TAPS_DKILLP 32
144#define DRXK_BL_ROM_OFFSET_TAPS_NTSC 40
145#define DRXK_BL_ROM_OFFSET_TAPS_FM 48
146#define DRXK_BL_ROM_OFFSET_UCODE 0
147
148#define DRXK_BLC_TIMEOUT 100
149
150#define DRXK_BLCC_NR_ELEMENTS_TAPS 2
151#define DRXK_BLCC_NR_ELEMENTS_UCODE 6
152
153#define DRXK_BLDC_NR_ELEMENTS_TAPS 28
154
155#ifndef DRXK_OFDM_NE_NOTCH_WIDTH
156#define DRXK_OFDM_NE_NOTCH_WIDTH (4)
157#endif
158
159#define DRXK_QAM_SL_SIG_POWER_QAM16 (40960)
160#define DRXK_QAM_SL_SIG_POWER_QAM32 (20480)
161#define DRXK_QAM_SL_SIG_POWER_QAM64 (43008)
162#define DRXK_QAM_SL_SIG_POWER_QAM128 (20992)
163#define DRXK_QAM_SL_SIG_POWER_QAM256 (43520)
164
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -0300165static unsigned int debug;
166module_param(debug, int, 0644);
167MODULE_PARM_DESC(debug, "enable debug messages");
168
Mauro Carvalho Chehab52ee29f2014-09-28 23:23:19 -0300169#define dprintk(level, fmt, arg...) do { \
170if (debug >= level) \
171 printk(KERN_DEBUG KBUILD_MODNAME ": %s " fmt, __func__, ##arg); \
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -0300172} while (0)
173
174
Mauro Carvalho Chehabb01fbc12011-07-03 17:18:57 -0300175static inline u32 MulDiv32(u32 a, u32 b, u32 c)
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300176{
177 u64 tmp64;
178
Oliver Endrissebc7de22011-07-03 13:49:44 -0300179 tmp64 = (u64) a * (u64) b;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300180 do_div(tmp64, c);
181
182 return (u32) tmp64;
183}
184
Mauro Carvalho Chehabff38c212012-10-25 13:40:04 -0200185static inline u32 Frac28a(u32 a, u32 c)
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300186{
187 int i = 0;
188 u32 Q1 = 0;
189 u32 R0 = 0;
190
Oliver Endrissebc7de22011-07-03 13:49:44 -0300191 R0 = (a % c) << 4; /* 32-28 == 4 shifts possible at max */
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -0300192 Q1 = a / c; /*
193 * integer part, only the 4 least significant
194 * bits will be visible in the result
195 */
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300196
197 /* division using radix 16, 7 nibbles in the result */
198 for (i = 0; i < 7; i++) {
199 Q1 = (Q1 << 4) | (R0 / c);
200 R0 = (R0 % c) << 4;
201 }
202 /* rounding */
203 if ((R0 >> 3) >= c)
204 Q1++;
205
206 return Q1;
207}
208
Mauro Carvalho Chehabb5e9eb62013-04-28 11:47:43 -0300209static inline u32 log10times100(u32 value)
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300210{
Mauro Carvalho Chehabb5e9eb62013-04-28 11:47:43 -0300211 return (100L * intlog10(value)) >> 24;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300212}
213
214/****************************************************************************/
215/* I2C **********************************************************************/
216/****************************************************************************/
217
Mauro Carvalho Chehab20bfe7a2012-06-29 14:43:32 -0300218static int drxk_i2c_lock(struct drxk_state *state)
219{
220 i2c_lock_adapter(state->i2c);
221 state->drxk_i2c_exclusive_lock = true;
222
223 return 0;
224}
225
226static void drxk_i2c_unlock(struct drxk_state *state)
227{
228 if (!state->drxk_i2c_exclusive_lock)
229 return;
230
231 i2c_unlock_adapter(state->i2c);
232 state->drxk_i2c_exclusive_lock = false;
233}
234
Mauro Carvalho Chehab2a5f6722012-06-29 14:24:18 -0300235static int drxk_i2c_transfer(struct drxk_state *state, struct i2c_msg *msgs,
236 unsigned len)
237{
Mauro Carvalho Chehab20bfe7a2012-06-29 14:43:32 -0300238 if (state->drxk_i2c_exclusive_lock)
239 return __i2c_transfer(state->i2c, msgs, len);
240 else
241 return i2c_transfer(state->i2c, msgs, len);
Mauro Carvalho Chehab2a5f6722012-06-29 14:24:18 -0300242}
243
244static int i2c_read1(struct drxk_state *state, u8 adr, u8 *val)
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300245{
Oliver Endrissebc7de22011-07-03 13:49:44 -0300246 struct i2c_msg msgs[1] = { {.addr = adr, .flags = I2C_M_RD,
247 .buf = val, .len = 1}
248 };
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300249
Mauro Carvalho Chehab2a5f6722012-06-29 14:24:18 -0300250 return drxk_i2c_transfer(state, msgs, 1);
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300251}
252
Mauro Carvalho Chehab2a5f6722012-06-29 14:24:18 -0300253static int i2c_write(struct drxk_state *state, u8 adr, u8 *data, int len)
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300254{
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300255 int status;
Oliver Endrissebc7de22011-07-03 13:49:44 -0300256 struct i2c_msg msg = {
257 .addr = adr, .flags = 0, .buf = data, .len = len };
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300258
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -0300259 dprintk(3, ":");
260 if (debug > 2) {
261 int i;
262 for (i = 0; i < len; i++)
Mauro Carvalho Chehab0fb220f2013-04-28 11:47:46 -0300263 pr_cont(" %02x", data[i]);
264 pr_cont("\n");
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -0300265 }
Mauro Carvalho Chehab2a5f6722012-06-29 14:24:18 -0300266 status = drxk_i2c_transfer(state, &msg, 1);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300267 if (status >= 0 && status != 1)
268 status = -EIO;
269
270 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -0300271 pr_err("i2c write error at addr 0x%02x\n", adr);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300272
273 return status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300274}
275
Mauro Carvalho Chehab2a5f6722012-06-29 14:24:18 -0300276static int i2c_read(struct drxk_state *state,
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300277 u8 adr, u8 *msg, int len, u8 *answ, int alen)
278{
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300279 int status;
Mauro Carvalho Chehabe4f4f872011-07-09 17:35:26 -0300280 struct i2c_msg msgs[2] = {
281 {.addr = adr, .flags = 0,
Oliver Endrissebc7de22011-07-03 13:49:44 -0300282 .buf = msg, .len = len},
Mauro Carvalho Chehabe4f4f872011-07-09 17:35:26 -0300283 {.addr = adr, .flags = I2C_M_RD,
284 .buf = answ, .len = alen}
Oliver Endrissebc7de22011-07-03 13:49:44 -0300285 };
Mauro Carvalho Chehabf07a0bc2011-07-21 22:30:27 -0300286
Mauro Carvalho Chehab2a5f6722012-06-29 14:24:18 -0300287 status = drxk_i2c_transfer(state, msgs, 2);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300288 if (status != 2) {
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -0300289 if (debug > 2)
Mauro Carvalho Chehab0fb220f2013-04-28 11:47:46 -0300290 pr_cont(": ERROR!\n");
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300291 if (status >= 0)
292 status = -EIO;
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -0300293
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -0300294 pr_err("i2c read error at addr 0x%02x\n", adr);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300295 return status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300296 }
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -0300297 if (debug > 2) {
298 int i;
Mauro Carvalho Chehab0d3e6fe2011-07-22 12:34:41 -0300299 dprintk(2, ": read from");
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -0300300 for (i = 0; i < len; i++)
Mauro Carvalho Chehab0fb220f2013-04-28 11:47:46 -0300301 pr_cont(" %02x", msg[i]);
302 pr_cont(", value = ");
Mauro Carvalho Chehabf07a0bc2011-07-21 22:30:27 -0300303 for (i = 0; i < alen; i++)
Mauro Carvalho Chehab0fb220f2013-04-28 11:47:46 -0300304 pr_cont(" %02x", answ[i]);
305 pr_cont("\n");
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -0300306 }
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300307 return 0;
308}
309
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -0300310static int read16_flags(struct drxk_state *state, u32 reg, u16 *data, u8 flags)
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300311{
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300312 int status;
Oliver Endrissebc7de22011-07-03 13:49:44 -0300313 u8 adr = state->demod_address, mm1[4], mm2[2], len;
Mauro Carvalho Chehabe076c922011-07-09 13:06:12 -0300314
315 if (state->single_master)
316 flags |= 0xC0;
317
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300318 if (DRXDAP_FASI_LONG_FORMAT(reg) || (flags != 0)) {
319 mm1[0] = (((reg << 1) & 0xFF) | 0x01);
320 mm1[1] = ((reg >> 16) & 0xFF);
321 mm1[2] = ((reg >> 24) & 0xFF) | flags;
322 mm1[3] = ((reg >> 7) & 0xFF);
323 len = 4;
324 } else {
325 mm1[0] = ((reg << 1) & 0xFF);
326 mm1[1] = (((reg >> 16) & 0x0F) | ((reg >> 18) & 0xF0));
327 len = 2;
328 }
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -0300329 dprintk(2, "(0x%08x, 0x%02x)\n", reg, flags);
Mauro Carvalho Chehab2a5f6722012-06-29 14:24:18 -0300330 status = i2c_read(state, adr, mm1, len, mm2, 2);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300331 if (status < 0)
332 return status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300333 if (data)
334 *data = mm2[0] | (mm2[1] << 8);
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -0300335
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300336 return 0;
337}
338
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -0300339static int read16(struct drxk_state *state, u32 reg, u16 *data)
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300340{
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -0300341 return read16_flags(state, reg, data, 0);
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300342}
343
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -0300344static int read32_flags(struct drxk_state *state, u32 reg, u32 *data, u8 flags)
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300345{
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300346 int status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300347 u8 adr = state->demod_address, mm1[4], mm2[4], len;
Mauro Carvalho Chehabe076c922011-07-09 13:06:12 -0300348
349 if (state->single_master)
350 flags |= 0xC0;
351
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300352 if (DRXDAP_FASI_LONG_FORMAT(reg) || (flags != 0)) {
353 mm1[0] = (((reg << 1) & 0xFF) | 0x01);
354 mm1[1] = ((reg >> 16) & 0xFF);
355 mm1[2] = ((reg >> 24) & 0xFF) | flags;
356 mm1[3] = ((reg >> 7) & 0xFF);
357 len = 4;
358 } else {
359 mm1[0] = ((reg << 1) & 0xFF);
360 mm1[1] = (((reg >> 16) & 0x0F) | ((reg >> 18) & 0xF0));
361 len = 2;
362 }
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -0300363 dprintk(2, "(0x%08x, 0x%02x)\n", reg, flags);
Mauro Carvalho Chehab2a5f6722012-06-29 14:24:18 -0300364 status = i2c_read(state, adr, mm1, len, mm2, 4);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300365 if (status < 0)
366 return status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300367 if (data)
368 *data = mm2[0] | (mm2[1] << 8) |
Oliver Endrissebc7de22011-07-03 13:49:44 -0300369 (mm2[2] << 16) | (mm2[3] << 24);
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -0300370
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300371 return 0;
372}
373
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -0300374static int read32(struct drxk_state *state, u32 reg, u32 *data)
375{
376 return read32_flags(state, reg, data, 0);
377}
378
379static int write16_flags(struct drxk_state *state, u32 reg, u16 data, u8 flags)
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300380{
381 u8 adr = state->demod_address, mm[6], len;
Mauro Carvalho Chehabe076c922011-07-09 13:06:12 -0300382
383 if (state->single_master)
384 flags |= 0xC0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300385 if (DRXDAP_FASI_LONG_FORMAT(reg) || (flags != 0)) {
386 mm[0] = (((reg << 1) & 0xFF) | 0x01);
387 mm[1] = ((reg >> 16) & 0xFF);
388 mm[2] = ((reg >> 24) & 0xFF) | flags;
389 mm[3] = ((reg >> 7) & 0xFF);
390 len = 4;
391 } else {
392 mm[0] = ((reg << 1) & 0xFF);
393 mm[1] = (((reg >> 16) & 0x0F) | ((reg >> 18) & 0xF0));
394 len = 2;
395 }
396 mm[len] = data & 0xff;
Oliver Endrissebc7de22011-07-03 13:49:44 -0300397 mm[len + 1] = (data >> 8) & 0xff;
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -0300398
399 dprintk(2, "(0x%08x, 0x%04x, 0x%02x)\n", reg, data, flags);
Mauro Carvalho Chehab2a5f6722012-06-29 14:24:18 -0300400 return i2c_write(state, adr, mm, len + 2);
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300401}
402
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -0300403static int write16(struct drxk_state *state, u32 reg, u16 data)
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300404{
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -0300405 return write16_flags(state, reg, data, 0);
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300406}
407
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -0300408static int write32_flags(struct drxk_state *state, u32 reg, u32 data, u8 flags)
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300409{
410 u8 adr = state->demod_address, mm[8], len;
Mauro Carvalho Chehabe076c922011-07-09 13:06:12 -0300411
412 if (state->single_master)
413 flags |= 0xC0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300414 if (DRXDAP_FASI_LONG_FORMAT(reg) || (flags != 0)) {
415 mm[0] = (((reg << 1) & 0xFF) | 0x01);
416 mm[1] = ((reg >> 16) & 0xFF);
417 mm[2] = ((reg >> 24) & 0xFF) | flags;
418 mm[3] = ((reg >> 7) & 0xFF);
419 len = 4;
420 } else {
421 mm[0] = ((reg << 1) & 0xFF);
422 mm[1] = (((reg >> 16) & 0x0F) | ((reg >> 18) & 0xF0));
423 len = 2;
424 }
425 mm[len] = data & 0xff;
Oliver Endrissebc7de22011-07-03 13:49:44 -0300426 mm[len + 1] = (data >> 8) & 0xff;
427 mm[len + 2] = (data >> 16) & 0xff;
428 mm[len + 3] = (data >> 24) & 0xff;
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -0300429 dprintk(2, "(0x%08x, 0x%08x, 0x%02x)\n", reg, data, flags);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300430
Mauro Carvalho Chehab2a5f6722012-06-29 14:24:18 -0300431 return i2c_write(state, adr, mm, len + 4);
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300432}
433
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -0300434static int write32(struct drxk_state *state, u32 reg, u32 data)
435{
436 return write32_flags(state, reg, data, 0);
437}
438
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300439static int write_block(struct drxk_state *state, u32 address,
440 const int block_size, const u8 p_block[])
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300441{
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300442 int status = 0, blk_size = block_size;
443 u8 flags = 0;
Mauro Carvalho Chehabe076c922011-07-09 13:06:12 -0300444
445 if (state->single_master)
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300446 flags |= 0xC0;
Mauro Carvalho Chehabe076c922011-07-09 13:06:12 -0300447
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300448 while (blk_size > 0) {
449 int chunk = blk_size > state->m_chunk_size ?
450 state->m_chunk_size : blk_size;
451 u8 *adr_buf = &state->chunk[0];
452 u32 adr_length = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300453
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300454 if (DRXDAP_FASI_LONG_FORMAT(address) || (flags != 0)) {
455 adr_buf[0] = (((address << 1) & 0xFF) | 0x01);
456 adr_buf[1] = ((address >> 16) & 0xFF);
457 adr_buf[2] = ((address >> 24) & 0xFF);
458 adr_buf[3] = ((address >> 7) & 0xFF);
459 adr_buf[2] |= flags;
460 adr_length = 4;
461 if (chunk == state->m_chunk_size)
462 chunk -= 2;
Oliver Endrissebc7de22011-07-03 13:49:44 -0300463 } else {
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300464 adr_buf[0] = ((address << 1) & 0xFF);
465 adr_buf[1] = (((address >> 16) & 0x0F) |
466 ((address >> 18) & 0xF0));
467 adr_length = 2;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300468 }
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300469 memcpy(&state->chunk[adr_length], p_block, chunk);
470 dprintk(2, "(0x%08x, 0x%02x)\n", address, flags);
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -0300471 if (debug > 1) {
472 int i;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300473 if (p_block)
474 for (i = 0; i < chunk; i++)
Mauro Carvalho Chehab0fb220f2013-04-28 11:47:46 -0300475 pr_cont(" %02x", p_block[i]);
476 pr_cont("\n");
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -0300477 }
Mauro Carvalho Chehab2a5f6722012-06-29 14:24:18 -0300478 status = i2c_write(state, state->demod_address,
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300479 &state->chunk[0], chunk + adr_length);
Oliver Endrissebc7de22011-07-03 13:49:44 -0300480 if (status < 0) {
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -0300481 pr_err("%s: i2c write error at addr 0x%02x\n",
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300482 __func__, address);
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300483 break;
484 }
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300485 p_block += chunk;
486 address += (chunk >> 1);
487 blk_size -= chunk;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300488 }
Oliver Endrissebc7de22011-07-03 13:49:44 -0300489 return status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300490}
491
492#ifndef DRXK_MAX_RETRIES_POWERUP
493#define DRXK_MAX_RETRIES_POWERUP 20
494#endif
495
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300496static int power_up_device(struct drxk_state *state)
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300497{
498 int status;
499 u8 data = 0;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300500 u16 retry_count = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300501
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -0300502 dprintk(1, "\n");
503
Mauro Carvalho Chehab2a5f6722012-06-29 14:24:18 -0300504 status = i2c_read1(state, state->demod_address, &data);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300505 if (status < 0) {
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300506 do {
507 data = 0;
Mauro Carvalho Chehab2a5f6722012-06-29 14:24:18 -0300508 status = i2c_write(state, state->demod_address,
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300509 &data, 1);
Mauro Carvalho Chehabb72852b2013-04-28 11:47:47 -0300510 usleep_range(10000, 11000);
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300511 retry_count++;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300512 if (status < 0)
513 continue;
Mauro Carvalho Chehab2a5f6722012-06-29 14:24:18 -0300514 status = i2c_read1(state, state->demod_address,
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300515 &data);
516 } while (status < 0 &&
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300517 (retry_count < DRXK_MAX_RETRIES_POWERUP));
518 if (status < 0 && retry_count >= DRXK_MAX_RETRIES_POWERUP)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300519 goto error;
520 }
521
522 /* Make sure all clk domains are active */
523 status = write16(state, SIO_CC_PWD_MODE__A, SIO_CC_PWD_MODE_LEVEL_NONE);
524 if (status < 0)
525 goto error;
526 status = write16(state, SIO_CC_UPDATE__A, SIO_CC_UPDATE_KEY);
527 if (status < 0)
528 goto error;
529 /* Enable pll lock tests */
530 status = write16(state, SIO_CC_PLL_LOCK__A, 1);
531 if (status < 0)
532 goto error;
533
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300534 state->m_current_power_mode = DRX_POWER_UP;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300535
536error:
537 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -0300538 pr_err("Error %d on %s\n", status, __func__);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300539
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300540 return status;
541}
542
543
544static int init_state(struct drxk_state *state)
545{
Mauro Carvalho Chehab147e1102011-07-10 08:24:26 -0300546 /*
Mauro Carvalho Chehab5a13e402015-05-08 08:59:16 -0300547 * FIXME: most (all?) of the values below should be moved into
Mauro Carvalho Chehab147e1102011-07-10 08:24:26 -0300548 * struct drxk_config, as they are probably board-specific
549 */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300550 u32 ul_vsb_if_agc_mode = DRXK_AGC_CTRL_AUTO;
551 u32 ul_vsb_if_agc_output_level = 0;
552 u32 ul_vsb_if_agc_min_level = 0;
553 u32 ul_vsb_if_agc_max_level = 0x7FFF;
554 u32 ul_vsb_if_agc_speed = 3;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300555
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300556 u32 ul_vsb_rf_agc_mode = DRXK_AGC_CTRL_AUTO;
557 u32 ul_vsb_rf_agc_output_level = 0;
558 u32 ul_vsb_rf_agc_min_level = 0;
559 u32 ul_vsb_rf_agc_max_level = 0x7FFF;
560 u32 ul_vsb_rf_agc_speed = 3;
561 u32 ul_vsb_rf_agc_top = 9500;
562 u32 ul_vsb_rf_agc_cut_off_current = 4000;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300563
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300564 u32 ul_atv_if_agc_mode = DRXK_AGC_CTRL_AUTO;
565 u32 ul_atv_if_agc_output_level = 0;
566 u32 ul_atv_if_agc_min_level = 0;
567 u32 ul_atv_if_agc_max_level = 0;
568 u32 ul_atv_if_agc_speed = 3;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300569
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300570 u32 ul_atv_rf_agc_mode = DRXK_AGC_CTRL_OFF;
571 u32 ul_atv_rf_agc_output_level = 0;
572 u32 ul_atv_rf_agc_min_level = 0;
573 u32 ul_atv_rf_agc_max_level = 0;
574 u32 ul_atv_rf_agc_top = 9500;
575 u32 ul_atv_rf_agc_cut_off_current = 4000;
576 u32 ul_atv_rf_agc_speed = 3;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300577
578 u32 ulQual83 = DEFAULT_MER_83;
579 u32 ulQual93 = DEFAULT_MER_93;
580
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300581 u32 ul_mpeg_lock_time_out = DEFAULT_DRXK_MPEG_LOCK_TIMEOUT;
582 u32 ul_demod_lock_time_out = DEFAULT_DRXK_DEMOD_LOCK_TIMEOUT;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300583
584 /* io_pad_cfg register (8 bit reg.) MSB bit is 1 (default value) */
585 /* io_pad_cfg_mode output mode is drive always */
586 /* io_pad_cfg_drive is set to power 2 (23 mA) */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300587 u32 ul_gpio_cfg = 0x0113;
588 u32 ul_invert_ts_clock = 0;
589 u32 ul_ts_data_strength = DRXK_MPEG_SERIAL_OUTPUT_PIN_DRIVE_STRENGTH;
590 u32 ul_dvbt_bitrate = 50000000;
591 u32 ul_dvbc_bitrate = DRXK_QAM_SYMBOLRATE_MAX * 8;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300592
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300593 u32 ul_insert_rs_byte = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300594
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300595 u32 ul_rf_mirror = 1;
596 u32 ul_power_down = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300597
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -0300598 dprintk(1, "\n");
599
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300600 state->m_has_lna = false;
601 state->m_has_dvbt = false;
602 state->m_has_dvbc = false;
603 state->m_has_atv = false;
604 state->m_has_oob = false;
605 state->m_has_audio = false;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300606
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300607 if (!state->m_chunk_size)
608 state->m_chunk_size = 124;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300609
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300610 state->m_osc_clock_freq = 0;
611 state->m_smart_ant_inverted = false;
612 state->m_b_p_down_open_bridge = false;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300613
614 /* real system clock frequency in kHz */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300615 state->m_sys_clock_freq = 151875;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300616 /* Timing div, 250ns/Psys */
617 /* Timing div, = (delay (nano seconds) * sysclk (kHz))/ 1000 */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300618 state->m_hi_cfg_timing_div = ((state->m_sys_clock_freq / 1000) *
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300619 HI_I2C_DELAY) / 1000;
620 /* Clipping */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300621 if (state->m_hi_cfg_timing_div > SIO_HI_RA_RAM_PAR_2_CFG_DIV__M)
622 state->m_hi_cfg_timing_div = SIO_HI_RA_RAM_PAR_2_CFG_DIV__M;
623 state->m_hi_cfg_wake_up_key = (state->demod_address << 1);
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300624 /* port/bridge/power down ctrl */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300625 state->m_hi_cfg_ctrl = SIO_HI_RA_RAM_PAR_5_CFG_SLV0_SLAVE;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300626
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300627 state->m_b_power_down = (ul_power_down != 0);
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300628
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300629 state->m_drxk_a3_patch_code = false;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300630
631 /* Init AGC and PGA parameters */
632 /* VSB IF */
Mauro Carvalho Chehab949dd082013-04-28 11:47:50 -0300633 state->m_vsb_if_agc_cfg.ctrl_mode = ul_vsb_if_agc_mode;
634 state->m_vsb_if_agc_cfg.output_level = ul_vsb_if_agc_output_level;
635 state->m_vsb_if_agc_cfg.min_output_level = ul_vsb_if_agc_min_level;
636 state->m_vsb_if_agc_cfg.max_output_level = ul_vsb_if_agc_max_level;
637 state->m_vsb_if_agc_cfg.speed = ul_vsb_if_agc_speed;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300638 state->m_vsb_pga_cfg = 140;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300639
640 /* VSB RF */
Mauro Carvalho Chehab949dd082013-04-28 11:47:50 -0300641 state->m_vsb_rf_agc_cfg.ctrl_mode = ul_vsb_rf_agc_mode;
642 state->m_vsb_rf_agc_cfg.output_level = ul_vsb_rf_agc_output_level;
643 state->m_vsb_rf_agc_cfg.min_output_level = ul_vsb_rf_agc_min_level;
644 state->m_vsb_rf_agc_cfg.max_output_level = ul_vsb_rf_agc_max_level;
645 state->m_vsb_rf_agc_cfg.speed = ul_vsb_rf_agc_speed;
646 state->m_vsb_rf_agc_cfg.top = ul_vsb_rf_agc_top;
647 state->m_vsb_rf_agc_cfg.cut_off_current = ul_vsb_rf_agc_cut_off_current;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300648 state->m_vsb_pre_saw_cfg.reference = 0x07;
649 state->m_vsb_pre_saw_cfg.use_pre_saw = true;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300650
651 state->m_Quality83percent = DEFAULT_MER_83;
652 state->m_Quality93percent = DEFAULT_MER_93;
653 if (ulQual93 <= 500 && ulQual83 < ulQual93) {
654 state->m_Quality83percent = ulQual83;
655 state->m_Quality93percent = ulQual93;
656 }
657
658 /* ATV IF */
Mauro Carvalho Chehab949dd082013-04-28 11:47:50 -0300659 state->m_atv_if_agc_cfg.ctrl_mode = ul_atv_if_agc_mode;
660 state->m_atv_if_agc_cfg.output_level = ul_atv_if_agc_output_level;
661 state->m_atv_if_agc_cfg.min_output_level = ul_atv_if_agc_min_level;
662 state->m_atv_if_agc_cfg.max_output_level = ul_atv_if_agc_max_level;
663 state->m_atv_if_agc_cfg.speed = ul_atv_if_agc_speed;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300664
665 /* ATV RF */
Mauro Carvalho Chehab949dd082013-04-28 11:47:50 -0300666 state->m_atv_rf_agc_cfg.ctrl_mode = ul_atv_rf_agc_mode;
667 state->m_atv_rf_agc_cfg.output_level = ul_atv_rf_agc_output_level;
668 state->m_atv_rf_agc_cfg.min_output_level = ul_atv_rf_agc_min_level;
669 state->m_atv_rf_agc_cfg.max_output_level = ul_atv_rf_agc_max_level;
670 state->m_atv_rf_agc_cfg.speed = ul_atv_rf_agc_speed;
671 state->m_atv_rf_agc_cfg.top = ul_atv_rf_agc_top;
672 state->m_atv_rf_agc_cfg.cut_off_current = ul_atv_rf_agc_cut_off_current;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300673 state->m_atv_pre_saw_cfg.reference = 0x04;
674 state->m_atv_pre_saw_cfg.use_pre_saw = true;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300675
676
677 /* DVBT RF */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300678 state->m_dvbt_rf_agc_cfg.ctrl_mode = DRXK_AGC_CTRL_OFF;
679 state->m_dvbt_rf_agc_cfg.output_level = 0;
680 state->m_dvbt_rf_agc_cfg.min_output_level = 0;
681 state->m_dvbt_rf_agc_cfg.max_output_level = 0xFFFF;
682 state->m_dvbt_rf_agc_cfg.top = 0x2100;
683 state->m_dvbt_rf_agc_cfg.cut_off_current = 4000;
684 state->m_dvbt_rf_agc_cfg.speed = 1;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300685
686
687 /* DVBT IF */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300688 state->m_dvbt_if_agc_cfg.ctrl_mode = DRXK_AGC_CTRL_AUTO;
689 state->m_dvbt_if_agc_cfg.output_level = 0;
690 state->m_dvbt_if_agc_cfg.min_output_level = 0;
691 state->m_dvbt_if_agc_cfg.max_output_level = 9000;
692 state->m_dvbt_if_agc_cfg.top = 13424;
693 state->m_dvbt_if_agc_cfg.cut_off_current = 0;
694 state->m_dvbt_if_agc_cfg.speed = 3;
695 state->m_dvbt_if_agc_cfg.fast_clip_ctrl_delay = 30;
696 state->m_dvbt_if_agc_cfg.ingain_tgt_max = 30000;
Oliver Endrissebc7de22011-07-03 13:49:44 -0300697 /* state->m_dvbtPgaCfg = 140; */
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300698
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300699 state->m_dvbt_pre_saw_cfg.reference = 4;
700 state->m_dvbt_pre_saw_cfg.use_pre_saw = false;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300701
702 /* QAM RF */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300703 state->m_qam_rf_agc_cfg.ctrl_mode = DRXK_AGC_CTRL_OFF;
704 state->m_qam_rf_agc_cfg.output_level = 0;
705 state->m_qam_rf_agc_cfg.min_output_level = 6023;
706 state->m_qam_rf_agc_cfg.max_output_level = 27000;
707 state->m_qam_rf_agc_cfg.top = 0x2380;
708 state->m_qam_rf_agc_cfg.cut_off_current = 4000;
709 state->m_qam_rf_agc_cfg.speed = 3;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300710
711 /* QAM IF */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300712 state->m_qam_if_agc_cfg.ctrl_mode = DRXK_AGC_CTRL_AUTO;
713 state->m_qam_if_agc_cfg.output_level = 0;
714 state->m_qam_if_agc_cfg.min_output_level = 0;
715 state->m_qam_if_agc_cfg.max_output_level = 9000;
716 state->m_qam_if_agc_cfg.top = 0x0511;
717 state->m_qam_if_agc_cfg.cut_off_current = 0;
718 state->m_qam_if_agc_cfg.speed = 3;
719 state->m_qam_if_agc_cfg.ingain_tgt_max = 5119;
720 state->m_qam_if_agc_cfg.fast_clip_ctrl_delay = 50;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300721
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300722 state->m_qam_pga_cfg = 140;
723 state->m_qam_pre_saw_cfg.reference = 4;
724 state->m_qam_pre_saw_cfg.use_pre_saw = false;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300725
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300726 state->m_operation_mode = OM_NONE;
727 state->m_drxk_state = DRXK_UNINITIALIZED;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300728
729 /* MPEG output configuration */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300730 state->m_enable_mpeg_output = true; /* If TRUE; enable MPEG ouput */
731 state->m_insert_rs_byte = false; /* If TRUE; insert RS byte */
732 state->m_invert_data = false; /* If TRUE; invert DATA signals */
733 state->m_invert_err = false; /* If TRUE; invert ERR signal */
734 state->m_invert_str = false; /* If TRUE; invert STR signals */
735 state->m_invert_val = false; /* If TRUE; invert VAL signals */
736 state->m_invert_clk = (ul_invert_ts_clock != 0); /* If TRUE; invert CLK signals */
Mauro Carvalho Chehab67f04612012-01-20 18:30:58 -0300737
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300738 /* If TRUE; static MPEG clockrate will be used;
739 otherwise clockrate will adapt to the bitrate of the TS */
740
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300741 state->m_dvbt_bitrate = ul_dvbt_bitrate;
742 state->m_dvbc_bitrate = ul_dvbc_bitrate;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300743
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300744 state->m_ts_data_strength = (ul_ts_data_strength & 0x07);
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300745
746 /* Maximum bitrate in b/s in case static clockrate is selected */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300747 state->m_mpeg_ts_static_bitrate = 19392658;
748 state->m_disable_te_ihandling = false;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300749
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300750 if (ul_insert_rs_byte)
751 state->m_insert_rs_byte = true;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300752
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300753 state->m_mpeg_lock_time_out = DEFAULT_DRXK_MPEG_LOCK_TIMEOUT;
754 if (ul_mpeg_lock_time_out < 10000)
755 state->m_mpeg_lock_time_out = ul_mpeg_lock_time_out;
756 state->m_demod_lock_time_out = DEFAULT_DRXK_DEMOD_LOCK_TIMEOUT;
757 if (ul_demod_lock_time_out < 10000)
758 state->m_demod_lock_time_out = ul_demod_lock_time_out;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300759
Oliver Endrissebc7de22011-07-03 13:49:44 -0300760 /* QAM defaults */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300761 state->m_constellation = DRX_CONSTELLATION_AUTO;
762 state->m_qam_interleave_mode = DRXK_QAM_I12_J17;
763 state->m_fec_rs_plen = 204 * 8; /* fecRsPlen annex A */
764 state->m_fec_rs_prescale = 1;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300765
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300766 state->m_sqi_speed = DRXK_DVBT_SQI_SPEED_MEDIUM;
767 state->m_agcfast_clip_ctrl_delay = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300768
Mauro Carvalho Chehab949dd082013-04-28 11:47:50 -0300769 state->m_gpio_cfg = ul_gpio_cfg;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300770
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300771 state->m_b_power_down = false;
772 state->m_current_power_mode = DRX_POWER_DOWN;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300773
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300774 state->m_rfmirror = (ul_rf_mirror == 0);
775 state->m_if_agc_pol = false;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300776 return 0;
777}
778
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300779static int drxx_open(struct drxk_state *state)
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300780{
781 int status = 0;
782 u32 jtag = 0;
783 u16 bid = 0;
784 u16 key = 0;
785
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -0300786 dprintk(1, "\n");
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300787 /* stop lock indicator process */
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -0300788 status = write16(state, SCU_RAM_GPIO__A,
789 SCU_RAM_GPIO_HW_LOCK_IND_DISABLE);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300790 if (status < 0)
791 goto error;
792 /* Check device id */
793 status = read16(state, SIO_TOP_COMM_KEY__A, &key);
794 if (status < 0)
795 goto error;
796 status = write16(state, SIO_TOP_COMM_KEY__A, SIO_TOP_COMM_KEY_KEY);
797 if (status < 0)
798 goto error;
799 status = read32(state, SIO_TOP_JTAGID_LO__A, &jtag);
800 if (status < 0)
801 goto error;
802 status = read16(state, SIO_PDR_UIO_IN_HI__A, &bid);
803 if (status < 0)
804 goto error;
805 status = write16(state, SIO_TOP_COMM_KEY__A, key);
806error:
807 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -0300808 pr_err("Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300809 return status;
810}
811
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300812static int get_device_capabilities(struct drxk_state *state)
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300813{
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300814 u16 sio_pdr_ohw_cfg = 0;
815 u32 sio_top_jtagid_lo = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300816 int status;
Mauro Carvalho Chehab9c6e1822011-07-10 08:38:18 -0300817 const char *spin = "";
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300818
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -0300819 dprintk(1, "\n");
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300820
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300821 /* driver 0.9.0 */
822 /* stop lock indicator process */
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -0300823 status = write16(state, SCU_RAM_GPIO__A,
824 SCU_RAM_GPIO_HW_LOCK_IND_DISABLE);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300825 if (status < 0)
826 goto error;
Martin Blumenstingl84183662012-10-04 14:22:55 -0300827 status = write16(state, SIO_TOP_COMM_KEY__A, SIO_TOP_COMM_KEY_KEY);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300828 if (status < 0)
829 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300830 status = read16(state, SIO_PDR_OHW_CFG__A, &sio_pdr_ohw_cfg);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300831 if (status < 0)
832 goto error;
833 status = write16(state, SIO_TOP_COMM_KEY__A, 0x0000);
834 if (status < 0)
835 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300836
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300837 switch ((sio_pdr_ohw_cfg & SIO_PDR_OHW_CFG_FREF_SEL__M)) {
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300838 case 0:
839 /* ignore (bypass ?) */
840 break;
841 case 1:
842 /* 27 MHz */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300843 state->m_osc_clock_freq = 27000;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300844 break;
845 case 2:
846 /* 20.25 MHz */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300847 state->m_osc_clock_freq = 20250;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300848 break;
849 case 3:
850 /* 4 MHz */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300851 state->m_osc_clock_freq = 20250;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300852 break;
853 default:
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -0300854 pr_err("Clock Frequency is unknown\n");
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300855 return -EINVAL;
856 }
857 /*
858 Determine device capabilities
859 Based on pinning v14
860 */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300861 status = read32(state, SIO_TOP_JTAGID_LO__A, &sio_top_jtagid_lo);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300862 if (status < 0)
863 goto error;
Mauro Carvalho Chehab0d3e6fe2011-07-22 12:34:41 -0300864
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -0300865 pr_info("status = 0x%08x\n", sio_top_jtagid_lo);
Mauro Carvalho Chehab0d3e6fe2011-07-22 12:34:41 -0300866
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300867 /* driver 0.9.0 */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300868 switch ((sio_top_jtagid_lo >> 29) & 0xF) {
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300869 case 0:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300870 state->m_device_spin = DRXK_SPIN_A1;
Mauro Carvalho Chehab9c6e1822011-07-10 08:38:18 -0300871 spin = "A1";
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300872 break;
873 case 2:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300874 state->m_device_spin = DRXK_SPIN_A2;
Mauro Carvalho Chehab9c6e1822011-07-10 08:38:18 -0300875 spin = "A2";
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300876 break;
877 case 3:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300878 state->m_device_spin = DRXK_SPIN_A3;
Mauro Carvalho Chehab9c6e1822011-07-10 08:38:18 -0300879 spin = "A3";
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300880 break;
881 default:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300882 state->m_device_spin = DRXK_SPIN_UNKNOWN;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300883 status = -EINVAL;
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -0300884 pr_err("Spin %d unknown\n", (sio_top_jtagid_lo >> 29) & 0xF);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300885 goto error2;
886 }
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300887 switch ((sio_top_jtagid_lo >> 12) & 0xFF) {
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300888 case 0x13:
889 /* typeId = DRX3913K_TYPE_ID */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300890 state->m_has_lna = false;
891 state->m_has_oob = false;
892 state->m_has_atv = false;
893 state->m_has_audio = false;
894 state->m_has_dvbt = true;
895 state->m_has_dvbc = true;
896 state->m_has_sawsw = true;
897 state->m_has_gpio2 = false;
898 state->m_has_gpio1 = false;
899 state->m_has_irqn = false;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300900 break;
901 case 0x15:
902 /* typeId = DRX3915K_TYPE_ID */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300903 state->m_has_lna = false;
904 state->m_has_oob = false;
905 state->m_has_atv = true;
906 state->m_has_audio = false;
907 state->m_has_dvbt = true;
908 state->m_has_dvbc = false;
909 state->m_has_sawsw = true;
910 state->m_has_gpio2 = true;
911 state->m_has_gpio1 = true;
912 state->m_has_irqn = false;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300913 break;
914 case 0x16:
915 /* typeId = DRX3916K_TYPE_ID */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300916 state->m_has_lna = false;
917 state->m_has_oob = false;
918 state->m_has_atv = true;
919 state->m_has_audio = false;
920 state->m_has_dvbt = true;
921 state->m_has_dvbc = false;
922 state->m_has_sawsw = true;
923 state->m_has_gpio2 = true;
924 state->m_has_gpio1 = true;
925 state->m_has_irqn = false;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300926 break;
927 case 0x18:
928 /* typeId = DRX3918K_TYPE_ID */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300929 state->m_has_lna = false;
930 state->m_has_oob = false;
931 state->m_has_atv = true;
932 state->m_has_audio = true;
933 state->m_has_dvbt = true;
934 state->m_has_dvbc = false;
935 state->m_has_sawsw = true;
936 state->m_has_gpio2 = true;
937 state->m_has_gpio1 = true;
938 state->m_has_irqn = false;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300939 break;
940 case 0x21:
941 /* typeId = DRX3921K_TYPE_ID */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300942 state->m_has_lna = false;
943 state->m_has_oob = false;
944 state->m_has_atv = true;
945 state->m_has_audio = true;
946 state->m_has_dvbt = true;
947 state->m_has_dvbc = true;
948 state->m_has_sawsw = true;
949 state->m_has_gpio2 = true;
950 state->m_has_gpio1 = true;
951 state->m_has_irqn = false;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300952 break;
953 case 0x23:
954 /* typeId = DRX3923K_TYPE_ID */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300955 state->m_has_lna = false;
956 state->m_has_oob = false;
957 state->m_has_atv = true;
958 state->m_has_audio = true;
959 state->m_has_dvbt = true;
960 state->m_has_dvbc = true;
961 state->m_has_sawsw = true;
962 state->m_has_gpio2 = true;
963 state->m_has_gpio1 = true;
964 state->m_has_irqn = false;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300965 break;
966 case 0x25:
967 /* typeId = DRX3925K_TYPE_ID */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300968 state->m_has_lna = false;
969 state->m_has_oob = false;
970 state->m_has_atv = true;
971 state->m_has_audio = true;
972 state->m_has_dvbt = true;
973 state->m_has_dvbc = true;
974 state->m_has_sawsw = true;
975 state->m_has_gpio2 = true;
976 state->m_has_gpio1 = true;
977 state->m_has_irqn = false;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300978 break;
979 case 0x26:
980 /* typeId = DRX3926K_TYPE_ID */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300981 state->m_has_lna = false;
982 state->m_has_oob = false;
983 state->m_has_atv = true;
984 state->m_has_audio = false;
985 state->m_has_dvbt = true;
986 state->m_has_dvbc = true;
987 state->m_has_sawsw = true;
988 state->m_has_gpio2 = true;
989 state->m_has_gpio1 = true;
990 state->m_has_irqn = false;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300991 break;
992 default:
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -0300993 pr_err("DeviceID 0x%02x not supported\n",
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300994 ((sio_top_jtagid_lo >> 12) & 0xFF));
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300995 status = -EINVAL;
996 goto error2;
997 }
998
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -0300999 pr_info("detected a drx-39%02xk, spin %s, xtal %d.%03d MHz\n",
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001000 ((sio_top_jtagid_lo >> 12) & 0xFF), spin,
1001 state->m_osc_clock_freq / 1000,
1002 state->m_osc_clock_freq % 1000);
Mauro Carvalho Chehab9c6e1822011-07-10 08:38:18 -03001003
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001004error:
1005 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03001006 pr_err("Error %d on %s\n", status, __func__);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001007
1008error2:
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001009 return status;
1010}
1011
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001012static int hi_command(struct drxk_state *state, u16 cmd, u16 *p_result)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001013{
1014 int status;
1015 bool powerdown_cmd;
1016
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03001017 dprintk(1, "\n");
1018
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001019 /* Write command */
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -03001020 status = write16(state, SIO_HI_RA_RAM_CMD__A, cmd);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001021 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001022 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001023 if (cmd == SIO_HI_RA_RAM_CMD_RESET)
Mauro Carvalho Chehabb72852b2013-04-28 11:47:47 -03001024 usleep_range(1000, 2000);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001025
1026 powerdown_cmd =
Oliver Endrissebc7de22011-07-03 13:49:44 -03001027 (bool) ((cmd == SIO_HI_RA_RAM_CMD_CONFIG) &&
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001028 ((state->m_hi_cfg_ctrl) &
Oliver Endrissebc7de22011-07-03 13:49:44 -03001029 SIO_HI_RA_RAM_PAR_5_CFG_SLEEP__M) ==
1030 SIO_HI_RA_RAM_PAR_5_CFG_SLEEP_ZZZ);
Mauro Carvalho Chehab5a7f7b72014-09-03 15:23:57 -03001031 if (!powerdown_cmd) {
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001032 /* Wait until command rdy */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001033 u32 retry_count = 0;
1034 u16 wait_cmd;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001035
1036 do {
Mauro Carvalho Chehabb72852b2013-04-28 11:47:47 -03001037 usleep_range(1000, 2000);
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001038 retry_count += 1;
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -03001039 status = read16(state, SIO_HI_RA_RAM_CMD__A,
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001040 &wait_cmd);
1041 } while ((status < 0) && (retry_count < DRXK_MAX_RETRIES)
1042 && (wait_cmd != 0));
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001043 if (status < 0)
1044 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001045 status = read16(state, SIO_HI_RA_RAM_RES__A, p_result);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001046 }
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001047error:
1048 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03001049 pr_err("Error %d on %s\n", status, __func__);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001050
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001051 return status;
1052}
1053
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001054static int hi_cfg_command(struct drxk_state *state)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001055{
1056 int status;
1057
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03001058 dprintk(1, "\n");
1059
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001060 mutex_lock(&state->mutex);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001061
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03001062 status = write16(state, SIO_HI_RA_RAM_PAR_6__A,
1063 state->m_hi_cfg_timeout);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001064 if (status < 0)
1065 goto error;
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03001066 status = write16(state, SIO_HI_RA_RAM_PAR_5__A,
1067 state->m_hi_cfg_ctrl);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001068 if (status < 0)
1069 goto error;
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03001070 status = write16(state, SIO_HI_RA_RAM_PAR_4__A,
1071 state->m_hi_cfg_wake_up_key);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001072 if (status < 0)
1073 goto error;
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03001074 status = write16(state, SIO_HI_RA_RAM_PAR_3__A,
1075 state->m_hi_cfg_bridge_delay);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001076 if (status < 0)
1077 goto error;
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03001078 status = write16(state, SIO_HI_RA_RAM_PAR_2__A,
1079 state->m_hi_cfg_timing_div);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001080 if (status < 0)
1081 goto error;
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03001082 status = write16(state, SIO_HI_RA_RAM_PAR_1__A,
1083 SIO_HI_RA_RAM_PAR_1_PAR1_SEC_KEY);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001084 if (status < 0)
1085 goto error;
Hans Verkuilb1cf2012013-10-04 11:01:45 -03001086 status = hi_command(state, SIO_HI_RA_RAM_CMD_CONFIG, NULL);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001087 if (status < 0)
1088 goto error;
1089
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001090 state->m_hi_cfg_ctrl &= ~SIO_HI_RA_RAM_PAR_5_CFG_SLEEP_ZZZ;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001091error:
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001092 mutex_unlock(&state->mutex);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001093 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03001094 pr_err("Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001095 return status;
1096}
1097
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001098static int init_hi(struct drxk_state *state)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001099{
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03001100 dprintk(1, "\n");
1101
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001102 state->m_hi_cfg_wake_up_key = (state->demod_address << 1);
1103 state->m_hi_cfg_timeout = 0x96FF;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001104 /* port/bridge/power down ctrl */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001105 state->m_hi_cfg_ctrl = SIO_HI_RA_RAM_PAR_5_CFG_SLV0_SLAVE;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001106
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001107 return hi_cfg_command(state);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001108}
1109
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001110static int mpegts_configure_pins(struct drxk_state *state, bool mpeg_enable)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001111{
1112 int status = -1;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001113 u16 sio_pdr_mclk_cfg = 0;
1114 u16 sio_pdr_mdx_cfg = 0;
Mauro Carvalho Chehabd5856812012-01-21 07:57:06 -03001115 u16 err_cfg = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001116
Mauro Carvalho Chehab534e0482011-07-24 14:59:20 -03001117 dprintk(1, ": mpeg %s, %s mode\n",
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001118 mpeg_enable ? "enable" : "disable",
1119 state->m_enable_parallel ? "parallel" : "serial");
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001120
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001121 /* stop lock indicator process */
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03001122 status = write16(state, SCU_RAM_GPIO__A,
1123 SCU_RAM_GPIO_HW_LOCK_IND_DISABLE);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001124 if (status < 0)
1125 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001126
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001127 /* MPEG TS pad configuration */
Martin Blumenstingl84183662012-10-04 14:22:55 -03001128 status = write16(state, SIO_TOP_COMM_KEY__A, SIO_TOP_COMM_KEY_KEY);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001129 if (status < 0)
1130 goto error;
1131
Mauro Carvalho Chehab5a7f7b72014-09-03 15:23:57 -03001132 if (!mpeg_enable) {
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001133 /* Set MPEG TS pads to inputmode */
1134 status = write16(state, SIO_PDR_MSTRT_CFG__A, 0x0000);
1135 if (status < 0)
1136 goto error;
1137 status = write16(state, SIO_PDR_MERR_CFG__A, 0x0000);
1138 if (status < 0)
1139 goto error;
1140 status = write16(state, SIO_PDR_MCLK_CFG__A, 0x0000);
1141 if (status < 0)
1142 goto error;
1143 status = write16(state, SIO_PDR_MVAL_CFG__A, 0x0000);
1144 if (status < 0)
1145 goto error;
1146 status = write16(state, SIO_PDR_MD0_CFG__A, 0x0000);
1147 if (status < 0)
1148 goto error;
1149 status = write16(state, SIO_PDR_MD1_CFG__A, 0x0000);
1150 if (status < 0)
1151 goto error;
1152 status = write16(state, SIO_PDR_MD2_CFG__A, 0x0000);
1153 if (status < 0)
1154 goto error;
1155 status = write16(state, SIO_PDR_MD3_CFG__A, 0x0000);
1156 if (status < 0)
1157 goto error;
1158 status = write16(state, SIO_PDR_MD4_CFG__A, 0x0000);
1159 if (status < 0)
1160 goto error;
1161 status = write16(state, SIO_PDR_MD5_CFG__A, 0x0000);
1162 if (status < 0)
1163 goto error;
1164 status = write16(state, SIO_PDR_MD6_CFG__A, 0x0000);
1165 if (status < 0)
1166 goto error;
1167 status = write16(state, SIO_PDR_MD7_CFG__A, 0x0000);
1168 if (status < 0)
1169 goto error;
1170 } else {
1171 /* Enable MPEG output */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001172 sio_pdr_mdx_cfg =
1173 ((state->m_ts_data_strength <<
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001174 SIO_PDR_MD0_CFG_DRIVE__B) | 0x0003);
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001175 sio_pdr_mclk_cfg = ((state->m_ts_clockk_strength <<
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001176 SIO_PDR_MCLK_CFG_DRIVE__B) |
1177 0x0003);
1178
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001179 status = write16(state, SIO_PDR_MSTRT_CFG__A, sio_pdr_mdx_cfg);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001180 if (status < 0)
1181 goto error;
Mauro Carvalho Chehabd5856812012-01-21 07:57:06 -03001182
1183 if (state->enable_merr_cfg)
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001184 err_cfg = sio_pdr_mdx_cfg;
Mauro Carvalho Chehabd5856812012-01-21 07:57:06 -03001185
1186 status = write16(state, SIO_PDR_MERR_CFG__A, err_cfg);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001187 if (status < 0)
1188 goto error;
Mauro Carvalho Chehabd5856812012-01-21 07:57:06 -03001189 status = write16(state, SIO_PDR_MVAL_CFG__A, err_cfg);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001190 if (status < 0)
1191 goto error;
Mauro Carvalho Chehabd5856812012-01-21 07:57:06 -03001192
Mauro Carvalho Chehab5a7f7b72014-09-03 15:23:57 -03001193 if (state->m_enable_parallel) {
Jonathan McCrohan39c1cb22013-10-20 21:34:01 -03001194 /* parallel -> enable MD1 to MD7 */
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03001195 status = write16(state, SIO_PDR_MD1_CFG__A,
1196 sio_pdr_mdx_cfg);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03001197 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001198 goto error;
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03001199 status = write16(state, SIO_PDR_MD2_CFG__A,
1200 sio_pdr_mdx_cfg);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03001201 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001202 goto error;
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03001203 status = write16(state, SIO_PDR_MD3_CFG__A,
1204 sio_pdr_mdx_cfg);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03001205 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001206 goto error;
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03001207 status = write16(state, SIO_PDR_MD4_CFG__A,
1208 sio_pdr_mdx_cfg);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03001209 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001210 goto error;
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03001211 status = write16(state, SIO_PDR_MD5_CFG__A,
1212 sio_pdr_mdx_cfg);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03001213 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001214 goto error;
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03001215 status = write16(state, SIO_PDR_MD6_CFG__A,
1216 sio_pdr_mdx_cfg);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001217 if (status < 0)
1218 goto error;
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03001219 status = write16(state, SIO_PDR_MD7_CFG__A,
1220 sio_pdr_mdx_cfg);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001221 if (status < 0)
1222 goto error;
1223 } else {
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001224 sio_pdr_mdx_cfg = ((state->m_ts_data_strength <<
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001225 SIO_PDR_MD0_CFG_DRIVE__B)
1226 | 0x0003);
1227 /* serial -> disable MD1 to MD7 */
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -03001228 status = write16(state, SIO_PDR_MD1_CFG__A, 0x0000);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03001229 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001230 goto error;
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -03001231 status = write16(state, SIO_PDR_MD2_CFG__A, 0x0000);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03001232 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001233 goto error;
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -03001234 status = write16(state, SIO_PDR_MD3_CFG__A, 0x0000);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03001235 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001236 goto error;
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -03001237 status = write16(state, SIO_PDR_MD4_CFG__A, 0x0000);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03001238 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001239 goto error;
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -03001240 status = write16(state, SIO_PDR_MD5_CFG__A, 0x0000);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03001241 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001242 goto error;
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -03001243 status = write16(state, SIO_PDR_MD6_CFG__A, 0x0000);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03001244 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001245 goto error;
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -03001246 status = write16(state, SIO_PDR_MD7_CFG__A, 0x0000);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03001247 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001248 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001249 }
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001250 status = write16(state, SIO_PDR_MCLK_CFG__A, sio_pdr_mclk_cfg);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03001251 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001252 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001253 status = write16(state, SIO_PDR_MD0_CFG__A, sio_pdr_mdx_cfg);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03001254 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001255 goto error;
1256 }
1257 /* Enable MB output over MPEG pads and ctl input */
1258 status = write16(state, SIO_PDR_MON_CFG__A, 0x0000);
1259 if (status < 0)
1260 goto error;
1261 /* Write nomagic word to enable pdr reg write */
1262 status = write16(state, SIO_TOP_COMM_KEY__A, 0x0000);
1263error:
1264 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03001265 pr_err("Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001266 return status;
1267}
1268
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001269static int mpegts_disable(struct drxk_state *state)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001270{
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03001271 dprintk(1, "\n");
1272
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001273 return mpegts_configure_pins(state, false);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001274}
1275
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001276static int bl_chain_cmd(struct drxk_state *state,
1277 u16 rom_offset, u16 nr_of_elements, u32 time_out)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001278{
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001279 u16 bl_status = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001280 int status;
1281 unsigned long end;
1282
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03001283 dprintk(1, "\n");
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001284 mutex_lock(&state->mutex);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001285 status = write16(state, SIO_BL_MODE__A, SIO_BL_MODE_CHAIN);
1286 if (status < 0)
1287 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001288 status = write16(state, SIO_BL_CHAIN_ADDR__A, rom_offset);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001289 if (status < 0)
1290 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001291 status = write16(state, SIO_BL_CHAIN_LEN__A, nr_of_elements);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001292 if (status < 0)
1293 goto error;
1294 status = write16(state, SIO_BL_ENABLE__A, SIO_BL_ENABLE_ON);
1295 if (status < 0)
1296 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001297
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001298 end = jiffies + msecs_to_jiffies(time_out);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001299 do {
Mauro Carvalho Chehabb72852b2013-04-28 11:47:47 -03001300 usleep_range(1000, 2000);
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001301 status = read16(state, SIO_BL_STATUS__A, &bl_status);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001302 if (status < 0)
1303 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001304 } while ((bl_status == 0x1) &&
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001305 ((time_is_after_jiffies(end))));
1306
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001307 if (bl_status == 0x1) {
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03001308 pr_err("SIO not ready\n");
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001309 status = -EINVAL;
1310 goto error2;
1311 }
1312error:
1313 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03001314 pr_err("Error %d on %s\n", status, __func__);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001315error2:
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001316 mutex_unlock(&state->mutex);
1317 return status;
1318}
1319
1320
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001321static int download_microcode(struct drxk_state *state,
1322 const u8 p_mc_image[], u32 length)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001323{
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001324 const u8 *p_src = p_mc_image;
1325 u32 address;
1326 u16 n_blocks;
1327 u16 block_size;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001328 u32 offset = 0;
1329 u32 i;
Mauro Carvalho Chehab1bd09dd2011-07-03 18:21:59 -03001330 int status = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001331
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03001332 dprintk(1, "\n");
1333
Hans Verkuil5becbc52012-05-14 10:22:58 -03001334 /* down the drain (we don't care about MAGIC_WORD) */
1335#if 0
1336 /* For future reference */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001337 drain = (p_src[0] << 8) | p_src[1];
Hans Verkuil5becbc52012-05-14 10:22:58 -03001338#endif
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001339 p_src += sizeof(u16);
Oliver Endrissebc7de22011-07-03 13:49:44 -03001340 offset += sizeof(u16);
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001341 n_blocks = (p_src[0] << 8) | p_src[1];
1342 p_src += sizeof(u16);
Oliver Endrissebc7de22011-07-03 13:49:44 -03001343 offset += sizeof(u16);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001344
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001345 for (i = 0; i < n_blocks; i += 1) {
1346 address = (p_src[0] << 24) | (p_src[1] << 16) |
1347 (p_src[2] << 8) | p_src[3];
1348 p_src += sizeof(u32);
Oliver Endrissebc7de22011-07-03 13:49:44 -03001349 offset += sizeof(u32);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001350
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001351 block_size = ((p_src[0] << 8) | p_src[1]) * sizeof(u16);
1352 p_src += sizeof(u16);
Oliver Endrissebc7de22011-07-03 13:49:44 -03001353 offset += sizeof(u16);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001354
Hans Verkuil5becbc52012-05-14 10:22:58 -03001355#if 0
1356 /* For future reference */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001357 flags = (p_src[0] << 8) | p_src[1];
Hans Verkuil5becbc52012-05-14 10:22:58 -03001358#endif
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001359 p_src += sizeof(u16);
Oliver Endrissebc7de22011-07-03 13:49:44 -03001360 offset += sizeof(u16);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001361
Hans Verkuil5becbc52012-05-14 10:22:58 -03001362#if 0
1363 /* For future reference */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001364 block_crc = (p_src[0] << 8) | p_src[1];
Hans Verkuil5becbc52012-05-14 10:22:58 -03001365#endif
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001366 p_src += sizeof(u16);
Oliver Endrissebc7de22011-07-03 13:49:44 -03001367 offset += sizeof(u16);
Mauro Carvalho Chehabbcd2ebb2011-07-09 18:57:54 -03001368
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001369 if (offset + block_size > length) {
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03001370 pr_err("Firmware is corrupted.\n");
Mauro Carvalho Chehabbcd2ebb2011-07-09 18:57:54 -03001371 return -EINVAL;
1372 }
1373
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001374 status = write_block(state, address, block_size, p_src);
Mauro Carvalho Chehab39624f72011-07-09 19:23:44 -03001375 if (status < 0) {
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03001376 pr_err("Error %d while loading firmware\n", status);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001377 break;
Mauro Carvalho Chehab39624f72011-07-09 19:23:44 -03001378 }
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001379 p_src += block_size;
1380 offset += block_size;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001381 }
1382 return status;
1383}
1384
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001385static int dvbt_enable_ofdm_token_ring(struct drxk_state *state, bool enable)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001386{
1387 int status;
Oliver Endrissebc7de22011-07-03 13:49:44 -03001388 u16 data = 0;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001389 u16 desired_ctrl = SIO_OFDM_SH_OFDM_RING_ENABLE_ON;
1390 u16 desired_status = SIO_OFDM_SH_OFDM_RING_STATUS_ENABLED;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001391 unsigned long end;
1392
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03001393 dprintk(1, "\n");
1394
Mauro Carvalho Chehab5a7f7b72014-09-03 15:23:57 -03001395 if (!enable) {
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001396 desired_ctrl = SIO_OFDM_SH_OFDM_RING_ENABLE_OFF;
1397 desired_status = SIO_OFDM_SH_OFDM_RING_STATUS_DOWN;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001398 }
1399
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001400 status = read16(state, SIO_OFDM_SH_OFDM_RING_STATUS__A, &data);
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001401 if (status >= 0 && data == desired_status) {
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001402 /* tokenring already has correct status */
1403 return status;
1404 }
1405 /* Disable/enable dvbt tokenring bridge */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001406 status = write16(state, SIO_OFDM_SH_OFDM_RING_ENABLE__A, desired_ctrl);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001407
Oliver Endrissebc7de22011-07-03 13:49:44 -03001408 end = jiffies + msecs_to_jiffies(DRXK_OFDM_TR_SHUTDOWN_TIMEOUT);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03001409 do {
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -03001410 status = read16(state, SIO_OFDM_SH_OFDM_RING_STATUS__A, &data);
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03001411 if ((status >= 0 && data == desired_status)
1412 || time_is_after_jiffies(end))
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03001413 break;
Mauro Carvalho Chehabb72852b2013-04-28 11:47:47 -03001414 usleep_range(1000, 2000);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001415 } while (1);
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001416 if (data != desired_status) {
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03001417 pr_err("SIO not ready\n");
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001418 return -EINVAL;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001419 }
1420 return status;
1421}
1422
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001423static int mpegts_stop(struct drxk_state *state)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001424{
1425 int status = 0;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001426 u16 fec_oc_snc_mode = 0;
1427 u16 fec_oc_ipr_mode = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001428
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03001429 dprintk(1, "\n");
1430
Jonathan McCrohan39c1cb22013-10-20 21:34:01 -03001431 /* Graceful shutdown (byte boundaries) */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001432 status = read16(state, FEC_OC_SNC_MODE__A, &fec_oc_snc_mode);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001433 if (status < 0)
1434 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001435 fec_oc_snc_mode |= FEC_OC_SNC_MODE_SHUTDOWN__M;
1436 status = write16(state, FEC_OC_SNC_MODE__A, fec_oc_snc_mode);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001437 if (status < 0)
1438 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001439
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001440 /* Suppress MCLK during absence of data */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001441 status = read16(state, FEC_OC_IPR_MODE__A, &fec_oc_ipr_mode);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001442 if (status < 0)
1443 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001444 fec_oc_ipr_mode |= FEC_OC_IPR_MODE_MCLK_DIS_DAT_ABS__M;
1445 status = write16(state, FEC_OC_IPR_MODE__A, fec_oc_ipr_mode);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001446
1447error:
1448 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03001449 pr_err("Error %d on %s\n", status, __func__);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001450
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001451 return status;
1452}
1453
1454static int scu_command(struct drxk_state *state,
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001455 u16 cmd, u8 parameter_len,
1456 u16 *parameter, u8 result_len, u16 *result)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001457{
1458#if (SCU_RAM_PARAM_0__A - SCU_RAM_PARAM_15__A) != 15
1459#error DRXK register mapping no longer compatible with this routine!
1460#endif
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001461 u16 cur_cmd = 0;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001462 int status = -EINVAL;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001463 unsigned long end;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001464 u8 buffer[34];
1465 int cnt = 0, ii;
Mauro Carvalho Chehab75589772011-07-10 13:25:48 -03001466 const char *p;
1467 char errname[30];
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001468
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03001469 dprintk(1, "\n");
1470
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001471 if ((cmd == 0) || ((parameter_len > 0) && (parameter == NULL)) ||
1472 ((result_len > 0) && (result == NULL))) {
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03001473 pr_err("Error %d on %s\n", status, __func__);
Alexey Khoroshilove4459e12012-04-05 18:53:20 -03001474 return status;
1475 }
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001476
1477 mutex_lock(&state->mutex);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001478
1479 /* assume that the command register is ready
1480 since it is checked afterwards */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001481 for (ii = parameter_len - 1; ii >= 0; ii -= 1) {
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001482 buffer[cnt++] = (parameter[ii] & 0xFF);
1483 buffer[cnt++] = ((parameter[ii] >> 8) & 0xFF);
1484 }
1485 buffer[cnt++] = (cmd & 0xFF);
1486 buffer[cnt++] = ((cmd >> 8) & 0xFF);
1487
1488 write_block(state, SCU_RAM_PARAM_0__A -
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001489 (parameter_len - 1), cnt, buffer);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001490 /* Wait until SCU has processed command */
1491 end = jiffies + msecs_to_jiffies(DRXK_MAX_WAITTIME);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001492 do {
Mauro Carvalho Chehabb72852b2013-04-28 11:47:47 -03001493 usleep_range(1000, 2000);
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001494 status = read16(state, SCU_RAM_COMMAND__A, &cur_cmd);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001495 if (status < 0)
1496 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001497 } while (!(cur_cmd == DRX_SCU_READY) && (time_is_after_jiffies(end)));
1498 if (cur_cmd != DRX_SCU_READY) {
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03001499 pr_err("SCU not ready\n");
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001500 status = -EIO;
1501 goto error2;
1502 }
1503 /* read results */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001504 if ((result_len > 0) && (result != NULL)) {
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001505 s16 err;
1506 int ii;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001507
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001508 for (ii = result_len - 1; ii >= 0; ii -= 1) {
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03001509 status = read16(state, SCU_RAM_PARAM_0__A - ii,
1510 &result[ii]);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03001511 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001512 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001513 }
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001514
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001515 /* Check if an error was reported by SCU */
1516 err = (s16)result[0];
Mauro Carvalho Chehab75589772011-07-10 13:25:48 -03001517 if (err >= 0)
1518 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001519
Mauro Carvalho Chehab75589772011-07-10 13:25:48 -03001520 /* check for the known error codes */
1521 switch (err) {
1522 case SCU_RESULT_UNKCMD:
1523 p = "SCU_RESULT_UNKCMD";
1524 break;
1525 case SCU_RESULT_UNKSTD:
1526 p = "SCU_RESULT_UNKSTD";
1527 break;
1528 case SCU_RESULT_SIZE:
1529 p = "SCU_RESULT_SIZE";
1530 break;
1531 case SCU_RESULT_INVPAR:
1532 p = "SCU_RESULT_INVPAR";
1533 break;
1534 default: /* Other negative values are errors */
1535 sprintf(errname, "ERROR: %d\n", err);
1536 p = errname;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001537 }
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03001538 pr_err("%s while sending cmd 0x%04x with params:", p, cmd);
Mauro Carvalho Chehab75589772011-07-10 13:25:48 -03001539 print_hex_dump_bytes("drxk: ", DUMP_PREFIX_NONE, buffer, cnt);
1540 status = -EINVAL;
1541 goto error2;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001542 }
1543
1544error:
Oliver Endrissebc7de22011-07-03 13:49:44 -03001545 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03001546 pr_err("Error %d on %s\n", status, __func__);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001547error2:
1548 mutex_unlock(&state->mutex);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001549 return status;
1550}
1551
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001552static int set_iqm_af(struct drxk_state *state, bool active)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001553{
1554 u16 data = 0;
1555 int status;
1556
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03001557 dprintk(1, "\n");
1558
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001559 /* Configure IQM */
1560 status = read16(state, IQM_AF_STDBY__A, &data);
1561 if (status < 0)
1562 goto error;
Oliver Endrissebc7de22011-07-03 13:49:44 -03001563
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001564 if (!active) {
1565 data |= (IQM_AF_STDBY_STDBY_ADC_STANDBY
1566 | IQM_AF_STDBY_STDBY_AMP_STANDBY
1567 | IQM_AF_STDBY_STDBY_PD_STANDBY
1568 | IQM_AF_STDBY_STDBY_TAGC_IF_STANDBY
1569 | IQM_AF_STDBY_STDBY_TAGC_RF_STANDBY);
1570 } else {
1571 data &= ((~IQM_AF_STDBY_STDBY_ADC_STANDBY)
1572 & (~IQM_AF_STDBY_STDBY_AMP_STANDBY)
1573 & (~IQM_AF_STDBY_STDBY_PD_STANDBY)
1574 & (~IQM_AF_STDBY_STDBY_TAGC_IF_STANDBY)
1575 & (~IQM_AF_STDBY_STDBY_TAGC_RF_STANDBY)
1576 );
1577 }
1578 status = write16(state, IQM_AF_STDBY__A, data);
1579
1580error:
1581 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03001582 pr_err("Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001583 return status;
1584}
1585
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001586static int ctrl_power_mode(struct drxk_state *state, enum drx_power_mode *mode)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001587{
1588 int status = 0;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001589 u16 sio_cc_pwd_mode = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001590
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03001591 dprintk(1, "\n");
1592
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001593 /* Check arguments */
1594 if (mode == NULL)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001595 return -EINVAL;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001596
1597 switch (*mode) {
1598 case DRX_POWER_UP:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001599 sio_cc_pwd_mode = SIO_CC_PWD_MODE_LEVEL_NONE;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001600 break;
1601 case DRXK_POWER_DOWN_OFDM:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001602 sio_cc_pwd_mode = SIO_CC_PWD_MODE_LEVEL_OFDM;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001603 break;
1604 case DRXK_POWER_DOWN_CORE:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001605 sio_cc_pwd_mode = SIO_CC_PWD_MODE_LEVEL_CLOCK;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001606 break;
1607 case DRXK_POWER_DOWN_PLL:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001608 sio_cc_pwd_mode = SIO_CC_PWD_MODE_LEVEL_PLL;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001609 break;
1610 case DRX_POWER_DOWN:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001611 sio_cc_pwd_mode = SIO_CC_PWD_MODE_LEVEL_OSC;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001612 break;
1613 default:
1614 /* Unknow sleep mode */
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001615 return -EINVAL;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001616 }
1617
1618 /* If already in requested power mode, do nothing */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001619 if (state->m_current_power_mode == *mode)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001620 return 0;
1621
1622 /* For next steps make sure to start from DRX_POWER_UP mode */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001623 if (state->m_current_power_mode != DRX_POWER_UP) {
1624 status = power_up_device(state);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001625 if (status < 0)
1626 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001627 status = dvbt_enable_ofdm_token_ring(state, true);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001628 if (status < 0)
1629 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001630 }
1631
1632 if (*mode == DRX_POWER_UP) {
1633 /* Restore analog & pin configuartion */
1634 } else {
1635 /* Power down to requested mode */
1636 /* Backup some register settings */
1637 /* Set pins with possible pull-ups connected
1638 to them in input mode */
1639 /* Analog power down */
1640 /* ADC power down */
1641 /* Power down device */
1642 /* stop all comm_exec */
1643 /* Stop and power down previous standard */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001644 switch (state->m_operation_mode) {
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001645 case OM_DVBT:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001646 status = mpegts_stop(state);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03001647 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001648 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001649 status = power_down_dvbt(state, false);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03001650 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001651 goto error;
1652 break;
1653 case OM_QAM_ITU_A:
1654 case OM_QAM_ITU_C:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001655 status = mpegts_stop(state);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03001656 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001657 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001658 status = power_down_qam(state);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001659 if (status < 0)
1660 goto error;
1661 break;
1662 default:
1663 break;
1664 }
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001665 status = dvbt_enable_ofdm_token_ring(state, false);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001666 if (status < 0)
1667 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001668 status = write16(state, SIO_CC_PWD_MODE__A, sio_cc_pwd_mode);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001669 if (status < 0)
1670 goto error;
1671 status = write16(state, SIO_CC_UPDATE__A, SIO_CC_UPDATE_KEY);
1672 if (status < 0)
1673 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001674
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001675 if (*mode != DRXK_POWER_DOWN_OFDM) {
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001676 state->m_hi_cfg_ctrl |=
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001677 SIO_HI_RA_RAM_PAR_5_CFG_SLEEP_ZZZ;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001678 status = hi_cfg_command(state);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001679 if (status < 0)
1680 goto error;
1681 }
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001682 }
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001683 state->m_current_power_mode = *mode;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001684
1685error:
1686 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03001687 pr_err("Error %d on %s\n", status, __func__);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001688
Oliver Endrissebc7de22011-07-03 13:49:44 -03001689 return status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001690}
1691
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001692static int power_down_dvbt(struct drxk_state *state, bool set_power_mode)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001693{
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001694 enum drx_power_mode power_mode = DRXK_POWER_DOWN_OFDM;
1695 u16 cmd_result = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001696 u16 data = 0;
1697 int status;
1698
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03001699 dprintk(1, "\n");
1700
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001701 status = read16(state, SCU_COMM_EXEC__A, &data);
1702 if (status < 0)
1703 goto error;
1704 if (data == SCU_COMM_EXEC_ACTIVE) {
1705 /* Send OFDM stop command */
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03001706 status = scu_command(state,
1707 SCU_RAM_COMMAND_STANDARD_OFDM
1708 | SCU_RAM_COMMAND_CMD_DEMOD_STOP,
1709 0, NULL, 1, &cmd_result);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03001710 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001711 goto error;
1712 /* Send OFDM reset command */
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03001713 status = scu_command(state,
1714 SCU_RAM_COMMAND_STANDARD_OFDM
1715 | SCU_RAM_COMMAND_CMD_DEMOD_RESET,
1716 0, NULL, 1, &cmd_result);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001717 if (status < 0)
1718 goto error;
1719 }
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001720
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001721 /* Reset datapath for OFDM, processors first */
1722 status = write16(state, OFDM_SC_COMM_EXEC__A, OFDM_SC_COMM_EXEC_STOP);
1723 if (status < 0)
1724 goto error;
1725 status = write16(state, OFDM_LC_COMM_EXEC__A, OFDM_LC_COMM_EXEC_STOP);
1726 if (status < 0)
1727 goto error;
1728 status = write16(state, IQM_COMM_EXEC__A, IQM_COMM_EXEC_B_STOP);
1729 if (status < 0)
1730 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001731
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001732 /* powerdown AFE */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001733 status = set_iqm_af(state, false);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001734 if (status < 0)
1735 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001736
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001737 /* powerdown to OFDM mode */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001738 if (set_power_mode) {
1739 status = ctrl_power_mode(state, &power_mode);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001740 if (status < 0)
1741 goto error;
1742 }
1743error:
1744 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03001745 pr_err("Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001746 return status;
1747}
1748
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001749static int setoperation_mode(struct drxk_state *state,
1750 enum operation_mode o_mode)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001751{
1752 int status = 0;
1753
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03001754 dprintk(1, "\n");
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001755 /*
Oliver Endrissebc7de22011-07-03 13:49:44 -03001756 Stop and power down previous standard
1757 TODO investigate total power down instead of partial
1758 power down depending on "previous" standard.
1759 */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001760
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001761 /* disable HW lock indicator */
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03001762 status = write16(state, SCU_RAM_GPIO__A,
1763 SCU_RAM_GPIO_HW_LOCK_IND_DISABLE);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001764 if (status < 0)
1765 goto error;
1766
Mauro Carvalho Chehabf1b82972011-07-10 13:08:44 -03001767 /* Device is already at the required mode */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001768 if (state->m_operation_mode == o_mode)
Mauro Carvalho Chehabf1b82972011-07-10 13:08:44 -03001769 return 0;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001770
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001771 switch (state->m_operation_mode) {
Mauro Carvalho Chehabf1b82972011-07-10 13:08:44 -03001772 /* OM_NONE was added for start up */
1773 case OM_NONE:
1774 break;
1775 case OM_DVBT:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001776 status = mpegts_stop(state);
Mauro Carvalho Chehabf1b82972011-07-10 13:08:44 -03001777 if (status < 0)
1778 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001779 status = power_down_dvbt(state, true);
Mauro Carvalho Chehabf1b82972011-07-10 13:08:44 -03001780 if (status < 0)
1781 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001782 state->m_operation_mode = OM_NONE;
Mauro Carvalho Chehabf1b82972011-07-10 13:08:44 -03001783 break;
1784 case OM_QAM_ITU_A: /* fallthrough */
1785 case OM_QAM_ITU_C:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001786 status = mpegts_stop(state);
Mauro Carvalho Chehabf1b82972011-07-10 13:08:44 -03001787 if (status < 0)
1788 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001789 status = power_down_qam(state);
Mauro Carvalho Chehabf1b82972011-07-10 13:08:44 -03001790 if (status < 0)
1791 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001792 state->m_operation_mode = OM_NONE;
Mauro Carvalho Chehabf1b82972011-07-10 13:08:44 -03001793 break;
1794 case OM_QAM_ITU_B:
1795 default:
1796 status = -EINVAL;
1797 goto error;
1798 }
1799
1800 /*
1801 Power up new standard
1802 */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001803 switch (o_mode) {
Mauro Carvalho Chehabf1b82972011-07-10 13:08:44 -03001804 case OM_DVBT:
Mauro Carvalho Chehab48763e22011-12-09 08:53:36 -02001805 dprintk(1, ": DVB-T\n");
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001806 state->m_operation_mode = o_mode;
1807 status = set_dvbt_standard(state, o_mode);
Mauro Carvalho Chehabf1b82972011-07-10 13:08:44 -03001808 if (status < 0)
1809 goto error;
1810 break;
1811 case OM_QAM_ITU_A: /* fallthrough */
1812 case OM_QAM_ITU_C:
Mauro Carvalho Chehab48763e22011-12-09 08:53:36 -02001813 dprintk(1, ": DVB-C Annex %c\n",
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001814 (state->m_operation_mode == OM_QAM_ITU_A) ? 'A' : 'C');
1815 state->m_operation_mode = o_mode;
1816 status = set_qam_standard(state, o_mode);
Mauro Carvalho Chehabf1b82972011-07-10 13:08:44 -03001817 if (status < 0)
1818 goto error;
1819 break;
1820 case OM_QAM_ITU_B:
1821 default:
1822 status = -EINVAL;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001823 }
1824error:
1825 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03001826 pr_err("Error %d on %s\n", status, __func__);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001827 return status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001828}
1829
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001830static int start(struct drxk_state *state, s32 offset_freq,
1831 s32 intermediate_frequency)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001832{
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001833 int status = -EINVAL;
1834
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001835 u16 i_freqk_hz;
1836 s32 offsetk_hz = offset_freq / 1000;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001837
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03001838 dprintk(1, "\n");
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001839 if (state->m_drxk_state != DRXK_STOPPED &&
1840 state->m_drxk_state != DRXK_DTV_STARTED)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001841 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001842
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001843 state->m_b_mirror_freq_spect = (state->props.inversion == INVERSION_ON);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001844
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001845 if (intermediate_frequency < 0) {
1846 state->m_b_mirror_freq_spect = !state->m_b_mirror_freq_spect;
1847 intermediate_frequency = -intermediate_frequency;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001848 }
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001849
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001850 switch (state->m_operation_mode) {
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001851 case OM_QAM_ITU_A:
1852 case OM_QAM_ITU_C:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001853 i_freqk_hz = (intermediate_frequency / 1000);
1854 status = set_qam(state, i_freqk_hz, offsetk_hz);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001855 if (status < 0)
1856 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001857 state->m_drxk_state = DRXK_DTV_STARTED;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001858 break;
1859 case OM_DVBT:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001860 i_freqk_hz = (intermediate_frequency / 1000);
1861 status = mpegts_stop(state);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001862 if (status < 0)
1863 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001864 status = set_dvbt(state, i_freqk_hz, offsetk_hz);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001865 if (status < 0)
1866 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001867 status = dvbt_start(state);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001868 if (status < 0)
1869 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001870 state->m_drxk_state = DRXK_DTV_STARTED;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001871 break;
1872 default:
1873 break;
1874 }
1875error:
1876 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03001877 pr_err("Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001878 return status;
1879}
1880
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001881static int shut_down(struct drxk_state *state)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001882{
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03001883 dprintk(1, "\n");
1884
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001885 mpegts_stop(state);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001886 return 0;
1887}
1888
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001889static int get_lock_status(struct drxk_state *state, u32 *p_lock_status)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001890{
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001891 int status = -EINVAL;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001892
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03001893 dprintk(1, "\n");
1894
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001895 if (p_lock_status == NULL)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001896 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001897
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001898 *p_lock_status = NOT_LOCKED;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001899
1900 /* define the SCU command code */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001901 switch (state->m_operation_mode) {
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001902 case OM_QAM_ITU_A:
1903 case OM_QAM_ITU_B:
1904 case OM_QAM_ITU_C:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001905 status = get_qam_lock_status(state, p_lock_status);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001906 break;
1907 case OM_DVBT:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001908 status = get_dvbt_lock_status(state, p_lock_status);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001909 break;
1910 default:
1911 break;
1912 }
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001913error:
1914 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03001915 pr_err("Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001916 return status;
1917}
1918
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001919static int mpegts_start(struct drxk_state *state)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001920{
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001921 int status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001922
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001923 u16 fec_oc_snc_mode = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001924
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001925 /* Allow OC to sync again */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001926 status = read16(state, FEC_OC_SNC_MODE__A, &fec_oc_snc_mode);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001927 if (status < 0)
1928 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001929 fec_oc_snc_mode &= ~FEC_OC_SNC_MODE_SHUTDOWN__M;
1930 status = write16(state, FEC_OC_SNC_MODE__A, fec_oc_snc_mode);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001931 if (status < 0)
1932 goto error;
1933 status = write16(state, FEC_OC_SNC_UNLOCK__A, 1);
1934error:
1935 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03001936 pr_err("Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001937 return status;
1938}
1939
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001940static int mpegts_dto_init(struct drxk_state *state)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001941{
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001942 int status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001943
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03001944 dprintk(1, "\n");
1945
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001946 /* Rate integration settings */
1947 status = write16(state, FEC_OC_RCN_CTL_STEP_LO__A, 0x0000);
1948 if (status < 0)
1949 goto error;
1950 status = write16(state, FEC_OC_RCN_CTL_STEP_HI__A, 0x000C);
1951 if (status < 0)
1952 goto error;
1953 status = write16(state, FEC_OC_RCN_GAIN__A, 0x000A);
1954 if (status < 0)
1955 goto error;
1956 status = write16(state, FEC_OC_AVR_PARM_A__A, 0x0008);
1957 if (status < 0)
1958 goto error;
1959 status = write16(state, FEC_OC_AVR_PARM_B__A, 0x0006);
1960 if (status < 0)
1961 goto error;
1962 status = write16(state, FEC_OC_TMD_HI_MARGIN__A, 0x0680);
1963 if (status < 0)
1964 goto error;
1965 status = write16(state, FEC_OC_TMD_LO_MARGIN__A, 0x0080);
1966 if (status < 0)
1967 goto error;
1968 status = write16(state, FEC_OC_TMD_COUNT__A, 0x03F4);
1969 if (status < 0)
1970 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001971
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001972 /* Additional configuration */
1973 status = write16(state, FEC_OC_OCR_INVERT__A, 0);
1974 if (status < 0)
1975 goto error;
1976 status = write16(state, FEC_OC_SNC_LWM__A, 2);
1977 if (status < 0)
1978 goto error;
1979 status = write16(state, FEC_OC_SNC_HWM__A, 12);
1980error:
1981 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03001982 pr_err("Error %d on %s\n", status, __func__);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001983
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001984 return status;
1985}
1986
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001987static int mpegts_dto_setup(struct drxk_state *state,
1988 enum operation_mode o_mode)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001989{
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001990 int status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001991
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001992 u16 fec_oc_reg_mode = 0; /* FEC_OC_MODE register value */
1993 u16 fec_oc_reg_ipr_mode = 0; /* FEC_OC_IPR_MODE register value */
1994 u16 fec_oc_dto_mode = 0; /* FEC_OC_IPR_INVERT register value */
1995 u16 fec_oc_fct_mode = 0; /* FEC_OC_IPR_INVERT register value */
1996 u16 fec_oc_dto_period = 2; /* FEC_OC_IPR_INVERT register value */
1997 u16 fec_oc_dto_burst_len = 188; /* FEC_OC_IPR_INVERT register value */
1998 u32 fec_oc_rcn_ctl_rate = 0; /* FEC_OC_IPR_INVERT register value */
1999 u16 fec_oc_tmd_mode = 0;
2000 u16 fec_oc_tmd_int_upd_rate = 0;
2001 u32 max_bit_rate = 0;
2002 bool static_clk = false;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002003
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03002004 dprintk(1, "\n");
2005
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002006 /* Check insertion of the Reed-Solomon parity bytes */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002007 status = read16(state, FEC_OC_MODE__A, &fec_oc_reg_mode);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002008 if (status < 0)
2009 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002010 status = read16(state, FEC_OC_IPR_MODE__A, &fec_oc_reg_ipr_mode);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002011 if (status < 0)
2012 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002013 fec_oc_reg_mode &= (~FEC_OC_MODE_PARITY__M);
2014 fec_oc_reg_ipr_mode &= (~FEC_OC_IPR_MODE_MVAL_DIS_PAR__M);
Mauro Carvalho Chehab5a7f7b72014-09-03 15:23:57 -03002015 if (state->m_insert_rs_byte) {
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002016 /* enable parity symbol forward */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002017 fec_oc_reg_mode |= FEC_OC_MODE_PARITY__M;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002018 /* MVAL disable during parity bytes */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002019 fec_oc_reg_ipr_mode |= FEC_OC_IPR_MODE_MVAL_DIS_PAR__M;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002020 /* TS burst length to 204 */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002021 fec_oc_dto_burst_len = 204;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002022 }
2023
Jonathan McCrohan39c1cb22013-10-20 21:34:01 -03002024 /* Check serial or parallel output */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002025 fec_oc_reg_ipr_mode &= (~(FEC_OC_IPR_MODE_SERIAL__M));
Mauro Carvalho Chehab5a7f7b72014-09-03 15:23:57 -03002026 if (!state->m_enable_parallel) {
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002027 /* MPEG data output is serial -> set ipr_mode[0] */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002028 fec_oc_reg_ipr_mode |= FEC_OC_IPR_MODE_SERIAL__M;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002029 }
2030
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002031 switch (o_mode) {
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002032 case OM_DVBT:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002033 max_bit_rate = state->m_dvbt_bitrate;
2034 fec_oc_tmd_mode = 3;
2035 fec_oc_rcn_ctl_rate = 0xC00000;
2036 static_clk = state->m_dvbt_static_clk;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002037 break;
2038 case OM_QAM_ITU_A: /* fallthrough */
2039 case OM_QAM_ITU_C:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002040 fec_oc_tmd_mode = 0x0004;
2041 fec_oc_rcn_ctl_rate = 0xD2B4EE; /* good for >63 Mb/s */
2042 max_bit_rate = state->m_dvbc_bitrate;
2043 static_clk = state->m_dvbc_static_clk;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002044 break;
2045 default:
2046 status = -EINVAL;
2047 } /* switch (standard) */
2048 if (status < 0)
2049 goto error;
2050
2051 /* Configure DTO's */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002052 if (static_clk) {
2053 u32 bit_rate = 0;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002054
2055 /* Rational DTO for MCLK source (static MCLK rate),
2056 Dynamic DTO for optimal grouping
2057 (avoid intra-packet gaps),
2058 DTO offset enable to sync TS burst with MSTRT */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002059 fec_oc_dto_mode = (FEC_OC_DTO_MODE_DYNAMIC__M |
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002060 FEC_OC_DTO_MODE_OFFSET_ENABLE__M);
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002061 fec_oc_fct_mode = (FEC_OC_FCT_MODE_RAT_ENA__M |
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002062 FEC_OC_FCT_MODE_VIRT_ENA__M);
2063
2064 /* Check user defined bitrate */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002065 bit_rate = max_bit_rate;
2066 if (bit_rate > 75900000UL) { /* max is 75.9 Mb/s */
2067 bit_rate = 75900000UL;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002068 }
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002069 /* Rational DTO period:
2070 dto_period = (Fsys / bitrate) - 2
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002071
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002072 result should be floored,
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002073 to make sure >= requested bitrate
2074 */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002075 fec_oc_dto_period = (u16) (((state->m_sys_clock_freq)
2076 * 1000) / bit_rate);
2077 if (fec_oc_dto_period <= 2)
2078 fec_oc_dto_period = 0;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002079 else
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002080 fec_oc_dto_period -= 2;
2081 fec_oc_tmd_int_upd_rate = 8;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002082 } else {
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002083 /* (commonAttr->static_clk == false) => dynamic mode */
2084 fec_oc_dto_mode = FEC_OC_DTO_MODE_DYNAMIC__M;
2085 fec_oc_fct_mode = FEC_OC_FCT_MODE__PRE;
2086 fec_oc_tmd_int_upd_rate = 5;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002087 }
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002088
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002089 /* Write appropriate registers with requested configuration */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002090 status = write16(state, FEC_OC_DTO_BURST_LEN__A, fec_oc_dto_burst_len);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002091 if (status < 0)
2092 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002093 status = write16(state, FEC_OC_DTO_PERIOD__A, fec_oc_dto_period);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002094 if (status < 0)
2095 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002096 status = write16(state, FEC_OC_DTO_MODE__A, fec_oc_dto_mode);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002097 if (status < 0)
2098 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002099 status = write16(state, FEC_OC_FCT_MODE__A, fec_oc_fct_mode);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002100 if (status < 0)
2101 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002102 status = write16(state, FEC_OC_MODE__A, fec_oc_reg_mode);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002103 if (status < 0)
2104 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002105 status = write16(state, FEC_OC_IPR_MODE__A, fec_oc_reg_ipr_mode);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002106 if (status < 0)
2107 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002108
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002109 /* Rate integration settings */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002110 status = write32(state, FEC_OC_RCN_CTL_RATE_LO__A, fec_oc_rcn_ctl_rate);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002111 if (status < 0)
2112 goto error;
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03002113 status = write16(state, FEC_OC_TMD_INT_UPD_RATE__A,
2114 fec_oc_tmd_int_upd_rate);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002115 if (status < 0)
2116 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002117 status = write16(state, FEC_OC_TMD_MODE__A, fec_oc_tmd_mode);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002118error:
2119 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03002120 pr_err("Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002121 return status;
2122}
2123
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002124static int mpegts_configure_polarity(struct drxk_state *state)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002125{
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002126 u16 fec_oc_reg_ipr_invert = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002127
2128 /* Data mask for the output data byte */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002129 u16 invert_data_mask =
Oliver Endrissebc7de22011-07-03 13:49:44 -03002130 FEC_OC_IPR_INVERT_MD7__M | FEC_OC_IPR_INVERT_MD6__M |
2131 FEC_OC_IPR_INVERT_MD5__M | FEC_OC_IPR_INVERT_MD4__M |
2132 FEC_OC_IPR_INVERT_MD3__M | FEC_OC_IPR_INVERT_MD2__M |
2133 FEC_OC_IPR_INVERT_MD1__M | FEC_OC_IPR_INVERT_MD0__M;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002134
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002135 dprintk(1, "\n");
2136
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002137 /* Control selective inversion of output bits */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002138 fec_oc_reg_ipr_invert &= (~(invert_data_mask));
Mauro Carvalho Chehab5a7f7b72014-09-03 15:23:57 -03002139 if (state->m_invert_data)
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002140 fec_oc_reg_ipr_invert |= invert_data_mask;
2141 fec_oc_reg_ipr_invert &= (~(FEC_OC_IPR_INVERT_MERR__M));
Mauro Carvalho Chehab5a7f7b72014-09-03 15:23:57 -03002142 if (state->m_invert_err)
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002143 fec_oc_reg_ipr_invert |= FEC_OC_IPR_INVERT_MERR__M;
2144 fec_oc_reg_ipr_invert &= (~(FEC_OC_IPR_INVERT_MSTRT__M));
Mauro Carvalho Chehab5a7f7b72014-09-03 15:23:57 -03002145 if (state->m_invert_str)
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002146 fec_oc_reg_ipr_invert |= FEC_OC_IPR_INVERT_MSTRT__M;
2147 fec_oc_reg_ipr_invert &= (~(FEC_OC_IPR_INVERT_MVAL__M));
Mauro Carvalho Chehab5a7f7b72014-09-03 15:23:57 -03002148 if (state->m_invert_val)
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002149 fec_oc_reg_ipr_invert |= FEC_OC_IPR_INVERT_MVAL__M;
2150 fec_oc_reg_ipr_invert &= (~(FEC_OC_IPR_INVERT_MCLK__M));
Mauro Carvalho Chehab5a7f7b72014-09-03 15:23:57 -03002151 if (state->m_invert_clk)
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002152 fec_oc_reg_ipr_invert |= FEC_OC_IPR_INVERT_MCLK__M;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002153
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002154 return write16(state, FEC_OC_IPR_INVERT__A, fec_oc_reg_ipr_invert);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002155}
2156
2157#define SCU_RAM_AGC_KI_INV_RF_POL__M 0x4000
2158
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002159static int set_agc_rf(struct drxk_state *state,
2160 struct s_cfg_agc *p_agc_cfg, bool is_dtv)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002161{
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002162 int status = -EINVAL;
2163 u16 data = 0;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002164 struct s_cfg_agc *p_if_agc_settings;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002165
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03002166 dprintk(1, "\n");
2167
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002168 if (p_agc_cfg == NULL)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002169 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002170
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002171 switch (p_agc_cfg->ctrl_mode) {
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002172 case DRXK_AGC_CTRL_AUTO:
2173 /* Enable RF AGC DAC */
2174 status = read16(state, IQM_AF_STDBY__A, &data);
2175 if (status < 0)
2176 goto error;
2177 data &= ~IQM_AF_STDBY_STDBY_TAGC_RF_STANDBY;
2178 status = write16(state, IQM_AF_STDBY__A, data);
2179 if (status < 0)
2180 goto error;
2181 status = read16(state, SCU_RAM_AGC_CONFIG__A, &data);
2182 if (status < 0)
2183 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002184
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002185 /* Enable SCU RF AGC loop */
2186 data &= ~SCU_RAM_AGC_CONFIG_DISABLE_RF_AGC__M;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002187
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002188 /* Polarity */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002189 if (state->m_rf_agc_pol)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002190 data |= SCU_RAM_AGC_CONFIG_INV_RF_POL__M;
2191 else
2192 data &= ~SCU_RAM_AGC_CONFIG_INV_RF_POL__M;
2193 status = write16(state, SCU_RAM_AGC_CONFIG__A, data);
2194 if (status < 0)
2195 goto error;
2196
2197 /* Set speed (using complementary reduction value) */
2198 status = read16(state, SCU_RAM_AGC_KI_RED__A, &data);
2199 if (status < 0)
2200 goto error;
2201
2202 data &= ~SCU_RAM_AGC_KI_RED_RAGC_RED__M;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002203 data |= (~(p_agc_cfg->speed <<
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002204 SCU_RAM_AGC_KI_RED_RAGC_RED__B)
2205 & SCU_RAM_AGC_KI_RED_RAGC_RED__M);
2206
2207 status = write16(state, SCU_RAM_AGC_KI_RED__A, data);
2208 if (status < 0)
2209 goto error;
2210
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002211 if (is_dvbt(state))
2212 p_if_agc_settings = &state->m_dvbt_if_agc_cfg;
2213 else if (is_qam(state))
2214 p_if_agc_settings = &state->m_qam_if_agc_cfg;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002215 else
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002216 p_if_agc_settings = &state->m_atv_if_agc_cfg;
2217 if (p_if_agc_settings == NULL) {
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002218 status = -EINVAL;
2219 goto error;
2220 }
2221
2222 /* Set TOP, only if IF-AGC is in AUTO mode */
Mauro Carvalho Chehab89fffac2014-09-03 19:11:45 -03002223 if (p_if_agc_settings->ctrl_mode == DRXK_AGC_CTRL_AUTO) {
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03002224 status = write16(state,
2225 SCU_RAM_AGC_IF_IACCU_HI_TGT_MAX__A,
2226 p_agc_cfg->top);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03002227 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002228 goto error;
Mauro Carvalho Chehab89fffac2014-09-03 19:11:45 -03002229 }
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002230
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002231 /* Cut-Off current */
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03002232 status = write16(state, SCU_RAM_AGC_RF_IACCU_HI_CO__A,
2233 p_agc_cfg->cut_off_current);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002234 if (status < 0)
2235 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002236
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002237 /* Max. output level */
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03002238 status = write16(state, SCU_RAM_AGC_RF_MAX__A,
2239 p_agc_cfg->max_output_level);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002240 if (status < 0)
2241 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002242
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002243 break;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002244
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002245 case DRXK_AGC_CTRL_USER:
2246 /* Enable RF AGC DAC */
2247 status = read16(state, IQM_AF_STDBY__A, &data);
2248 if (status < 0)
2249 goto error;
2250 data &= ~IQM_AF_STDBY_STDBY_TAGC_RF_STANDBY;
2251 status = write16(state, IQM_AF_STDBY__A, data);
2252 if (status < 0)
2253 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002254
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002255 /* Disable SCU RF AGC loop */
2256 status = read16(state, SCU_RAM_AGC_CONFIG__A, &data);
2257 if (status < 0)
2258 goto error;
2259 data |= SCU_RAM_AGC_CONFIG_DISABLE_RF_AGC__M;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002260 if (state->m_rf_agc_pol)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002261 data |= SCU_RAM_AGC_CONFIG_INV_RF_POL__M;
2262 else
2263 data &= ~SCU_RAM_AGC_CONFIG_INV_RF_POL__M;
2264 status = write16(state, SCU_RAM_AGC_CONFIG__A, data);
2265 if (status < 0)
2266 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002267
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002268 /* SCU c.o.c. to 0, enabling full control range */
2269 status = write16(state, SCU_RAM_AGC_RF_IACCU_HI_CO__A, 0);
2270 if (status < 0)
2271 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002272
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002273 /* Write value to output pin */
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03002274 status = write16(state, SCU_RAM_AGC_RF_IACCU_HI__A,
2275 p_agc_cfg->output_level);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002276 if (status < 0)
2277 goto error;
2278 break;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002279
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002280 case DRXK_AGC_CTRL_OFF:
2281 /* Disable RF AGC DAC */
2282 status = read16(state, IQM_AF_STDBY__A, &data);
2283 if (status < 0)
2284 goto error;
2285 data |= IQM_AF_STDBY_STDBY_TAGC_RF_STANDBY;
2286 status = write16(state, IQM_AF_STDBY__A, data);
2287 if (status < 0)
2288 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002289
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002290 /* Disable SCU RF AGC loop */
2291 status = read16(state, SCU_RAM_AGC_CONFIG__A, &data);
2292 if (status < 0)
2293 goto error;
2294 data |= SCU_RAM_AGC_CONFIG_DISABLE_RF_AGC__M;
2295 status = write16(state, SCU_RAM_AGC_CONFIG__A, data);
2296 if (status < 0)
2297 goto error;
2298 break;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002299
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002300 default:
2301 status = -EINVAL;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002302
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002303 }
2304error:
2305 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03002306 pr_err("Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002307 return status;
2308}
2309
2310#define SCU_RAM_AGC_KI_INV_IF_POL__M 0x2000
2311
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002312static int set_agc_if(struct drxk_state *state,
2313 struct s_cfg_agc *p_agc_cfg, bool is_dtv)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002314{
2315 u16 data = 0;
2316 int status = 0;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002317 struct s_cfg_agc *p_rf_agc_settings;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002318
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03002319 dprintk(1, "\n");
2320
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002321 switch (p_agc_cfg->ctrl_mode) {
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002322 case DRXK_AGC_CTRL_AUTO:
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002323
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002324 /* Enable IF AGC DAC */
2325 status = read16(state, IQM_AF_STDBY__A, &data);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03002326 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002327 goto error;
2328 data &= ~IQM_AF_STDBY_STDBY_TAGC_IF_STANDBY;
2329 status = write16(state, IQM_AF_STDBY__A, data);
2330 if (status < 0)
2331 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002332
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002333 status = read16(state, SCU_RAM_AGC_CONFIG__A, &data);
2334 if (status < 0)
2335 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002336
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002337 /* Enable SCU IF AGC loop */
2338 data &= ~SCU_RAM_AGC_CONFIG_DISABLE_IF_AGC__M;
2339
2340 /* Polarity */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002341 if (state->m_if_agc_pol)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002342 data |= SCU_RAM_AGC_CONFIG_INV_IF_POL__M;
2343 else
2344 data &= ~SCU_RAM_AGC_CONFIG_INV_IF_POL__M;
2345 status = write16(state, SCU_RAM_AGC_CONFIG__A, data);
2346 if (status < 0)
2347 goto error;
2348
2349 /* Set speed (using complementary reduction value) */
2350 status = read16(state, SCU_RAM_AGC_KI_RED__A, &data);
2351 if (status < 0)
2352 goto error;
2353 data &= ~SCU_RAM_AGC_KI_RED_IAGC_RED__M;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002354 data |= (~(p_agc_cfg->speed <<
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002355 SCU_RAM_AGC_KI_RED_IAGC_RED__B)
2356 & SCU_RAM_AGC_KI_RED_IAGC_RED__M);
2357
2358 status = write16(state, SCU_RAM_AGC_KI_RED__A, data);
2359 if (status < 0)
2360 goto error;
2361
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002362 if (is_qam(state))
2363 p_rf_agc_settings = &state->m_qam_rf_agc_cfg;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002364 else
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002365 p_rf_agc_settings = &state->m_atv_rf_agc_cfg;
2366 if (p_rf_agc_settings == NULL)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002367 return -1;
2368 /* Restore TOP */
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03002369 status = write16(state, SCU_RAM_AGC_IF_IACCU_HI_TGT_MAX__A,
2370 p_rf_agc_settings->top);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002371 if (status < 0)
2372 goto error;
2373 break;
2374
2375 case DRXK_AGC_CTRL_USER:
2376
2377 /* Enable IF AGC DAC */
2378 status = read16(state, IQM_AF_STDBY__A, &data);
2379 if (status < 0)
2380 goto error;
2381 data &= ~IQM_AF_STDBY_STDBY_TAGC_IF_STANDBY;
2382 status = write16(state, IQM_AF_STDBY__A, data);
2383 if (status < 0)
2384 goto error;
2385
2386 status = read16(state, SCU_RAM_AGC_CONFIG__A, &data);
2387 if (status < 0)
2388 goto error;
2389
2390 /* Disable SCU IF AGC loop */
2391 data |= SCU_RAM_AGC_CONFIG_DISABLE_IF_AGC__M;
2392
2393 /* Polarity */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002394 if (state->m_if_agc_pol)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002395 data |= SCU_RAM_AGC_CONFIG_INV_IF_POL__M;
2396 else
2397 data &= ~SCU_RAM_AGC_CONFIG_INV_IF_POL__M;
2398 status = write16(state, SCU_RAM_AGC_CONFIG__A, data);
2399 if (status < 0)
2400 goto error;
2401
2402 /* Write value to output pin */
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03002403 status = write16(state, SCU_RAM_AGC_IF_IACCU_HI_TGT_MAX__A,
2404 p_agc_cfg->output_level);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002405 if (status < 0)
2406 goto error;
2407 break;
2408
2409 case DRXK_AGC_CTRL_OFF:
2410
2411 /* Disable If AGC DAC */
2412 status = read16(state, IQM_AF_STDBY__A, &data);
2413 if (status < 0)
2414 goto error;
2415 data |= IQM_AF_STDBY_STDBY_TAGC_IF_STANDBY;
2416 status = write16(state, IQM_AF_STDBY__A, data);
2417 if (status < 0)
2418 goto error;
2419
2420 /* Disable SCU IF AGC loop */
2421 status = read16(state, SCU_RAM_AGC_CONFIG__A, &data);
2422 if (status < 0)
2423 goto error;
2424 data |= SCU_RAM_AGC_CONFIG_DISABLE_IF_AGC__M;
2425 status = write16(state, SCU_RAM_AGC_CONFIG__A, data);
2426 if (status < 0)
2427 goto error;
2428 break;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002429 } /* switch (agcSettingsIf->ctrl_mode) */
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002430
2431 /* always set the top to support
2432 configurations without if-loop */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002433 status = write16(state, SCU_RAM_AGC_INGAIN_TGT_MIN__A, p_agc_cfg->top);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002434error:
2435 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03002436 pr_err("Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002437 return status;
2438}
2439
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002440static int get_qam_signal_to_noise(struct drxk_state *state,
2441 s32 *p_signal_to_noise)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002442{
2443 int status = 0;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002444 u16 qam_sl_err_power = 0; /* accum. error between
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002445 raw and sliced symbols */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002446 u32 qam_sl_sig_power = 0; /* used for MER, depends of
Mauro Carvalho Chehabed5452a2011-12-26 09:57:11 -03002447 QAM modulation */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002448 u32 qam_sl_mer = 0; /* QAM MER */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002449
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03002450 dprintk(1, "\n");
2451
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002452 /* MER calculation */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002453
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002454 /* get the register value needed for MER */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002455 status = read16(state, QAM_SL_ERR_POWER__A, &qam_sl_err_power);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002456 if (status < 0) {
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03002457 pr_err("Error %d on %s\n", status, __func__);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002458 return -EINVAL;
2459 }
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002460
Mauro Carvalho Chehabed5452a2011-12-26 09:57:11 -03002461 switch (state->props.modulation) {
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002462 case QAM_16:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002463 qam_sl_sig_power = DRXK_QAM_SL_SIG_POWER_QAM16 << 2;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002464 break;
2465 case QAM_32:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002466 qam_sl_sig_power = DRXK_QAM_SL_SIG_POWER_QAM32 << 2;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002467 break;
2468 case QAM_64:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002469 qam_sl_sig_power = DRXK_QAM_SL_SIG_POWER_QAM64 << 2;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002470 break;
2471 case QAM_128:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002472 qam_sl_sig_power = DRXK_QAM_SL_SIG_POWER_QAM128 << 2;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002473 break;
2474 default:
2475 case QAM_256:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002476 qam_sl_sig_power = DRXK_QAM_SL_SIG_POWER_QAM256 << 2;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002477 break;
2478 }
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002479
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002480 if (qam_sl_err_power > 0) {
2481 qam_sl_mer = log10times100(qam_sl_sig_power) -
2482 log10times100((u32) qam_sl_err_power);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002483 }
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002484 *p_signal_to_noise = qam_sl_mer;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002485
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002486 return status;
2487}
2488
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002489static int get_dvbt_signal_to_noise(struct drxk_state *state,
2490 s32 *p_signal_to_noise)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002491{
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002492 int status;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002493 u16 reg_data = 0;
2494 u32 eq_reg_td_sqr_err_i = 0;
2495 u32 eq_reg_td_sqr_err_q = 0;
2496 u16 eq_reg_td_sqr_err_exp = 0;
2497 u16 eq_reg_td_tps_pwr_ofs = 0;
2498 u16 eq_reg_td_req_smb_cnt = 0;
2499 u32 tps_cnt = 0;
2500 u32 sqr_err_iq = 0;
Oliver Endrissebc7de22011-07-03 13:49:44 -03002501 u32 a = 0;
2502 u32 b = 0;
2503 u32 c = 0;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002504 u32 i_mer = 0;
2505 u16 transmission_params = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002506
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03002507 dprintk(1, "\n");
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002508
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03002509 status = read16(state, OFDM_EQ_TOP_TD_TPS_PWR_OFS__A,
2510 &eq_reg_td_tps_pwr_ofs);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002511 if (status < 0)
2512 goto error;
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03002513 status = read16(state, OFDM_EQ_TOP_TD_REQ_SMB_CNT__A,
2514 &eq_reg_td_req_smb_cnt);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002515 if (status < 0)
2516 goto error;
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03002517 status = read16(state, OFDM_EQ_TOP_TD_SQR_ERR_EXP__A,
2518 &eq_reg_td_sqr_err_exp);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002519 if (status < 0)
2520 goto error;
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03002521 status = read16(state, OFDM_EQ_TOP_TD_SQR_ERR_I__A,
2522 &reg_data);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002523 if (status < 0)
2524 goto error;
2525 /* Extend SQR_ERR_I operational range */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002526 eq_reg_td_sqr_err_i = (u32) reg_data;
2527 if ((eq_reg_td_sqr_err_exp > 11) &&
2528 (eq_reg_td_sqr_err_i < 0x00000FFFUL)) {
2529 eq_reg_td_sqr_err_i += 0x00010000UL;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002530 }
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002531 status = read16(state, OFDM_EQ_TOP_TD_SQR_ERR_Q__A, &reg_data);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002532 if (status < 0)
2533 goto error;
2534 /* Extend SQR_ERR_Q operational range */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002535 eq_reg_td_sqr_err_q = (u32) reg_data;
2536 if ((eq_reg_td_sqr_err_exp > 11) &&
2537 (eq_reg_td_sqr_err_q < 0x00000FFFUL))
2538 eq_reg_td_sqr_err_q += 0x00010000UL;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002539
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03002540 status = read16(state, OFDM_SC_RA_RAM_OP_PARAM__A,
2541 &transmission_params);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002542 if (status < 0)
2543 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002544
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002545 /* Check input data for MER */
2546
2547 /* MER calculation (in 0.1 dB) without math.h */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002548 if ((eq_reg_td_tps_pwr_ofs == 0) || (eq_reg_td_req_smb_cnt == 0))
2549 i_mer = 0;
2550 else if ((eq_reg_td_sqr_err_i + eq_reg_td_sqr_err_q) == 0) {
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002551 /* No error at all, this must be the HW reset value
2552 * Apparently no first measurement yet
2553 * Set MER to 0.0 */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002554 i_mer = 0;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002555 } else {
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002556 sqr_err_iq = (eq_reg_td_sqr_err_i + eq_reg_td_sqr_err_q) <<
2557 eq_reg_td_sqr_err_exp;
2558 if ((transmission_params &
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002559 OFDM_SC_RA_RAM_OP_PARAM_MODE__M)
2560 == OFDM_SC_RA_RAM_OP_PARAM_MODE_2K)
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002561 tps_cnt = 17;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002562 else
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002563 tps_cnt = 68;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002564
2565 /* IMER = 100 * log10 (x)
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002566 where x = (eq_reg_td_tps_pwr_ofs^2 *
2567 eq_reg_td_req_smb_cnt * tps_cnt)/sqr_err_iq
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002568
2569 => IMER = a + b -c
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002570 where a = 100 * log10 (eq_reg_td_tps_pwr_ofs^2)
2571 b = 100 * log10 (eq_reg_td_req_smb_cnt * tps_cnt)
2572 c = 100 * log10 (sqr_err_iq)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002573 */
2574
2575 /* log(x) x = 9bits * 9bits->18 bits */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002576 a = log10times100(eq_reg_td_tps_pwr_ofs *
2577 eq_reg_td_tps_pwr_ofs);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002578 /* log(x) x = 16bits * 7bits->23 bits */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002579 b = log10times100(eq_reg_td_req_smb_cnt * tps_cnt);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002580 /* log(x) x = (16bits + 16bits) << 15 ->32 bits */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002581 c = log10times100(sqr_err_iq);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002582
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002583 i_mer = a + b - c;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002584 }
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002585 *p_signal_to_noise = i_mer;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002586
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002587error:
2588 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03002589 pr_err("Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002590 return status;
2591}
2592
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002593static int get_signal_to_noise(struct drxk_state *state, s32 *p_signal_to_noise)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002594{
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03002595 dprintk(1, "\n");
2596
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002597 *p_signal_to_noise = 0;
2598 switch (state->m_operation_mode) {
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002599 case OM_DVBT:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002600 return get_dvbt_signal_to_noise(state, p_signal_to_noise);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002601 case OM_QAM_ITU_A:
2602 case OM_QAM_ITU_C:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002603 return get_qam_signal_to_noise(state, p_signal_to_noise);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002604 default:
2605 break;
2606 }
2607 return 0;
2608}
2609
2610#if 0
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002611static int get_dvbt_quality(struct drxk_state *state, s32 *p_quality)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002612{
2613 /* SNR Values for quasi errorfree reception rom Nordig 2.2 */
2614 int status = 0;
2615
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03002616 dprintk(1, "\n");
2617
Oliver Endrissebc7de22011-07-03 13:49:44 -03002618 static s32 QE_SN[] = {
2619 51, /* QPSK 1/2 */
2620 69, /* QPSK 2/3 */
2621 79, /* QPSK 3/4 */
2622 89, /* QPSK 5/6 */
2623 97, /* QPSK 7/8 */
2624 108, /* 16-QAM 1/2 */
2625 131, /* 16-QAM 2/3 */
2626 146, /* 16-QAM 3/4 */
2627 156, /* 16-QAM 5/6 */
2628 160, /* 16-QAM 7/8 */
2629 165, /* 64-QAM 1/2 */
2630 187, /* 64-QAM 2/3 */
2631 202, /* 64-QAM 3/4 */
2632 216, /* 64-QAM 5/6 */
2633 225, /* 64-QAM 7/8 */
2634 };
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002635
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002636 *p_quality = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002637
2638 do {
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002639 s32 signal_to_noise = 0;
2640 u16 constellation = 0;
2641 u16 code_rate = 0;
2642 u32 signal_to_noise_rel;
2643 u32 ber_quality;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002644
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002645 status = get_dvbt_signal_to_noise(state, &signal_to_noise);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03002646 if (status < 0)
2647 break;
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03002648 status = read16(state, OFDM_EQ_TOP_TD_TPS_CONST__A,
2649 &constellation);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03002650 if (status < 0)
2651 break;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002652 constellation &= OFDM_EQ_TOP_TD_TPS_CONST__M;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002653
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03002654 status = read16(state, OFDM_EQ_TOP_TD_TPS_CODE_HP__A,
2655 &code_rate);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03002656 if (status < 0)
2657 break;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002658 code_rate &= OFDM_EQ_TOP_TD_TPS_CODE_HP__M;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002659
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002660 if (constellation > OFDM_EQ_TOP_TD_TPS_CONST_64QAM ||
2661 code_rate > OFDM_EQ_TOP_TD_TPS_CODE_LP_7_8)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002662 break;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002663 signal_to_noise_rel = signal_to_noise -
2664 QE_SN[constellation * 5 + code_rate];
2665 ber_quality = 100;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002666
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002667 if (signal_to_noise_rel < -70)
2668 *p_quality = 0;
2669 else if (signal_to_noise_rel < 30)
2670 *p_quality = ((signal_to_noise_rel + 70) *
2671 ber_quality) / 100;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002672 else
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002673 *p_quality = ber_quality;
Oliver Endrissebc7de22011-07-03 13:49:44 -03002674 } while (0);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002675 return 0;
2676};
2677
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002678static int get_dvbc_quality(struct drxk_state *state, s32 *p_quality)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002679{
2680 int status = 0;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002681 *p_quality = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002682
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03002683 dprintk(1, "\n");
2684
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002685 do {
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002686 u32 signal_to_noise = 0;
2687 u32 ber_quality = 100;
2688 u32 signal_to_noise_rel = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002689
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002690 status = get_qam_signal_to_noise(state, &signal_to_noise);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03002691 if (status < 0)
2692 break;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002693
Mauro Carvalho Chehabed5452a2011-12-26 09:57:11 -03002694 switch (state->props.modulation) {
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002695 case QAM_16:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002696 signal_to_noise_rel = signal_to_noise - 200;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002697 break;
2698 case QAM_32:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002699 signal_to_noise_rel = signal_to_noise - 230;
Oliver Endrissebc7de22011-07-03 13:49:44 -03002700 break; /* Not in NorDig */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002701 case QAM_64:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002702 signal_to_noise_rel = signal_to_noise - 260;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002703 break;
2704 case QAM_128:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002705 signal_to_noise_rel = signal_to_noise - 290;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002706 break;
2707 default:
2708 case QAM_256:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002709 signal_to_noise_rel = signal_to_noise - 320;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002710 break;
2711 }
2712
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002713 if (signal_to_noise_rel < -70)
2714 *p_quality = 0;
2715 else if (signal_to_noise_rel < 30)
2716 *p_quality = ((signal_to_noise_rel + 70) *
2717 ber_quality) / 100;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002718 else
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002719 *p_quality = ber_quality;
Oliver Endrissebc7de22011-07-03 13:49:44 -03002720 } while (0);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002721
2722 return status;
2723}
2724
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002725static int get_quality(struct drxk_state *state, s32 *p_quality)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002726{
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03002727 dprintk(1, "\n");
2728
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002729 switch (state->m_operation_mode) {
Oliver Endrissebc7de22011-07-03 13:49:44 -03002730 case OM_DVBT:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002731 return get_dvbt_quality(state, p_quality);
Oliver Endrissebc7de22011-07-03 13:49:44 -03002732 case OM_QAM_ITU_A:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002733 return get_dvbc_quality(state, p_quality);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002734 default:
2735 break;
2736 }
2737
2738 return 0;
2739}
2740#endif
2741
2742/* Free data ram in SIO HI */
2743#define SIO_HI_RA_RAM_USR_BEGIN__A 0x420040
2744#define SIO_HI_RA_RAM_USR_END__A 0x420060
2745
2746#define DRXK_HI_ATOMIC_BUF_START (SIO_HI_RA_RAM_USR_BEGIN__A)
2747#define DRXK_HI_ATOMIC_BUF_END (SIO_HI_RA_RAM_USR_BEGIN__A + 7)
2748#define DRXK_HI_ATOMIC_READ SIO_HI_RA_RAM_PAR_3_ACP_RW_READ
2749#define DRXK_HI_ATOMIC_WRITE SIO_HI_RA_RAM_PAR_3_ACP_RW_WRITE
2750
2751#define DRXDAP_FASI_ADDR2BLOCK(addr) (((addr) >> 22) & 0x3F)
2752#define DRXDAP_FASI_ADDR2BANK(addr) (((addr) >> 16) & 0x3F)
2753#define DRXDAP_FASI_ADDR2OFFSET(addr) ((addr) & 0x7FFF)
2754
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002755static int ConfigureI2CBridge(struct drxk_state *state, bool b_enable_bridge)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002756{
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002757 int status = -EINVAL;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002758
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03002759 dprintk(1, "\n");
2760
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002761 if (state->m_drxk_state == DRXK_UNINITIALIZED)
Mauro Carvalho Chehab704a28e2012-06-29 15:45:04 -03002762 return 0;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002763 if (state->m_drxk_state == DRXK_POWERED_DOWN)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002764 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002765
Mauro Carvalho Chehabf1fe1b72011-07-09 21:59:33 -03002766 if (state->no_i2c_bridge)
2767 return 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002768
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03002769 status = write16(state, SIO_HI_RA_RAM_PAR_1__A,
2770 SIO_HI_RA_RAM_PAR_1_PAR1_SEC_KEY);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002771 if (status < 0)
2772 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002773 if (b_enable_bridge) {
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03002774 status = write16(state, SIO_HI_RA_RAM_PAR_2__A,
2775 SIO_HI_RA_RAM_PAR_2_BRD_CFG_CLOSED);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03002776 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002777 goto error;
2778 } else {
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03002779 status = write16(state, SIO_HI_RA_RAM_PAR_2__A,
2780 SIO_HI_RA_RAM_PAR_2_BRD_CFG_OPEN);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002781 if (status < 0)
2782 goto error;
2783 }
2784
Hans Verkuilb1cf2012013-10-04 11:01:45 -03002785 status = hi_command(state, SIO_HI_RA_RAM_CMD_BRDCTRL, NULL);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002786
2787error:
2788 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03002789 pr_err("Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002790 return status;
2791}
2792
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002793static int set_pre_saw(struct drxk_state *state,
2794 struct s_cfg_pre_saw *p_pre_saw_cfg)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002795{
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002796 int status = -EINVAL;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002797
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03002798 dprintk(1, "\n");
2799
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002800 if ((p_pre_saw_cfg == NULL)
2801 || (p_pre_saw_cfg->reference > IQM_AF_PDREF__M))
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002802 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002803
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002804 status = write16(state, IQM_AF_PDREF__A, p_pre_saw_cfg->reference);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002805error:
2806 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03002807 pr_err("Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002808 return status;
2809}
2810
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002811static int bl_direct_cmd(struct drxk_state *state, u32 target_addr,
2812 u16 rom_offset, u16 nr_of_elements, u32 time_out)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002813{
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002814 u16 bl_status = 0;
2815 u16 offset = (u16) ((target_addr >> 0) & 0x00FFFF);
2816 u16 blockbank = (u16) ((target_addr >> 16) & 0x000FFF);
Oliver Endrissebc7de22011-07-03 13:49:44 -03002817 int status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002818 unsigned long end;
2819
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03002820 dprintk(1, "\n");
2821
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002822 mutex_lock(&state->mutex);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002823 status = write16(state, SIO_BL_MODE__A, SIO_BL_MODE_DIRECT);
2824 if (status < 0)
2825 goto error;
2826 status = write16(state, SIO_BL_TGT_HDR__A, blockbank);
2827 if (status < 0)
2828 goto error;
2829 status = write16(state, SIO_BL_TGT_ADDR__A, offset);
2830 if (status < 0)
2831 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002832 status = write16(state, SIO_BL_SRC_ADDR__A, rom_offset);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002833 if (status < 0)
2834 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002835 status = write16(state, SIO_BL_SRC_LEN__A, nr_of_elements);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002836 if (status < 0)
2837 goto error;
2838 status = write16(state, SIO_BL_ENABLE__A, SIO_BL_ENABLE_ON);
2839 if (status < 0)
2840 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002841
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002842 end = jiffies + msecs_to_jiffies(time_out);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002843 do {
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002844 status = read16(state, SIO_BL_STATUS__A, &bl_status);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002845 if (status < 0)
2846 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002847 } while ((bl_status == 0x1) && time_is_after_jiffies(end));
2848 if (bl_status == 0x1) {
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03002849 pr_err("SIO not ready\n");
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002850 status = -EINVAL;
2851 goto error2;
2852 }
2853error:
2854 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03002855 pr_err("Error %d on %s\n", status, __func__);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002856error2:
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002857 mutex_unlock(&state->mutex);
2858 return status;
2859
2860}
2861
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002862static int adc_sync_measurement(struct drxk_state *state, u16 *count)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002863{
2864 u16 data = 0;
2865 int status;
2866
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03002867 dprintk(1, "\n");
2868
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002869 /* start measurement */
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002870 status = write16(state, IQM_AF_COMM_EXEC__A, IQM_AF_COMM_EXEC_ACTIVE);
2871 if (status < 0)
2872 goto error;
2873 status = write16(state, IQM_AF_START_LOCK__A, 1);
2874 if (status < 0)
2875 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002876
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002877 *count = 0;
2878 status = read16(state, IQM_AF_PHASE0__A, &data);
2879 if (status < 0)
2880 goto error;
2881 if (data == 127)
2882 *count = *count + 1;
2883 status = read16(state, IQM_AF_PHASE1__A, &data);
2884 if (status < 0)
2885 goto error;
2886 if (data == 127)
2887 *count = *count + 1;
2888 status = read16(state, IQM_AF_PHASE2__A, &data);
2889 if (status < 0)
2890 goto error;
2891 if (data == 127)
2892 *count = *count + 1;
2893
2894error:
2895 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03002896 pr_err("Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002897 return status;
2898}
2899
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002900static int adc_synchronization(struct drxk_state *state)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002901{
2902 u16 count = 0;
2903 int status;
2904
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03002905 dprintk(1, "\n");
2906
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002907 status = adc_sync_measurement(state, &count);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002908 if (status < 0)
2909 goto error;
2910
2911 if (count == 1) {
Jonathan McCrohan39c1cb22013-10-20 21:34:01 -03002912 /* Try sampling on a different edge */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002913 u16 clk_neg = 0;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002914
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002915 status = read16(state, IQM_AF_CLKNEG__A, &clk_neg);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002916 if (status < 0)
2917 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002918 if ((clk_neg & IQM_AF_CLKNEG_CLKNEGDATA__M) ==
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002919 IQM_AF_CLKNEG_CLKNEGDATA_CLK_ADC_DATA_POS) {
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002920 clk_neg &= (~(IQM_AF_CLKNEG_CLKNEGDATA__M));
2921 clk_neg |=
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002922 IQM_AF_CLKNEG_CLKNEGDATA_CLK_ADC_DATA_NEG;
2923 } else {
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002924 clk_neg &= (~(IQM_AF_CLKNEG_CLKNEGDATA__M));
2925 clk_neg |=
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002926 IQM_AF_CLKNEG_CLKNEGDATA_CLK_ADC_DATA_POS;
2927 }
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002928 status = write16(state, IQM_AF_CLKNEG__A, clk_neg);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002929 if (status < 0)
2930 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002931 status = adc_sync_measurement(state, &count);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03002932 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002933 goto error;
2934 }
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002935
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002936 if (count < 2)
2937 status = -EINVAL;
2938error:
2939 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03002940 pr_err("Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002941 return status;
2942}
2943
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002944static int set_frequency_shifter(struct drxk_state *state,
2945 u16 intermediate_freqk_hz,
2946 s32 tuner_freq_offset, bool is_dtv)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002947{
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002948 bool select_pos_image = false;
2949 u32 rf_freq_residual = tuner_freq_offset;
2950 u32 fm_frequency_shift = 0;
2951 bool tuner_mirror = !state->m_b_mirror_freq_spect;
2952 u32 adc_freq;
2953 bool adc_flip;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002954 int status;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002955 u32 if_freq_actual;
2956 u32 sampling_frequency = (u32) (state->m_sys_clock_freq / 3);
2957 u32 frequency_shift;
2958 bool image_to_select;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002959
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03002960 dprintk(1, "\n");
2961
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002962 /*
Oliver Endrissebc7de22011-07-03 13:49:44 -03002963 Program frequency shifter
2964 No need to account for mirroring on RF
2965 */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002966 if (is_dtv) {
2967 if ((state->m_operation_mode == OM_QAM_ITU_A) ||
2968 (state->m_operation_mode == OM_QAM_ITU_C) ||
2969 (state->m_operation_mode == OM_DVBT))
2970 select_pos_image = true;
Oliver Endrissebc7de22011-07-03 13:49:44 -03002971 else
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002972 select_pos_image = false;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002973 }
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002974 if (tuner_mirror)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002975 /* tuner doesn't mirror */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002976 if_freq_actual = intermediate_freqk_hz +
2977 rf_freq_residual + fm_frequency_shift;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002978 else
2979 /* tuner mirrors */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002980 if_freq_actual = intermediate_freqk_hz -
2981 rf_freq_residual - fm_frequency_shift;
2982 if (if_freq_actual > sampling_frequency / 2) {
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002983 /* adc mirrors */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002984 adc_freq = sampling_frequency - if_freq_actual;
2985 adc_flip = true;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002986 } else {
2987 /* adc doesn't mirror */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002988 adc_freq = if_freq_actual;
2989 adc_flip = false;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002990 }
2991
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002992 frequency_shift = adc_freq;
2993 image_to_select = state->m_rfmirror ^ tuner_mirror ^
2994 adc_flip ^ select_pos_image;
2995 state->m_iqm_fs_rate_ofs =
2996 Frac28a((frequency_shift), sampling_frequency);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002997
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002998 if (image_to_select)
2999 state->m_iqm_fs_rate_ofs = ~state->m_iqm_fs_rate_ofs + 1;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003000
3001 /* Program frequency shifter with tuner offset compensation */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003002 /* frequency_shift += tuner_freq_offset; TODO */
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -03003003 status = write32(state, IQM_FS_RATE_OFS_LO__A,
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003004 state->m_iqm_fs_rate_ofs);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003005 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03003006 pr_err("Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003007 return status;
3008}
3009
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003010static int init_agc(struct drxk_state *state, bool is_dtv)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003011{
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003012 u16 ingain_tgt = 0;
3013 u16 ingain_tgt_min = 0;
3014 u16 ingain_tgt_max = 0;
3015 u16 clp_cyclen = 0;
3016 u16 clp_sum_min = 0;
3017 u16 clp_dir_to = 0;
3018 u16 sns_sum_min = 0;
3019 u16 sns_sum_max = 0;
3020 u16 clp_sum_max = 0;
3021 u16 sns_dir_to = 0;
3022 u16 ki_innergain_min = 0;
3023 u16 if_iaccu_hi_tgt = 0;
3024 u16 if_iaccu_hi_tgt_min = 0;
3025 u16 if_iaccu_hi_tgt_max = 0;
Oliver Endrissebc7de22011-07-03 13:49:44 -03003026 u16 data = 0;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003027 u16 fast_clp_ctrl_delay = 0;
3028 u16 clp_ctrl_mode = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003029 int status = 0;
3030
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03003031 dprintk(1, "\n");
3032
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003033 /* Common settings */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003034 sns_sum_max = 1023;
3035 if_iaccu_hi_tgt_min = 2047;
3036 clp_cyclen = 500;
3037 clp_sum_max = 1023;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003038
Mauro Carvalho Chehabf1b82972011-07-10 13:08:44 -03003039 /* AGCInit() not available for DVBT; init done in microcode */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003040 if (!is_qam(state)) {
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03003041 pr_err("%s: mode %d is not DVB-C\n",
3042 __func__, state->m_operation_mode);
Mauro Carvalho Chehabf1b82972011-07-10 13:08:44 -03003043 return -EINVAL;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003044 }
Mauro Carvalho Chehabf1b82972011-07-10 13:08:44 -03003045
3046 /* FIXME: Analog TV AGC require different settings */
3047
3048 /* Standard specific settings */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003049 clp_sum_min = 8;
3050 clp_dir_to = (u16) -9;
3051 clp_ctrl_mode = 0;
3052 sns_sum_min = 8;
3053 sns_dir_to = (u16) -9;
3054 ki_innergain_min = (u16) -1030;
3055 if_iaccu_hi_tgt_max = 0x2380;
3056 if_iaccu_hi_tgt = 0x2380;
3057 ingain_tgt_min = 0x0511;
3058 ingain_tgt = 0x0511;
3059 ingain_tgt_max = 5119;
3060 fast_clp_ctrl_delay = state->m_qam_if_agc_cfg.fast_clip_ctrl_delay;
Mauro Carvalho Chehabf1b82972011-07-10 13:08:44 -03003061
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03003062 status = write16(state, SCU_RAM_AGC_FAST_CLP_CTRL_DELAY__A,
3063 fast_clp_ctrl_delay);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003064 if (status < 0)
3065 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003066
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003067 status = write16(state, SCU_RAM_AGC_CLP_CTRL_MODE__A, clp_ctrl_mode);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003068 if (status < 0)
3069 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003070 status = write16(state, SCU_RAM_AGC_INGAIN_TGT__A, ingain_tgt);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003071 if (status < 0)
3072 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003073 status = write16(state, SCU_RAM_AGC_INGAIN_TGT_MIN__A, ingain_tgt_min);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003074 if (status < 0)
3075 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003076 status = write16(state, SCU_RAM_AGC_INGAIN_TGT_MAX__A, ingain_tgt_max);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003077 if (status < 0)
3078 goto error;
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03003079 status = write16(state, SCU_RAM_AGC_IF_IACCU_HI_TGT_MIN__A,
3080 if_iaccu_hi_tgt_min);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003081 if (status < 0)
3082 goto error;
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03003083 status = write16(state, SCU_RAM_AGC_IF_IACCU_HI_TGT_MAX__A,
3084 if_iaccu_hi_tgt_max);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003085 if (status < 0)
3086 goto error;
3087 status = write16(state, SCU_RAM_AGC_IF_IACCU_HI__A, 0);
3088 if (status < 0)
3089 goto error;
3090 status = write16(state, SCU_RAM_AGC_IF_IACCU_LO__A, 0);
3091 if (status < 0)
3092 goto error;
3093 status = write16(state, SCU_RAM_AGC_RF_IACCU_HI__A, 0);
3094 if (status < 0)
3095 goto error;
3096 status = write16(state, SCU_RAM_AGC_RF_IACCU_LO__A, 0);
3097 if (status < 0)
3098 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003099 status = write16(state, SCU_RAM_AGC_CLP_SUM_MAX__A, clp_sum_max);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003100 if (status < 0)
3101 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003102 status = write16(state, SCU_RAM_AGC_SNS_SUM_MAX__A, sns_sum_max);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003103 if (status < 0)
3104 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003105
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03003106 status = write16(state, SCU_RAM_AGC_KI_INNERGAIN_MIN__A,
3107 ki_innergain_min);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003108 if (status < 0)
3109 goto error;
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03003110 status = write16(state, SCU_RAM_AGC_IF_IACCU_HI_TGT__A,
3111 if_iaccu_hi_tgt);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003112 if (status < 0)
3113 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003114 status = write16(state, SCU_RAM_AGC_CLP_CYCLEN__A, clp_cyclen);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003115 if (status < 0)
3116 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003117
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003118 status = write16(state, SCU_RAM_AGC_RF_SNS_DEV_MAX__A, 1023);
3119 if (status < 0)
3120 goto error;
3121 status = write16(state, SCU_RAM_AGC_RF_SNS_DEV_MIN__A, (u16) -1023);
3122 if (status < 0)
3123 goto error;
3124 status = write16(state, SCU_RAM_AGC_FAST_SNS_CTRL_DELAY__A, 50);
3125 if (status < 0)
3126 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003127
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003128 status = write16(state, SCU_RAM_AGC_KI_MAXMINGAIN_TH__A, 20);
3129 if (status < 0)
3130 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003131 status = write16(state, SCU_RAM_AGC_CLP_SUM_MIN__A, clp_sum_min);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003132 if (status < 0)
3133 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003134 status = write16(state, SCU_RAM_AGC_SNS_SUM_MIN__A, sns_sum_min);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003135 if (status < 0)
3136 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003137 status = write16(state, SCU_RAM_AGC_CLP_DIR_TO__A, clp_dir_to);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003138 if (status < 0)
3139 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003140 status = write16(state, SCU_RAM_AGC_SNS_DIR_TO__A, sns_dir_to);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003141 if (status < 0)
3142 goto error;
3143 status = write16(state, SCU_RAM_AGC_KI_MINGAIN__A, 0x7fff);
3144 if (status < 0)
3145 goto error;
3146 status = write16(state, SCU_RAM_AGC_KI_MAXGAIN__A, 0x0);
3147 if (status < 0)
3148 goto error;
3149 status = write16(state, SCU_RAM_AGC_KI_MIN__A, 0x0117);
3150 if (status < 0)
3151 goto error;
3152 status = write16(state, SCU_RAM_AGC_KI_MAX__A, 0x0657);
3153 if (status < 0)
3154 goto error;
3155 status = write16(state, SCU_RAM_AGC_CLP_SUM__A, 0);
3156 if (status < 0)
3157 goto error;
3158 status = write16(state, SCU_RAM_AGC_CLP_CYCCNT__A, 0);
3159 if (status < 0)
3160 goto error;
3161 status = write16(state, SCU_RAM_AGC_CLP_DIR_WD__A, 0);
3162 if (status < 0)
3163 goto error;
3164 status = write16(state, SCU_RAM_AGC_CLP_DIR_STP__A, 1);
3165 if (status < 0)
3166 goto error;
3167 status = write16(state, SCU_RAM_AGC_SNS_SUM__A, 0);
3168 if (status < 0)
3169 goto error;
3170 status = write16(state, SCU_RAM_AGC_SNS_CYCCNT__A, 0);
3171 if (status < 0)
3172 goto error;
3173 status = write16(state, SCU_RAM_AGC_SNS_DIR_WD__A, 0);
3174 if (status < 0)
3175 goto error;
3176 status = write16(state, SCU_RAM_AGC_SNS_DIR_STP__A, 1);
3177 if (status < 0)
3178 goto error;
3179 status = write16(state, SCU_RAM_AGC_SNS_CYCLEN__A, 500);
3180 if (status < 0)
3181 goto error;
3182 status = write16(state, SCU_RAM_AGC_KI_CYCLEN__A, 500);
3183 if (status < 0)
3184 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003185
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003186 /* Initialize inner-loop KI gain factors */
3187 status = read16(state, SCU_RAM_AGC_KI__A, &data);
3188 if (status < 0)
3189 goto error;
Mauro Carvalho Chehabf1b82972011-07-10 13:08:44 -03003190
3191 data = 0x0657;
3192 data &= ~SCU_RAM_AGC_KI_RF__M;
3193 data |= (DRXK_KI_RAGC_QAM << SCU_RAM_AGC_KI_RF__B);
3194 data &= ~SCU_RAM_AGC_KI_IF__M;
3195 data |= (DRXK_KI_IAGC_QAM << SCU_RAM_AGC_KI_IF__B);
3196
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003197 status = write16(state, SCU_RAM_AGC_KI__A, data);
3198error:
3199 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03003200 pr_err("Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003201 return status;
3202}
3203
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003204static int dvbtqam_get_acc_pkt_err(struct drxk_state *state, u16 *packet_err)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003205{
3206 int status;
3207
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03003208 dprintk(1, "\n");
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003209 if (packet_err == NULL)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003210 status = write16(state, SCU_RAM_FEC_ACCUM_PKT_FAILURES__A, 0);
3211 else
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03003212 status = read16(state, SCU_RAM_FEC_ACCUM_PKT_FAILURES__A,
3213 packet_err);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003214 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03003215 pr_err("Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003216 return status;
3217}
3218
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003219static int dvbt_sc_command(struct drxk_state *state,
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003220 u16 cmd, u16 subcmd,
3221 u16 param0, u16 param1, u16 param2,
3222 u16 param3, u16 param4)
3223{
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003224 u16 cur_cmd = 0;
3225 u16 err_code = 0;
3226 u16 retry_cnt = 0;
3227 u16 sc_exec = 0;
Oliver Endrissebc7de22011-07-03 13:49:44 -03003228 int status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003229
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03003230 dprintk(1, "\n");
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003231 status = read16(state, OFDM_SC_COMM_EXEC__A, &sc_exec);
3232 if (sc_exec != 1) {
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003233 /* SC is not running */
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003234 status = -EINVAL;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003235 }
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003236 if (status < 0)
3237 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003238
3239 /* Wait until sc is ready to receive command */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003240 retry_cnt = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003241 do {
Mauro Carvalho Chehabb72852b2013-04-28 11:47:47 -03003242 usleep_range(1000, 2000);
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003243 status = read16(state, OFDM_SC_RA_RAM_CMD__A, &cur_cmd);
3244 retry_cnt++;
3245 } while ((cur_cmd != 0) && (retry_cnt < DRXK_MAX_RETRIES));
3246 if (retry_cnt >= DRXK_MAX_RETRIES && (status < 0))
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003247 goto error;
3248
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003249 /* Write sub-command */
3250 switch (cmd) {
3251 /* All commands using sub-cmd */
3252 case OFDM_SC_RA_RAM_CMD_PROC_START:
3253 case OFDM_SC_RA_RAM_CMD_SET_PREF_PARAM:
3254 case OFDM_SC_RA_RAM_CMD_PROGRAM_PARAM:
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003255 status = write16(state, OFDM_SC_RA_RAM_CMD_ADDR__A, subcmd);
3256 if (status < 0)
3257 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003258 break;
3259 default:
3260 /* Do nothing */
3261 break;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003262 }
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003263
3264 /* Write needed parameters and the command */
Mauro Carvalho Chehab2f60f132015-06-05 07:58:52 -03003265 status = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003266 switch (cmd) {
3267 /* All commands using 5 parameters */
3268 /* All commands using 4 parameters */
3269 /* All commands using 3 parameters */
3270 /* All commands using 2 parameters */
3271 case OFDM_SC_RA_RAM_CMD_PROC_START:
3272 case OFDM_SC_RA_RAM_CMD_SET_PREF_PARAM:
3273 case OFDM_SC_RA_RAM_CMD_PROGRAM_PARAM:
Mauro Carvalho Chehab2f60f132015-06-05 07:58:52 -03003274 status |= write16(state, OFDM_SC_RA_RAM_PARAM1__A, param1);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003275 /* All commands using 1 parameters */
3276 case OFDM_SC_RA_RAM_CMD_SET_ECHO_TIMING:
3277 case OFDM_SC_RA_RAM_CMD_USER_IO:
Mauro Carvalho Chehab2f60f132015-06-05 07:58:52 -03003278 status |= write16(state, OFDM_SC_RA_RAM_PARAM0__A, param0);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003279 /* All commands using 0 parameters */
3280 case OFDM_SC_RA_RAM_CMD_GET_OP_PARAM:
3281 case OFDM_SC_RA_RAM_CMD_NULL:
3282 /* Write command */
Mauro Carvalho Chehab2f60f132015-06-05 07:58:52 -03003283 status |= write16(state, OFDM_SC_RA_RAM_CMD__A, cmd);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003284 break;
3285 default:
3286 /* Unknown command */
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003287 status = -EINVAL;
3288 }
3289 if (status < 0)
3290 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003291
3292 /* Wait until sc is ready processing command */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003293 retry_cnt = 0;
Oliver Endrissebc7de22011-07-03 13:49:44 -03003294 do {
Mauro Carvalho Chehabb72852b2013-04-28 11:47:47 -03003295 usleep_range(1000, 2000);
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003296 status = read16(state, OFDM_SC_RA_RAM_CMD__A, &cur_cmd);
3297 retry_cnt++;
3298 } while ((cur_cmd != 0) && (retry_cnt < DRXK_MAX_RETRIES));
3299 if (retry_cnt >= DRXK_MAX_RETRIES && (status < 0))
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003300 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003301
3302 /* Check for illegal cmd */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003303 status = read16(state, OFDM_SC_RA_RAM_CMD_ADDR__A, &err_code);
3304 if (err_code == 0xFFFF) {
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003305 /* illegal command */
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003306 status = -EINVAL;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003307 }
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003308 if (status < 0)
3309 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003310
Jonathan McCrohan39c1cb22013-10-20 21:34:01 -03003311 /* Retrieve results parameters from SC */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003312 switch (cmd) {
3313 /* All commands yielding 5 results */
3314 /* All commands yielding 4 results */
3315 /* All commands yielding 3 results */
3316 /* All commands yielding 2 results */
3317 /* All commands yielding 1 result */
3318 case OFDM_SC_RA_RAM_CMD_USER_IO:
3319 case OFDM_SC_RA_RAM_CMD_GET_OP_PARAM:
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003320 status = read16(state, OFDM_SC_RA_RAM_PARAM0__A, &(param0));
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003321 /* All commands yielding 0 results */
3322 case OFDM_SC_RA_RAM_CMD_SET_ECHO_TIMING:
3323 case OFDM_SC_RA_RAM_CMD_SET_TIMER:
3324 case OFDM_SC_RA_RAM_CMD_PROC_START:
3325 case OFDM_SC_RA_RAM_CMD_SET_PREF_PARAM:
3326 case OFDM_SC_RA_RAM_CMD_PROGRAM_PARAM:
3327 case OFDM_SC_RA_RAM_CMD_NULL:
3328 break;
3329 default:
3330 /* Unknown command */
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003331 status = -EINVAL;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003332 break;
Oliver Endrissebc7de22011-07-03 13:49:44 -03003333 } /* switch (cmd->cmd) */
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003334error:
3335 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03003336 pr_err("Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003337 return status;
3338}
3339
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003340static int power_up_dvbt(struct drxk_state *state)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003341{
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003342 enum drx_power_mode power_mode = DRX_POWER_UP;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003343 int status;
3344
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03003345 dprintk(1, "\n");
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003346 status = ctrl_power_mode(state, &power_mode);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003347 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03003348 pr_err("Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003349 return status;
3350}
3351
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003352static int dvbt_ctrl_set_inc_enable(struct drxk_state *state, bool *enabled)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003353{
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003354 int status;
Oliver Endrissebc7de22011-07-03 13:49:44 -03003355
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03003356 dprintk(1, "\n");
Mauro Carvalho Chehab5a7f7b72014-09-03 15:23:57 -03003357 if (*enabled)
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -03003358 status = write16(state, IQM_CF_BYPASSDET__A, 0);
Oliver Endrissebc7de22011-07-03 13:49:44 -03003359 else
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -03003360 status = write16(state, IQM_CF_BYPASSDET__A, 1);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003361 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03003362 pr_err("Error %d on %s\n", status, __func__);
Oliver Endrissebc7de22011-07-03 13:49:44 -03003363 return status;
3364}
3365
3366#define DEFAULT_FR_THRES_8K 4000
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003367static int dvbt_ctrl_set_fr_enable(struct drxk_state *state, bool *enabled)
Oliver Endrissebc7de22011-07-03 13:49:44 -03003368{
3369
3370 int status;
3371
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03003372 dprintk(1, "\n");
Mauro Carvalho Chehab5a7f7b72014-09-03 15:23:57 -03003373 if (*enabled) {
Oliver Endrissebc7de22011-07-03 13:49:44 -03003374 /* write mask to 1 */
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -03003375 status = write16(state, OFDM_SC_RA_RAM_FR_THRES_8K__A,
Oliver Endrissebc7de22011-07-03 13:49:44 -03003376 DEFAULT_FR_THRES_8K);
3377 } else {
3378 /* write mask to 0 */
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -03003379 status = write16(state, OFDM_SC_RA_RAM_FR_THRES_8K__A, 0);
Oliver Endrissebc7de22011-07-03 13:49:44 -03003380 }
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003381 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03003382 pr_err("Error %d on %s\n", status, __func__);
Oliver Endrissebc7de22011-07-03 13:49:44 -03003383
3384 return status;
3385}
3386
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003387static int dvbt_ctrl_set_echo_threshold(struct drxk_state *state,
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03003388 struct drxk_cfg_dvbt_echo_thres_t *echo_thres)
Oliver Endrissebc7de22011-07-03 13:49:44 -03003389{
3390 u16 data = 0;
3391 int status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003392
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03003393 dprintk(1, "\n");
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003394 status = read16(state, OFDM_SC_RA_RAM_ECHO_THRES__A, &data);
3395 if (status < 0)
3396 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003397
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003398 switch (echo_thres->fft_mode) {
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003399 case DRX_FFTMODE_2K:
3400 data &= ~OFDM_SC_RA_RAM_ECHO_THRES_2K__M;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003401 data |= ((echo_thres->threshold <<
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003402 OFDM_SC_RA_RAM_ECHO_THRES_2K__B)
3403 & (OFDM_SC_RA_RAM_ECHO_THRES_2K__M));
Mauro Carvalho Chehab320ed232011-07-15 01:14:17 -03003404 break;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003405 case DRX_FFTMODE_8K:
3406 data &= ~OFDM_SC_RA_RAM_ECHO_THRES_8K__M;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003407 data |= ((echo_thres->threshold <<
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003408 OFDM_SC_RA_RAM_ECHO_THRES_8K__B)
3409 & (OFDM_SC_RA_RAM_ECHO_THRES_8K__M));
Mauro Carvalho Chehab320ed232011-07-15 01:14:17 -03003410 break;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003411 default:
3412 return -EINVAL;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003413 }
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003414
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003415 status = write16(state, OFDM_SC_RA_RAM_ECHO_THRES__A, data);
3416error:
3417 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03003418 pr_err("Error %d on %s\n", status, __func__);
Oliver Endrissebc7de22011-07-03 13:49:44 -03003419 return status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003420}
3421
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003422static int dvbt_ctrl_set_sqi_speed(struct drxk_state *state,
3423 enum drxk_cfg_dvbt_sqi_speed *speed)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003424{
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003425 int status = -EINVAL;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003426
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03003427 dprintk(1, "\n");
3428
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003429 switch (*speed) {
3430 case DRXK_DVBT_SQI_SPEED_FAST:
3431 case DRXK_DVBT_SQI_SPEED_MEDIUM:
3432 case DRXK_DVBT_SQI_SPEED_SLOW:
3433 break;
3434 default:
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003435 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003436 }
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -03003437 status = write16(state, SCU_RAM_FEC_PRE_RS_BER_FILTER_SH__A,
Oliver Endrissebc7de22011-07-03 13:49:44 -03003438 (u16) *speed);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003439error:
3440 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03003441 pr_err("Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003442 return status;
3443}
3444
3445/*============================================================================*/
3446
3447/**
3448* \brief Activate DVBT specific presets
3449* \param demod instance of demodulator.
3450* \return DRXStatus_t.
3451*
3452* Called in DVBTSetStandard
3453*
3454*/
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003455static int dvbt_activate_presets(struct drxk_state *state)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003456{
Oliver Endrissebc7de22011-07-03 13:49:44 -03003457 int status;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003458 bool setincenable = false;
3459 bool setfrenable = true;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003460
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003461 struct drxk_cfg_dvbt_echo_thres_t echo_thres2k = { 0, DRX_FFTMODE_2K };
3462 struct drxk_cfg_dvbt_echo_thres_t echo_thres8k = { 0, DRX_FFTMODE_8K };
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003463
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03003464 dprintk(1, "\n");
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003465 status = dvbt_ctrl_set_inc_enable(state, &setincenable);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003466 if (status < 0)
3467 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003468 status = dvbt_ctrl_set_fr_enable(state, &setfrenable);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003469 if (status < 0)
3470 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003471 status = dvbt_ctrl_set_echo_threshold(state, &echo_thres2k);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003472 if (status < 0)
3473 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003474 status = dvbt_ctrl_set_echo_threshold(state, &echo_thres8k);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003475 if (status < 0)
3476 goto error;
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03003477 status = write16(state, SCU_RAM_AGC_INGAIN_TGT_MAX__A,
3478 state->m_dvbt_if_agc_cfg.ingain_tgt_max);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003479error:
3480 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03003481 pr_err("Error %d on %s\n", status, __func__);
Oliver Endrissebc7de22011-07-03 13:49:44 -03003482 return status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003483}
Oliver Endrissebc7de22011-07-03 13:49:44 -03003484
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003485/*============================================================================*/
3486
3487/**
3488* \brief Initialize channelswitch-independent settings for DVBT.
3489* \param demod instance of demodulator.
3490* \return DRXStatus_t.
3491*
3492* For ROM code channel filter taps are loaded from the bootloader. For microcode
3493* the DVB-T taps from the drxk_filters.h are used.
3494*/
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003495static int set_dvbt_standard(struct drxk_state *state,
3496 enum operation_mode o_mode)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003497{
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003498 u16 cmd_result = 0;
Oliver Endrissebc7de22011-07-03 13:49:44 -03003499 u16 data = 0;
3500 int status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003501
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03003502 dprintk(1, "\n");
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003503
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003504 power_up_dvbt(state);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003505 /* added antenna switch */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003506 switch_antenna_to_dvbt(state);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003507 /* send OFDM reset command */
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03003508 status = scu_command(state,
3509 SCU_RAM_COMMAND_STANDARD_OFDM
3510 | SCU_RAM_COMMAND_CMD_DEMOD_RESET,
3511 0, NULL, 1, &cmd_result);
Oliver Endrissebc7de22011-07-03 13:49:44 -03003512 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003513 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003514
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003515 /* send OFDM setenv command */
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03003516 status = scu_command(state, SCU_RAM_COMMAND_STANDARD_OFDM
3517 | SCU_RAM_COMMAND_CMD_DEMOD_SET_ENV,
3518 0, NULL, 1, &cmd_result);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003519 if (status < 0)
3520 goto error;
3521
3522 /* reset datapath for OFDM, processors first */
3523 status = write16(state, OFDM_SC_COMM_EXEC__A, OFDM_SC_COMM_EXEC_STOP);
3524 if (status < 0)
3525 goto error;
3526 status = write16(state, OFDM_LC_COMM_EXEC__A, OFDM_LC_COMM_EXEC_STOP);
3527 if (status < 0)
3528 goto error;
3529 status = write16(state, IQM_COMM_EXEC__A, IQM_COMM_EXEC_B_STOP);
3530 if (status < 0)
3531 goto error;
3532
3533 /* IQM setup */
3534 /* synchronize on ofdstate->m_festart */
3535 status = write16(state, IQM_AF_UPD_SEL__A, 1);
3536 if (status < 0)
3537 goto error;
3538 /* window size for clipping ADC detection */
3539 status = write16(state, IQM_AF_CLP_LEN__A, 0);
3540 if (status < 0)
3541 goto error;
3542 /* window size for for sense pre-SAW detection */
3543 status = write16(state, IQM_AF_SNS_LEN__A, 0);
3544 if (status < 0)
3545 goto error;
3546 /* sense threshold for sense pre-SAW detection */
3547 status = write16(state, IQM_AF_AMUX__A, IQM_AF_AMUX_SIGNAL2ADC);
3548 if (status < 0)
3549 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003550 status = set_iqm_af(state, true);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003551 if (status < 0)
3552 goto error;
3553
3554 status = write16(state, IQM_AF_AGC_RF__A, 0);
3555 if (status < 0)
3556 goto error;
3557
3558 /* Impulse noise cruncher setup */
3559 status = write16(state, IQM_AF_INC_LCT__A, 0); /* crunch in IQM_CF */
3560 if (status < 0)
3561 goto error;
3562 status = write16(state, IQM_CF_DET_LCT__A, 0); /* detect in IQM_CF */
3563 if (status < 0)
3564 goto error;
3565 status = write16(state, IQM_CF_WND_LEN__A, 3); /* peak detector window length */
3566 if (status < 0)
3567 goto error;
3568
3569 status = write16(state, IQM_RC_STRETCH__A, 16);
3570 if (status < 0)
3571 goto error;
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03003572 status = write16(state, IQM_CF_OUT_ENA__A, 0x4); /* enable output 2 */
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003573 if (status < 0)
3574 goto error;
3575 status = write16(state, IQM_CF_DS_ENA__A, 0x4); /* decimate output 2 */
3576 if (status < 0)
3577 goto error;
3578 status = write16(state, IQM_CF_SCALE__A, 1600);
3579 if (status < 0)
3580 goto error;
3581 status = write16(state, IQM_CF_SCALE_SH__A, 0);
3582 if (status < 0)
3583 goto error;
3584
3585 /* virtual clipping threshold for clipping ADC detection */
3586 status = write16(state, IQM_AF_CLP_TH__A, 448);
3587 if (status < 0)
3588 goto error;
3589 status = write16(state, IQM_CF_DATATH__A, 495); /* crunching threshold */
3590 if (status < 0)
3591 goto error;
3592
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03003593 status = bl_chain_cmd(state, DRXK_BL_ROM_OFFSET_TAPS_DVBT,
3594 DRXK_BLCC_NR_ELEMENTS_TAPS, DRXK_BLC_TIMEOUT);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003595 if (status < 0)
3596 goto error;
3597
3598 status = write16(state, IQM_CF_PKDTH__A, 2); /* peak detector threshold */
3599 if (status < 0)
3600 goto error;
3601 status = write16(state, IQM_CF_POW_MEAS_LEN__A, 2);
3602 if (status < 0)
3603 goto error;
3604 /* enable power measurement interrupt */
3605 status = write16(state, IQM_CF_COMM_INT_MSK__A, 1);
3606 if (status < 0)
3607 goto error;
3608 status = write16(state, IQM_COMM_EXEC__A, IQM_COMM_EXEC_B_ACTIVE);
3609 if (status < 0)
3610 goto error;
3611
3612 /* IQM will not be reset from here, sync ADC and update/init AGC */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003613 status = adc_synchronization(state);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003614 if (status < 0)
3615 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003616 status = set_pre_saw(state, &state->m_dvbt_pre_saw_cfg);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003617 if (status < 0)
3618 goto error;
3619
3620 /* Halt SCU to enable safe non-atomic accesses */
3621 status = write16(state, SCU_COMM_EXEC__A, SCU_COMM_EXEC_HOLD);
3622 if (status < 0)
3623 goto error;
3624
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003625 status = set_agc_rf(state, &state->m_dvbt_rf_agc_cfg, true);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003626 if (status < 0)
3627 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003628 status = set_agc_if(state, &state->m_dvbt_if_agc_cfg, true);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003629 if (status < 0)
3630 goto error;
3631
3632 /* Set Noise Estimation notch width and enable DC fix */
3633 status = read16(state, OFDM_SC_RA_RAM_CONFIG__A, &data);
3634 if (status < 0)
3635 goto error;
3636 data |= OFDM_SC_RA_RAM_CONFIG_NE_FIX_ENABLE__M;
3637 status = write16(state, OFDM_SC_RA_RAM_CONFIG__A, data);
3638 if (status < 0)
3639 goto error;
3640
3641 /* Activate SCU to enable SCU commands */
3642 status = write16(state, SCU_COMM_EXEC__A, SCU_COMM_EXEC_ACTIVE);
3643 if (status < 0)
3644 goto error;
3645
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003646 if (!state->m_drxk_a3_rom_code) {
3647 /* AGCInit() is not done for DVBT, so set agcfast_clip_ctrl_delay */
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03003648 status = write16(state, SCU_RAM_AGC_FAST_CLP_CTRL_DELAY__A,
3649 state->m_dvbt_if_agc_cfg.fast_clip_ctrl_delay);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003650 if (status < 0)
3651 goto error;
3652 }
3653
3654 /* OFDM_SC setup */
3655#ifdef COMPILE_FOR_NONRT
3656 status = write16(state, OFDM_SC_RA_RAM_BE_OPT_DELAY__A, 1);
3657 if (status < 0)
3658 goto error;
3659 status = write16(state, OFDM_SC_RA_RAM_BE_OPT_INIT_DELAY__A, 2);
3660 if (status < 0)
3661 goto error;
3662#endif
3663
3664 /* FEC setup */
3665 status = write16(state, FEC_DI_INPUT_CTL__A, 1); /* OFDM input */
3666 if (status < 0)
3667 goto error;
3668
3669
3670#ifdef COMPILE_FOR_NONRT
3671 status = write16(state, FEC_RS_MEASUREMENT_PERIOD__A, 0x400);
3672 if (status < 0)
3673 goto error;
3674#else
3675 status = write16(state, FEC_RS_MEASUREMENT_PERIOD__A, 0x1000);
3676 if (status < 0)
3677 goto error;
3678#endif
3679 status = write16(state, FEC_RS_MEASUREMENT_PRESCALE__A, 0x0001);
3680 if (status < 0)
3681 goto error;
3682
3683 /* Setup MPEG bus */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003684 status = mpegts_dto_setup(state, OM_DVBT);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003685 if (status < 0)
3686 goto error;
3687 /* Set DVBT Presets */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003688 status = dvbt_activate_presets(state);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003689 if (status < 0)
3690 goto error;
3691
3692error:
3693 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03003694 pr_err("Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003695 return status;
3696}
3697
3698/*============================================================================*/
3699/**
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003700* \brief start dvbt demodulating for channel.
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003701* \param demod instance of demodulator.
3702* \return DRXStatus_t.
3703*/
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003704static int dvbt_start(struct drxk_state *state)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003705{
Oliver Endrissebc7de22011-07-03 13:49:44 -03003706 u16 param1;
3707 int status;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003708 /* drxk_ofdm_sc_cmd_t scCmd; */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003709
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03003710 dprintk(1, "\n");
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003711 /* start correct processes to get in lock */
Oliver Endrissebc7de22011-07-03 13:49:44 -03003712 /* DRXK: OFDM_SC_RA_RAM_PROC_LOCKTRACK is no longer in mapfile! */
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003713 param1 = OFDM_SC_RA_RAM_LOCKTRACK_MIN;
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03003714 status = dvbt_sc_command(state, OFDM_SC_RA_RAM_CMD_PROC_START, 0,
3715 OFDM_SC_RA_RAM_SW_EVENT_RUN_NMASK__M, param1,
3716 0, 0, 0);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003717 if (status < 0)
3718 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003719 /* start FEC OC */
3720 status = mpegts_start(state);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003721 if (status < 0)
3722 goto error;
3723 status = write16(state, FEC_COMM_EXEC__A, FEC_COMM_EXEC_ACTIVE);
3724 if (status < 0)
3725 goto error;
3726error:
3727 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03003728 pr_err("Error %d on %s\n", status, __func__);
Oliver Endrissebc7de22011-07-03 13:49:44 -03003729 return status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003730}
3731
3732
3733/*============================================================================*/
3734
3735/**
3736* \brief Set up dvbt demodulator for channel.
3737* \param demod instance of demodulator.
3738* \return DRXStatus_t.
3739* // original DVBTSetChannel()
3740*/
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003741static int set_dvbt(struct drxk_state *state, u16 intermediate_freqk_hz,
3742 s32 tuner_freq_offset)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003743{
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003744 u16 cmd_result = 0;
3745 u16 transmission_params = 0;
3746 u16 operation_mode = 0;
3747 u32 iqm_rc_rate_ofs = 0;
Oliver Endrissebc7de22011-07-03 13:49:44 -03003748 u32 bandwidth = 0;
3749 u16 param1;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003750 int status;
3751
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03003752 dprintk(1, "IF =%d, TFO = %d\n",
3753 intermediate_freqk_hz, tuner_freq_offset);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003754
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03003755 status = scu_command(state, SCU_RAM_COMMAND_STANDARD_OFDM
3756 | SCU_RAM_COMMAND_CMD_DEMOD_STOP,
3757 0, NULL, 1, &cmd_result);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003758 if (status < 0)
3759 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003760
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003761 /* Halt SCU to enable safe non-atomic accesses */
3762 status = write16(state, SCU_COMM_EXEC__A, SCU_COMM_EXEC_HOLD);
3763 if (status < 0)
3764 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003765
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003766 /* Stop processors */
3767 status = write16(state, OFDM_SC_COMM_EXEC__A, OFDM_SC_COMM_EXEC_STOP);
3768 if (status < 0)
3769 goto error;
3770 status = write16(state, OFDM_LC_COMM_EXEC__A, OFDM_LC_COMM_EXEC_STOP);
3771 if (status < 0)
3772 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003773
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003774 /* Mandatory fix, always stop CP, required to set spl offset back to
3775 hardware default (is set to 0 by ucode during pilot detection */
3776 status = write16(state, OFDM_CP_COMM_EXEC__A, OFDM_CP_COMM_EXEC_STOP);
3777 if (status < 0)
3778 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003779
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03003780 /*== Write channel settings to device ================================*/
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003781
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003782 /* mode */
Mauro Carvalho Chehabed5452a2011-12-26 09:57:11 -03003783 switch (state->props.transmission_mode) {
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003784 case TRANSMISSION_MODE_AUTO:
3785 default:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003786 operation_mode |= OFDM_SC_RA_RAM_OP_AUTO_MODE__M;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003787 /* fall through , try first guess DRX_FFTMODE_8K */
3788 case TRANSMISSION_MODE_8K:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003789 transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_MODE_8K;
Mauro Carvalho Chehab320ed232011-07-15 01:14:17 -03003790 break;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003791 case TRANSMISSION_MODE_2K:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003792 transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_MODE_2K;
Mauro Carvalho Chehab320ed232011-07-15 01:14:17 -03003793 break;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003794 }
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003795
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003796 /* guard */
Mauro Carvalho Chehabed5452a2011-12-26 09:57:11 -03003797 switch (state->props.guard_interval) {
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003798 default:
3799 case GUARD_INTERVAL_AUTO:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003800 operation_mode |= OFDM_SC_RA_RAM_OP_AUTO_GUARD__M;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003801 /* fall through , try first guess DRX_GUARD_1DIV4 */
3802 case GUARD_INTERVAL_1_4:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003803 transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_GUARD_4;
Mauro Carvalho Chehab320ed232011-07-15 01:14:17 -03003804 break;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003805 case GUARD_INTERVAL_1_32:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003806 transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_GUARD_32;
Mauro Carvalho Chehab320ed232011-07-15 01:14:17 -03003807 break;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003808 case GUARD_INTERVAL_1_16:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003809 transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_GUARD_16;
Mauro Carvalho Chehab320ed232011-07-15 01:14:17 -03003810 break;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003811 case GUARD_INTERVAL_1_8:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003812 transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_GUARD_8;
Mauro Carvalho Chehab320ed232011-07-15 01:14:17 -03003813 break;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003814 }
3815
3816 /* hierarchy */
Mauro Carvalho Chehabed5452a2011-12-26 09:57:11 -03003817 switch (state->props.hierarchy) {
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003818 case HIERARCHY_AUTO:
3819 case HIERARCHY_NONE:
3820 default:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003821 operation_mode |= OFDM_SC_RA_RAM_OP_AUTO_HIER__M;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003822 /* fall through , try first guess SC_RA_RAM_OP_PARAM_HIER_NO */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003823 /* transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_HIER_NO; */
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003824 /* break; */
3825 case HIERARCHY_1:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003826 transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_HIER_A1;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003827 break;
3828 case HIERARCHY_2:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003829 transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_HIER_A2;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003830 break;
3831 case HIERARCHY_4:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003832 transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_HIER_A4;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003833 break;
3834 }
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003835
3836
Mauro Carvalho Chehabed5452a2011-12-26 09:57:11 -03003837 /* modulation */
3838 switch (state->props.modulation) {
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003839 case QAM_AUTO:
3840 default:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003841 operation_mode |= OFDM_SC_RA_RAM_OP_AUTO_CONST__M;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003842 /* fall through , try first guess DRX_CONSTELLATION_QAM64 */
3843 case QAM_64:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003844 transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_CONST_QAM64;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003845 break;
3846 case QPSK:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003847 transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_CONST_QPSK;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003848 break;
3849 case QAM_16:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003850 transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_CONST_QAM16;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003851 break;
3852 }
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003853#if 0
Jonathan McCrohan39c1cb22013-10-20 21:34:01 -03003854 /* No hierarchical channels support in BDA */
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003855 /* Priority (only for hierarchical channels) */
3856 switch (channel->priority) {
3857 case DRX_PRIORITY_LOW:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003858 transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_PRIO_LO;
3859 WR16(dev_addr, OFDM_EC_SB_PRIOR__A,
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003860 OFDM_EC_SB_PRIOR_LO);
3861 break;
3862 case DRX_PRIORITY_HIGH:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003863 transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_PRIO_HI;
3864 WR16(dev_addr, OFDM_EC_SB_PRIOR__A,
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003865 OFDM_EC_SB_PRIOR_HI));
3866 break;
3867 case DRX_PRIORITY_UNKNOWN: /* fall through */
3868 default:
3869 status = -EINVAL;
3870 goto error;
3871 }
3872#else
3873 /* Set Priorty high */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003874 transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_PRIO_HI;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003875 status = write16(state, OFDM_EC_SB_PRIOR__A, OFDM_EC_SB_PRIOR_HI);
3876 if (status < 0)
3877 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003878#endif
3879
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003880 /* coderate */
Mauro Carvalho Chehabed5452a2011-12-26 09:57:11 -03003881 switch (state->props.code_rate_HP) {
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003882 case FEC_AUTO:
3883 default:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003884 operation_mode |= OFDM_SC_RA_RAM_OP_AUTO_RATE__M;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003885 /* fall through , try first guess DRX_CODERATE_2DIV3 */
3886 case FEC_2_3:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003887 transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_RATE_2_3;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003888 break;
3889 case FEC_1_2:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003890 transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_RATE_1_2;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003891 break;
3892 case FEC_3_4:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003893 transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_RATE_3_4;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003894 break;
3895 case FEC_5_6:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003896 transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_RATE_5_6;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003897 break;
3898 case FEC_7_8:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003899 transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_RATE_7_8;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003900 break;
3901 }
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003902
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03003903 /*
3904 * SAW filter selection: normaly not necesarry, but if wanted
3905 * the application can select a SAW filter via the driver by
3906 * using UIOs
3907 */
3908
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003909 /* First determine real bandwidth (Hz) */
3910 /* Also set delay for impulse noise cruncher */
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03003911 /*
3912 * Also set parameters for EC_OC fix, note EC_OC_REG_TMD_HIL_MAR is
3913 * changed by SC for fix for some 8K,1/8 guard but is restored by
3914 * InitEC and ResetEC functions
3915 */
Mauro Carvalho Chehabed5452a2011-12-26 09:57:11 -03003916 switch (state->props.bandwidth_hz) {
3917 case 0:
3918 state->props.bandwidth_hz = 8000000;
3919 /* fall though */
3920 case 8000000:
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003921 bandwidth = DRXK_BANDWIDTH_8MHZ_IN_HZ;
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03003922 status = write16(state, OFDM_SC_RA_RAM_SRMM_FIX_FACT_8K__A,
3923 3052);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03003924 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003925 goto error;
3926 /* cochannel protection for PAL 8 MHz */
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03003927 status = write16(state, OFDM_SC_RA_RAM_NI_INIT_8K_PER_LEFT__A,
3928 7);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003929 if (status < 0)
3930 goto error;
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03003931 status = write16(state, OFDM_SC_RA_RAM_NI_INIT_8K_PER_RIGHT__A,
3932 7);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003933 if (status < 0)
3934 goto error;
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03003935 status = write16(state, OFDM_SC_RA_RAM_NI_INIT_2K_PER_LEFT__A,
3936 7);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003937 if (status < 0)
3938 goto error;
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03003939 status = write16(state, OFDM_SC_RA_RAM_NI_INIT_2K_PER_RIGHT__A,
3940 1);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003941 if (status < 0)
3942 goto error;
3943 break;
Mauro Carvalho Chehabed5452a2011-12-26 09:57:11 -03003944 case 7000000:
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003945 bandwidth = DRXK_BANDWIDTH_7MHZ_IN_HZ;
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03003946 status = write16(state, OFDM_SC_RA_RAM_SRMM_FIX_FACT_8K__A,
3947 3491);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003948 if (status < 0)
3949 goto error;
3950 /* cochannel protection for PAL 7 MHz */
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03003951 status = write16(state, OFDM_SC_RA_RAM_NI_INIT_8K_PER_LEFT__A,
3952 8);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003953 if (status < 0)
3954 goto error;
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03003955 status = write16(state, OFDM_SC_RA_RAM_NI_INIT_8K_PER_RIGHT__A,
3956 8);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003957 if (status < 0)
3958 goto error;
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03003959 status = write16(state, OFDM_SC_RA_RAM_NI_INIT_2K_PER_LEFT__A,
3960 4);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003961 if (status < 0)
3962 goto error;
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03003963 status = write16(state, OFDM_SC_RA_RAM_NI_INIT_2K_PER_RIGHT__A,
3964 1);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003965 if (status < 0)
3966 goto error;
3967 break;
Mauro Carvalho Chehabed5452a2011-12-26 09:57:11 -03003968 case 6000000:
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003969 bandwidth = DRXK_BANDWIDTH_6MHZ_IN_HZ;
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03003970 status = write16(state, OFDM_SC_RA_RAM_SRMM_FIX_FACT_8K__A,
3971 4073);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003972 if (status < 0)
3973 goto error;
3974 /* cochannel protection for NTSC 6 MHz */
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03003975 status = write16(state, OFDM_SC_RA_RAM_NI_INIT_8K_PER_LEFT__A,
3976 19);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003977 if (status < 0)
3978 goto error;
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03003979 status = write16(state, OFDM_SC_RA_RAM_NI_INIT_8K_PER_RIGHT__A,
3980 19);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003981 if (status < 0)
3982 goto error;
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03003983 status = write16(state, OFDM_SC_RA_RAM_NI_INIT_2K_PER_LEFT__A,
3984 14);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003985 if (status < 0)
3986 goto error;
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03003987 status = write16(state, OFDM_SC_RA_RAM_NI_INIT_2K_PER_RIGHT__A,
3988 1);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003989 if (status < 0)
3990 goto error;
3991 break;
3992 default:
3993 status = -EINVAL;
3994 goto error;
3995 }
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003996
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003997 if (iqm_rc_rate_ofs == 0) {
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003998 /* Now compute IQM_RC_RATE_OFS
3999 (((SysFreq/BandWidth)/2)/2) -1) * 2^23)
4000 =>
4001 ((SysFreq / BandWidth) * (2^21)) - (2^23)
4002 */
4003 /* (SysFreq / BandWidth) * (2^28) */
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03004004 /*
4005 * assert (MAX(sysClk)/MIN(bandwidth) < 16)
4006 * => assert(MAX(sysClk) < 16*MIN(bandwidth))
4007 * => assert(109714272 > 48000000) = true
4008 * so Frac 28 can be used
4009 */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03004010 iqm_rc_rate_ofs = Frac28a((u32)
4011 ((state->m_sys_clock_freq *
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004012 1000) / 3), bandwidth);
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03004013 /* (SysFreq / BandWidth) * (2^21), rounding before truncating */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03004014 if ((iqm_rc_rate_ofs & 0x7fL) >= 0x40)
4015 iqm_rc_rate_ofs += 0x80L;
4016 iqm_rc_rate_ofs = iqm_rc_rate_ofs >> 7;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004017 /* ((SysFreq / BandWidth) * (2^21)) - (2^23) */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03004018 iqm_rc_rate_ofs = iqm_rc_rate_ofs - (1 << 23);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004019 }
4020
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03004021 iqm_rc_rate_ofs &=
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004022 ((((u32) IQM_RC_RATE_OFS_HI__M) <<
4023 IQM_RC_RATE_OFS_LO__W) | IQM_RC_RATE_OFS_LO__M);
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03004024 status = write32(state, IQM_RC_RATE_OFS_LO__A, iqm_rc_rate_ofs);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004025 if (status < 0)
4026 goto error;
4027
4028 /* Bandwidth setting done */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004029
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03004030#if 0
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03004031 status = dvbt_set_frequency_shift(demod, channel, tuner_offset);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004032 if (status < 0)
4033 goto error;
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03004034#endif
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03004035 status = set_frequency_shifter(state, intermediate_freqk_hz,
4036 tuner_freq_offset, true);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004037 if (status < 0)
4038 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004039
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03004040 /*== start SC, write channel settings to SC ==========================*/
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004041
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004042 /* Activate SCU to enable SCU commands */
4043 status = write16(state, SCU_COMM_EXEC__A, SCU_COMM_EXEC_ACTIVE);
4044 if (status < 0)
4045 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004046
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004047 /* Enable SC after setting all other parameters */
4048 status = write16(state, OFDM_SC_COMM_STATE__A, 0);
4049 if (status < 0)
4050 goto error;
4051 status = write16(state, OFDM_SC_COMM_EXEC__A, 1);
4052 if (status < 0)
4053 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004054
4055
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03004056 status = scu_command(state, SCU_RAM_COMMAND_STANDARD_OFDM
4057 | SCU_RAM_COMMAND_CMD_DEMOD_START,
4058 0, NULL, 1, &cmd_result);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004059 if (status < 0)
4060 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004061
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004062 /* Write SC parameter registers, set all AUTO flags in operation mode */
4063 param1 = (OFDM_SC_RA_RAM_OP_AUTO_MODE__M |
4064 OFDM_SC_RA_RAM_OP_AUTO_GUARD__M |
4065 OFDM_SC_RA_RAM_OP_AUTO_CONST__M |
4066 OFDM_SC_RA_RAM_OP_AUTO_HIER__M |
4067 OFDM_SC_RA_RAM_OP_AUTO_RATE__M);
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03004068 status = dvbt_sc_command(state, OFDM_SC_RA_RAM_CMD_SET_PREF_PARAM,
4069 0, transmission_params, param1, 0, 0, 0);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004070 if (status < 0)
4071 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004072
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03004073 if (!state->m_drxk_a3_rom_code)
4074 status = dvbt_ctrl_set_sqi_speed(state, &state->m_sqi_speed);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004075error:
4076 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03004077 pr_err("Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004078
4079 return status;
4080}
4081
4082
4083/*============================================================================*/
4084
4085/**
Jonathan McCrohan39c1cb22013-10-20 21:34:01 -03004086* \brief Retrieve lock status .
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004087* \param demod Pointer to demodulator instance.
4088* \param lockStat Pointer to lock status structure.
4089* \return DRXStatus_t.
4090*
4091*/
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03004092static int get_dvbt_lock_status(struct drxk_state *state, u32 *p_lock_status)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004093{
Oliver Endrissebc7de22011-07-03 13:49:44 -03004094 int status;
4095 const u16 mpeg_lock_mask = (OFDM_SC_RA_RAM_LOCK_MPEG__M |
4096 OFDM_SC_RA_RAM_LOCK_FEC__M);
4097 const u16 fec_lock_mask = (OFDM_SC_RA_RAM_LOCK_FEC__M);
4098 const u16 demod_lock_mask = OFDM_SC_RA_RAM_LOCK_DEMOD__M;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004099
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03004100 u16 sc_ra_ram_lock = 0;
4101 u16 sc_comm_exec = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004102
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03004103 dprintk(1, "\n");
4104
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03004105 *p_lock_status = NOT_LOCKED;
Oliver Endrissebc7de22011-07-03 13:49:44 -03004106 /* driver 0.9.0 */
4107 /* Check if SC is running */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03004108 status = read16(state, OFDM_SC_COMM_EXEC__A, &sc_comm_exec);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004109 if (status < 0)
4110 goto end;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03004111 if (sc_comm_exec == OFDM_SC_COMM_EXEC_STOP)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004112 goto end;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004113
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03004114 status = read16(state, OFDM_SC_RA_RAM_LOCK__A, &sc_ra_ram_lock);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004115 if (status < 0)
4116 goto end;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004117
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03004118 if ((sc_ra_ram_lock & mpeg_lock_mask) == mpeg_lock_mask)
4119 *p_lock_status = MPEG_LOCK;
4120 else if ((sc_ra_ram_lock & fec_lock_mask) == fec_lock_mask)
4121 *p_lock_status = FEC_LOCK;
4122 else if ((sc_ra_ram_lock & demod_lock_mask) == demod_lock_mask)
4123 *p_lock_status = DEMOD_LOCK;
4124 else if (sc_ra_ram_lock & OFDM_SC_RA_RAM_LOCK_NODVBT__M)
4125 *p_lock_status = NEVER_LOCK;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004126end:
4127 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03004128 pr_err("Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004129
Oliver Endrissebc7de22011-07-03 13:49:44 -03004130 return status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004131}
4132
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03004133static int power_up_qam(struct drxk_state *state)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004134{
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03004135 enum drx_power_mode power_mode = DRXK_POWER_DOWN_OFDM;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004136 int status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004137
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03004138 dprintk(1, "\n");
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03004139 status = ctrl_power_mode(state, &power_mode);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004140 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03004141 pr_err("Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004142
Oliver Endrissebc7de22011-07-03 13:49:44 -03004143 return status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004144}
4145
4146
Oliver Endrissebc7de22011-07-03 13:49:44 -03004147/** Power Down QAM */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03004148static int power_down_qam(struct drxk_state *state)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004149{
Oliver Endrissebc7de22011-07-03 13:49:44 -03004150 u16 data = 0;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03004151 u16 cmd_result;
Oliver Endrissebc7de22011-07-03 13:49:44 -03004152 int status = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004153
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03004154 dprintk(1, "\n");
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004155 status = read16(state, SCU_COMM_EXEC__A, &data);
4156 if (status < 0)
4157 goto error;
4158 if (data == SCU_COMM_EXEC_ACTIVE) {
4159 /*
4160 STOP demodulator
4161 QAM and HW blocks
4162 */
4163 /* stop all comstate->m_exec */
4164 status = write16(state, QAM_COMM_EXEC__A, QAM_COMM_EXEC_STOP);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03004165 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004166 goto error;
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03004167 status = scu_command(state, SCU_RAM_COMMAND_STANDARD_QAM
4168 | SCU_RAM_COMMAND_CMD_DEMOD_STOP,
4169 0, NULL, 1, &cmd_result);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03004170 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004171 goto error;
4172 }
4173 /* powerdown AFE */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03004174 status = set_iqm_af(state, false);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004175
4176error:
4177 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03004178 pr_err("Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004179
Oliver Endrissebc7de22011-07-03 13:49:44 -03004180 return status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004181}
Oliver Endrissebc7de22011-07-03 13:49:44 -03004182
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004183/*============================================================================*/
4184
4185/**
4186* \brief Setup of the QAM Measurement intervals for signal quality
4187* \param demod instance of demod.
Mauro Carvalho Chehabed5452a2011-12-26 09:57:11 -03004188* \param modulation current modulation.
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004189* \return DRXStatus_t.
4190*
4191* NOTE:
4192* Take into account that for certain settings the errorcounters can overflow.
4193* The implementation does not check this.
4194*
4195*/
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03004196static int set_qam_measurement(struct drxk_state *state,
4197 enum e_drxk_constellation modulation,
4198 u32 symbol_rate)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004199{
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03004200 u32 fec_bits_desired = 0; /* BER accounting period */
4201 u32 fec_rs_period_total = 0; /* Total period */
4202 u16 fec_rs_prescale = 0; /* ReedSolomon Measurement Prescale */
4203 u16 fec_rs_period = 0; /* Value for corresponding I2C register */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004204 int status = 0;
4205
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03004206 dprintk(1, "\n");
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004207
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03004208 fec_rs_prescale = 1;
4209 /* fec_bits_desired = symbol_rate [kHz] *
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004210 FrameLenght [ms] *
Mauro Carvalho Chehabed5452a2011-12-26 09:57:11 -03004211 (modulation + 1) *
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004212 SyncLoss (== 1) *
4213 ViterbiLoss (==1)
4214 */
Mauro Carvalho Chehabed5452a2011-12-26 09:57:11 -03004215 switch (modulation) {
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004216 case DRX_CONSTELLATION_QAM16:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03004217 fec_bits_desired = 4 * symbol_rate;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004218 break;
4219 case DRX_CONSTELLATION_QAM32:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03004220 fec_bits_desired = 5 * symbol_rate;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004221 break;
4222 case DRX_CONSTELLATION_QAM64:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03004223 fec_bits_desired = 6 * symbol_rate;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004224 break;
4225 case DRX_CONSTELLATION_QAM128:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03004226 fec_bits_desired = 7 * symbol_rate;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004227 break;
4228 case DRX_CONSTELLATION_QAM256:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03004229 fec_bits_desired = 8 * symbol_rate;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004230 break;
4231 default:
4232 status = -EINVAL;
4233 }
Oliver Endrissebc7de22011-07-03 13:49:44 -03004234 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004235 goto error;
Oliver Endrissebc7de22011-07-03 13:49:44 -03004236
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03004237 fec_bits_desired /= 1000; /* symbol_rate [Hz] -> symbol_rate [kHz] */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03004238 fec_bits_desired *= 500; /* meas. period [ms] */
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004239
4240 /* Annex A/C: bits/RsPeriod = 204 * 8 = 1632 */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03004241 /* fec_rs_period_total = fec_bits_desired / 1632 */
4242 fec_rs_period_total = (fec_bits_desired / 1632UL) + 1; /* roughly ceil */
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004243
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03004244 /* fec_rs_period_total = fec_rs_prescale * fec_rs_period */
4245 fec_rs_prescale = 1 + (u16) (fec_rs_period_total >> 16);
4246 if (fec_rs_prescale == 0) {
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004247 /* Divide by zero (though impossible) */
4248 status = -EINVAL;
4249 if (status < 0)
4250 goto error;
4251 }
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03004252 fec_rs_period =
4253 ((u16) fec_rs_period_total +
4254 (fec_rs_prescale >> 1)) / fec_rs_prescale;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004255
4256 /* write corresponding registers */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03004257 status = write16(state, FEC_RS_MEASUREMENT_PERIOD__A, fec_rs_period);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004258 if (status < 0)
4259 goto error;
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03004260 status = write16(state, FEC_RS_MEASUREMENT_PRESCALE__A,
4261 fec_rs_prescale);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004262 if (status < 0)
4263 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03004264 status = write16(state, FEC_OC_SNC_FAIL_PERIOD__A, fec_rs_period);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004265error:
4266 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03004267 pr_err("Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004268 return status;
4269}
4270
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03004271static int set_qam16(struct drxk_state *state)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004272{
Oliver Endrissebc7de22011-07-03 13:49:44 -03004273 int status = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004274
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03004275 dprintk(1, "\n");
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004276 /* QAM Equalizer Setup */
4277 /* Equalizer */
4278 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD0__A, 13517);
4279 if (status < 0)
4280 goto error;
4281 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD1__A, 13517);
4282 if (status < 0)
4283 goto error;
4284 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD2__A, 13517);
4285 if (status < 0)
4286 goto error;
4287 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD3__A, 13517);
4288 if (status < 0)
4289 goto error;
4290 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD4__A, 13517);
4291 if (status < 0)
4292 goto error;
4293 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD5__A, 13517);
4294 if (status < 0)
4295 goto error;
4296 /* Decision Feedback Equalizer */
4297 status = write16(state, QAM_DQ_QUAL_FUN0__A, 2);
4298 if (status < 0)
4299 goto error;
4300 status = write16(state, QAM_DQ_QUAL_FUN1__A, 2);
4301 if (status < 0)
4302 goto error;
4303 status = write16(state, QAM_DQ_QUAL_FUN2__A, 2);
4304 if (status < 0)
4305 goto error;
4306 status = write16(state, QAM_DQ_QUAL_FUN3__A, 2);
4307 if (status < 0)
4308 goto error;
4309 status = write16(state, QAM_DQ_QUAL_FUN4__A, 2);
4310 if (status < 0)
4311 goto error;
4312 status = write16(state, QAM_DQ_QUAL_FUN5__A, 0);
4313 if (status < 0)
4314 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004315
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004316 status = write16(state, QAM_SY_SYNC_HWM__A, 5);
4317 if (status < 0)
4318 goto error;
4319 status = write16(state, QAM_SY_SYNC_AWM__A, 4);
4320 if (status < 0)
4321 goto error;
4322 status = write16(state, QAM_SY_SYNC_LWM__A, 3);
4323 if (status < 0)
4324 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004325
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004326 /* QAM Slicer Settings */
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03004327 status = write16(state, SCU_RAM_QAM_SL_SIG_POWER__A,
4328 DRXK_QAM_SL_SIG_POWER_QAM16);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004329 if (status < 0)
4330 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004331
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004332 /* QAM Loop Controller Coeficients */
4333 status = write16(state, SCU_RAM_QAM_LC_CA_FINE__A, 15);
4334 if (status < 0)
4335 goto error;
4336 status = write16(state, SCU_RAM_QAM_LC_CA_COARSE__A, 40);
4337 if (status < 0)
4338 goto error;
4339 status = write16(state, SCU_RAM_QAM_LC_EP_FINE__A, 12);
4340 if (status < 0)
4341 goto error;
4342 status = write16(state, SCU_RAM_QAM_LC_EP_MEDIUM__A, 24);
4343 if (status < 0)
4344 goto error;
4345 status = write16(state, SCU_RAM_QAM_LC_EP_COARSE__A, 24);
4346 if (status < 0)
4347 goto error;
4348 status = write16(state, SCU_RAM_QAM_LC_EI_FINE__A, 12);
4349 if (status < 0)
4350 goto error;
4351 status = write16(state, SCU_RAM_QAM_LC_EI_MEDIUM__A, 16);
4352 if (status < 0)
4353 goto error;
4354 status = write16(state, SCU_RAM_QAM_LC_EI_COARSE__A, 16);
4355 if (status < 0)
4356 goto error;
Oliver Endrissebc7de22011-07-03 13:49:44 -03004357
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004358 status = write16(state, SCU_RAM_QAM_LC_CP_FINE__A, 5);
4359 if (status < 0)
4360 goto error;
4361 status = write16(state, SCU_RAM_QAM_LC_CP_MEDIUM__A, 20);
4362 if (status < 0)
4363 goto error;
4364 status = write16(state, SCU_RAM_QAM_LC_CP_COARSE__A, 80);
4365 if (status < 0)
4366 goto error;
4367 status = write16(state, SCU_RAM_QAM_LC_CI_FINE__A, 5);
4368 if (status < 0)
4369 goto error;
4370 status = write16(state, SCU_RAM_QAM_LC_CI_MEDIUM__A, 20);
4371 if (status < 0)
4372 goto error;
4373 status = write16(state, SCU_RAM_QAM_LC_CI_COARSE__A, 50);
4374 if (status < 0)
4375 goto error;
4376 status = write16(state, SCU_RAM_QAM_LC_CF_FINE__A, 16);
4377 if (status < 0)
4378 goto error;
4379 status = write16(state, SCU_RAM_QAM_LC_CF_MEDIUM__A, 16);
4380 if (status < 0)
4381 goto error;
4382 status = write16(state, SCU_RAM_QAM_LC_CF_COARSE__A, 32);
4383 if (status < 0)
4384 goto error;
4385 status = write16(state, SCU_RAM_QAM_LC_CF1_FINE__A, 5);
4386 if (status < 0)
4387 goto error;
4388 status = write16(state, SCU_RAM_QAM_LC_CF1_MEDIUM__A, 10);
4389 if (status < 0)
4390 goto error;
4391 status = write16(state, SCU_RAM_QAM_LC_CF1_COARSE__A, 10);
4392 if (status < 0)
4393 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004394
4395
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004396 /* QAM State Machine (FSM) Thresholds */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004397
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004398 status = write16(state, SCU_RAM_QAM_FSM_RTH__A, 140);
4399 if (status < 0)
4400 goto error;
4401 status = write16(state, SCU_RAM_QAM_FSM_FTH__A, 50);
4402 if (status < 0)
4403 goto error;
4404 status = write16(state, SCU_RAM_QAM_FSM_CTH__A, 95);
4405 if (status < 0)
4406 goto error;
4407 status = write16(state, SCU_RAM_QAM_FSM_PTH__A, 120);
4408 if (status < 0)
4409 goto error;
4410 status = write16(state, SCU_RAM_QAM_FSM_QTH__A, 230);
4411 if (status < 0)
4412 goto error;
4413 status = write16(state, SCU_RAM_QAM_FSM_MTH__A, 105);
4414 if (status < 0)
4415 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004416
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004417 status = write16(state, SCU_RAM_QAM_FSM_RATE_LIM__A, 40);
4418 if (status < 0)
4419 goto error;
4420 status = write16(state, SCU_RAM_QAM_FSM_COUNT_LIM__A, 4);
4421 if (status < 0)
4422 goto error;
4423 status = write16(state, SCU_RAM_QAM_FSM_FREQ_LIM__A, 24);
4424 if (status < 0)
4425 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004426
4427
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004428 /* QAM FSM Tracking Parameters */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004429
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004430 status = write16(state, SCU_RAM_QAM_FSM_MEDIAN_AV_MULT__A, (u16) 16);
4431 if (status < 0)
4432 goto error;
4433 status = write16(state, SCU_RAM_QAM_FSM_RADIUS_AV_LIMIT__A, (u16) 220);
4434 if (status < 0)
4435 goto error;
4436 status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET1__A, (u16) 25);
4437 if (status < 0)
4438 goto error;
4439 status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET2__A, (u16) 6);
4440 if (status < 0)
4441 goto error;
4442 status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET3__A, (u16) -24);
4443 if (status < 0)
4444 goto error;
4445 status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET4__A, (u16) -65);
4446 if (status < 0)
4447 goto error;
4448 status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET5__A, (u16) -127);
4449 if (status < 0)
4450 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004451
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004452error:
4453 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03004454 pr_err("Error %d on %s\n", status, __func__);
Oliver Endrissebc7de22011-07-03 13:49:44 -03004455 return status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004456}
4457
4458/*============================================================================*/
4459
4460/**
4461* \brief QAM32 specific setup
4462* \param demod instance of demod.
4463* \return DRXStatus_t.
4464*/
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03004465static int set_qam32(struct drxk_state *state)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004466{
Oliver Endrissebc7de22011-07-03 13:49:44 -03004467 int status = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004468
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03004469 dprintk(1, "\n");
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004470
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004471 /* QAM Equalizer Setup */
4472 /* Equalizer */
4473 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD0__A, 6707);
4474 if (status < 0)
4475 goto error;
4476 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD1__A, 6707);
4477 if (status < 0)
4478 goto error;
4479 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD2__A, 6707);
4480 if (status < 0)
4481 goto error;
4482 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD3__A, 6707);
4483 if (status < 0)
4484 goto error;
4485 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD4__A, 6707);
4486 if (status < 0)
4487 goto error;
4488 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD5__A, 6707);
4489 if (status < 0)
4490 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004491
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004492 /* Decision Feedback Equalizer */
4493 status = write16(state, QAM_DQ_QUAL_FUN0__A, 3);
4494 if (status < 0)
4495 goto error;
4496 status = write16(state, QAM_DQ_QUAL_FUN1__A, 3);
4497 if (status < 0)
4498 goto error;
4499 status = write16(state, QAM_DQ_QUAL_FUN2__A, 3);
4500 if (status < 0)
4501 goto error;
4502 status = write16(state, QAM_DQ_QUAL_FUN3__A, 3);
4503 if (status < 0)
4504 goto error;
4505 status = write16(state, QAM_DQ_QUAL_FUN4__A, 3);
4506 if (status < 0)
4507 goto error;
4508 status = write16(state, QAM_DQ_QUAL_FUN5__A, 0);
4509 if (status < 0)
4510 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004511
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004512 status = write16(state, QAM_SY_SYNC_HWM__A, 6);
4513 if (status < 0)
4514 goto error;
4515 status = write16(state, QAM_SY_SYNC_AWM__A, 5);
4516 if (status < 0)
4517 goto error;
4518 status = write16(state, QAM_SY_SYNC_LWM__A, 3);
4519 if (status < 0)
4520 goto error;
Oliver Endrissebc7de22011-07-03 13:49:44 -03004521
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004522 /* QAM Slicer Settings */
4523
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03004524 status = write16(state, SCU_RAM_QAM_SL_SIG_POWER__A,
4525 DRXK_QAM_SL_SIG_POWER_QAM32);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004526 if (status < 0)
4527 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004528
4529
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004530 /* QAM Loop Controller Coeficients */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004531
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004532 status = write16(state, SCU_RAM_QAM_LC_CA_FINE__A, 15);
4533 if (status < 0)
4534 goto error;
4535 status = write16(state, SCU_RAM_QAM_LC_CA_COARSE__A, 40);
4536 if (status < 0)
4537 goto error;
4538 status = write16(state, SCU_RAM_QAM_LC_EP_FINE__A, 12);
4539 if (status < 0)
4540 goto error;
4541 status = write16(state, SCU_RAM_QAM_LC_EP_MEDIUM__A, 24);
4542 if (status < 0)
4543 goto error;
4544 status = write16(state, SCU_RAM_QAM_LC_EP_COARSE__A, 24);
4545 if (status < 0)
4546 goto error;
4547 status = write16(state, SCU_RAM_QAM_LC_EI_FINE__A, 12);
4548 if (status < 0)
4549 goto error;
4550 status = write16(state, SCU_RAM_QAM_LC_EI_MEDIUM__A, 16);
4551 if (status < 0)
4552 goto error;
4553 status = write16(state, SCU_RAM_QAM_LC_EI_COARSE__A, 16);
4554 if (status < 0)
4555 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004556
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004557 status = write16(state, SCU_RAM_QAM_LC_CP_FINE__A, 5);
4558 if (status < 0)
4559 goto error;
4560 status = write16(state, SCU_RAM_QAM_LC_CP_MEDIUM__A, 20);
4561 if (status < 0)
4562 goto error;
4563 status = write16(state, SCU_RAM_QAM_LC_CP_COARSE__A, 80);
4564 if (status < 0)
4565 goto error;
4566 status = write16(state, SCU_RAM_QAM_LC_CI_FINE__A, 5);
4567 if (status < 0)
4568 goto error;
4569 status = write16(state, SCU_RAM_QAM_LC_CI_MEDIUM__A, 20);
4570 if (status < 0)
4571 goto error;
4572 status = write16(state, SCU_RAM_QAM_LC_CI_COARSE__A, 50);
4573 if (status < 0)
4574 goto error;
4575 status = write16(state, SCU_RAM_QAM_LC_CF_FINE__A, 16);
4576 if (status < 0)
4577 goto error;
4578 status = write16(state, SCU_RAM_QAM_LC_CF_MEDIUM__A, 16);
4579 if (status < 0)
4580 goto error;
4581 status = write16(state, SCU_RAM_QAM_LC_CF_COARSE__A, 16);
4582 if (status < 0)
4583 goto error;
4584 status = write16(state, SCU_RAM_QAM_LC_CF1_FINE__A, 5);
4585 if (status < 0)
4586 goto error;
4587 status = write16(state, SCU_RAM_QAM_LC_CF1_MEDIUM__A, 10);
4588 if (status < 0)
4589 goto error;
4590 status = write16(state, SCU_RAM_QAM_LC_CF1_COARSE__A, 0);
4591 if (status < 0)
4592 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004593
4594
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004595 /* QAM State Machine (FSM) Thresholds */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004596
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004597 status = write16(state, SCU_RAM_QAM_FSM_RTH__A, 90);
4598 if (status < 0)
4599 goto error;
4600 status = write16(state, SCU_RAM_QAM_FSM_FTH__A, 50);
4601 if (status < 0)
4602 goto error;
4603 status = write16(state, SCU_RAM_QAM_FSM_CTH__A, 80);
4604 if (status < 0)
4605 goto error;
4606 status = write16(state, SCU_RAM_QAM_FSM_PTH__A, 100);
4607 if (status < 0)
4608 goto error;
4609 status = write16(state, SCU_RAM_QAM_FSM_QTH__A, 170);
4610 if (status < 0)
4611 goto error;
4612 status = write16(state, SCU_RAM_QAM_FSM_MTH__A, 100);
4613 if (status < 0)
4614 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004615
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004616 status = write16(state, SCU_RAM_QAM_FSM_RATE_LIM__A, 40);
4617 if (status < 0)
4618 goto error;
4619 status = write16(state, SCU_RAM_QAM_FSM_COUNT_LIM__A, 4);
4620 if (status < 0)
4621 goto error;
4622 status = write16(state, SCU_RAM_QAM_FSM_FREQ_LIM__A, 10);
4623 if (status < 0)
4624 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004625
4626
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004627 /* QAM FSM Tracking Parameters */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004628
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004629 status = write16(state, SCU_RAM_QAM_FSM_MEDIAN_AV_MULT__A, (u16) 12);
4630 if (status < 0)
4631 goto error;
4632 status = write16(state, SCU_RAM_QAM_FSM_RADIUS_AV_LIMIT__A, (u16) 140);
4633 if (status < 0)
4634 goto error;
4635 status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET1__A, (u16) -8);
4636 if (status < 0)
4637 goto error;
4638 status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET2__A, (u16) -16);
4639 if (status < 0)
4640 goto error;
4641 status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET3__A, (u16) -26);
4642 if (status < 0)
4643 goto error;
4644 status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET4__A, (u16) -56);
4645 if (status < 0)
4646 goto error;
4647 status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET5__A, (u16) -86);
4648error:
4649 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03004650 pr_err("Error %d on %s\n", status, __func__);
Oliver Endrissebc7de22011-07-03 13:49:44 -03004651 return status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004652}
4653
4654/*============================================================================*/
4655
4656/**
4657* \brief QAM64 specific setup
4658* \param demod instance of demod.
4659* \return DRXStatus_t.
4660*/
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03004661static int set_qam64(struct drxk_state *state)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004662{
Oliver Endrissebc7de22011-07-03 13:49:44 -03004663 int status = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004664
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03004665 dprintk(1, "\n");
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004666 /* QAM Equalizer Setup */
4667 /* Equalizer */
4668 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD0__A, 13336);
4669 if (status < 0)
4670 goto error;
4671 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD1__A, 12618);
4672 if (status < 0)
4673 goto error;
4674 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD2__A, 11988);
4675 if (status < 0)
4676 goto error;
4677 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD3__A, 13809);
4678 if (status < 0)
4679 goto error;
4680 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD4__A, 13809);
4681 if (status < 0)
4682 goto error;
4683 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD5__A, 15609);
4684 if (status < 0)
4685 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004686
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004687 /* Decision Feedback Equalizer */
4688 status = write16(state, QAM_DQ_QUAL_FUN0__A, 4);
4689 if (status < 0)
4690 goto error;
4691 status = write16(state, QAM_DQ_QUAL_FUN1__A, 4);
4692 if (status < 0)
4693 goto error;
4694 status = write16(state, QAM_DQ_QUAL_FUN2__A, 4);
4695 if (status < 0)
4696 goto error;
4697 status = write16(state, QAM_DQ_QUAL_FUN3__A, 4);
4698 if (status < 0)
4699 goto error;
4700 status = write16(state, QAM_DQ_QUAL_FUN4__A, 3);
4701 if (status < 0)
4702 goto error;
4703 status = write16(state, QAM_DQ_QUAL_FUN5__A, 0);
4704 if (status < 0)
4705 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004706
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004707 status = write16(state, QAM_SY_SYNC_HWM__A, 5);
4708 if (status < 0)
4709 goto error;
4710 status = write16(state, QAM_SY_SYNC_AWM__A, 4);
4711 if (status < 0)
4712 goto error;
4713 status = write16(state, QAM_SY_SYNC_LWM__A, 3);
4714 if (status < 0)
4715 goto error;
Oliver Endrissebc7de22011-07-03 13:49:44 -03004716
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004717 /* QAM Slicer Settings */
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03004718 status = write16(state, SCU_RAM_QAM_SL_SIG_POWER__A,
4719 DRXK_QAM_SL_SIG_POWER_QAM64);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004720 if (status < 0)
4721 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004722
4723
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004724 /* QAM Loop Controller Coeficients */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004725
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004726 status = write16(state, SCU_RAM_QAM_LC_CA_FINE__A, 15);
4727 if (status < 0)
4728 goto error;
4729 status = write16(state, SCU_RAM_QAM_LC_CA_COARSE__A, 40);
4730 if (status < 0)
4731 goto error;
4732 status = write16(state, SCU_RAM_QAM_LC_EP_FINE__A, 12);
4733 if (status < 0)
4734 goto error;
4735 status = write16(state, SCU_RAM_QAM_LC_EP_MEDIUM__A, 24);
4736 if (status < 0)
4737 goto error;
4738 status = write16(state, SCU_RAM_QAM_LC_EP_COARSE__A, 24);
4739 if (status < 0)
4740 goto error;
4741 status = write16(state, SCU_RAM_QAM_LC_EI_FINE__A, 12);
4742 if (status < 0)
4743 goto error;
4744 status = write16(state, SCU_RAM_QAM_LC_EI_MEDIUM__A, 16);
4745 if (status < 0)
4746 goto error;
4747 status = write16(state, SCU_RAM_QAM_LC_EI_COARSE__A, 16);
4748 if (status < 0)
4749 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004750
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004751 status = write16(state, SCU_RAM_QAM_LC_CP_FINE__A, 5);
4752 if (status < 0)
4753 goto error;
4754 status = write16(state, SCU_RAM_QAM_LC_CP_MEDIUM__A, 30);
4755 if (status < 0)
4756 goto error;
4757 status = write16(state, SCU_RAM_QAM_LC_CP_COARSE__A, 100);
4758 if (status < 0)
4759 goto error;
4760 status = write16(state, SCU_RAM_QAM_LC_CI_FINE__A, 5);
4761 if (status < 0)
4762 goto error;
4763 status = write16(state, SCU_RAM_QAM_LC_CI_MEDIUM__A, 30);
4764 if (status < 0)
4765 goto error;
4766 status = write16(state, SCU_RAM_QAM_LC_CI_COARSE__A, 50);
4767 if (status < 0)
4768 goto error;
4769 status = write16(state, SCU_RAM_QAM_LC_CF_FINE__A, 16);
4770 if (status < 0)
4771 goto error;
4772 status = write16(state, SCU_RAM_QAM_LC_CF_MEDIUM__A, 25);
4773 if (status < 0)
4774 goto error;
4775 status = write16(state, SCU_RAM_QAM_LC_CF_COARSE__A, 48);
4776 if (status < 0)
4777 goto error;
4778 status = write16(state, SCU_RAM_QAM_LC_CF1_FINE__A, 5);
4779 if (status < 0)
4780 goto error;
4781 status = write16(state, SCU_RAM_QAM_LC_CF1_MEDIUM__A, 10);
4782 if (status < 0)
4783 goto error;
4784 status = write16(state, SCU_RAM_QAM_LC_CF1_COARSE__A, 10);
4785 if (status < 0)
4786 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004787
4788
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004789 /* QAM State Machine (FSM) Thresholds */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004790
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004791 status = write16(state, SCU_RAM_QAM_FSM_RTH__A, 100);
4792 if (status < 0)
4793 goto error;
4794 status = write16(state, SCU_RAM_QAM_FSM_FTH__A, 60);
4795 if (status < 0)
4796 goto error;
4797 status = write16(state, SCU_RAM_QAM_FSM_CTH__A, 80);
4798 if (status < 0)
4799 goto error;
4800 status = write16(state, SCU_RAM_QAM_FSM_PTH__A, 110);
4801 if (status < 0)
4802 goto error;
4803 status = write16(state, SCU_RAM_QAM_FSM_QTH__A, 200);
4804 if (status < 0)
4805 goto error;
4806 status = write16(state, SCU_RAM_QAM_FSM_MTH__A, 95);
4807 if (status < 0)
4808 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004809
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004810 status = write16(state, SCU_RAM_QAM_FSM_RATE_LIM__A, 40);
4811 if (status < 0)
4812 goto error;
4813 status = write16(state, SCU_RAM_QAM_FSM_COUNT_LIM__A, 4);
4814 if (status < 0)
4815 goto error;
4816 status = write16(state, SCU_RAM_QAM_FSM_FREQ_LIM__A, 15);
4817 if (status < 0)
4818 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004819
4820
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004821 /* QAM FSM Tracking Parameters */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004822
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004823 status = write16(state, SCU_RAM_QAM_FSM_MEDIAN_AV_MULT__A, (u16) 12);
4824 if (status < 0)
4825 goto error;
4826 status = write16(state, SCU_RAM_QAM_FSM_RADIUS_AV_LIMIT__A, (u16) 141);
4827 if (status < 0)
4828 goto error;
4829 status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET1__A, (u16) 7);
4830 if (status < 0)
4831 goto error;
4832 status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET2__A, (u16) 0);
4833 if (status < 0)
4834 goto error;
4835 status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET3__A, (u16) -15);
4836 if (status < 0)
4837 goto error;
4838 status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET4__A, (u16) -45);
4839 if (status < 0)
4840 goto error;
4841 status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET5__A, (u16) -80);
4842error:
4843 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03004844 pr_err("Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004845
Oliver Endrissebc7de22011-07-03 13:49:44 -03004846 return status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004847}
4848
4849/*============================================================================*/
4850
4851/**
4852* \brief QAM128 specific setup
4853* \param demod: instance of demod.
4854* \return DRXStatus_t.
4855*/
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03004856static int set_qam128(struct drxk_state *state)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004857{
Oliver Endrissebc7de22011-07-03 13:49:44 -03004858 int status = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004859
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03004860 dprintk(1, "\n");
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004861 /* QAM Equalizer Setup */
4862 /* Equalizer */
4863 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD0__A, 6564);
4864 if (status < 0)
4865 goto error;
4866 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD1__A, 6598);
4867 if (status < 0)
4868 goto error;
4869 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD2__A, 6394);
4870 if (status < 0)
4871 goto error;
4872 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD3__A, 6409);
4873 if (status < 0)
4874 goto error;
4875 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD4__A, 6656);
4876 if (status < 0)
4877 goto error;
4878 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD5__A, 7238);
4879 if (status < 0)
4880 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004881
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004882 /* Decision Feedback Equalizer */
4883 status = write16(state, QAM_DQ_QUAL_FUN0__A, 6);
4884 if (status < 0)
4885 goto error;
4886 status = write16(state, QAM_DQ_QUAL_FUN1__A, 6);
4887 if (status < 0)
4888 goto error;
4889 status = write16(state, QAM_DQ_QUAL_FUN2__A, 6);
4890 if (status < 0)
4891 goto error;
4892 status = write16(state, QAM_DQ_QUAL_FUN3__A, 6);
4893 if (status < 0)
4894 goto error;
4895 status = write16(state, QAM_DQ_QUAL_FUN4__A, 5);
4896 if (status < 0)
4897 goto error;
4898 status = write16(state, QAM_DQ_QUAL_FUN5__A, 0);
4899 if (status < 0)
4900 goto error;
Oliver Endrissebc7de22011-07-03 13:49:44 -03004901
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004902 status = write16(state, QAM_SY_SYNC_HWM__A, 6);
4903 if (status < 0)
4904 goto error;
4905 status = write16(state, QAM_SY_SYNC_AWM__A, 5);
4906 if (status < 0)
4907 goto error;
4908 status = write16(state, QAM_SY_SYNC_LWM__A, 3);
4909 if (status < 0)
4910 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004911
4912
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004913 /* QAM Slicer Settings */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004914
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03004915 status = write16(state, SCU_RAM_QAM_SL_SIG_POWER__A,
4916 DRXK_QAM_SL_SIG_POWER_QAM128);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004917 if (status < 0)
4918 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004919
4920
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004921 /* QAM Loop Controller Coeficients */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004922
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004923 status = write16(state, SCU_RAM_QAM_LC_CA_FINE__A, 15);
4924 if (status < 0)
4925 goto error;
4926 status = write16(state, SCU_RAM_QAM_LC_CA_COARSE__A, 40);
4927 if (status < 0)
4928 goto error;
4929 status = write16(state, SCU_RAM_QAM_LC_EP_FINE__A, 12);
4930 if (status < 0)
4931 goto error;
4932 status = write16(state, SCU_RAM_QAM_LC_EP_MEDIUM__A, 24);
4933 if (status < 0)
4934 goto error;
4935 status = write16(state, SCU_RAM_QAM_LC_EP_COARSE__A, 24);
4936 if (status < 0)
4937 goto error;
4938 status = write16(state, SCU_RAM_QAM_LC_EI_FINE__A, 12);
4939 if (status < 0)
4940 goto error;
4941 status = write16(state, SCU_RAM_QAM_LC_EI_MEDIUM__A, 16);
4942 if (status < 0)
4943 goto error;
4944 status = write16(state, SCU_RAM_QAM_LC_EI_COARSE__A, 16);
4945 if (status < 0)
4946 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004947
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004948 status = write16(state, SCU_RAM_QAM_LC_CP_FINE__A, 5);
4949 if (status < 0)
4950 goto error;
4951 status = write16(state, SCU_RAM_QAM_LC_CP_MEDIUM__A, 40);
4952 if (status < 0)
4953 goto error;
4954 status = write16(state, SCU_RAM_QAM_LC_CP_COARSE__A, 120);
4955 if (status < 0)
4956 goto error;
4957 status = write16(state, SCU_RAM_QAM_LC_CI_FINE__A, 5);
4958 if (status < 0)
4959 goto error;
4960 status = write16(state, SCU_RAM_QAM_LC_CI_MEDIUM__A, 40);
4961 if (status < 0)
4962 goto error;
4963 status = write16(state, SCU_RAM_QAM_LC_CI_COARSE__A, 60);
4964 if (status < 0)
4965 goto error;
4966 status = write16(state, SCU_RAM_QAM_LC_CF_FINE__A, 16);
4967 if (status < 0)
4968 goto error;
4969 status = write16(state, SCU_RAM_QAM_LC_CF_MEDIUM__A, 25);
4970 if (status < 0)
4971 goto error;
4972 status = write16(state, SCU_RAM_QAM_LC_CF_COARSE__A, 64);
4973 if (status < 0)
4974 goto error;
4975 status = write16(state, SCU_RAM_QAM_LC_CF1_FINE__A, 5);
4976 if (status < 0)
4977 goto error;
4978 status = write16(state, SCU_RAM_QAM_LC_CF1_MEDIUM__A, 10);
4979 if (status < 0)
4980 goto error;
4981 status = write16(state, SCU_RAM_QAM_LC_CF1_COARSE__A, 0);
4982 if (status < 0)
4983 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004984
4985
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004986 /* QAM State Machine (FSM) Thresholds */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004987
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004988 status = write16(state, SCU_RAM_QAM_FSM_RTH__A, 50);
4989 if (status < 0)
4990 goto error;
4991 status = write16(state, SCU_RAM_QAM_FSM_FTH__A, 60);
4992 if (status < 0)
4993 goto error;
4994 status = write16(state, SCU_RAM_QAM_FSM_CTH__A, 80);
4995 if (status < 0)
4996 goto error;
4997 status = write16(state, SCU_RAM_QAM_FSM_PTH__A, 100);
4998 if (status < 0)
4999 goto error;
5000 status = write16(state, SCU_RAM_QAM_FSM_QTH__A, 140);
5001 if (status < 0)
5002 goto error;
5003 status = write16(state, SCU_RAM_QAM_FSM_MTH__A, 100);
5004 if (status < 0)
5005 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005006
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005007 status = write16(state, SCU_RAM_QAM_FSM_RATE_LIM__A, 40);
5008 if (status < 0)
5009 goto error;
5010 status = write16(state, SCU_RAM_QAM_FSM_COUNT_LIM__A, 5);
5011 if (status < 0)
5012 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005013
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005014 status = write16(state, SCU_RAM_QAM_FSM_FREQ_LIM__A, 12);
5015 if (status < 0)
5016 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005017
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005018 /* QAM FSM Tracking Parameters */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005019
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005020 status = write16(state, SCU_RAM_QAM_FSM_MEDIAN_AV_MULT__A, (u16) 8);
5021 if (status < 0)
5022 goto error;
5023 status = write16(state, SCU_RAM_QAM_FSM_RADIUS_AV_LIMIT__A, (u16) 65);
5024 if (status < 0)
5025 goto error;
5026 status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET1__A, (u16) 5);
5027 if (status < 0)
5028 goto error;
5029 status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET2__A, (u16) 3);
5030 if (status < 0)
5031 goto error;
5032 status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET3__A, (u16) -1);
5033 if (status < 0)
5034 goto error;
5035 status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET4__A, (u16) -12);
5036 if (status < 0)
5037 goto error;
5038 status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET5__A, (u16) -23);
5039error:
5040 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03005041 pr_err("Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005042
Oliver Endrissebc7de22011-07-03 13:49:44 -03005043 return status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005044}
5045
5046/*============================================================================*/
5047
5048/**
5049* \brief QAM256 specific setup
5050* \param demod: instance of demod.
5051* \return DRXStatus_t.
5052*/
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005053static int set_qam256(struct drxk_state *state)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005054{
Oliver Endrissebc7de22011-07-03 13:49:44 -03005055 int status = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005056
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03005057 dprintk(1, "\n");
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005058 /* QAM Equalizer Setup */
5059 /* Equalizer */
5060 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD0__A, 11502);
5061 if (status < 0)
5062 goto error;
5063 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD1__A, 12084);
5064 if (status < 0)
5065 goto error;
5066 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD2__A, 12543);
5067 if (status < 0)
5068 goto error;
5069 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD3__A, 12931);
5070 if (status < 0)
5071 goto error;
5072 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD4__A, 13629);
5073 if (status < 0)
5074 goto error;
5075 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD5__A, 15385);
5076 if (status < 0)
5077 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005078
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005079 /* Decision Feedback Equalizer */
5080 status = write16(state, QAM_DQ_QUAL_FUN0__A, 8);
5081 if (status < 0)
5082 goto error;
5083 status = write16(state, QAM_DQ_QUAL_FUN1__A, 8);
5084 if (status < 0)
5085 goto error;
5086 status = write16(state, QAM_DQ_QUAL_FUN2__A, 8);
5087 if (status < 0)
5088 goto error;
5089 status = write16(state, QAM_DQ_QUAL_FUN3__A, 8);
5090 if (status < 0)
5091 goto error;
5092 status = write16(state, QAM_DQ_QUAL_FUN4__A, 6);
5093 if (status < 0)
5094 goto error;
5095 status = write16(state, QAM_DQ_QUAL_FUN5__A, 0);
5096 if (status < 0)
5097 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005098
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005099 status = write16(state, QAM_SY_SYNC_HWM__A, 5);
5100 if (status < 0)
5101 goto error;
5102 status = write16(state, QAM_SY_SYNC_AWM__A, 4);
5103 if (status < 0)
5104 goto error;
5105 status = write16(state, QAM_SY_SYNC_LWM__A, 3);
5106 if (status < 0)
5107 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005108
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005109 /* QAM Slicer Settings */
Oliver Endrissebc7de22011-07-03 13:49:44 -03005110
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03005111 status = write16(state, SCU_RAM_QAM_SL_SIG_POWER__A,
5112 DRXK_QAM_SL_SIG_POWER_QAM256);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005113 if (status < 0)
5114 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005115
5116
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005117 /* QAM Loop Controller Coeficients */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005118
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005119 status = write16(state, SCU_RAM_QAM_LC_CA_FINE__A, 15);
5120 if (status < 0)
5121 goto error;
5122 status = write16(state, SCU_RAM_QAM_LC_CA_COARSE__A, 40);
5123 if (status < 0)
5124 goto error;
5125 status = write16(state, SCU_RAM_QAM_LC_EP_FINE__A, 12);
5126 if (status < 0)
5127 goto error;
5128 status = write16(state, SCU_RAM_QAM_LC_EP_MEDIUM__A, 24);
5129 if (status < 0)
5130 goto error;
5131 status = write16(state, SCU_RAM_QAM_LC_EP_COARSE__A, 24);
5132 if (status < 0)
5133 goto error;
5134 status = write16(state, SCU_RAM_QAM_LC_EI_FINE__A, 12);
5135 if (status < 0)
5136 goto error;
5137 status = write16(state, SCU_RAM_QAM_LC_EI_MEDIUM__A, 16);
5138 if (status < 0)
5139 goto error;
5140 status = write16(state, SCU_RAM_QAM_LC_EI_COARSE__A, 16);
5141 if (status < 0)
5142 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005143
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005144 status = write16(state, SCU_RAM_QAM_LC_CP_FINE__A, 5);
5145 if (status < 0)
5146 goto error;
5147 status = write16(state, SCU_RAM_QAM_LC_CP_MEDIUM__A, 50);
5148 if (status < 0)
5149 goto error;
5150 status = write16(state, SCU_RAM_QAM_LC_CP_COARSE__A, 250);
5151 if (status < 0)
5152 goto error;
5153 status = write16(state, SCU_RAM_QAM_LC_CI_FINE__A, 5);
5154 if (status < 0)
5155 goto error;
5156 status = write16(state, SCU_RAM_QAM_LC_CI_MEDIUM__A, 50);
5157 if (status < 0)
5158 goto error;
5159 status = write16(state, SCU_RAM_QAM_LC_CI_COARSE__A, 125);
5160 if (status < 0)
5161 goto error;
5162 status = write16(state, SCU_RAM_QAM_LC_CF_FINE__A, 16);
5163 if (status < 0)
5164 goto error;
5165 status = write16(state, SCU_RAM_QAM_LC_CF_MEDIUM__A, 25);
5166 if (status < 0)
5167 goto error;
5168 status = write16(state, SCU_RAM_QAM_LC_CF_COARSE__A, 48);
5169 if (status < 0)
5170 goto error;
5171 status = write16(state, SCU_RAM_QAM_LC_CF1_FINE__A, 5);
5172 if (status < 0)
5173 goto error;
5174 status = write16(state, SCU_RAM_QAM_LC_CF1_MEDIUM__A, 10);
5175 if (status < 0)
5176 goto error;
5177 status = write16(state, SCU_RAM_QAM_LC_CF1_COARSE__A, 10);
5178 if (status < 0)
5179 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005180
5181
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005182 /* QAM State Machine (FSM) Thresholds */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005183
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005184 status = write16(state, SCU_RAM_QAM_FSM_RTH__A, 50);
5185 if (status < 0)
5186 goto error;
5187 status = write16(state, SCU_RAM_QAM_FSM_FTH__A, 60);
5188 if (status < 0)
5189 goto error;
5190 status = write16(state, SCU_RAM_QAM_FSM_CTH__A, 80);
5191 if (status < 0)
5192 goto error;
5193 status = write16(state, SCU_RAM_QAM_FSM_PTH__A, 100);
5194 if (status < 0)
5195 goto error;
5196 status = write16(state, SCU_RAM_QAM_FSM_QTH__A, 150);
5197 if (status < 0)
5198 goto error;
5199 status = write16(state, SCU_RAM_QAM_FSM_MTH__A, 110);
5200 if (status < 0)
5201 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005202
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005203 status = write16(state, SCU_RAM_QAM_FSM_RATE_LIM__A, 40);
5204 if (status < 0)
5205 goto error;
5206 status = write16(state, SCU_RAM_QAM_FSM_COUNT_LIM__A, 4);
5207 if (status < 0)
5208 goto error;
5209 status = write16(state, SCU_RAM_QAM_FSM_FREQ_LIM__A, 12);
5210 if (status < 0)
5211 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005212
5213
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005214 /* QAM FSM Tracking Parameters */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005215
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005216 status = write16(state, SCU_RAM_QAM_FSM_MEDIAN_AV_MULT__A, (u16) 8);
5217 if (status < 0)
5218 goto error;
5219 status = write16(state, SCU_RAM_QAM_FSM_RADIUS_AV_LIMIT__A, (u16) 74);
5220 if (status < 0)
5221 goto error;
5222 status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET1__A, (u16) 18);
5223 if (status < 0)
5224 goto error;
5225 status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET2__A, (u16) 13);
5226 if (status < 0)
5227 goto error;
5228 status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET3__A, (u16) 7);
5229 if (status < 0)
5230 goto error;
5231 status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET4__A, (u16) 0);
5232 if (status < 0)
5233 goto error;
5234 status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET5__A, (u16) -8);
5235error:
5236 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03005237 pr_err("Error %d on %s\n", status, __func__);
Oliver Endrissebc7de22011-07-03 13:49:44 -03005238 return status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005239}
5240
5241
5242/*============================================================================*/
5243/**
5244* \brief Reset QAM block.
5245* \param demod: instance of demod.
5246* \param channel: pointer to channel data.
5247* \return DRXStatus_t.
5248*/
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005249static int qam_reset_qam(struct drxk_state *state)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005250{
Oliver Endrissebc7de22011-07-03 13:49:44 -03005251 int status;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005252 u16 cmd_result;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005253
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03005254 dprintk(1, "\n");
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005255 /* Stop QAM comstate->m_exec */
5256 status = write16(state, QAM_COMM_EXEC__A, QAM_COMM_EXEC_STOP);
5257 if (status < 0)
5258 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005259
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03005260 status = scu_command(state, SCU_RAM_COMMAND_STANDARD_QAM
5261 | SCU_RAM_COMMAND_CMD_DEMOD_RESET,
5262 0, NULL, 1, &cmd_result);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005263error:
5264 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03005265 pr_err("Error %d on %s\n", status, __func__);
Oliver Endrissebc7de22011-07-03 13:49:44 -03005266 return status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005267}
5268
5269/*============================================================================*/
5270
5271/**
5272* \brief Set QAM symbolrate.
5273* \param demod: instance of demod.
5274* \param channel: pointer to channel data.
5275* \return DRXStatus_t.
5276*/
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005277static int qam_set_symbolrate(struct drxk_state *state)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005278{
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005279 u32 adc_frequency = 0;
5280 u32 symb_freq = 0;
5281 u32 iqm_rc_rate = 0;
Oliver Endrissebc7de22011-07-03 13:49:44 -03005282 u16 ratesel = 0;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005283 u32 lc_symb_rate = 0;
Oliver Endrissebc7de22011-07-03 13:49:44 -03005284 int status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005285
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03005286 dprintk(1, "\n");
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005287 /* Select & calculate correct IQM rate */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005288 adc_frequency = (state->m_sys_clock_freq * 1000) / 3;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005289 ratesel = 0;
Mauro Carvalho Chehabed5452a2011-12-26 09:57:11 -03005290 /* printk(KERN_DEBUG "drxk: SR %d\n", state->props.symbol_rate); */
5291 if (state->props.symbol_rate <= 1188750)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005292 ratesel = 3;
Mauro Carvalho Chehabed5452a2011-12-26 09:57:11 -03005293 else if (state->props.symbol_rate <= 2377500)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005294 ratesel = 2;
Mauro Carvalho Chehabed5452a2011-12-26 09:57:11 -03005295 else if (state->props.symbol_rate <= 4755000)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005296 ratesel = 1;
5297 status = write16(state, IQM_FD_RATESEL__A, ratesel);
5298 if (status < 0)
5299 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005300
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005301 /*
5302 IqmRcRate = ((Fadc / (symbolrate * (4<<ratesel))) - 1) * (1<<23)
5303 */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005304 symb_freq = state->props.symbol_rate * (1 << ratesel);
5305 if (symb_freq == 0) {
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005306 /* Divide by zero */
5307 status = -EINVAL;
5308 goto error;
5309 }
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005310 iqm_rc_rate = (adc_frequency / symb_freq) * (1 << 21) +
5311 (Frac28a((adc_frequency % symb_freq), symb_freq) >> 7) -
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005312 (1 << 23);
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005313 status = write32(state, IQM_RC_RATE_OFS_LO__A, iqm_rc_rate);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005314 if (status < 0)
5315 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005316 state->m_iqm_rc_rate = iqm_rc_rate;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005317 /*
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005318 LcSymbFreq = round (.125 * symbolrate / adc_freq * (1<<15))
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005319 */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005320 symb_freq = state->props.symbol_rate;
5321 if (adc_frequency == 0) {
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005322 /* Divide by zero */
5323 status = -EINVAL;
5324 goto error;
5325 }
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005326 lc_symb_rate = (symb_freq / adc_frequency) * (1 << 12) +
5327 (Frac28a((symb_freq % adc_frequency), adc_frequency) >>
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005328 16);
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005329 if (lc_symb_rate > 511)
5330 lc_symb_rate = 511;
5331 status = write16(state, QAM_LC_SYMBOL_FREQ__A, (u16) lc_symb_rate);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005332
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005333error:
5334 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03005335 pr_err("Error %d on %s\n", status, __func__);
Oliver Endrissebc7de22011-07-03 13:49:44 -03005336 return status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005337}
5338
5339/*============================================================================*/
5340
5341/**
5342* \brief Get QAM lock status.
5343* \param demod: instance of demod.
5344* \param channel: pointer to channel data.
5345* \return DRXStatus_t.
5346*/
5347
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005348static int get_qam_lock_status(struct drxk_state *state, u32 *p_lock_status)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005349{
5350 int status;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005351 u16 result[2] = { 0, 0 };
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005352
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03005353 dprintk(1, "\n");
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005354 *p_lock_status = NOT_LOCKED;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005355 status = scu_command(state,
Oliver Endrissebc7de22011-07-03 13:49:44 -03005356 SCU_RAM_COMMAND_STANDARD_QAM |
5357 SCU_RAM_COMMAND_CMD_DEMOD_GET_LOCK, 0, NULL, 2,
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005358 result);
Oliver Endrissebc7de22011-07-03 13:49:44 -03005359 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03005360 pr_err("Error %d on %s\n", status, __func__);
Oliver Endrissebc7de22011-07-03 13:49:44 -03005361
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005362 if (result[1] < SCU_RAM_QAM_LOCKED_LOCKED_DEMOD_LOCKED) {
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005363 /* 0x0000 NOT LOCKED */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005364 } else if (result[1] < SCU_RAM_QAM_LOCKED_LOCKED_LOCKED) {
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005365 /* 0x4000 DEMOD LOCKED */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005366 *p_lock_status = DEMOD_LOCK;
5367 } else if (result[1] < SCU_RAM_QAM_LOCKED_LOCKED_NEVER_LOCK) {
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005368 /* 0x8000 DEMOD + FEC LOCKED (system lock) */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005369 *p_lock_status = MPEG_LOCK;
Oliver Endrissebc7de22011-07-03 13:49:44 -03005370 } else {
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005371 /* 0xC000 NEVER LOCKED */
5372 /* (system will never be able to lock to the signal) */
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03005373 /*
5374 * TODO: check this, intermediate & standard specific lock
5375 * states are not taken into account here
5376 */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005377 *p_lock_status = NEVER_LOCK;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005378 }
5379 return status;
5380}
5381
5382#define QAM_MIRROR__M 0x03
5383#define QAM_MIRROR_NORMAL 0x00
5384#define QAM_MIRRORED 0x01
5385#define QAM_MIRROR_AUTO_ON 0x02
5386#define QAM_LOCKRANGE__M 0x10
5387#define QAM_LOCKRANGE_NORMAL 0x10
5388
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005389static int qam_demodulator_command(struct drxk_state *state,
5390 int number_of_parameters)
Martin Blumenstingl9e23f50a2012-07-04 17:36:55 -03005391{
5392 int status;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005393 u16 cmd_result;
5394 u16 set_param_parameters[4] = { 0, 0, 0, 0 };
Martin Blumenstingl9e23f50a2012-07-04 17:36:55 -03005395
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005396 set_param_parameters[0] = state->m_constellation; /* modulation */
5397 set_param_parameters[1] = DRXK_QAM_I12_J17; /* interleave mode */
Martin Blumenstingl9e23f50a2012-07-04 17:36:55 -03005398
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005399 if (number_of_parameters == 2) {
5400 u16 set_env_parameters[1] = { 0 };
Martin Blumenstingl9e23f50a2012-07-04 17:36:55 -03005401
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005402 if (state->m_operation_mode == OM_QAM_ITU_C)
5403 set_env_parameters[0] = QAM_TOP_ANNEX_C;
Martin Blumenstingl9e23f50a2012-07-04 17:36:55 -03005404 else
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005405 set_env_parameters[0] = QAM_TOP_ANNEX_A;
Martin Blumenstingl9e23f50a2012-07-04 17:36:55 -03005406
5407 status = scu_command(state,
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03005408 SCU_RAM_COMMAND_STANDARD_QAM
5409 | SCU_RAM_COMMAND_CMD_DEMOD_SET_ENV,
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005410 1, set_env_parameters, 1, &cmd_result);
Martin Blumenstingl9e23f50a2012-07-04 17:36:55 -03005411 if (status < 0)
5412 goto error;
5413
5414 status = scu_command(state,
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03005415 SCU_RAM_COMMAND_STANDARD_QAM
5416 | SCU_RAM_COMMAND_CMD_DEMOD_SET_PARAM,
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005417 number_of_parameters, set_param_parameters,
5418 1, &cmd_result);
5419 } else if (number_of_parameters == 4) {
5420 if (state->m_operation_mode == OM_QAM_ITU_C)
5421 set_param_parameters[2] = QAM_TOP_ANNEX_C;
Martin Blumenstingl9e23f50a2012-07-04 17:36:55 -03005422 else
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005423 set_param_parameters[2] = QAM_TOP_ANNEX_A;
Martin Blumenstingl9e23f50a2012-07-04 17:36:55 -03005424
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005425 set_param_parameters[3] |= (QAM_MIRROR_AUTO_ON);
Martin Blumenstingl9e23f50a2012-07-04 17:36:55 -03005426 /* Env parameters */
5427 /* check for LOCKRANGE Extented */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005428 /* set_param_parameters[3] |= QAM_LOCKRANGE_NORMAL; */
Martin Blumenstingl9e23f50a2012-07-04 17:36:55 -03005429
5430 status = scu_command(state,
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03005431 SCU_RAM_COMMAND_STANDARD_QAM
5432 | SCU_RAM_COMMAND_CMD_DEMOD_SET_PARAM,
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005433 number_of_parameters, set_param_parameters,
5434 1, &cmd_result);
Martin Blumenstingl9e23f50a2012-07-04 17:36:55 -03005435 } else {
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03005436 pr_warn("Unknown QAM demodulator parameter count %d\n",
5437 number_of_parameters);
Mauro Carvalho Chehab94af1b62012-10-29 07:58:59 -02005438 status = -EINVAL;
Martin Blumenstingl9e23f50a2012-07-04 17:36:55 -03005439 }
5440
5441error:
5442 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03005443 pr_warn("Warning %d on %s\n", status, __func__);
Martin Blumenstingl9e23f50a2012-07-04 17:36:55 -03005444 return status;
5445}
5446
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005447static int set_qam(struct drxk_state *state, u16 intermediate_freqk_hz,
5448 s32 tuner_freq_offset)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005449{
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005450 int status;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005451 u16 cmd_result;
5452 int qam_demod_param_count = state->qam_demod_parameter_count;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005453
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03005454 dprintk(1, "\n");
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005455 /*
Mauro Carvalho Chehab119faf92011-07-24 09:11:36 -03005456 * STEP 1: reset demodulator
5457 * resets FEC DI and FEC RS
5458 * resets QAM block
5459 * resets SCU variables
5460 */
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005461 status = write16(state, FEC_DI_COMM_EXEC__A, FEC_DI_COMM_EXEC_STOP);
Oliver Endrissebc7de22011-07-03 13:49:44 -03005462 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005463 goto error;
5464 status = write16(state, FEC_RS_COMM_EXEC__A, FEC_RS_COMM_EXEC_STOP);
5465 if (status < 0)
5466 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005467 status = qam_reset_qam(state);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005468 if (status < 0)
5469 goto error;
Oliver Endrissebc7de22011-07-03 13:49:44 -03005470
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005471 /*
Mauro Carvalho Chehab119faf92011-07-24 09:11:36 -03005472 * STEP 2: configure demodulator
5473 * -set params; resets IQM,QAM,FEC HW; initializes some
5474 * SCU variables
5475 */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005476 status = qam_set_symbolrate(state);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005477 if (status < 0)
5478 goto error;
5479
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005480 /* Set params */
Mauro Carvalho Chehabed5452a2011-12-26 09:57:11 -03005481 switch (state->props.modulation) {
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005482 case QAM_256:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005483 state->m_constellation = DRX_CONSTELLATION_QAM256;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005484 break;
5485 case QAM_AUTO:
5486 case QAM_64:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005487 state->m_constellation = DRX_CONSTELLATION_QAM64;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005488 break;
5489 case QAM_16:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005490 state->m_constellation = DRX_CONSTELLATION_QAM16;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005491 break;
5492 case QAM_32:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005493 state->m_constellation = DRX_CONSTELLATION_QAM32;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005494 break;
5495 case QAM_128:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005496 state->m_constellation = DRX_CONSTELLATION_QAM128;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005497 break;
5498 default:
5499 status = -EINVAL;
5500 break;
5501 }
5502 if (status < 0)
5503 goto error;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005504
Martin Blumenstingl9e23f50a2012-07-04 17:36:55 -03005505 /* Use the 4-parameter if it's requested or we're probing for
5506 * the correct command. */
5507 if (state->qam_demod_parameter_count == 4
5508 || !state->qam_demod_parameter_count) {
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005509 qam_demod_param_count = 4;
5510 status = qam_demodulator_command(state, qam_demod_param_count);
Mauro Carvalho Chehab5eee2bb2011-07-10 14:33:29 -03005511 }
Martin Blumenstingl9e23f50a2012-07-04 17:36:55 -03005512
5513 /* Use the 2-parameter command if it was requested or if we're
5514 * probing for the correct command and the 4-parameter command
5515 * failed. */
5516 if (state->qam_demod_parameter_count == 2
5517 || (!state->qam_demod_parameter_count && status < 0)) {
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005518 qam_demod_param_count = 2;
5519 status = qam_demodulator_command(state, qam_demod_param_count);
Martin Blumenstingl9e23f50a2012-07-04 17:36:55 -03005520 }
5521
5522 if (status < 0) {
Mauro Carvalho Chehab0fb220f2013-04-28 11:47:46 -03005523 dprintk(1, "Could not set demodulator parameters.\n");
5524 dprintk(1,
5525 "Make sure qam_demod_parameter_count (%d) is correct for your firmware (%s).\n",
Martin Blumenstingl9e23f50a2012-07-04 17:36:55 -03005526 state->qam_demod_parameter_count,
5527 state->microcode_name);
Mauro Carvalho Chehab5eee2bb2011-07-10 14:33:29 -03005528 goto error;
Martin Blumenstingl9e23f50a2012-07-04 17:36:55 -03005529 } else if (!state->qam_demod_parameter_count) {
Mauro Carvalho Chehab0fb220f2013-04-28 11:47:46 -03005530 dprintk(1,
5531 "Auto-probing the QAM command parameters was successful - using %d parameters.\n",
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005532 qam_demod_param_count);
Martin Blumenstingl9e23f50a2012-07-04 17:36:55 -03005533
Mauro Carvalho Chehab7eaf7182012-07-06 14:53:51 -03005534 /*
5535 * One of our commands was successful. We don't need to
5536 * auto-probe anymore, now that we got the correct command.
5537 */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005538 state->qam_demod_parameter_count = qam_demod_param_count;
Martin Blumenstingl9e23f50a2012-07-04 17:36:55 -03005539 }
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005540
Mauro Carvalho Chehab119faf92011-07-24 09:11:36 -03005541 /*
5542 * STEP 3: enable the system in a mode where the ADC provides valid
Mauro Carvalho Chehabed5452a2011-12-26 09:57:11 -03005543 * signal setup modulation independent registers
Mauro Carvalho Chehab119faf92011-07-24 09:11:36 -03005544 */
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005545#if 0
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005546 status = set_frequency(channel, tuner_freq_offset));
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005547 if (status < 0)
5548 goto error;
5549#endif
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03005550 status = set_frequency_shifter(state, intermediate_freqk_hz,
5551 tuner_freq_offset, true);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005552 if (status < 0)
5553 goto error;
5554
5555 /* Setup BER measurement */
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03005556 status = set_qam_measurement(state, state->m_constellation,
5557 state->props.symbol_rate);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005558 if (status < 0)
5559 goto error;
5560
5561 /* Reset default values */
5562 status = write16(state, IQM_CF_SCALE_SH__A, IQM_CF_SCALE_SH__PRE);
5563 if (status < 0)
5564 goto error;
5565 status = write16(state, QAM_SY_TIMEOUT__A, QAM_SY_TIMEOUT__PRE);
5566 if (status < 0)
5567 goto error;
5568
5569 /* Reset default LC values */
5570 status = write16(state, QAM_LC_RATE_LIMIT__A, 3);
5571 if (status < 0)
5572 goto error;
5573 status = write16(state, QAM_LC_LPF_FACTORP__A, 4);
5574 if (status < 0)
5575 goto error;
5576 status = write16(state, QAM_LC_LPF_FACTORI__A, 4);
5577 if (status < 0)
5578 goto error;
5579 status = write16(state, QAM_LC_MODE__A, 7);
5580 if (status < 0)
5581 goto error;
5582
5583 status = write16(state, QAM_LC_QUAL_TAB0__A, 1);
5584 if (status < 0)
5585 goto error;
5586 status = write16(state, QAM_LC_QUAL_TAB1__A, 1);
5587 if (status < 0)
5588 goto error;
5589 status = write16(state, QAM_LC_QUAL_TAB2__A, 1);
5590 if (status < 0)
5591 goto error;
5592 status = write16(state, QAM_LC_QUAL_TAB3__A, 1);
5593 if (status < 0)
5594 goto error;
5595 status = write16(state, QAM_LC_QUAL_TAB4__A, 2);
5596 if (status < 0)
5597 goto error;
5598 status = write16(state, QAM_LC_QUAL_TAB5__A, 2);
5599 if (status < 0)
5600 goto error;
5601 status = write16(state, QAM_LC_QUAL_TAB6__A, 2);
5602 if (status < 0)
5603 goto error;
5604 status = write16(state, QAM_LC_QUAL_TAB8__A, 2);
5605 if (status < 0)
5606 goto error;
5607 status = write16(state, QAM_LC_QUAL_TAB9__A, 2);
5608 if (status < 0)
5609 goto error;
5610 status = write16(state, QAM_LC_QUAL_TAB10__A, 2);
5611 if (status < 0)
5612 goto error;
5613 status = write16(state, QAM_LC_QUAL_TAB12__A, 2);
5614 if (status < 0)
5615 goto error;
5616 status = write16(state, QAM_LC_QUAL_TAB15__A, 3);
5617 if (status < 0)
5618 goto error;
5619 status = write16(state, QAM_LC_QUAL_TAB16__A, 3);
5620 if (status < 0)
5621 goto error;
5622 status = write16(state, QAM_LC_QUAL_TAB20__A, 4);
5623 if (status < 0)
5624 goto error;
5625 status = write16(state, QAM_LC_QUAL_TAB25__A, 4);
5626 if (status < 0)
5627 goto error;
5628
5629 /* Mirroring, QAM-block starting point not inverted */
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03005630 status = write16(state, QAM_SY_SP_INV__A,
5631 QAM_SY_SP_INV_SPECTRUM_INV_DIS);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005632 if (status < 0)
5633 goto error;
5634
5635 /* Halt SCU to enable safe non-atomic accesses */
5636 status = write16(state, SCU_COMM_EXEC__A, SCU_COMM_EXEC_HOLD);
5637 if (status < 0)
5638 goto error;
5639
Mauro Carvalho Chehabed5452a2011-12-26 09:57:11 -03005640 /* STEP 4: modulation specific setup */
5641 switch (state->props.modulation) {
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005642 case QAM_16:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005643 status = set_qam16(state);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005644 break;
5645 case QAM_32:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005646 status = set_qam32(state);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005647 break;
5648 case QAM_AUTO:
5649 case QAM_64:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005650 status = set_qam64(state);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005651 break;
5652 case QAM_128:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005653 status = set_qam128(state);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005654 break;
5655 case QAM_256:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005656 status = set_qam256(state);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005657 break;
5658 default:
5659 status = -EINVAL;
5660 break;
5661 }
5662 if (status < 0)
5663 goto error;
5664
5665 /* Activate SCU to enable SCU commands */
5666 status = write16(state, SCU_COMM_EXEC__A, SCU_COMM_EXEC_ACTIVE);
5667 if (status < 0)
5668 goto error;
5669
5670 /* Re-configure MPEG output, requires knowledge of channel bitrate */
Mauro Carvalho Chehabed5452a2011-12-26 09:57:11 -03005671 /* extAttr->currentChannel.modulation = channel->modulation; */
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005672 /* extAttr->currentChannel.symbolrate = channel->symbolrate; */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005673 status = mpegts_dto_setup(state, state->m_operation_mode);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005674 if (status < 0)
5675 goto error;
5676
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005677 /* start processes */
5678 status = mpegts_start(state);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005679 if (status < 0)
5680 goto error;
5681 status = write16(state, FEC_COMM_EXEC__A, FEC_COMM_EXEC_ACTIVE);
5682 if (status < 0)
5683 goto error;
5684 status = write16(state, QAM_COMM_EXEC__A, QAM_COMM_EXEC_ACTIVE);
5685 if (status < 0)
5686 goto error;
5687 status = write16(state, IQM_COMM_EXEC__A, IQM_COMM_EXEC_B_ACTIVE);
5688 if (status < 0)
5689 goto error;
5690
5691 /* STEP 5: start QAM demodulator (starts FEC, QAM and IQM HW) */
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03005692 status = scu_command(state, SCU_RAM_COMMAND_STANDARD_QAM
5693 | SCU_RAM_COMMAND_CMD_DEMOD_START,
5694 0, NULL, 1, &cmd_result);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005695 if (status < 0)
5696 goto error;
5697
5698 /* update global DRXK data container */
5699/*? extAttr->qamInterleaveMode = DRXK_QAM_I12_J17; */
5700
5701error:
5702 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03005703 pr_err("Error %d on %s\n", status, __func__);
Oliver Endrissebc7de22011-07-03 13:49:44 -03005704 return status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005705}
5706
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005707static int set_qam_standard(struct drxk_state *state,
5708 enum operation_mode o_mode)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005709{
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005710 int status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005711#ifdef DRXK_QAM_TAPS
5712#define DRXK_QAMA_TAPS_SELECT
5713#include "drxk_filters.h"
5714#undef DRXK_QAMA_TAPS_SELECT
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005715#endif
5716
Mauro Carvalho Chehabf1b82972011-07-10 13:08:44 -03005717 dprintk(1, "\n");
5718
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005719 /* added antenna switch */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005720 switch_antenna_to_qam(state);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005721
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005722 /* Ensure correct power-up mode */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005723 status = power_up_qam(state);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005724 if (status < 0)
5725 goto error;
5726 /* Reset QAM block */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005727 status = qam_reset_qam(state);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005728 if (status < 0)
5729 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005730
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005731 /* Setup IQM */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005732
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005733 status = write16(state, IQM_COMM_EXEC__A, IQM_COMM_EXEC_B_STOP);
5734 if (status < 0)
5735 goto error;
5736 status = write16(state, IQM_AF_AMUX__A, IQM_AF_AMUX_SIGNAL2ADC);
5737 if (status < 0)
5738 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005739
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005740 /* Upload IQM Channel Filter settings by
5741 boot loader from ROM table */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005742 switch (o_mode) {
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005743 case OM_QAM_ITU_A:
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03005744 status = bl_chain_cmd(state, DRXK_BL_ROM_OFFSET_TAPS_ITU_A,
5745 DRXK_BLCC_NR_ELEMENTS_TAPS,
5746 DRXK_BLC_TIMEOUT);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005747 break;
5748 case OM_QAM_ITU_C:
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03005749 status = bl_direct_cmd(state, IQM_CF_TAP_RE0__A,
5750 DRXK_BL_ROM_OFFSET_TAPS_ITU_C,
5751 DRXK_BLDC_NR_ELEMENTS_TAPS,
5752 DRXK_BLC_TIMEOUT);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03005753 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005754 goto error;
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03005755 status = bl_direct_cmd(state,
5756 IQM_CF_TAP_IM0__A,
5757 DRXK_BL_ROM_OFFSET_TAPS_ITU_C,
5758 DRXK_BLDC_NR_ELEMENTS_TAPS,
5759 DRXK_BLC_TIMEOUT);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005760 break;
5761 default:
5762 status = -EINVAL;
5763 }
5764 if (status < 0)
5765 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005766
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03005767 status = write16(state, IQM_CF_OUT_ENA__A, 1 << IQM_CF_OUT_ENA_QAM__B);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005768 if (status < 0)
5769 goto error;
5770 status = write16(state, IQM_CF_SYMMETRIC__A, 0);
5771 if (status < 0)
5772 goto error;
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03005773 status = write16(state, IQM_CF_MIDTAP__A,
5774 ((1 << IQM_CF_MIDTAP_RE__B) | (1 << IQM_CF_MIDTAP_IM__B)));
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005775 if (status < 0)
5776 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005777
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005778 status = write16(state, IQM_RC_STRETCH__A, 21);
5779 if (status < 0)
5780 goto error;
5781 status = write16(state, IQM_AF_CLP_LEN__A, 0);
5782 if (status < 0)
5783 goto error;
5784 status = write16(state, IQM_AF_CLP_TH__A, 448);
5785 if (status < 0)
5786 goto error;
5787 status = write16(state, IQM_AF_SNS_LEN__A, 0);
5788 if (status < 0)
5789 goto error;
5790 status = write16(state, IQM_CF_POW_MEAS_LEN__A, 0);
5791 if (status < 0)
5792 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005793
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005794 status = write16(state, IQM_FS_ADJ_SEL__A, 1);
5795 if (status < 0)
5796 goto error;
5797 status = write16(state, IQM_RC_ADJ_SEL__A, 1);
5798 if (status < 0)
5799 goto error;
5800 status = write16(state, IQM_CF_ADJ_SEL__A, 1);
5801 if (status < 0)
5802 goto error;
5803 status = write16(state, IQM_AF_UPD_SEL__A, 0);
5804 if (status < 0)
5805 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005806
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005807 /* IQM Impulse Noise Processing Unit */
5808 status = write16(state, IQM_CF_CLP_VAL__A, 500);
5809 if (status < 0)
5810 goto error;
5811 status = write16(state, IQM_CF_DATATH__A, 1000);
5812 if (status < 0)
5813 goto error;
5814 status = write16(state, IQM_CF_BYPASSDET__A, 1);
5815 if (status < 0)
5816 goto error;
5817 status = write16(state, IQM_CF_DET_LCT__A, 0);
5818 if (status < 0)
5819 goto error;
5820 status = write16(state, IQM_CF_WND_LEN__A, 1);
5821 if (status < 0)
5822 goto error;
5823 status = write16(state, IQM_CF_PKDTH__A, 1);
5824 if (status < 0)
5825 goto error;
5826 status = write16(state, IQM_AF_INC_BYPASS__A, 1);
5827 if (status < 0)
5828 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005829
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005830 /* turn on IQMAF. Must be done before setAgc**() */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005831 status = set_iqm_af(state, true);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005832 if (status < 0)
5833 goto error;
5834 status = write16(state, IQM_AF_START_LOCK__A, 0x01);
5835 if (status < 0)
5836 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005837
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005838 /* IQM will not be reset from here, sync ADC and update/init AGC */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005839 status = adc_synchronization(state);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005840 if (status < 0)
5841 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005842
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005843 /* Set the FSM step period */
5844 status = write16(state, SCU_RAM_QAM_FSM_STEP_PERIOD__A, 2000);
5845 if (status < 0)
5846 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005847
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005848 /* Halt SCU to enable safe non-atomic accesses */
5849 status = write16(state, SCU_COMM_EXEC__A, SCU_COMM_EXEC_HOLD);
5850 if (status < 0)
5851 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005852
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005853 /* No more resets of the IQM, current standard correctly set =>
5854 now AGCs can be configured. */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005855
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005856 status = init_agc(state, true);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005857 if (status < 0)
5858 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005859 status = set_pre_saw(state, &(state->m_qam_pre_saw_cfg));
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005860 if (status < 0)
5861 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005862
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005863 /* Configure AGC's */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005864 status = set_agc_rf(state, &(state->m_qam_rf_agc_cfg), true);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005865 if (status < 0)
5866 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005867 status = set_agc_if(state, &(state->m_qam_if_agc_cfg), true);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005868 if (status < 0)
5869 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005870
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005871 /* Activate SCU to enable SCU commands */
5872 status = write16(state, SCU_COMM_EXEC__A, SCU_COMM_EXEC_ACTIVE);
5873error:
5874 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03005875 pr_err("Error %d on %s\n", status, __func__);
Oliver Endrissebc7de22011-07-03 13:49:44 -03005876 return status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005877}
5878
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005879static int write_gpio(struct drxk_state *state)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005880{
Oliver Endrissebc7de22011-07-03 13:49:44 -03005881 int status;
5882 u16 value = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005883
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03005884 dprintk(1, "\n");
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005885 /* stop lock indicator process */
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03005886 status = write16(state, SCU_RAM_GPIO__A,
5887 SCU_RAM_GPIO_HW_LOCK_IND_DISABLE);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005888 if (status < 0)
5889 goto error;
5890
5891 /* Write magic word to enable pdr reg write */
5892 status = write16(state, SIO_TOP_COMM_KEY__A, SIO_TOP_COMM_KEY_KEY);
5893 if (status < 0)
5894 goto error;
5895
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005896 if (state->m_has_sawsw) {
5897 if (state->uio_mask & 0x0001) { /* UIO-1 */
Mauro Carvalho Chehab90796ac2011-07-10 09:36:30 -03005898 /* write to io pad configuration register - output mode */
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03005899 status = write16(state, SIO_PDR_SMA_TX_CFG__A,
5900 state->m_gpio_cfg);
Mauro Carvalho Chehab90796ac2011-07-10 09:36:30 -03005901 if (status < 0)
5902 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005903
Mauro Carvalho Chehab90796ac2011-07-10 09:36:30 -03005904 /* use corresponding bit in io data output registar */
5905 status = read16(state, SIO_PDR_UIO_OUT_LO__A, &value);
5906 if (status < 0)
5907 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005908 if ((state->m_gpio & 0x0001) == 0)
Mauro Carvalho Chehab90796ac2011-07-10 09:36:30 -03005909 value &= 0x7FFF; /* write zero to 15th bit - 1st UIO */
5910 else
5911 value |= 0x8000; /* write one to 15th bit - 1st UIO */
5912 /* write back to io data output register */
5913 status = write16(state, SIO_PDR_UIO_OUT_LO__A, value);
5914 if (status < 0)
5915 goto error;
5916 }
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005917 if (state->uio_mask & 0x0002) { /* UIO-2 */
Mauro Carvalho Chehab90796ac2011-07-10 09:36:30 -03005918 /* write to io pad configuration register - output mode */
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03005919 status = write16(state, SIO_PDR_SMA_RX_CFG__A,
5920 state->m_gpio_cfg);
Mauro Carvalho Chehab90796ac2011-07-10 09:36:30 -03005921 if (status < 0)
5922 goto error;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005923
Mauro Carvalho Chehab90796ac2011-07-10 09:36:30 -03005924 /* use corresponding bit in io data output registar */
5925 status = read16(state, SIO_PDR_UIO_OUT_LO__A, &value);
5926 if (status < 0)
5927 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005928 if ((state->m_gpio & 0x0002) == 0)
Mauro Carvalho Chehab90796ac2011-07-10 09:36:30 -03005929 value &= 0xBFFF; /* write zero to 14th bit - 2st UIO */
5930 else
5931 value |= 0x4000; /* write one to 14th bit - 2st UIO */
5932 /* write back to io data output register */
5933 status = write16(state, SIO_PDR_UIO_OUT_LO__A, value);
5934 if (status < 0)
5935 goto error;
5936 }
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005937 if (state->uio_mask & 0x0004) { /* UIO-3 */
Mauro Carvalho Chehab90796ac2011-07-10 09:36:30 -03005938 /* write to io pad configuration register - output mode */
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03005939 status = write16(state, SIO_PDR_GPIO_CFG__A,
5940 state->m_gpio_cfg);
Mauro Carvalho Chehab90796ac2011-07-10 09:36:30 -03005941 if (status < 0)
5942 goto error;
5943
5944 /* use corresponding bit in io data output registar */
5945 status = read16(state, SIO_PDR_UIO_OUT_LO__A, &value);
5946 if (status < 0)
5947 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005948 if ((state->m_gpio & 0x0004) == 0)
Mauro Carvalho Chehab90796ac2011-07-10 09:36:30 -03005949 value &= 0xFFFB; /* write zero to 2nd bit - 3rd UIO */
5950 else
5951 value |= 0x0004; /* write one to 2nd bit - 3rd UIO */
5952 /* write back to io data output register */
5953 status = write16(state, SIO_PDR_UIO_OUT_LO__A, value);
5954 if (status < 0)
5955 goto error;
5956 }
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005957 }
5958 /* Write magic word to disable pdr reg write */
5959 status = write16(state, SIO_TOP_COMM_KEY__A, 0x0000);
5960error:
5961 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03005962 pr_err("Error %d on %s\n", status, __func__);
Oliver Endrissebc7de22011-07-03 13:49:44 -03005963 return status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005964}
5965
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005966static int switch_antenna_to_qam(struct drxk_state *state)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005967{
Mauro Carvalho Chehab147e1102011-07-10 08:24:26 -03005968 int status = 0;
Mauro Carvalho Chehab90796ac2011-07-10 09:36:30 -03005969 bool gpio_state;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005970
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03005971 dprintk(1, "\n");
Mauro Carvalho Chehab147e1102011-07-10 08:24:26 -03005972
Mauro Carvalho Chehab90796ac2011-07-10 09:36:30 -03005973 if (!state->antenna_gpio)
5974 return 0;
5975
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005976 gpio_state = state->m_gpio & state->antenna_gpio;
Mauro Carvalho Chehab90796ac2011-07-10 09:36:30 -03005977
5978 if (state->antenna_dvbt ^ gpio_state) {
5979 /* Antenna is on DVB-T mode. Switch */
5980 if (state->antenna_dvbt)
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005981 state->m_gpio &= ~state->antenna_gpio;
Mauro Carvalho Chehab90796ac2011-07-10 09:36:30 -03005982 else
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005983 state->m_gpio |= state->antenna_gpio;
5984 status = write_gpio(state);
Oliver Endrissebc7de22011-07-03 13:49:44 -03005985 }
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005986 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03005987 pr_err("Error %d on %s\n", status, __func__);
Oliver Endrissebc7de22011-07-03 13:49:44 -03005988 return status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005989}
5990
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005991static int switch_antenna_to_dvbt(struct drxk_state *state)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005992{
Mauro Carvalho Chehab147e1102011-07-10 08:24:26 -03005993 int status = 0;
Mauro Carvalho Chehab90796ac2011-07-10 09:36:30 -03005994 bool gpio_state;
Oliver Endrissebc7de22011-07-03 13:49:44 -03005995
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03005996 dprintk(1, "\n");
Mauro Carvalho Chehab90796ac2011-07-10 09:36:30 -03005997
5998 if (!state->antenna_gpio)
5999 return 0;
6000
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006001 gpio_state = state->m_gpio & state->antenna_gpio;
Mauro Carvalho Chehab90796ac2011-07-10 09:36:30 -03006002
6003 if (!(state->antenna_dvbt ^ gpio_state)) {
6004 /* Antenna is on DVB-C mode. Switch */
6005 if (state->antenna_dvbt)
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006006 state->m_gpio |= state->antenna_gpio;
Mauro Carvalho Chehab90796ac2011-07-10 09:36:30 -03006007 else
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006008 state->m_gpio &= ~state->antenna_gpio;
6009 status = write_gpio(state);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006010 }
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006011 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03006012 pr_err("Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006013 return status;
6014}
6015
6016
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006017static int power_down_device(struct drxk_state *state)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006018{
6019 /* Power down to requested mode */
6020 /* Backup some register settings */
6021 /* Set pins with possible pull-ups connected to them in input mode */
6022 /* Analog power down */
6023 /* ADC power down */
6024 /* Power down device */
6025 int status;
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03006026
6027 dprintk(1, "\n");
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006028 if (state->m_b_p_down_open_bridge) {
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006029 /* Open I2C bridge before power down of DRXK */
6030 status = ConfigureI2CBridge(state, true);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03006031 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006032 goto error;
6033 }
6034 /* driver 0.9.0 */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006035 status = dvbt_enable_ofdm_token_ring(state, false);
Oliver Endrissebc7de22011-07-03 13:49:44 -03006036 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006037 goto error;
Oliver Endrissebc7de22011-07-03 13:49:44 -03006038
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03006039 status = write16(state, SIO_CC_PWD_MODE__A,
6040 SIO_CC_PWD_MODE_LEVEL_CLOCK);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006041 if (status < 0)
6042 goto error;
6043 status = write16(state, SIO_CC_UPDATE__A, SIO_CC_UPDATE_KEY);
6044 if (status < 0)
6045 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006046 state->m_hi_cfg_ctrl |= SIO_HI_RA_RAM_PAR_5_CFG_SLEEP_ZZZ;
6047 status = hi_cfg_command(state);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006048error:
6049 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03006050 pr_err("Error %d on %s\n", status, __func__);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006051
6052 return status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006053}
6054
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006055static int init_drxk(struct drxk_state *state)
6056{
Mauro Carvalho Chehab177bc7d2012-06-21 09:36:38 -03006057 int status = 0, n = 0;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006058 enum drx_power_mode power_mode = DRXK_POWER_DOWN_OFDM;
6059 u16 driver_version;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006060
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03006061 dprintk(1, "\n");
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006062 if ((state->m_drxk_state == DRXK_UNINITIALIZED)) {
Mauro Carvalho Chehab20bfe7a2012-06-29 14:43:32 -03006063 drxk_i2c_lock(state);
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006064 status = power_up_device(state);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006065 if (status < 0)
6066 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006067 status = drxx_open(state);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006068 if (status < 0)
6069 goto error;
6070 /* Soft reset of OFDM-, sys- and osc-clockdomain */
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03006071 status = write16(state, SIO_CC_SOFT_RST__A,
6072 SIO_CC_SOFT_RST_OFDM__M
6073 | SIO_CC_SOFT_RST_SYS__M
6074 | SIO_CC_SOFT_RST_OSC__M);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006075 if (status < 0)
6076 goto error;
6077 status = write16(state, SIO_CC_UPDATE__A, SIO_CC_UPDATE_KEY);
6078 if (status < 0)
6079 goto error;
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03006080 /*
6081 * TODO is this needed? If yes, how much delay in
6082 * worst case scenario
6083 */
Mauro Carvalho Chehabb72852b2013-04-28 11:47:47 -03006084 usleep_range(1000, 2000);
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006085 state->m_drxk_a3_patch_code = true;
6086 status = get_device_capabilities(state);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006087 if (status < 0)
6088 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006089
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006090 /* Bridge delay, uses oscilator clock */
6091 /* Delay = (delay (nano seconds) * oscclk (kHz))/ 1000 */
6092 /* SDA brdige delay */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006093 state->m_hi_cfg_bridge_delay =
6094 (u16) ((state->m_osc_clock_freq / 1000) *
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006095 HI_I2C_BRIDGE_DELAY) / 1000;
6096 /* Clipping */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006097 if (state->m_hi_cfg_bridge_delay >
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006098 SIO_HI_RA_RAM_PAR_3_CFG_DBL_SDA__M) {
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006099 state->m_hi_cfg_bridge_delay =
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006100 SIO_HI_RA_RAM_PAR_3_CFG_DBL_SDA__M;
6101 }
6102 /* SCL bridge delay, same as SDA for now */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006103 state->m_hi_cfg_bridge_delay +=
6104 state->m_hi_cfg_bridge_delay <<
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006105 SIO_HI_RA_RAM_PAR_3_CFG_DBL_SCL__B;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006106
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006107 status = init_hi(state);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006108 if (status < 0)
6109 goto error;
6110 /* disable various processes */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006111#if NOA1ROM
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006112 if (!(state->m_DRXK_A1_ROM_CODE)
6113 && !(state->m_DRXK_A2_ROM_CODE))
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006114#endif
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006115 {
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03006116 status = write16(state, SCU_RAM_GPIO__A,
6117 SCU_RAM_GPIO_HW_LOCK_IND_DISABLE);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006118 if (status < 0)
6119 goto error;
6120 }
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006121
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006122 /* disable MPEG port */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006123 status = mpegts_disable(state);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006124 if (status < 0)
6125 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006126
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006127 /* Stop AUD and SCU */
6128 status = write16(state, AUD_COMM_EXEC__A, AUD_COMM_EXEC_STOP);
6129 if (status < 0)
6130 goto error;
6131 status = write16(state, SCU_COMM_EXEC__A, SCU_COMM_EXEC_STOP);
6132 if (status < 0)
6133 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006134
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006135 /* enable token-ring bus through OFDM block for possible ucode upload */
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03006136 status = write16(state, SIO_OFDM_SH_OFDM_RING_ENABLE__A,
6137 SIO_OFDM_SH_OFDM_RING_ENABLE_ON);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006138 if (status < 0)
6139 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006140
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006141 /* include boot loader section */
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03006142 status = write16(state, SIO_BL_COMM_EXEC__A,
6143 SIO_BL_COMM_EXEC_ACTIVE);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006144 if (status < 0)
6145 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006146 status = bl_chain_cmd(state, 0, 6, 100);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006147 if (status < 0)
6148 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006149
Mauro Carvalho Chehab177bc7d2012-06-21 09:36:38 -03006150 if (state->fw) {
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006151 status = download_microcode(state, state->fw->data,
Mauro Carvalho Chehab177bc7d2012-06-21 09:36:38 -03006152 state->fw->size);
6153 if (status < 0)
6154 goto error;
6155 }
Mauro Carvalho Chehabe4f4f872011-07-09 17:35:26 -03006156
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006157 /* disable token-ring bus through OFDM block for possible ucode upload */
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03006158 status = write16(state, SIO_OFDM_SH_OFDM_RING_ENABLE__A,
6159 SIO_OFDM_SH_OFDM_RING_ENABLE_OFF);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006160 if (status < 0)
6161 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006162
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006163 /* Run SCU for a little while to initialize microcode version numbers */
6164 status = write16(state, SCU_COMM_EXEC__A, SCU_COMM_EXEC_ACTIVE);
6165 if (status < 0)
6166 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006167 status = drxx_open(state);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006168 if (status < 0)
6169 goto error;
6170 /* added for test */
6171 msleep(30);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006172
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006173 power_mode = DRXK_POWER_DOWN_OFDM;
6174 status = ctrl_power_mode(state, &power_mode);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006175 if (status < 0)
6176 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006177
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006178 /* Stamp driver version number in SCU data RAM in BCD code
Jonathan McCrohan39c1cb22013-10-20 21:34:01 -03006179 Done to enable field application engineers to retrieve drxdriver version
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006180 via I2C from SCU RAM.
6181 Not using SCU command interface for SCU register access since no
6182 microcode may be present.
6183 */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006184 driver_version =
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006185 (((DRXK_VERSION_MAJOR / 100) % 10) << 12) +
6186 (((DRXK_VERSION_MAJOR / 10) % 10) << 8) +
6187 ((DRXK_VERSION_MAJOR % 10) << 4) +
6188 (DRXK_VERSION_MINOR % 10);
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03006189 status = write16(state, SCU_RAM_DRIVER_VER_HI__A,
6190 driver_version);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006191 if (status < 0)
6192 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006193 driver_version =
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006194 (((DRXK_VERSION_PATCH / 1000) % 10) << 12) +
6195 (((DRXK_VERSION_PATCH / 100) % 10) << 8) +
6196 (((DRXK_VERSION_PATCH / 10) % 10) << 4) +
6197 (DRXK_VERSION_PATCH % 10);
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03006198 status = write16(state, SCU_RAM_DRIVER_VER_LO__A,
6199 driver_version);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006200 if (status < 0)
6201 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006202
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03006203 pr_info("DRXK driver version %d.%d.%d\n",
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006204 DRXK_VERSION_MAJOR, DRXK_VERSION_MINOR,
6205 DRXK_VERSION_PATCH);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006206
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03006207 /*
6208 * Dirty fix of default values for ROM/PATCH microcode
6209 * Dirty because this fix makes it impossible to setup
6210 * suitable values before calling DRX_Open. This solution
6211 * requires changes to RF AGC speed to be done via the CTRL
6212 * function after calling DRX_Open
6213 */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006214
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006215 /* m_dvbt_rf_agc_cfg.speed = 3; */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006216
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006217 /* Reset driver debug flags to 0 */
6218 status = write16(state, SCU_RAM_DRIVER_DEBUG__A, 0);
6219 if (status < 0)
6220 goto error;
6221 /* driver 0.9.0 */
6222 /* Setup FEC OC:
6223 NOTE: No more full FEC resets allowed afterwards!! */
6224 status = write16(state, FEC_COMM_EXEC__A, FEC_COMM_EXEC_STOP);
6225 if (status < 0)
6226 goto error;
6227 /* MPEGTS functions are still the same */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006228 status = mpegts_dto_init(state);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006229 if (status < 0)
6230 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006231 status = mpegts_stop(state);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006232 if (status < 0)
6233 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006234 status = mpegts_configure_polarity(state);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006235 if (status < 0)
6236 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006237 status = mpegts_configure_pins(state, state->m_enable_mpeg_output);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006238 if (status < 0)
6239 goto error;
6240 /* added: configure GPIO */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006241 status = write_gpio(state);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006242 if (status < 0)
6243 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006244
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006245 state->m_drxk_state = DRXK_STOPPED;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006246
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006247 if (state->m_b_power_down) {
6248 status = power_down_device(state);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006249 if (status < 0)
6250 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006251 state->m_drxk_state = DRXK_POWERED_DOWN;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006252 } else
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006253 state->m_drxk_state = DRXK_STOPPED;
Mauro Carvalho Chehab177bc7d2012-06-21 09:36:38 -03006254
6255 /* Initialize the supported delivery systems */
6256 n = 0;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006257 if (state->m_has_dvbc) {
Mauro Carvalho Chehab177bc7d2012-06-21 09:36:38 -03006258 state->frontend.ops.delsys[n++] = SYS_DVBC_ANNEX_A;
6259 state->frontend.ops.delsys[n++] = SYS_DVBC_ANNEX_C;
6260 strlcat(state->frontend.ops.info.name, " DVB-C",
6261 sizeof(state->frontend.ops.info.name));
6262 }
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006263 if (state->m_has_dvbt) {
Mauro Carvalho Chehab177bc7d2012-06-21 09:36:38 -03006264 state->frontend.ops.delsys[n++] = SYS_DVBT;
6265 strlcat(state->frontend.ops.info.name, " DVB-T",
6266 sizeof(state->frontend.ops.info.name));
6267 }
Mauro Carvalho Chehab20bfe7a2012-06-29 14:43:32 -03006268 drxk_i2c_unlock(state);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006269 }
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006270error:
Mauro Carvalho Chehab20bfe7a2012-06-29 14:43:32 -03006271 if (status < 0) {
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006272 state->m_drxk_state = DRXK_NO_DEV;
Mauro Carvalho Chehab20bfe7a2012-06-29 14:43:32 -03006273 drxk_i2c_unlock(state);
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03006274 pr_err("Error %d on %s\n", status, __func__);
Mauro Carvalho Chehab20bfe7a2012-06-29 14:43:32 -03006275 }
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006276
Mauro Carvalho Chehabe716ada2011-07-21 19:35:04 -03006277 return status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006278}
6279
Mauro Carvalho Chehab177bc7d2012-06-21 09:36:38 -03006280static void load_firmware_cb(const struct firmware *fw,
6281 void *context)
6282{
6283 struct drxk_state *state = context;
6284
Mauro Carvalho Chehab704a28e2012-06-29 15:45:04 -03006285 dprintk(1, ": %s\n", fw ? "firmware loaded" : "firmware not loaded");
Mauro Carvalho Chehab177bc7d2012-06-21 09:36:38 -03006286 if (!fw) {
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03006287 pr_err("Could not load firmware file %s.\n",
Mauro Carvalho Chehab177bc7d2012-06-21 09:36:38 -03006288 state->microcode_name);
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03006289 pr_info("Copy %s to your hotplug directory!\n",
Mauro Carvalho Chehab177bc7d2012-06-21 09:36:38 -03006290 state->microcode_name);
6291 state->microcode_name = NULL;
6292
6293 /*
6294 * As firmware is now load asynchronous, it is not possible
6295 * anymore to fail at frontend attach. We might silently
6296 * return here, and hope that the driver won't crash.
6297 * We might also change all DVB callbacks to return -ENODEV
6298 * if the device is not initialized.
6299 * As the DRX-K devices have their own internal firmware,
6300 * let's just hope that it will match a firmware revision
6301 * compatible with this driver and proceed.
6302 */
6303 }
6304 state->fw = fw;
6305
6306 init_drxk(state);
6307}
6308
Mauro Carvalho Chehabfa4b2a12012-01-05 08:07:32 -02006309static void drxk_release(struct dvb_frontend *fe)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006310{
Oliver Endrissebc7de22011-07-03 13:49:44 -03006311 struct drxk_state *state = fe->demodulator_priv;
6312
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03006313 dprintk(1, "\n");
Markus Elfring9bc2dd72014-11-19 18:27:24 -03006314 release_firmware(state->fw);
Mauro Carvalho Chehab177bc7d2012-06-21 09:36:38 -03006315
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006316 kfree(state);
6317}
6318
Mauro Carvalho Chehabfa4b2a12012-01-05 08:07:32 -02006319static int drxk_sleep(struct dvb_frontend *fe)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006320{
Oliver Endrissebc7de22011-07-03 13:49:44 -03006321 struct drxk_state *state = fe->demodulator_priv;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006322
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03006323 dprintk(1, "\n");
Mauro Carvalho Chehab704a28e2012-06-29 15:45:04 -03006324
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006325 if (state->m_drxk_state == DRXK_NO_DEV)
Mauro Carvalho Chehab704a28e2012-06-29 15:45:04 -03006326 return -ENODEV;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006327 if (state->m_drxk_state == DRXK_UNINITIALIZED)
Mauro Carvalho Chehab704a28e2012-06-29 15:45:04 -03006328 return 0;
6329
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006330 shut_down(state);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006331 return 0;
6332}
6333
Oliver Endrissebc7de22011-07-03 13:49:44 -03006334static int drxk_gate_ctrl(struct dvb_frontend *fe, int enable)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006335{
6336 struct drxk_state *state = fe->demodulator_priv;
6337
Martin Blumenstingl257ee972012-07-04 17:38:23 -03006338 dprintk(1, ": %s\n", enable ? "enable" : "disable");
Mauro Carvalho Chehab704a28e2012-06-29 15:45:04 -03006339
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006340 if (state->m_drxk_state == DRXK_NO_DEV)
Mauro Carvalho Chehab704a28e2012-06-29 15:45:04 -03006341 return -ENODEV;
6342
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006343 return ConfigureI2CBridge(state, enable ? true : false);
6344}
6345
Mauro Carvalho Chehabed5452a2011-12-26 09:57:11 -03006346static int drxk_set_parameters(struct dvb_frontend *fe)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006347{
Mauro Carvalho Chehabed5452a2011-12-26 09:57:11 -03006348 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
Mauro Carvalho Chehab6cb393c2012-01-05 09:26:40 -02006349 u32 delsys = p->delivery_system, old_delsys;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006350 struct drxk_state *state = fe->demodulator_priv;
6351 u32 IF;
6352
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03006353 dprintk(1, "\n");
Mauro Carvalho Chehab8513e142011-09-03 11:40:02 -03006354
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006355 if (state->m_drxk_state == DRXK_NO_DEV)
Mauro Carvalho Chehab704a28e2012-06-29 15:45:04 -03006356 return -ENODEV;
6357
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006358 if (state->m_drxk_state == DRXK_UNINITIALIZED)
Mauro Carvalho Chehab704a28e2012-06-29 15:45:04 -03006359 return -EAGAIN;
6360
Mauro Carvalho Chehab8513e142011-09-03 11:40:02 -03006361 if (!fe->ops.tuner_ops.get_if_frequency) {
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03006362 pr_err("Error: get_if_frequency() not defined at tuner. Can't work without it!\n");
Mauro Carvalho Chehab8513e142011-09-03 11:40:02 -03006363 return -EINVAL;
6364 }
6365
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006366 if (fe->ops.i2c_gate_ctrl)
6367 fe->ops.i2c_gate_ctrl(fe, 1);
6368 if (fe->ops.tuner_ops.set_params)
Mauro Carvalho Chehab14d24d12011-12-24 12:24:33 -03006369 fe->ops.tuner_ops.set_params(fe);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006370 if (fe->ops.i2c_gate_ctrl)
6371 fe->ops.i2c_gate_ctrl(fe, 0);
Mauro Carvalho Chehab6cb393c2012-01-05 09:26:40 -02006372
6373 old_delsys = state->props.delivery_system;
Mauro Carvalho Chehabed5452a2011-12-26 09:57:11 -03006374 state->props = *p;
Mauro Carvalho Chehabfa4b2a12012-01-05 08:07:32 -02006375
Mauro Carvalho Chehab6cb393c2012-01-05 09:26:40 -02006376 if (old_delsys != delsys) {
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006377 shut_down(state);
Mauro Carvalho Chehab6cb393c2012-01-05 09:26:40 -02006378 switch (delsys) {
6379 case SYS_DVBC_ANNEX_A:
6380 case SYS_DVBC_ANNEX_C:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006381 if (!state->m_has_dvbc)
Mauro Carvalho Chehab6cb393c2012-01-05 09:26:40 -02006382 return -EINVAL;
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03006383 state->m_itut_annex_c = (delsys == SYS_DVBC_ANNEX_C) ?
6384 true : false;
Mauro Carvalho Chehab6cb393c2012-01-05 09:26:40 -02006385 if (state->m_itut_annex_c)
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006386 setoperation_mode(state, OM_QAM_ITU_C);
Mauro Carvalho Chehab6cb393c2012-01-05 09:26:40 -02006387 else
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006388 setoperation_mode(state, OM_QAM_ITU_A);
Mauro Carvalho Chehabfa4b2a12012-01-05 08:07:32 -02006389 break;
Mauro Carvalho Chehab6cb393c2012-01-05 09:26:40 -02006390 case SYS_DVBT:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006391 if (!state->m_has_dvbt)
Mauro Carvalho Chehab6cb393c2012-01-05 09:26:40 -02006392 return -EINVAL;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006393 setoperation_mode(state, OM_DVBT);
Mauro Carvalho Chehab6cb393c2012-01-05 09:26:40 -02006394 break;
6395 default:
Mauro Carvalho Chehabfa4b2a12012-01-05 08:07:32 -02006396 return -EINVAL;
Mauro Carvalho Chehab6cb393c2012-01-05 09:26:40 -02006397 }
Mauro Carvalho Chehabfa4b2a12012-01-05 08:07:32 -02006398 }
6399
Mauro Carvalho Chehab8513e142011-09-03 11:40:02 -03006400 fe->ops.tuner_ops.get_if_frequency(fe, &IF);
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006401 start(state, 0, IF);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006402
Jonathan McCrohan39c1cb22013-10-20 21:34:01 -03006403 /* After set_frontend, stats aren't available */
Mauro Carvalho Chehab8f3741e2013-03-20 06:15:45 -03006404 p->strength.stat[0].scale = FE_SCALE_RELATIVE;
6405 p->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
6406 p->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
6407 p->block_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
6408 p->pre_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
6409 p->pre_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
6410 p->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
6411 p->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
6412
Mauro Carvalho Chehabe0e6eca2011-07-04 08:27:47 -03006413 /* printk(KERN_DEBUG "drxk: %s IF=%d done\n", __func__, IF); */
Oliver Endrissebc7de22011-07-03 13:49:44 -03006414
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006415 return 0;
6416}
6417
Mauro Carvalho Chehab59a7a232013-03-20 08:21:52 -03006418static int get_strength(struct drxk_state *state, u64 *strength)
6419{
6420 int status;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006421 struct s_cfg_agc rf_agc, if_agc;
6422 u32 total_gain = 0;
Mauro Carvalho Chehab59a7a232013-03-20 08:21:52 -03006423 u32 atten = 0;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006424 u32 agc_range = 0;
Mauro Carvalho Chehab59a7a232013-03-20 08:21:52 -03006425 u16 scu_lvl = 0;
6426 u16 scu_coc = 0;
6427 /* FIXME: those are part of the tuner presets */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006428 u16 tuner_rf_gain = 50; /* Default value on az6007 driver */
6429 u16 tuner_if_gain = 40; /* Default value on az6007 driver */
Mauro Carvalho Chehab59a7a232013-03-20 08:21:52 -03006430
6431 *strength = 0;
6432
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006433 if (is_dvbt(state)) {
6434 rf_agc = state->m_dvbt_rf_agc_cfg;
6435 if_agc = state->m_dvbt_if_agc_cfg;
6436 } else if (is_qam(state)) {
6437 rf_agc = state->m_qam_rf_agc_cfg;
6438 if_agc = state->m_qam_if_agc_cfg;
Mauro Carvalho Chehab59a7a232013-03-20 08:21:52 -03006439 } else {
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006440 rf_agc = state->m_atv_rf_agc_cfg;
6441 if_agc = state->m_atv_if_agc_cfg;
Mauro Carvalho Chehab59a7a232013-03-20 08:21:52 -03006442 }
6443
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006444 if (rf_agc.ctrl_mode == DRXK_AGC_CTRL_AUTO) {
6445 /* SCU output_level */
Mauro Carvalho Chehab59a7a232013-03-20 08:21:52 -03006446 status = read16(state, SCU_RAM_AGC_RF_IACCU_HI__A, &scu_lvl);
6447 if (status < 0)
6448 return status;
6449
6450 /* SCU c.o.c. */
6451 read16(state, SCU_RAM_AGC_RF_IACCU_HI_CO__A, &scu_coc);
6452 if (status < 0)
6453 return status;
6454
6455 if (((u32) scu_lvl + (u32) scu_coc) < 0xffff)
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006456 rf_agc.output_level = scu_lvl + scu_coc;
Mauro Carvalho Chehab59a7a232013-03-20 08:21:52 -03006457 else
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006458 rf_agc.output_level = 0xffff;
Mauro Carvalho Chehab59a7a232013-03-20 08:21:52 -03006459
6460 /* Take RF gain into account */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006461 total_gain += tuner_rf_gain;
Mauro Carvalho Chehab59a7a232013-03-20 08:21:52 -03006462
6463 /* clip output value */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006464 if (rf_agc.output_level < rf_agc.min_output_level)
6465 rf_agc.output_level = rf_agc.min_output_level;
6466 if (rf_agc.output_level > rf_agc.max_output_level)
6467 rf_agc.output_level = rf_agc.max_output_level;
Mauro Carvalho Chehab59a7a232013-03-20 08:21:52 -03006468
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006469 agc_range = (u32) (rf_agc.max_output_level - rf_agc.min_output_level);
6470 if (agc_range > 0) {
Mauro Carvalho Chehab59a7a232013-03-20 08:21:52 -03006471 atten += 100UL *
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006472 ((u32)(tuner_rf_gain)) *
6473 ((u32)(rf_agc.output_level - rf_agc.min_output_level))
6474 / agc_range;
Mauro Carvalho Chehab59a7a232013-03-20 08:21:52 -03006475 }
6476 }
6477
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006478 if (if_agc.ctrl_mode == DRXK_AGC_CTRL_AUTO) {
Mauro Carvalho Chehab59a7a232013-03-20 08:21:52 -03006479 status = read16(state, SCU_RAM_AGC_IF_IACCU_HI__A,
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006480 &if_agc.output_level);
Mauro Carvalho Chehab59a7a232013-03-20 08:21:52 -03006481 if (status < 0)
6482 return status;
6483
6484 status = read16(state, SCU_RAM_AGC_INGAIN_TGT_MIN__A,
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006485 &if_agc.top);
Mauro Carvalho Chehab59a7a232013-03-20 08:21:52 -03006486 if (status < 0)
6487 return status;
6488
6489 /* Take IF gain into account */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006490 total_gain += (u32) tuner_if_gain;
Mauro Carvalho Chehab59a7a232013-03-20 08:21:52 -03006491
6492 /* clip output value */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006493 if (if_agc.output_level < if_agc.min_output_level)
6494 if_agc.output_level = if_agc.min_output_level;
6495 if (if_agc.output_level > if_agc.max_output_level)
6496 if_agc.output_level = if_agc.max_output_level;
Mauro Carvalho Chehab59a7a232013-03-20 08:21:52 -03006497
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03006498 agc_range = (u32)(if_agc.max_output_level - if_agc.min_output_level);
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006499 if (agc_range > 0) {
Mauro Carvalho Chehab59a7a232013-03-20 08:21:52 -03006500 atten += 100UL *
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006501 ((u32)(tuner_if_gain)) *
6502 ((u32)(if_agc.output_level - if_agc.min_output_level))
6503 / agc_range;
Mauro Carvalho Chehab59a7a232013-03-20 08:21:52 -03006504 }
6505 }
6506
6507 /*
6508 * Convert to 0..65535 scale.
6509 * If it can't be measured (AGC is disabled), just show 100%.
6510 */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006511 if (total_gain > 0)
6512 *strength = (65535UL * atten / total_gain / 100);
Mauro Carvalho Chehab59a7a232013-03-20 08:21:52 -03006513 else
6514 *strength = 65535;
6515
6516 return 0;
6517}
6518
Mauro Carvalho Chehab8f3741e2013-03-20 06:15:45 -03006519static int drxk_get_stats(struct dvb_frontend *fe)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006520{
Mauro Carvalho Chehab8f3741e2013-03-20 06:15:45 -03006521 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006522 struct drxk_state *state = fe->demodulator_priv;
Mauro Carvalho Chehab8f3741e2013-03-20 06:15:45 -03006523 int status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006524 u32 stat;
Mauro Carvalho Chehab8f3741e2013-03-20 06:15:45 -03006525 u16 reg16;
6526 u32 post_bit_count;
6527 u32 post_bit_err_count;
6528 u32 post_bit_error_scale;
6529 u32 pre_bit_err_count;
6530 u32 pre_bit_count;
6531 u32 pkt_count;
6532 u32 pkt_error_count;
Mauro Carvalho Chehab59a7a232013-03-20 08:21:52 -03006533 s32 cnr;
Mauro Carvalho Chehab704a28e2012-06-29 15:45:04 -03006534
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006535 if (state->m_drxk_state == DRXK_NO_DEV)
Mauro Carvalho Chehab704a28e2012-06-29 15:45:04 -03006536 return -ENODEV;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006537 if (state->m_drxk_state == DRXK_UNINITIALIZED)
Mauro Carvalho Chehab704a28e2012-06-29 15:45:04 -03006538 return -EAGAIN;
6539
Mauro Carvalho Chehab8f3741e2013-03-20 06:15:45 -03006540 /* get status */
6541 state->fe_status = 0;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006542 get_lock_status(state, &stat);
Oliver Endrissebc7de22011-07-03 13:49:44 -03006543 if (stat == MPEG_LOCK)
Mauro Carvalho Chehab8f3741e2013-03-20 06:15:45 -03006544 state->fe_status |= 0x1f;
Oliver Endrissebc7de22011-07-03 13:49:44 -03006545 if (stat == FEC_LOCK)
Mauro Carvalho Chehab8f3741e2013-03-20 06:15:45 -03006546 state->fe_status |= 0x0f;
Oliver Endrissebc7de22011-07-03 13:49:44 -03006547 if (stat == DEMOD_LOCK)
Mauro Carvalho Chehab8f3741e2013-03-20 06:15:45 -03006548 state->fe_status |= 0x07;
6549
Mauro Carvalho Chehab59a7a232013-03-20 08:21:52 -03006550 /*
6551 * Estimate signal strength from AGC
6552 */
6553 get_strength(state, &c->strength.stat[0].uvalue);
6554 c->strength.stat[0].scale = FE_SCALE_RELATIVE;
6555
6556
Mauro Carvalho Chehab8f3741e2013-03-20 06:15:45 -03006557 if (stat >= DEMOD_LOCK) {
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006558 get_signal_to_noise(state, &cnr);
Mauro Carvalho Chehab8f3741e2013-03-20 06:15:45 -03006559 c->cnr.stat[0].svalue = cnr * 100;
6560 c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
6561 } else {
6562 c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
6563 }
6564
6565 if (stat < FEC_LOCK) {
6566 c->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
6567 c->block_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
6568 c->pre_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
6569 c->pre_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
6570 c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
6571 c->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
6572 return 0;
6573 }
6574
6575 /* Get post BER */
6576
6577 /* BER measurement is valid if at least FEC lock is achieved */
6578
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03006579 /*
6580 * OFDM_EC_VD_REQ_SMB_CNT__A and/or OFDM_EC_VD_REQ_BIT_CNT can be
6581 * written to set nr of symbols or bits over which to measure
6582 * EC_VD_REG_ERR_BIT_CNT__A . See CtrlSetCfg().
6583 */
Mauro Carvalho Chehab8f3741e2013-03-20 06:15:45 -03006584
6585 /* Read registers for post/preViterbi BER calculation */
6586 status = read16(state, OFDM_EC_VD_ERR_BIT_CNT__A, &reg16);
6587 if (status < 0)
6588 goto error;
6589 pre_bit_err_count = reg16;
6590
6591 status = read16(state, OFDM_EC_VD_IN_BIT_CNT__A , &reg16);
6592 if (status < 0)
6593 goto error;
6594 pre_bit_count = reg16;
6595
6596 /* Number of bit-errors */
6597 status = read16(state, FEC_RS_NR_BIT_ERRORS__A, &reg16);
6598 if (status < 0)
6599 goto error;
6600 post_bit_err_count = reg16;
6601
6602 status = read16(state, FEC_RS_MEASUREMENT_PRESCALE__A, &reg16);
6603 if (status < 0)
6604 goto error;
6605 post_bit_error_scale = reg16;
6606
6607 status = read16(state, FEC_RS_MEASUREMENT_PERIOD__A, &reg16);
6608 if (status < 0)
6609 goto error;
6610 pkt_count = reg16;
6611
6612 status = read16(state, SCU_RAM_FEC_ACCUM_PKT_FAILURES__A, &reg16);
6613 if (status < 0)
6614 goto error;
6615 pkt_error_count = reg16;
6616 write16(state, SCU_RAM_FEC_ACCUM_PKT_FAILURES__A, 0);
6617
6618 post_bit_err_count *= post_bit_error_scale;
6619
6620 post_bit_count = pkt_count * 204 * 8;
6621
6622 /* Store the results */
6623 c->block_error.stat[0].scale = FE_SCALE_COUNTER;
6624 c->block_error.stat[0].uvalue += pkt_error_count;
6625 c->block_count.stat[0].scale = FE_SCALE_COUNTER;
6626 c->block_count.stat[0].uvalue += pkt_count;
6627
6628 c->pre_bit_error.stat[0].scale = FE_SCALE_COUNTER;
6629 c->pre_bit_error.stat[0].uvalue += pre_bit_err_count;
6630 c->pre_bit_count.stat[0].scale = FE_SCALE_COUNTER;
6631 c->pre_bit_count.stat[0].uvalue += pre_bit_count;
6632
6633 c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
6634 c->post_bit_error.stat[0].uvalue += post_bit_err_count;
6635 c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
6636 c->post_bit_count.stat[0].uvalue += post_bit_count;
6637
Mauro Carvalho Chehab8f3741e2013-03-20 06:15:45 -03006638error:
6639 return status;
6640}
6641
6642
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -03006643static int drxk_read_status(struct dvb_frontend *fe, enum fe_status *status)
Mauro Carvalho Chehab8f3741e2013-03-20 06:15:45 -03006644{
6645 struct drxk_state *state = fe->demodulator_priv;
6646 int rc;
6647
6648 dprintk(1, "\n");
6649
6650 rc = drxk_get_stats(fe);
6651 if (rc < 0)
6652 return rc;
6653
6654 *status = state->fe_status;
6655
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006656 return 0;
6657}
6658
Oliver Endrissebc7de22011-07-03 13:49:44 -03006659static int drxk_read_signal_strength(struct dvb_frontend *fe,
6660 u16 *strength)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006661{
6662 struct drxk_state *state = fe->demodulator_priv;
Mauro Carvalho Chehab340e7692013-03-20 08:57:42 -03006663 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006664
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03006665 dprintk(1, "\n");
Mauro Carvalho Chehab704a28e2012-06-29 15:45:04 -03006666
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006667 if (state->m_drxk_state == DRXK_NO_DEV)
Mauro Carvalho Chehab704a28e2012-06-29 15:45:04 -03006668 return -ENODEV;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006669 if (state->m_drxk_state == DRXK_UNINITIALIZED)
Mauro Carvalho Chehab704a28e2012-06-29 15:45:04 -03006670 return -EAGAIN;
6671
Mauro Carvalho Chehab340e7692013-03-20 08:57:42 -03006672 *strength = c->strength.stat[0].uvalue;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006673 return 0;
6674}
6675
6676static int drxk_read_snr(struct dvb_frontend *fe, u16 *snr)
6677{
6678 struct drxk_state *state = fe->demodulator_priv;
6679 s32 snr2;
6680
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03006681 dprintk(1, "\n");
Mauro Carvalho Chehab704a28e2012-06-29 15:45:04 -03006682
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006683 if (state->m_drxk_state == DRXK_NO_DEV)
Mauro Carvalho Chehab704a28e2012-06-29 15:45:04 -03006684 return -ENODEV;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006685 if (state->m_drxk_state == DRXK_UNINITIALIZED)
Mauro Carvalho Chehab704a28e2012-06-29 15:45:04 -03006686 return -EAGAIN;
6687
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006688 get_signal_to_noise(state, &snr2);
Mauro Carvalho Chehab8f3741e2013-03-20 06:15:45 -03006689
6690 /* No negative SNR, clip to zero */
6691 if (snr2 < 0)
6692 snr2 = 0;
Oliver Endrissebc7de22011-07-03 13:49:44 -03006693 *snr = snr2 & 0xffff;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006694 return 0;
6695}
6696
6697static int drxk_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
6698{
6699 struct drxk_state *state = fe->demodulator_priv;
6700 u16 err;
6701
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03006702 dprintk(1, "\n");
Mauro Carvalho Chehab704a28e2012-06-29 15:45:04 -03006703
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006704 if (state->m_drxk_state == DRXK_NO_DEV)
Mauro Carvalho Chehab704a28e2012-06-29 15:45:04 -03006705 return -ENODEV;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006706 if (state->m_drxk_state == DRXK_UNINITIALIZED)
Mauro Carvalho Chehab704a28e2012-06-29 15:45:04 -03006707 return -EAGAIN;
6708
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006709 dvbtqam_get_acc_pkt_err(state, &err);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006710 *ucblocks = (u32) err;
6711 return 0;
6712}
6713
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03006714static int drxk_get_tune_settings(struct dvb_frontend *fe,
6715 struct dvb_frontend_tune_settings *sets)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006716{
Mauro Carvalho Chehab704a28e2012-06-29 15:45:04 -03006717 struct drxk_state *state = fe->demodulator_priv;
Mauro Carvalho Chehabfa4b2a12012-01-05 08:07:32 -02006718 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03006719
6720 dprintk(1, "\n");
Mauro Carvalho Chehab704a28e2012-06-29 15:45:04 -03006721
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006722 if (state->m_drxk_state == DRXK_NO_DEV)
Mauro Carvalho Chehab704a28e2012-06-29 15:45:04 -03006723 return -ENODEV;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006724 if (state->m_drxk_state == DRXK_UNINITIALIZED)
Mauro Carvalho Chehab704a28e2012-06-29 15:45:04 -03006725 return -EAGAIN;
6726
Mauro Carvalho Chehabfa4b2a12012-01-05 08:07:32 -02006727 switch (p->delivery_system) {
6728 case SYS_DVBC_ANNEX_A:
6729 case SYS_DVBC_ANNEX_C:
Jose Alberto Reguerodc66d7f2012-01-27 18:34:49 -03006730 case SYS_DVBT:
Mauro Carvalho Chehabfa4b2a12012-01-05 08:07:32 -02006731 sets->min_delay_ms = 3000;
6732 sets->max_drift = 0;
6733 sets->step_size = 0;
6734 return 0;
6735 default:
Mauro Carvalho Chehabfa4b2a12012-01-05 08:07:32 -02006736 return -EINVAL;
6737 }
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006738}
6739
Mauro Carvalho Chehabfa4b2a12012-01-05 08:07:32 -02006740static struct dvb_frontend_ops drxk_ops = {
6741 /* .delsys will be filled dynamically */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006742 .info = {
Mauro Carvalho Chehabfa4b2a12012-01-05 08:07:32 -02006743 .name = "DRXK",
6744 .frequency_min = 47000000,
6745 .frequency_max = 865000000,
6746 /* For DVB-C */
6747 .symbol_rate_min = 870000,
6748 .symbol_rate_max = 11700000,
6749 /* For DVB-T */
6750 .frequency_stepsize = 166667,
6751
6752 .caps = FE_CAN_QAM_16 | FE_CAN_QAM_32 | FE_CAN_QAM_64 |
6753 FE_CAN_QAM_128 | FE_CAN_QAM_256 | FE_CAN_FEC_AUTO |
6754 FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
6755 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_MUTE_TS |
6756 FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_RECOVER |
6757 FE_CAN_GUARD_INTERVAL_AUTO | FE_CAN_HIERARCHY_AUTO
6758 },
6759
6760 .release = drxk_release,
6761 .sleep = drxk_sleep,
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006762 .i2c_gate_ctrl = drxk_gate_ctrl,
6763
Mauro Carvalho Chehabed5452a2011-12-26 09:57:11 -03006764 .set_frontend = drxk_set_parameters,
Mauro Carvalho Chehabfa4b2a12012-01-05 08:07:32 -02006765 .get_tune_settings = drxk_get_tune_settings,
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006766
6767 .read_status = drxk_read_status,
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006768 .read_signal_strength = drxk_read_signal_strength,
6769 .read_snr = drxk_read_snr,
6770 .read_ucblocks = drxk_read_ucblocks,
6771};
6772
Mauro Carvalho Chehab0fc55e82011-07-09 12:36:58 -03006773struct dvb_frontend *drxk_attach(const struct drxk_config *config,
Mauro Carvalho Chehabfa4b2a12012-01-05 08:07:32 -02006774 struct i2c_adapter *i2c)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006775{
Mauro Carvalho Chehab8f3741e2013-03-20 06:15:45 -03006776 struct dtv_frontend_properties *p;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006777 struct drxk_state *state = NULL;
Mauro Carvalho Chehab0fc55e82011-07-09 12:36:58 -03006778 u8 adr = config->adr;
Mauro Carvalho Chehab177bc7d2012-06-21 09:36:38 -03006779 int status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006780
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03006781 dprintk(1, "\n");
Oliver Endrissebc7de22011-07-03 13:49:44 -03006782 state = kzalloc(sizeof(struct drxk_state), GFP_KERNEL);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006783 if (!state)
6784 return NULL;
6785
Oliver Endrissebc7de22011-07-03 13:49:44 -03006786 state->i2c = i2c;
6787 state->demod_address = adr;
Mauro Carvalho Chehabe076c922011-07-09 13:06:12 -03006788 state->single_master = config->single_master;
Mauro Carvalho Chehabe4f4f872011-07-09 17:35:26 -03006789 state->microcode_name = config->microcode_name;
Martin Blumenstingl9e23f50a2012-07-04 17:36:55 -03006790 state->qam_demod_parameter_count = config->qam_demod_parameter_count;
Mauro Carvalho Chehabf1fe1b72011-07-09 21:59:33 -03006791 state->no_i2c_bridge = config->no_i2c_bridge;
Mauro Carvalho Chehab90796ac2011-07-10 09:36:30 -03006792 state->antenna_gpio = config->antenna_gpio;
6793 state->antenna_dvbt = config->antenna_dvbt;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006794 state->m_chunk_size = config->chunk_size;
Mauro Carvalho Chehabd5856812012-01-21 07:57:06 -03006795 state->enable_merr_cfg = config->enable_merr_cfg;
Mauro Carvalho Chehab90796ac2011-07-10 09:36:30 -03006796
Mauro Carvalho Chehab67f04612012-01-20 18:30:58 -03006797 if (config->dynamic_clk) {
Mauro Carvalho Chehab5a7f7b72014-09-03 15:23:57 -03006798 state->m_dvbt_static_clk = false;
6799 state->m_dvbc_static_clk = false;
Mauro Carvalho Chehab67f04612012-01-20 18:30:58 -03006800 } else {
Mauro Carvalho Chehab5a7f7b72014-09-03 15:23:57 -03006801 state->m_dvbt_static_clk = true;
6802 state->m_dvbc_static_clk = true;
Mauro Carvalho Chehab67f04612012-01-20 18:30:58 -03006803 }
6804
Mauro Carvalho Chehab6fb65a62012-01-20 19:13:07 -03006805
6806 if (config->mpeg_out_clk_strength)
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006807 state->m_ts_clockk_strength = config->mpeg_out_clk_strength & 0x07;
Mauro Carvalho Chehab6fb65a62012-01-20 19:13:07 -03006808 else
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006809 state->m_ts_clockk_strength = 0x06;
Mauro Carvalho Chehab6fb65a62012-01-20 19:13:07 -03006810
Mauro Carvalho Chehab534e0482011-07-24 14:59:20 -03006811 if (config->parallel_ts)
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006812 state->m_enable_parallel = true;
Mauro Carvalho Chehab534e0482011-07-24 14:59:20 -03006813 else
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006814 state->m_enable_parallel = false;
Mauro Carvalho Chehab534e0482011-07-24 14:59:20 -03006815
Mauro Carvalho Chehab90796ac2011-07-10 09:36:30 -03006816 /* NOTE: as more UIO bits will be used, add them to the mask */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006817 state->uio_mask = config->antenna_gpio;
Mauro Carvalho Chehab90796ac2011-07-10 09:36:30 -03006818
6819 /* Default gpio to DVB-C */
6820 if (!state->antenna_dvbt && state->antenna_gpio)
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006821 state->m_gpio |= state->antenna_gpio;
Mauro Carvalho Chehab90796ac2011-07-10 09:36:30 -03006822 else
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006823 state->m_gpio &= ~state->antenna_gpio;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006824
6825 mutex_init(&state->mutex);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006826
Mauro Carvalho Chehabfa4b2a12012-01-05 08:07:32 -02006827 memcpy(&state->frontend.ops, &drxk_ops, sizeof(drxk_ops));
6828 state->frontend.demodulator_priv = state;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006829
6830 init_state(state);
Mauro Carvalho Chehabfa4b2a12012-01-05 08:07:32 -02006831
Mauro Carvalho Chehab177bc7d2012-06-21 09:36:38 -03006832 /* Load firmware and initialize DRX-K */
6833 if (state->microcode_name) {
Mauro Carvalho Chehab4b819722014-01-13 04:31:31 -03006834 const struct firmware *fw = NULL;
Mauro Carvalho Chehab8e307832012-10-02 16:01:15 -03006835
Mauro Carvalho Chehab4b819722014-01-13 04:31:31 -03006836 status = request_firmware(&fw, state->microcode_name,
6837 state->i2c->dev.parent);
6838 if (status < 0)
6839 fw = NULL;
6840 load_firmware_cb(fw, state);
Mauro Carvalho Chehab177bc7d2012-06-21 09:36:38 -03006841 } else if (init_drxk(state) < 0)
6842 goto error;
Mauro Carvalho Chehabcf694b12011-07-10 10:26:06 -03006843
Mauro Carvalho Chehab8f3741e2013-03-20 06:15:45 -03006844
6845 /* Initialize stats */
6846 p = &state->frontend.dtv_property_cache;
6847 p->strength.len = 1;
6848 p->cnr.len = 1;
6849 p->block_error.len = 1;
6850 p->block_count.len = 1;
6851 p->pre_bit_error.len = 1;
6852 p->pre_bit_count.len = 1;
6853 p->post_bit_error.len = 1;
6854 p->post_bit_count.len = 1;
6855
6856 p->strength.stat[0].scale = FE_SCALE_RELATIVE;
6857 p->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
6858 p->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
6859 p->block_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
6860 p->pre_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
6861 p->pre_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
6862 p->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
6863 p->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
6864
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03006865 pr_info("frontend initialized.\n");
Mauro Carvalho Chehabfa4b2a12012-01-05 08:07:32 -02006866 return &state->frontend;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006867
6868error:
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03006869 pr_err("not found\n");
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006870 kfree(state);
6871 return NULL;
6872}
Oliver Endrissebc7de22011-07-03 13:49:44 -03006873EXPORT_SYMBOL(drxk_attach);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006874
6875MODULE_DESCRIPTION("DRX-K driver");
6876MODULE_AUTHOR("Ralph Metzler");
6877MODULE_LICENSE("GPL");