blob: 0a4875b391d9ca17a8c13a0aec7b904ccab85418 [file] [log] [blame]
Thomas Gleixner89ee7f42019-06-04 10:10:54 +02001// SPDX-License-Identifier: GPL-2.0-only
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002/*
3 * drxk_hard: DRX-K DVB-C/T demodulator driver
4 *
5 * Copyright (C) 2010-2011 Digital Devices GmbH
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006 */
7
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03008#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
Ralph Metzler43dd07f2011-07-03 13:42:18 -030010#include <linux/kernel.h>
11#include <linux/module.h>
12#include <linux/moduleparam.h>
13#include <linux/init.h>
14#include <linux/delay.h>
15#include <linux/firmware.h>
16#include <linux/i2c.h>
Mauro Carvalho Chehab20bfe7a2012-06-29 14:43:32 -030017#include <linux/hardirq.h>
Ralph Metzler43dd07f2011-07-03 13:42:18 -030018#include <asm/div64.h>
19
Mauro Carvalho Chehabfada1932017-12-28 13:03:51 -050020#include <media/dvb_frontend.h>
Ralph Metzler43dd07f2011-07-03 13:42:18 -030021#include "drxk.h"
22#include "drxk_hard.h"
Mauro Carvalho Chehabfada1932017-12-28 13:03:51 -050023#include <media/dvb_math.h>
Ralph Metzler43dd07f2011-07-03 13:42:18 -030024
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -030025static int power_down_dvbt(struct drxk_state *state, bool set_power_mode);
26static int power_down_qam(struct drxk_state *state);
27static int set_dvbt_standard(struct drxk_state *state,
28 enum operation_mode o_mode);
29static int set_qam_standard(struct drxk_state *state,
30 enum operation_mode o_mode);
31static int set_qam(struct drxk_state *state, u16 intermediate_freqk_hz,
32 s32 tuner_freq_offset);
33static int set_dvbt_standard(struct drxk_state *state,
34 enum operation_mode o_mode);
35static int dvbt_start(struct drxk_state *state);
36static int set_dvbt(struct drxk_state *state, u16 intermediate_freqk_hz,
37 s32 tuner_freq_offset);
38static int get_qam_lock_status(struct drxk_state *state, u32 *p_lock_status);
39static int get_dvbt_lock_status(struct drxk_state *state, u32 *p_lock_status);
40static int switch_antenna_to_qam(struct drxk_state *state);
41static int switch_antenna_to_dvbt(struct drxk_state *state);
Ralph Metzler43dd07f2011-07-03 13:42:18 -030042
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -030043static bool is_dvbt(struct drxk_state *state)
Ralph Metzler43dd07f2011-07-03 13:42:18 -030044{
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -030045 return state->m_operation_mode == OM_DVBT;
Ralph Metzler43dd07f2011-07-03 13:42:18 -030046}
47
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -030048static bool is_qam(struct drxk_state *state)
Ralph Metzler43dd07f2011-07-03 13:42:18 -030049{
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -030050 return state->m_operation_mode == OM_QAM_ITU_A ||
51 state->m_operation_mode == OM_QAM_ITU_B ||
52 state->m_operation_mode == OM_QAM_ITU_C;
Ralph Metzler43dd07f2011-07-03 13:42:18 -030053}
54
Ralph Metzler43dd07f2011-07-03 13:42:18 -030055#define NOA1ROM 0
56
Ralph Metzler43dd07f2011-07-03 13:42:18 -030057#define DRXDAP_FASI_SHORT_FORMAT(addr) (((addr) & 0xFC30FF80) == 0)
58#define DRXDAP_FASI_LONG_FORMAT(addr) (((addr) & 0xFC30FF80) != 0)
59
60#define DEFAULT_MER_83 165
61#define DEFAULT_MER_93 250
62
63#ifndef DRXK_MPEG_SERIAL_OUTPUT_PIN_DRIVE_STRENGTH
64#define DRXK_MPEG_SERIAL_OUTPUT_PIN_DRIVE_STRENGTH (0x02)
65#endif
66
67#ifndef DRXK_MPEG_PARALLEL_OUTPUT_PIN_DRIVE_STRENGTH
68#define DRXK_MPEG_PARALLEL_OUTPUT_PIN_DRIVE_STRENGTH (0x03)
69#endif
70
Ralph Metzler43dd07f2011-07-03 13:42:18 -030071#define DEFAULT_DRXK_MPEG_LOCK_TIMEOUT 700
72#define DEFAULT_DRXK_DEMOD_LOCK_TIMEOUT 500
73
74#ifndef DRXK_KI_RAGC_ATV
75#define DRXK_KI_RAGC_ATV 4
76#endif
77#ifndef DRXK_KI_IAGC_ATV
78#define DRXK_KI_IAGC_ATV 6
79#endif
80#ifndef DRXK_KI_DAGC_ATV
81#define DRXK_KI_DAGC_ATV 7
82#endif
83
84#ifndef DRXK_KI_RAGC_QAM
85#define DRXK_KI_RAGC_QAM 3
86#endif
87#ifndef DRXK_KI_IAGC_QAM
88#define DRXK_KI_IAGC_QAM 4
89#endif
90#ifndef DRXK_KI_DAGC_QAM
91#define DRXK_KI_DAGC_QAM 7
92#endif
93#ifndef DRXK_KI_RAGC_DVBT
94#define DRXK_KI_RAGC_DVBT (IsA1WithPatchCode(state) ? 3 : 2)
95#endif
96#ifndef DRXK_KI_IAGC_DVBT
97#define DRXK_KI_IAGC_DVBT (IsA1WithPatchCode(state) ? 4 : 2)
98#endif
99#ifndef DRXK_KI_DAGC_DVBT
100#define DRXK_KI_DAGC_DVBT (IsA1WithPatchCode(state) ? 10 : 7)
101#endif
102
103#ifndef DRXK_AGC_DAC_OFFSET
104#define DRXK_AGC_DAC_OFFSET (0x800)
105#endif
106
107#ifndef DRXK_BANDWIDTH_8MHZ_IN_HZ
108#define DRXK_BANDWIDTH_8MHZ_IN_HZ (0x8B8249L)
109#endif
110
111#ifndef DRXK_BANDWIDTH_7MHZ_IN_HZ
112#define DRXK_BANDWIDTH_7MHZ_IN_HZ (0x7A1200L)
113#endif
114
115#ifndef DRXK_BANDWIDTH_6MHZ_IN_HZ
116#define DRXK_BANDWIDTH_6MHZ_IN_HZ (0x68A1B6L)
117#endif
118
119#ifndef DRXK_QAM_SYMBOLRATE_MAX
120#define DRXK_QAM_SYMBOLRATE_MAX (7233000)
121#endif
122
123#define DRXK_BL_ROM_OFFSET_TAPS_DVBT 56
124#define DRXK_BL_ROM_OFFSET_TAPS_ITU_A 64
125#define DRXK_BL_ROM_OFFSET_TAPS_ITU_C 0x5FE0
126#define DRXK_BL_ROM_OFFSET_TAPS_BG 24
127#define DRXK_BL_ROM_OFFSET_TAPS_DKILLP 32
128#define DRXK_BL_ROM_OFFSET_TAPS_NTSC 40
129#define DRXK_BL_ROM_OFFSET_TAPS_FM 48
130#define DRXK_BL_ROM_OFFSET_UCODE 0
131
132#define DRXK_BLC_TIMEOUT 100
133
134#define DRXK_BLCC_NR_ELEMENTS_TAPS 2
135#define DRXK_BLCC_NR_ELEMENTS_UCODE 6
136
137#define DRXK_BLDC_NR_ELEMENTS_TAPS 28
138
139#ifndef DRXK_OFDM_NE_NOTCH_WIDTH
140#define DRXK_OFDM_NE_NOTCH_WIDTH (4)
141#endif
142
143#define DRXK_QAM_SL_SIG_POWER_QAM16 (40960)
144#define DRXK_QAM_SL_SIG_POWER_QAM32 (20480)
145#define DRXK_QAM_SL_SIG_POWER_QAM64 (43008)
146#define DRXK_QAM_SL_SIG_POWER_QAM128 (20992)
147#define DRXK_QAM_SL_SIG_POWER_QAM256 (43520)
148
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -0300149static unsigned int debug;
150module_param(debug, int, 0644);
151MODULE_PARM_DESC(debug, "enable debug messages");
152
Mauro Carvalho Chehab52ee29f2014-09-28 23:23:19 -0300153#define dprintk(level, fmt, arg...) do { \
154if (debug >= level) \
155 printk(KERN_DEBUG KBUILD_MODNAME ": %s " fmt, __func__, ##arg); \
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -0300156} while (0)
157
158
Mauro Carvalho Chehabb01fbc12011-07-03 17:18:57 -0300159static inline u32 MulDiv32(u32 a, u32 b, u32 c)
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300160{
161 u64 tmp64;
162
Oliver Endrissebc7de22011-07-03 13:49:44 -0300163 tmp64 = (u64) a * (u64) b;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300164 do_div(tmp64, c);
165
166 return (u32) tmp64;
167}
168
Mauro Carvalho Chehabff38c212012-10-25 13:40:04 -0200169static inline u32 Frac28a(u32 a, u32 c)
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300170{
171 int i = 0;
172 u32 Q1 = 0;
173 u32 R0 = 0;
174
Oliver Endrissebc7de22011-07-03 13:49:44 -0300175 R0 = (a % c) << 4; /* 32-28 == 4 shifts possible at max */
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -0300176 Q1 = a / c; /*
177 * integer part, only the 4 least significant
178 * bits will be visible in the result
179 */
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300180
181 /* division using radix 16, 7 nibbles in the result */
182 for (i = 0; i < 7; i++) {
183 Q1 = (Q1 << 4) | (R0 / c);
184 R0 = (R0 % c) << 4;
185 }
186 /* rounding */
187 if ((R0 >> 3) >= c)
188 Q1++;
189
190 return Q1;
191}
192
Mauro Carvalho Chehabb5e9eb62013-04-28 11:47:43 -0300193static inline u32 log10times100(u32 value)
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300194{
Mauro Carvalho Chehabb5e9eb62013-04-28 11:47:43 -0300195 return (100L * intlog10(value)) >> 24;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300196}
197
Mauro Carvalho Chehab34eb9752017-11-27 10:10:28 -0500198/***************************************************************************/
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300199/* I2C **********************************************************************/
Mauro Carvalho Chehab34eb9752017-11-27 10:10:28 -0500200/***************************************************************************/
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300201
Mauro Carvalho Chehab20bfe7a2012-06-29 14:43:32 -0300202static int drxk_i2c_lock(struct drxk_state *state)
203{
Peter Rosinc060a9f2018-06-20 07:17:58 +0200204 i2c_lock_bus(state->i2c, I2C_LOCK_SEGMENT);
Mauro Carvalho Chehab20bfe7a2012-06-29 14:43:32 -0300205 state->drxk_i2c_exclusive_lock = true;
206
207 return 0;
208}
209
210static void drxk_i2c_unlock(struct drxk_state *state)
211{
212 if (!state->drxk_i2c_exclusive_lock)
213 return;
214
Peter Rosinc060a9f2018-06-20 07:17:58 +0200215 i2c_unlock_bus(state->i2c, I2C_LOCK_SEGMENT);
Mauro Carvalho Chehab20bfe7a2012-06-29 14:43:32 -0300216 state->drxk_i2c_exclusive_lock = false;
217}
218
Mauro Carvalho Chehab2a5f6722012-06-29 14:24:18 -0300219static int drxk_i2c_transfer(struct drxk_state *state, struct i2c_msg *msgs,
220 unsigned len)
221{
Mauro Carvalho Chehab20bfe7a2012-06-29 14:43:32 -0300222 if (state->drxk_i2c_exclusive_lock)
223 return __i2c_transfer(state->i2c, msgs, len);
224 else
225 return i2c_transfer(state->i2c, msgs, len);
Mauro Carvalho Chehab2a5f6722012-06-29 14:24:18 -0300226}
227
228static int i2c_read1(struct drxk_state *state, u8 adr, u8 *val)
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300229{
Oliver Endrissebc7de22011-07-03 13:49:44 -0300230 struct i2c_msg msgs[1] = { {.addr = adr, .flags = I2C_M_RD,
231 .buf = val, .len = 1}
232 };
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300233
Mauro Carvalho Chehab2a5f6722012-06-29 14:24:18 -0300234 return drxk_i2c_transfer(state, msgs, 1);
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300235}
236
Mauro Carvalho Chehab2a5f6722012-06-29 14:24:18 -0300237static int i2c_write(struct drxk_state *state, u8 adr, u8 *data, int len)
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300238{
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300239 int status;
Oliver Endrissebc7de22011-07-03 13:49:44 -0300240 struct i2c_msg msg = {
241 .addr = adr, .flags = 0, .buf = data, .len = len };
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300242
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -0300243 dprintk(3, ":");
244 if (debug > 2) {
245 int i;
246 for (i = 0; i < len; i++)
Mauro Carvalho Chehab0fb220f2013-04-28 11:47:46 -0300247 pr_cont(" %02x", data[i]);
248 pr_cont("\n");
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -0300249 }
Mauro Carvalho Chehab2a5f6722012-06-29 14:24:18 -0300250 status = drxk_i2c_transfer(state, &msg, 1);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300251 if (status >= 0 && status != 1)
252 status = -EIO;
253
254 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -0300255 pr_err("i2c write error at addr 0x%02x\n", adr);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300256
257 return status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300258}
259
Mauro Carvalho Chehab2a5f6722012-06-29 14:24:18 -0300260static int i2c_read(struct drxk_state *state,
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300261 u8 adr, u8 *msg, int len, u8 *answ, int alen)
262{
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300263 int status;
Mauro Carvalho Chehabe4f4f872011-07-09 17:35:26 -0300264 struct i2c_msg msgs[2] = {
265 {.addr = adr, .flags = 0,
Oliver Endrissebc7de22011-07-03 13:49:44 -0300266 .buf = msg, .len = len},
Mauro Carvalho Chehabe4f4f872011-07-09 17:35:26 -0300267 {.addr = adr, .flags = I2C_M_RD,
268 .buf = answ, .len = alen}
Oliver Endrissebc7de22011-07-03 13:49:44 -0300269 };
Mauro Carvalho Chehabf07a0bc2011-07-21 22:30:27 -0300270
Mauro Carvalho Chehab2a5f6722012-06-29 14:24:18 -0300271 status = drxk_i2c_transfer(state, msgs, 2);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300272 if (status != 2) {
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -0300273 if (debug > 2)
Mauro Carvalho Chehab0fb220f2013-04-28 11:47:46 -0300274 pr_cont(": ERROR!\n");
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300275 if (status >= 0)
276 status = -EIO;
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -0300277
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -0300278 pr_err("i2c read error at addr 0x%02x\n", adr);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300279 return status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300280 }
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -0300281 if (debug > 2) {
282 int i;
Mauro Carvalho Chehab0d3e6fe2011-07-22 12:34:41 -0300283 dprintk(2, ": read from");
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -0300284 for (i = 0; i < len; i++)
Mauro Carvalho Chehab0fb220f2013-04-28 11:47:46 -0300285 pr_cont(" %02x", msg[i]);
286 pr_cont(", value = ");
Mauro Carvalho Chehabf07a0bc2011-07-21 22:30:27 -0300287 for (i = 0; i < alen; i++)
Mauro Carvalho Chehab0fb220f2013-04-28 11:47:46 -0300288 pr_cont(" %02x", answ[i]);
289 pr_cont("\n");
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -0300290 }
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300291 return 0;
292}
293
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -0300294static int read16_flags(struct drxk_state *state, u32 reg, u16 *data, u8 flags)
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300295{
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300296 int status;
Oliver Endrissebc7de22011-07-03 13:49:44 -0300297 u8 adr = state->demod_address, mm1[4], mm2[2], len;
Mauro Carvalho Chehabe076c922011-07-09 13:06:12 -0300298
299 if (state->single_master)
300 flags |= 0xC0;
301
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300302 if (DRXDAP_FASI_LONG_FORMAT(reg) || (flags != 0)) {
303 mm1[0] = (((reg << 1) & 0xFF) | 0x01);
304 mm1[1] = ((reg >> 16) & 0xFF);
305 mm1[2] = ((reg >> 24) & 0xFF) | flags;
306 mm1[3] = ((reg >> 7) & 0xFF);
307 len = 4;
308 } else {
309 mm1[0] = ((reg << 1) & 0xFF);
310 mm1[1] = (((reg >> 16) & 0x0F) | ((reg >> 18) & 0xF0));
311 len = 2;
312 }
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -0300313 dprintk(2, "(0x%08x, 0x%02x)\n", reg, flags);
Mauro Carvalho Chehab2a5f6722012-06-29 14:24:18 -0300314 status = i2c_read(state, adr, mm1, len, mm2, 2);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300315 if (status < 0)
316 return status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300317 if (data)
318 *data = mm2[0] | (mm2[1] << 8);
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -0300319
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300320 return 0;
321}
322
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -0300323static int read16(struct drxk_state *state, u32 reg, u16 *data)
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300324{
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -0300325 return read16_flags(state, reg, data, 0);
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300326}
327
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -0300328static int read32_flags(struct drxk_state *state, u32 reg, u32 *data, u8 flags)
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300329{
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300330 int status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300331 u8 adr = state->demod_address, mm1[4], mm2[4], len;
Mauro Carvalho Chehabe076c922011-07-09 13:06:12 -0300332
333 if (state->single_master)
334 flags |= 0xC0;
335
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300336 if (DRXDAP_FASI_LONG_FORMAT(reg) || (flags != 0)) {
337 mm1[0] = (((reg << 1) & 0xFF) | 0x01);
338 mm1[1] = ((reg >> 16) & 0xFF);
339 mm1[2] = ((reg >> 24) & 0xFF) | flags;
340 mm1[3] = ((reg >> 7) & 0xFF);
341 len = 4;
342 } else {
343 mm1[0] = ((reg << 1) & 0xFF);
344 mm1[1] = (((reg >> 16) & 0x0F) | ((reg >> 18) & 0xF0));
345 len = 2;
346 }
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -0300347 dprintk(2, "(0x%08x, 0x%02x)\n", reg, flags);
Mauro Carvalho Chehab2a5f6722012-06-29 14:24:18 -0300348 status = i2c_read(state, adr, mm1, len, mm2, 4);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300349 if (status < 0)
350 return status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300351 if (data)
352 *data = mm2[0] | (mm2[1] << 8) |
Oliver Endrissebc7de22011-07-03 13:49:44 -0300353 (mm2[2] << 16) | (mm2[3] << 24);
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -0300354
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300355 return 0;
356}
357
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -0300358static int read32(struct drxk_state *state, u32 reg, u32 *data)
359{
360 return read32_flags(state, reg, data, 0);
361}
362
363static int write16_flags(struct drxk_state *state, u32 reg, u16 data, u8 flags)
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300364{
365 u8 adr = state->demod_address, mm[6], len;
Mauro Carvalho Chehabe076c922011-07-09 13:06:12 -0300366
367 if (state->single_master)
368 flags |= 0xC0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300369 if (DRXDAP_FASI_LONG_FORMAT(reg) || (flags != 0)) {
370 mm[0] = (((reg << 1) & 0xFF) | 0x01);
371 mm[1] = ((reg >> 16) & 0xFF);
372 mm[2] = ((reg >> 24) & 0xFF) | flags;
373 mm[3] = ((reg >> 7) & 0xFF);
374 len = 4;
375 } else {
376 mm[0] = ((reg << 1) & 0xFF);
377 mm[1] = (((reg >> 16) & 0x0F) | ((reg >> 18) & 0xF0));
378 len = 2;
379 }
380 mm[len] = data & 0xff;
Oliver Endrissebc7de22011-07-03 13:49:44 -0300381 mm[len + 1] = (data >> 8) & 0xff;
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -0300382
383 dprintk(2, "(0x%08x, 0x%04x, 0x%02x)\n", reg, data, flags);
Mauro Carvalho Chehab2a5f6722012-06-29 14:24:18 -0300384 return i2c_write(state, adr, mm, len + 2);
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300385}
386
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -0300387static int write16(struct drxk_state *state, u32 reg, u16 data)
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300388{
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -0300389 return write16_flags(state, reg, data, 0);
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300390}
391
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -0300392static int write32_flags(struct drxk_state *state, u32 reg, u32 data, u8 flags)
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300393{
394 u8 adr = state->demod_address, mm[8], len;
Mauro Carvalho Chehabe076c922011-07-09 13:06:12 -0300395
396 if (state->single_master)
397 flags |= 0xC0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300398 if (DRXDAP_FASI_LONG_FORMAT(reg) || (flags != 0)) {
399 mm[0] = (((reg << 1) & 0xFF) | 0x01);
400 mm[1] = ((reg >> 16) & 0xFF);
401 mm[2] = ((reg >> 24) & 0xFF) | flags;
402 mm[3] = ((reg >> 7) & 0xFF);
403 len = 4;
404 } else {
405 mm[0] = ((reg << 1) & 0xFF);
406 mm[1] = (((reg >> 16) & 0x0F) | ((reg >> 18) & 0xF0));
407 len = 2;
408 }
409 mm[len] = data & 0xff;
Oliver Endrissebc7de22011-07-03 13:49:44 -0300410 mm[len + 1] = (data >> 8) & 0xff;
411 mm[len + 2] = (data >> 16) & 0xff;
412 mm[len + 3] = (data >> 24) & 0xff;
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -0300413 dprintk(2, "(0x%08x, 0x%08x, 0x%02x)\n", reg, data, flags);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300414
Mauro Carvalho Chehab2a5f6722012-06-29 14:24:18 -0300415 return i2c_write(state, adr, mm, len + 4);
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300416}
417
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -0300418static int write32(struct drxk_state *state, u32 reg, u32 data)
419{
420 return write32_flags(state, reg, data, 0);
421}
422
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300423static int write_block(struct drxk_state *state, u32 address,
424 const int block_size, const u8 p_block[])
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300425{
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300426 int status = 0, blk_size = block_size;
427 u8 flags = 0;
Mauro Carvalho Chehabe076c922011-07-09 13:06:12 -0300428
429 if (state->single_master)
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300430 flags |= 0xC0;
Mauro Carvalho Chehabe076c922011-07-09 13:06:12 -0300431
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300432 while (blk_size > 0) {
433 int chunk = blk_size > state->m_chunk_size ?
434 state->m_chunk_size : blk_size;
435 u8 *adr_buf = &state->chunk[0];
436 u32 adr_length = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300437
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300438 if (DRXDAP_FASI_LONG_FORMAT(address) || (flags != 0)) {
439 adr_buf[0] = (((address << 1) & 0xFF) | 0x01);
440 adr_buf[1] = ((address >> 16) & 0xFF);
441 adr_buf[2] = ((address >> 24) & 0xFF);
442 adr_buf[3] = ((address >> 7) & 0xFF);
443 adr_buf[2] |= flags;
444 adr_length = 4;
445 if (chunk == state->m_chunk_size)
446 chunk -= 2;
Oliver Endrissebc7de22011-07-03 13:49:44 -0300447 } else {
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300448 adr_buf[0] = ((address << 1) & 0xFF);
449 adr_buf[1] = (((address >> 16) & 0x0F) |
450 ((address >> 18) & 0xF0));
451 adr_length = 2;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300452 }
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300453 memcpy(&state->chunk[adr_length], p_block, chunk);
454 dprintk(2, "(0x%08x, 0x%02x)\n", address, flags);
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -0300455 if (debug > 1) {
456 int i;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300457 if (p_block)
458 for (i = 0; i < chunk; i++)
Mauro Carvalho Chehab0fb220f2013-04-28 11:47:46 -0300459 pr_cont(" %02x", p_block[i]);
460 pr_cont("\n");
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -0300461 }
Mauro Carvalho Chehab2a5f6722012-06-29 14:24:18 -0300462 status = i2c_write(state, state->demod_address,
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300463 &state->chunk[0], chunk + adr_length);
Oliver Endrissebc7de22011-07-03 13:49:44 -0300464 if (status < 0) {
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -0300465 pr_err("%s: i2c write error at addr 0x%02x\n",
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300466 __func__, address);
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300467 break;
468 }
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300469 p_block += chunk;
470 address += (chunk >> 1);
471 blk_size -= chunk;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300472 }
Oliver Endrissebc7de22011-07-03 13:49:44 -0300473 return status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300474}
475
476#ifndef DRXK_MAX_RETRIES_POWERUP
477#define DRXK_MAX_RETRIES_POWERUP 20
478#endif
479
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300480static int power_up_device(struct drxk_state *state)
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300481{
482 int status;
483 u8 data = 0;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300484 u16 retry_count = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300485
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -0300486 dprintk(1, "\n");
487
Mauro Carvalho Chehab2a5f6722012-06-29 14:24:18 -0300488 status = i2c_read1(state, state->demod_address, &data);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300489 if (status < 0) {
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300490 do {
491 data = 0;
Mauro Carvalho Chehab2a5f6722012-06-29 14:24:18 -0300492 status = i2c_write(state, state->demod_address,
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300493 &data, 1);
Mauro Carvalho Chehabb72852b2013-04-28 11:47:47 -0300494 usleep_range(10000, 11000);
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300495 retry_count++;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300496 if (status < 0)
497 continue;
Mauro Carvalho Chehab2a5f6722012-06-29 14:24:18 -0300498 status = i2c_read1(state, state->demod_address,
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300499 &data);
500 } while (status < 0 &&
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300501 (retry_count < DRXK_MAX_RETRIES_POWERUP));
502 if (status < 0 && retry_count >= DRXK_MAX_RETRIES_POWERUP)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300503 goto error;
504 }
505
506 /* Make sure all clk domains are active */
507 status = write16(state, SIO_CC_PWD_MODE__A, SIO_CC_PWD_MODE_LEVEL_NONE);
508 if (status < 0)
509 goto error;
510 status = write16(state, SIO_CC_UPDATE__A, SIO_CC_UPDATE_KEY);
511 if (status < 0)
512 goto error;
513 /* Enable pll lock tests */
514 status = write16(state, SIO_CC_PLL_LOCK__A, 1);
515 if (status < 0)
516 goto error;
517
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300518 state->m_current_power_mode = DRX_POWER_UP;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300519
520error:
521 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -0300522 pr_err("Error %d on %s\n", status, __func__);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300523
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300524 return status;
525}
526
527
528static int init_state(struct drxk_state *state)
529{
Mauro Carvalho Chehab147e1102011-07-10 08:24:26 -0300530 /*
Mauro Carvalho Chehab5a13e402015-05-08 08:59:16 -0300531 * FIXME: most (all?) of the values below should be moved into
Mauro Carvalho Chehab147e1102011-07-10 08:24:26 -0300532 * struct drxk_config, as they are probably board-specific
533 */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300534 u32 ul_vsb_if_agc_mode = DRXK_AGC_CTRL_AUTO;
535 u32 ul_vsb_if_agc_output_level = 0;
536 u32 ul_vsb_if_agc_min_level = 0;
537 u32 ul_vsb_if_agc_max_level = 0x7FFF;
538 u32 ul_vsb_if_agc_speed = 3;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300539
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300540 u32 ul_vsb_rf_agc_mode = DRXK_AGC_CTRL_AUTO;
541 u32 ul_vsb_rf_agc_output_level = 0;
542 u32 ul_vsb_rf_agc_min_level = 0;
543 u32 ul_vsb_rf_agc_max_level = 0x7FFF;
544 u32 ul_vsb_rf_agc_speed = 3;
545 u32 ul_vsb_rf_agc_top = 9500;
546 u32 ul_vsb_rf_agc_cut_off_current = 4000;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300547
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300548 u32 ul_atv_if_agc_mode = DRXK_AGC_CTRL_AUTO;
549 u32 ul_atv_if_agc_output_level = 0;
550 u32 ul_atv_if_agc_min_level = 0;
551 u32 ul_atv_if_agc_max_level = 0;
552 u32 ul_atv_if_agc_speed = 3;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300553
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300554 u32 ul_atv_rf_agc_mode = DRXK_AGC_CTRL_OFF;
555 u32 ul_atv_rf_agc_output_level = 0;
556 u32 ul_atv_rf_agc_min_level = 0;
557 u32 ul_atv_rf_agc_max_level = 0;
558 u32 ul_atv_rf_agc_top = 9500;
559 u32 ul_atv_rf_agc_cut_off_current = 4000;
560 u32 ul_atv_rf_agc_speed = 3;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300561
562 u32 ulQual83 = DEFAULT_MER_83;
563 u32 ulQual93 = DEFAULT_MER_93;
564
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300565 u32 ul_mpeg_lock_time_out = DEFAULT_DRXK_MPEG_LOCK_TIMEOUT;
566 u32 ul_demod_lock_time_out = DEFAULT_DRXK_DEMOD_LOCK_TIMEOUT;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300567
568 /* io_pad_cfg register (8 bit reg.) MSB bit is 1 (default value) */
569 /* io_pad_cfg_mode output mode is drive always */
570 /* io_pad_cfg_drive is set to power 2 (23 mA) */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300571 u32 ul_gpio_cfg = 0x0113;
572 u32 ul_invert_ts_clock = 0;
573 u32 ul_ts_data_strength = DRXK_MPEG_SERIAL_OUTPUT_PIN_DRIVE_STRENGTH;
574 u32 ul_dvbt_bitrate = 50000000;
575 u32 ul_dvbc_bitrate = DRXK_QAM_SYMBOLRATE_MAX * 8;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300576
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300577 u32 ul_insert_rs_byte = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300578
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300579 u32 ul_rf_mirror = 1;
580 u32 ul_power_down = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300581
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -0300582 dprintk(1, "\n");
583
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300584 state->m_has_lna = false;
585 state->m_has_dvbt = false;
586 state->m_has_dvbc = false;
587 state->m_has_atv = false;
588 state->m_has_oob = false;
589 state->m_has_audio = false;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300590
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300591 if (!state->m_chunk_size)
592 state->m_chunk_size = 124;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300593
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300594 state->m_osc_clock_freq = 0;
595 state->m_smart_ant_inverted = false;
596 state->m_b_p_down_open_bridge = false;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300597
598 /* real system clock frequency in kHz */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300599 state->m_sys_clock_freq = 151875;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300600 /* Timing div, 250ns/Psys */
601 /* Timing div, = (delay (nano seconds) * sysclk (kHz))/ 1000 */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300602 state->m_hi_cfg_timing_div = ((state->m_sys_clock_freq / 1000) *
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300603 HI_I2C_DELAY) / 1000;
604 /* Clipping */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300605 if (state->m_hi_cfg_timing_div > SIO_HI_RA_RAM_PAR_2_CFG_DIV__M)
606 state->m_hi_cfg_timing_div = SIO_HI_RA_RAM_PAR_2_CFG_DIV__M;
607 state->m_hi_cfg_wake_up_key = (state->demod_address << 1);
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300608 /* port/bridge/power down ctrl */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300609 state->m_hi_cfg_ctrl = SIO_HI_RA_RAM_PAR_5_CFG_SLV0_SLAVE;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300610
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300611 state->m_b_power_down = (ul_power_down != 0);
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300612
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300613 state->m_drxk_a3_patch_code = false;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300614
615 /* Init AGC and PGA parameters */
616 /* VSB IF */
Mauro Carvalho Chehab949dd082013-04-28 11:47:50 -0300617 state->m_vsb_if_agc_cfg.ctrl_mode = ul_vsb_if_agc_mode;
618 state->m_vsb_if_agc_cfg.output_level = ul_vsb_if_agc_output_level;
619 state->m_vsb_if_agc_cfg.min_output_level = ul_vsb_if_agc_min_level;
620 state->m_vsb_if_agc_cfg.max_output_level = ul_vsb_if_agc_max_level;
621 state->m_vsb_if_agc_cfg.speed = ul_vsb_if_agc_speed;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300622 state->m_vsb_pga_cfg = 140;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300623
624 /* VSB RF */
Mauro Carvalho Chehab949dd082013-04-28 11:47:50 -0300625 state->m_vsb_rf_agc_cfg.ctrl_mode = ul_vsb_rf_agc_mode;
626 state->m_vsb_rf_agc_cfg.output_level = ul_vsb_rf_agc_output_level;
627 state->m_vsb_rf_agc_cfg.min_output_level = ul_vsb_rf_agc_min_level;
628 state->m_vsb_rf_agc_cfg.max_output_level = ul_vsb_rf_agc_max_level;
629 state->m_vsb_rf_agc_cfg.speed = ul_vsb_rf_agc_speed;
630 state->m_vsb_rf_agc_cfg.top = ul_vsb_rf_agc_top;
631 state->m_vsb_rf_agc_cfg.cut_off_current = ul_vsb_rf_agc_cut_off_current;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300632 state->m_vsb_pre_saw_cfg.reference = 0x07;
633 state->m_vsb_pre_saw_cfg.use_pre_saw = true;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300634
635 state->m_Quality83percent = DEFAULT_MER_83;
636 state->m_Quality93percent = DEFAULT_MER_93;
637 if (ulQual93 <= 500 && ulQual83 < ulQual93) {
638 state->m_Quality83percent = ulQual83;
639 state->m_Quality93percent = ulQual93;
640 }
641
642 /* ATV IF */
Mauro Carvalho Chehab949dd082013-04-28 11:47:50 -0300643 state->m_atv_if_agc_cfg.ctrl_mode = ul_atv_if_agc_mode;
644 state->m_atv_if_agc_cfg.output_level = ul_atv_if_agc_output_level;
645 state->m_atv_if_agc_cfg.min_output_level = ul_atv_if_agc_min_level;
646 state->m_atv_if_agc_cfg.max_output_level = ul_atv_if_agc_max_level;
647 state->m_atv_if_agc_cfg.speed = ul_atv_if_agc_speed;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300648
649 /* ATV RF */
Mauro Carvalho Chehab949dd082013-04-28 11:47:50 -0300650 state->m_atv_rf_agc_cfg.ctrl_mode = ul_atv_rf_agc_mode;
651 state->m_atv_rf_agc_cfg.output_level = ul_atv_rf_agc_output_level;
652 state->m_atv_rf_agc_cfg.min_output_level = ul_atv_rf_agc_min_level;
653 state->m_atv_rf_agc_cfg.max_output_level = ul_atv_rf_agc_max_level;
654 state->m_atv_rf_agc_cfg.speed = ul_atv_rf_agc_speed;
655 state->m_atv_rf_agc_cfg.top = ul_atv_rf_agc_top;
656 state->m_atv_rf_agc_cfg.cut_off_current = ul_atv_rf_agc_cut_off_current;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300657 state->m_atv_pre_saw_cfg.reference = 0x04;
658 state->m_atv_pre_saw_cfg.use_pre_saw = true;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300659
660
661 /* DVBT RF */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300662 state->m_dvbt_rf_agc_cfg.ctrl_mode = DRXK_AGC_CTRL_OFF;
663 state->m_dvbt_rf_agc_cfg.output_level = 0;
664 state->m_dvbt_rf_agc_cfg.min_output_level = 0;
665 state->m_dvbt_rf_agc_cfg.max_output_level = 0xFFFF;
666 state->m_dvbt_rf_agc_cfg.top = 0x2100;
667 state->m_dvbt_rf_agc_cfg.cut_off_current = 4000;
668 state->m_dvbt_rf_agc_cfg.speed = 1;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300669
670
671 /* DVBT IF */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300672 state->m_dvbt_if_agc_cfg.ctrl_mode = DRXK_AGC_CTRL_AUTO;
673 state->m_dvbt_if_agc_cfg.output_level = 0;
674 state->m_dvbt_if_agc_cfg.min_output_level = 0;
675 state->m_dvbt_if_agc_cfg.max_output_level = 9000;
676 state->m_dvbt_if_agc_cfg.top = 13424;
677 state->m_dvbt_if_agc_cfg.cut_off_current = 0;
678 state->m_dvbt_if_agc_cfg.speed = 3;
679 state->m_dvbt_if_agc_cfg.fast_clip_ctrl_delay = 30;
680 state->m_dvbt_if_agc_cfg.ingain_tgt_max = 30000;
Oliver Endrissebc7de22011-07-03 13:49:44 -0300681 /* state->m_dvbtPgaCfg = 140; */
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300682
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300683 state->m_dvbt_pre_saw_cfg.reference = 4;
684 state->m_dvbt_pre_saw_cfg.use_pre_saw = false;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300685
686 /* QAM RF */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300687 state->m_qam_rf_agc_cfg.ctrl_mode = DRXK_AGC_CTRL_OFF;
688 state->m_qam_rf_agc_cfg.output_level = 0;
689 state->m_qam_rf_agc_cfg.min_output_level = 6023;
690 state->m_qam_rf_agc_cfg.max_output_level = 27000;
691 state->m_qam_rf_agc_cfg.top = 0x2380;
692 state->m_qam_rf_agc_cfg.cut_off_current = 4000;
693 state->m_qam_rf_agc_cfg.speed = 3;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300694
695 /* QAM IF */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300696 state->m_qam_if_agc_cfg.ctrl_mode = DRXK_AGC_CTRL_AUTO;
697 state->m_qam_if_agc_cfg.output_level = 0;
698 state->m_qam_if_agc_cfg.min_output_level = 0;
699 state->m_qam_if_agc_cfg.max_output_level = 9000;
700 state->m_qam_if_agc_cfg.top = 0x0511;
701 state->m_qam_if_agc_cfg.cut_off_current = 0;
702 state->m_qam_if_agc_cfg.speed = 3;
703 state->m_qam_if_agc_cfg.ingain_tgt_max = 5119;
704 state->m_qam_if_agc_cfg.fast_clip_ctrl_delay = 50;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300705
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300706 state->m_qam_pga_cfg = 140;
707 state->m_qam_pre_saw_cfg.reference = 4;
708 state->m_qam_pre_saw_cfg.use_pre_saw = false;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300709
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300710 state->m_operation_mode = OM_NONE;
711 state->m_drxk_state = DRXK_UNINITIALIZED;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300712
713 /* MPEG output configuration */
Mauro Carvalho Chehab868c9a12019-02-18 14:28:55 -0500714 state->m_enable_mpeg_output = true; /* If TRUE; enable MPEG output */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300715 state->m_insert_rs_byte = false; /* If TRUE; insert RS byte */
716 state->m_invert_data = false; /* If TRUE; invert DATA signals */
717 state->m_invert_err = false; /* If TRUE; invert ERR signal */
718 state->m_invert_str = false; /* If TRUE; invert STR signals */
719 state->m_invert_val = false; /* If TRUE; invert VAL signals */
720 state->m_invert_clk = (ul_invert_ts_clock != 0); /* If TRUE; invert CLK signals */
Mauro Carvalho Chehab67f04612012-01-20 18:30:58 -0300721
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300722 /* If TRUE; static MPEG clockrate will be used;
723 otherwise clockrate will adapt to the bitrate of the TS */
724
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300725 state->m_dvbt_bitrate = ul_dvbt_bitrate;
726 state->m_dvbc_bitrate = ul_dvbc_bitrate;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300727
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300728 state->m_ts_data_strength = (ul_ts_data_strength & 0x07);
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300729
730 /* Maximum bitrate in b/s in case static clockrate is selected */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300731 state->m_mpeg_ts_static_bitrate = 19392658;
732 state->m_disable_te_ihandling = false;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300733
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300734 if (ul_insert_rs_byte)
735 state->m_insert_rs_byte = true;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300736
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300737 state->m_mpeg_lock_time_out = DEFAULT_DRXK_MPEG_LOCK_TIMEOUT;
738 if (ul_mpeg_lock_time_out < 10000)
739 state->m_mpeg_lock_time_out = ul_mpeg_lock_time_out;
740 state->m_demod_lock_time_out = DEFAULT_DRXK_DEMOD_LOCK_TIMEOUT;
741 if (ul_demod_lock_time_out < 10000)
742 state->m_demod_lock_time_out = ul_demod_lock_time_out;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300743
Oliver Endrissebc7de22011-07-03 13:49:44 -0300744 /* QAM defaults */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300745 state->m_constellation = DRX_CONSTELLATION_AUTO;
746 state->m_qam_interleave_mode = DRXK_QAM_I12_J17;
747 state->m_fec_rs_plen = 204 * 8; /* fecRsPlen annex A */
748 state->m_fec_rs_prescale = 1;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300749
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300750 state->m_sqi_speed = DRXK_DVBT_SQI_SPEED_MEDIUM;
751 state->m_agcfast_clip_ctrl_delay = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300752
Mauro Carvalho Chehab949dd082013-04-28 11:47:50 -0300753 state->m_gpio_cfg = ul_gpio_cfg;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300754
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300755 state->m_b_power_down = false;
756 state->m_current_power_mode = DRX_POWER_DOWN;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300757
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300758 state->m_rfmirror = (ul_rf_mirror == 0);
759 state->m_if_agc_pol = false;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300760 return 0;
761}
762
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300763static int drxx_open(struct drxk_state *state)
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300764{
765 int status = 0;
766 u32 jtag = 0;
767 u16 bid = 0;
768 u16 key = 0;
769
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -0300770 dprintk(1, "\n");
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300771 /* stop lock indicator process */
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -0300772 status = write16(state, SCU_RAM_GPIO__A,
773 SCU_RAM_GPIO_HW_LOCK_IND_DISABLE);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300774 if (status < 0)
775 goto error;
776 /* Check device id */
777 status = read16(state, SIO_TOP_COMM_KEY__A, &key);
778 if (status < 0)
779 goto error;
780 status = write16(state, SIO_TOP_COMM_KEY__A, SIO_TOP_COMM_KEY_KEY);
781 if (status < 0)
782 goto error;
783 status = read32(state, SIO_TOP_JTAGID_LO__A, &jtag);
784 if (status < 0)
785 goto error;
786 status = read16(state, SIO_PDR_UIO_IN_HI__A, &bid);
787 if (status < 0)
788 goto error;
789 status = write16(state, SIO_TOP_COMM_KEY__A, key);
790error:
791 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -0300792 pr_err("Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300793 return status;
794}
795
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300796static int get_device_capabilities(struct drxk_state *state)
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300797{
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300798 u16 sio_pdr_ohw_cfg = 0;
799 u32 sio_top_jtagid_lo = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300800 int status;
Mauro Carvalho Chehab9c6e1822011-07-10 08:38:18 -0300801 const char *spin = "";
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300802
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -0300803 dprintk(1, "\n");
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300804
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300805 /* driver 0.9.0 */
806 /* stop lock indicator process */
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -0300807 status = write16(state, SCU_RAM_GPIO__A,
808 SCU_RAM_GPIO_HW_LOCK_IND_DISABLE);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300809 if (status < 0)
810 goto error;
Martin Blumenstingl84183662012-10-04 14:22:55 -0300811 status = write16(state, SIO_TOP_COMM_KEY__A, SIO_TOP_COMM_KEY_KEY);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300812 if (status < 0)
813 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300814 status = read16(state, SIO_PDR_OHW_CFG__A, &sio_pdr_ohw_cfg);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300815 if (status < 0)
816 goto error;
817 status = write16(state, SIO_TOP_COMM_KEY__A, 0x0000);
818 if (status < 0)
819 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300820
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300821 switch ((sio_pdr_ohw_cfg & SIO_PDR_OHW_CFG_FREF_SEL__M)) {
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300822 case 0:
823 /* ignore (bypass ?) */
824 break;
825 case 1:
826 /* 27 MHz */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300827 state->m_osc_clock_freq = 27000;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300828 break;
829 case 2:
830 /* 20.25 MHz */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300831 state->m_osc_clock_freq = 20250;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300832 break;
833 case 3:
834 /* 4 MHz */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300835 state->m_osc_clock_freq = 20250;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300836 break;
837 default:
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -0300838 pr_err("Clock Frequency is unknown\n");
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300839 return -EINVAL;
840 }
841 /*
842 Determine device capabilities
843 Based on pinning v14
844 */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300845 status = read32(state, SIO_TOP_JTAGID_LO__A, &sio_top_jtagid_lo);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300846 if (status < 0)
847 goto error;
Mauro Carvalho Chehab0d3e6fe2011-07-22 12:34:41 -0300848
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -0300849 pr_info("status = 0x%08x\n", sio_top_jtagid_lo);
Mauro Carvalho Chehab0d3e6fe2011-07-22 12:34:41 -0300850
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300851 /* driver 0.9.0 */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300852 switch ((sio_top_jtagid_lo >> 29) & 0xF) {
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300853 case 0:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300854 state->m_device_spin = DRXK_SPIN_A1;
Mauro Carvalho Chehab9c6e1822011-07-10 08:38:18 -0300855 spin = "A1";
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300856 break;
857 case 2:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300858 state->m_device_spin = DRXK_SPIN_A2;
Mauro Carvalho Chehab9c6e1822011-07-10 08:38:18 -0300859 spin = "A2";
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300860 break;
861 case 3:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300862 state->m_device_spin = DRXK_SPIN_A3;
Mauro Carvalho Chehab9c6e1822011-07-10 08:38:18 -0300863 spin = "A3";
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300864 break;
865 default:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300866 state->m_device_spin = DRXK_SPIN_UNKNOWN;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300867 status = -EINVAL;
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -0300868 pr_err("Spin %d unknown\n", (sio_top_jtagid_lo >> 29) & 0xF);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300869 goto error2;
870 }
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300871 switch ((sio_top_jtagid_lo >> 12) & 0xFF) {
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300872 case 0x13:
873 /* typeId = DRX3913K_TYPE_ID */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300874 state->m_has_lna = false;
875 state->m_has_oob = false;
876 state->m_has_atv = false;
877 state->m_has_audio = false;
878 state->m_has_dvbt = true;
879 state->m_has_dvbc = true;
880 state->m_has_sawsw = true;
881 state->m_has_gpio2 = false;
882 state->m_has_gpio1 = false;
883 state->m_has_irqn = false;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300884 break;
885 case 0x15:
886 /* typeId = DRX3915K_TYPE_ID */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300887 state->m_has_lna = false;
888 state->m_has_oob = false;
889 state->m_has_atv = true;
890 state->m_has_audio = false;
891 state->m_has_dvbt = true;
892 state->m_has_dvbc = false;
893 state->m_has_sawsw = true;
894 state->m_has_gpio2 = true;
895 state->m_has_gpio1 = true;
896 state->m_has_irqn = false;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300897 break;
898 case 0x16:
899 /* typeId = DRX3916K_TYPE_ID */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300900 state->m_has_lna = false;
901 state->m_has_oob = false;
902 state->m_has_atv = true;
903 state->m_has_audio = false;
904 state->m_has_dvbt = true;
905 state->m_has_dvbc = false;
906 state->m_has_sawsw = true;
907 state->m_has_gpio2 = true;
908 state->m_has_gpio1 = true;
909 state->m_has_irqn = false;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300910 break;
911 case 0x18:
912 /* typeId = DRX3918K_TYPE_ID */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300913 state->m_has_lna = false;
914 state->m_has_oob = false;
915 state->m_has_atv = true;
916 state->m_has_audio = true;
917 state->m_has_dvbt = true;
918 state->m_has_dvbc = false;
919 state->m_has_sawsw = true;
920 state->m_has_gpio2 = true;
921 state->m_has_gpio1 = true;
922 state->m_has_irqn = false;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300923 break;
924 case 0x21:
925 /* typeId = DRX3921K_TYPE_ID */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300926 state->m_has_lna = false;
927 state->m_has_oob = false;
928 state->m_has_atv = true;
929 state->m_has_audio = true;
930 state->m_has_dvbt = true;
931 state->m_has_dvbc = true;
932 state->m_has_sawsw = true;
933 state->m_has_gpio2 = true;
934 state->m_has_gpio1 = true;
935 state->m_has_irqn = false;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300936 break;
937 case 0x23:
938 /* typeId = DRX3923K_TYPE_ID */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300939 state->m_has_lna = false;
940 state->m_has_oob = false;
941 state->m_has_atv = true;
942 state->m_has_audio = true;
943 state->m_has_dvbt = true;
944 state->m_has_dvbc = true;
945 state->m_has_sawsw = true;
946 state->m_has_gpio2 = true;
947 state->m_has_gpio1 = true;
948 state->m_has_irqn = false;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300949 break;
950 case 0x25:
951 /* typeId = DRX3925K_TYPE_ID */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300952 state->m_has_lna = false;
953 state->m_has_oob = false;
954 state->m_has_atv = true;
955 state->m_has_audio = true;
956 state->m_has_dvbt = true;
957 state->m_has_dvbc = true;
958 state->m_has_sawsw = true;
959 state->m_has_gpio2 = true;
960 state->m_has_gpio1 = true;
961 state->m_has_irqn = false;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300962 break;
963 case 0x26:
964 /* typeId = DRX3926K_TYPE_ID */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300965 state->m_has_lna = false;
966 state->m_has_oob = false;
967 state->m_has_atv = true;
968 state->m_has_audio = false;
969 state->m_has_dvbt = true;
970 state->m_has_dvbc = true;
971 state->m_has_sawsw = true;
972 state->m_has_gpio2 = true;
973 state->m_has_gpio1 = true;
974 state->m_has_irqn = false;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300975 break;
976 default:
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -0300977 pr_err("DeviceID 0x%02x not supported\n",
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300978 ((sio_top_jtagid_lo >> 12) & 0xFF));
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300979 status = -EINVAL;
980 goto error2;
981 }
982
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -0300983 pr_info("detected a drx-39%02xk, spin %s, xtal %d.%03d MHz\n",
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300984 ((sio_top_jtagid_lo >> 12) & 0xFF), spin,
985 state->m_osc_clock_freq / 1000,
986 state->m_osc_clock_freq % 1000);
Mauro Carvalho Chehab9c6e1822011-07-10 08:38:18 -0300987
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300988error:
989 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -0300990 pr_err("Error %d on %s\n", status, __func__);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -0300991
992error2:
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300993 return status;
994}
995
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -0300996static int hi_command(struct drxk_state *state, u16 cmd, u16 *p_result)
Ralph Metzler43dd07f2011-07-03 13:42:18 -0300997{
998 int status;
999 bool powerdown_cmd;
1000
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03001001 dprintk(1, "\n");
1002
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001003 /* Write command */
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -03001004 status = write16(state, SIO_HI_RA_RAM_CMD__A, cmd);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001005 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001006 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001007 if (cmd == SIO_HI_RA_RAM_CMD_RESET)
Mauro Carvalho Chehabb72852b2013-04-28 11:47:47 -03001008 usleep_range(1000, 2000);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001009
1010 powerdown_cmd =
Oliver Endrissebc7de22011-07-03 13:49:44 -03001011 (bool) ((cmd == SIO_HI_RA_RAM_CMD_CONFIG) &&
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001012 ((state->m_hi_cfg_ctrl) &
Oliver Endrissebc7de22011-07-03 13:49:44 -03001013 SIO_HI_RA_RAM_PAR_5_CFG_SLEEP__M) ==
1014 SIO_HI_RA_RAM_PAR_5_CFG_SLEEP_ZZZ);
Mauro Carvalho Chehab5a7f7b72014-09-03 15:23:57 -03001015 if (!powerdown_cmd) {
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001016 /* Wait until command rdy */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001017 u32 retry_count = 0;
1018 u16 wait_cmd;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001019
1020 do {
Mauro Carvalho Chehabb72852b2013-04-28 11:47:47 -03001021 usleep_range(1000, 2000);
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001022 retry_count += 1;
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -03001023 status = read16(state, SIO_HI_RA_RAM_CMD__A,
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001024 &wait_cmd);
1025 } while ((status < 0) && (retry_count < DRXK_MAX_RETRIES)
1026 && (wait_cmd != 0));
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001027 if (status < 0)
1028 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001029 status = read16(state, SIO_HI_RA_RAM_RES__A, p_result);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001030 }
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001031error:
1032 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03001033 pr_err("Error %d on %s\n", status, __func__);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001034
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001035 return status;
1036}
1037
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001038static int hi_cfg_command(struct drxk_state *state)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001039{
1040 int status;
1041
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03001042 dprintk(1, "\n");
1043
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001044 mutex_lock(&state->mutex);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001045
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03001046 status = write16(state, SIO_HI_RA_RAM_PAR_6__A,
1047 state->m_hi_cfg_timeout);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001048 if (status < 0)
1049 goto error;
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03001050 status = write16(state, SIO_HI_RA_RAM_PAR_5__A,
1051 state->m_hi_cfg_ctrl);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001052 if (status < 0)
1053 goto error;
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03001054 status = write16(state, SIO_HI_RA_RAM_PAR_4__A,
1055 state->m_hi_cfg_wake_up_key);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001056 if (status < 0)
1057 goto error;
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03001058 status = write16(state, SIO_HI_RA_RAM_PAR_3__A,
1059 state->m_hi_cfg_bridge_delay);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001060 if (status < 0)
1061 goto error;
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03001062 status = write16(state, SIO_HI_RA_RAM_PAR_2__A,
1063 state->m_hi_cfg_timing_div);
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_1__A,
1067 SIO_HI_RA_RAM_PAR_1_PAR1_SEC_KEY);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001068 if (status < 0)
1069 goto error;
Hans Verkuilb1cf2012013-10-04 11:01:45 -03001070 status = hi_command(state, SIO_HI_RA_RAM_CMD_CONFIG, NULL);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001071 if (status < 0)
1072 goto error;
1073
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001074 state->m_hi_cfg_ctrl &= ~SIO_HI_RA_RAM_PAR_5_CFG_SLEEP_ZZZ;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001075error:
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001076 mutex_unlock(&state->mutex);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001077 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03001078 pr_err("Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001079 return status;
1080}
1081
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001082static int init_hi(struct drxk_state *state)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001083{
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03001084 dprintk(1, "\n");
1085
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001086 state->m_hi_cfg_wake_up_key = (state->demod_address << 1);
1087 state->m_hi_cfg_timeout = 0x96FF;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001088 /* port/bridge/power down ctrl */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001089 state->m_hi_cfg_ctrl = SIO_HI_RA_RAM_PAR_5_CFG_SLV0_SLAVE;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001090
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001091 return hi_cfg_command(state);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001092}
1093
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001094static int mpegts_configure_pins(struct drxk_state *state, bool mpeg_enable)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001095{
1096 int status = -1;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001097 u16 sio_pdr_mclk_cfg = 0;
1098 u16 sio_pdr_mdx_cfg = 0;
Mauro Carvalho Chehabd5856812012-01-21 07:57:06 -03001099 u16 err_cfg = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001100
Mauro Carvalho Chehab534e0482011-07-24 14:59:20 -03001101 dprintk(1, ": mpeg %s, %s mode\n",
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001102 mpeg_enable ? "enable" : "disable",
1103 state->m_enable_parallel ? "parallel" : "serial");
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001104
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001105 /* stop lock indicator process */
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03001106 status = write16(state, SCU_RAM_GPIO__A,
1107 SCU_RAM_GPIO_HW_LOCK_IND_DISABLE);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001108 if (status < 0)
1109 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001110
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001111 /* MPEG TS pad configuration */
Martin Blumenstingl84183662012-10-04 14:22:55 -03001112 status = write16(state, SIO_TOP_COMM_KEY__A, SIO_TOP_COMM_KEY_KEY);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001113 if (status < 0)
1114 goto error;
1115
Mauro Carvalho Chehab5a7f7b72014-09-03 15:23:57 -03001116 if (!mpeg_enable) {
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001117 /* Set MPEG TS pads to inputmode */
1118 status = write16(state, SIO_PDR_MSTRT_CFG__A, 0x0000);
1119 if (status < 0)
1120 goto error;
1121 status = write16(state, SIO_PDR_MERR_CFG__A, 0x0000);
1122 if (status < 0)
1123 goto error;
1124 status = write16(state, SIO_PDR_MCLK_CFG__A, 0x0000);
1125 if (status < 0)
1126 goto error;
1127 status = write16(state, SIO_PDR_MVAL_CFG__A, 0x0000);
1128 if (status < 0)
1129 goto error;
1130 status = write16(state, SIO_PDR_MD0_CFG__A, 0x0000);
1131 if (status < 0)
1132 goto error;
1133 status = write16(state, SIO_PDR_MD1_CFG__A, 0x0000);
1134 if (status < 0)
1135 goto error;
1136 status = write16(state, SIO_PDR_MD2_CFG__A, 0x0000);
1137 if (status < 0)
1138 goto error;
1139 status = write16(state, SIO_PDR_MD3_CFG__A, 0x0000);
1140 if (status < 0)
1141 goto error;
1142 status = write16(state, SIO_PDR_MD4_CFG__A, 0x0000);
1143 if (status < 0)
1144 goto error;
1145 status = write16(state, SIO_PDR_MD5_CFG__A, 0x0000);
1146 if (status < 0)
1147 goto error;
1148 status = write16(state, SIO_PDR_MD6_CFG__A, 0x0000);
1149 if (status < 0)
1150 goto error;
1151 status = write16(state, SIO_PDR_MD7_CFG__A, 0x0000);
1152 if (status < 0)
1153 goto error;
1154 } else {
1155 /* Enable MPEG output */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001156 sio_pdr_mdx_cfg =
1157 ((state->m_ts_data_strength <<
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001158 SIO_PDR_MD0_CFG_DRIVE__B) | 0x0003);
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001159 sio_pdr_mclk_cfg = ((state->m_ts_clockk_strength <<
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001160 SIO_PDR_MCLK_CFG_DRIVE__B) |
1161 0x0003);
1162
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001163 status = write16(state, SIO_PDR_MSTRT_CFG__A, sio_pdr_mdx_cfg);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001164 if (status < 0)
1165 goto error;
Mauro Carvalho Chehabd5856812012-01-21 07:57:06 -03001166
1167 if (state->enable_merr_cfg)
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001168 err_cfg = sio_pdr_mdx_cfg;
Mauro Carvalho Chehabd5856812012-01-21 07:57:06 -03001169
1170 status = write16(state, SIO_PDR_MERR_CFG__A, err_cfg);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001171 if (status < 0)
1172 goto error;
Mauro Carvalho Chehabd5856812012-01-21 07:57:06 -03001173 status = write16(state, SIO_PDR_MVAL_CFG__A, err_cfg);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001174 if (status < 0)
1175 goto error;
Mauro Carvalho Chehabd5856812012-01-21 07:57:06 -03001176
Mauro Carvalho Chehab5a7f7b72014-09-03 15:23:57 -03001177 if (state->m_enable_parallel) {
Jonathan McCrohan39c1cb22013-10-20 21:34:01 -03001178 /* parallel -> enable MD1 to MD7 */
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03001179 status = write16(state, SIO_PDR_MD1_CFG__A,
1180 sio_pdr_mdx_cfg);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03001181 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001182 goto error;
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03001183 status = write16(state, SIO_PDR_MD2_CFG__A,
1184 sio_pdr_mdx_cfg);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03001185 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001186 goto error;
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03001187 status = write16(state, SIO_PDR_MD3_CFG__A,
1188 sio_pdr_mdx_cfg);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03001189 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001190 goto error;
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03001191 status = write16(state, SIO_PDR_MD4_CFG__A,
1192 sio_pdr_mdx_cfg);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03001193 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001194 goto error;
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03001195 status = write16(state, SIO_PDR_MD5_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_MD6_CFG__A,
1200 sio_pdr_mdx_cfg);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001201 if (status < 0)
1202 goto error;
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03001203 status = write16(state, SIO_PDR_MD7_CFG__A,
1204 sio_pdr_mdx_cfg);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001205 if (status < 0)
1206 goto error;
1207 } else {
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001208 sio_pdr_mdx_cfg = ((state->m_ts_data_strength <<
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001209 SIO_PDR_MD0_CFG_DRIVE__B)
1210 | 0x0003);
1211 /* serial -> disable MD1 to MD7 */
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -03001212 status = write16(state, SIO_PDR_MD1_CFG__A, 0x0000);
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 Chehab5e66b872011-07-09 09:50:21 -03001215 status = write16(state, SIO_PDR_MD2_CFG__A, 0x0000);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03001216 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001217 goto error;
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -03001218 status = write16(state, SIO_PDR_MD3_CFG__A, 0x0000);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03001219 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001220 goto error;
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -03001221 status = write16(state, SIO_PDR_MD4_CFG__A, 0x0000);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03001222 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001223 goto error;
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -03001224 status = write16(state, SIO_PDR_MD5_CFG__A, 0x0000);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03001225 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001226 goto error;
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -03001227 status = write16(state, SIO_PDR_MD6_CFG__A, 0x0000);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03001228 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001229 goto error;
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -03001230 status = write16(state, SIO_PDR_MD7_CFG__A, 0x0000);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03001231 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001232 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001233 }
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001234 status = write16(state, SIO_PDR_MCLK_CFG__A, sio_pdr_mclk_cfg);
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 Chehabcd7a67a2013-04-28 11:47:44 -03001237 status = write16(state, SIO_PDR_MD0_CFG__A, sio_pdr_mdx_cfg);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03001238 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001239 goto error;
1240 }
1241 /* Enable MB output over MPEG pads and ctl input */
1242 status = write16(state, SIO_PDR_MON_CFG__A, 0x0000);
1243 if (status < 0)
1244 goto error;
1245 /* Write nomagic word to enable pdr reg write */
1246 status = write16(state, SIO_TOP_COMM_KEY__A, 0x0000);
1247error:
1248 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03001249 pr_err("Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001250 return status;
1251}
1252
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001253static int mpegts_disable(struct drxk_state *state)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001254{
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03001255 dprintk(1, "\n");
1256
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001257 return mpegts_configure_pins(state, false);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001258}
1259
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001260static int bl_chain_cmd(struct drxk_state *state,
1261 u16 rom_offset, u16 nr_of_elements, u32 time_out)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001262{
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001263 u16 bl_status = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001264 int status;
1265 unsigned long end;
1266
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03001267 dprintk(1, "\n");
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001268 mutex_lock(&state->mutex);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001269 status = write16(state, SIO_BL_MODE__A, SIO_BL_MODE_CHAIN);
1270 if (status < 0)
1271 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001272 status = write16(state, SIO_BL_CHAIN_ADDR__A, rom_offset);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001273 if (status < 0)
1274 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001275 status = write16(state, SIO_BL_CHAIN_LEN__A, nr_of_elements);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001276 if (status < 0)
1277 goto error;
1278 status = write16(state, SIO_BL_ENABLE__A, SIO_BL_ENABLE_ON);
1279 if (status < 0)
1280 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001281
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001282 end = jiffies + msecs_to_jiffies(time_out);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001283 do {
Mauro Carvalho Chehabb72852b2013-04-28 11:47:47 -03001284 usleep_range(1000, 2000);
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001285 status = read16(state, SIO_BL_STATUS__A, &bl_status);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001286 if (status < 0)
1287 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001288 } while ((bl_status == 0x1) &&
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001289 ((time_is_after_jiffies(end))));
1290
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001291 if (bl_status == 0x1) {
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03001292 pr_err("SIO not ready\n");
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001293 status = -EINVAL;
1294 goto error2;
1295 }
1296error:
1297 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03001298 pr_err("Error %d on %s\n", status, __func__);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001299error2:
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001300 mutex_unlock(&state->mutex);
1301 return status;
1302}
1303
1304
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001305static int download_microcode(struct drxk_state *state,
1306 const u8 p_mc_image[], u32 length)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001307{
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001308 const u8 *p_src = p_mc_image;
1309 u32 address;
1310 u16 n_blocks;
1311 u16 block_size;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001312 u32 offset = 0;
1313 u32 i;
Mauro Carvalho Chehab1bd09dd2011-07-03 18:21:59 -03001314 int status = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001315
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03001316 dprintk(1, "\n");
1317
Hans Verkuil5becbc52012-05-14 10:22:58 -03001318 /* down the drain (we don't care about MAGIC_WORD) */
1319#if 0
1320 /* For future reference */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001321 drain = (p_src[0] << 8) | p_src[1];
Hans Verkuil5becbc52012-05-14 10:22:58 -03001322#endif
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001323 p_src += sizeof(u16);
Oliver Endrissebc7de22011-07-03 13:49:44 -03001324 offset += sizeof(u16);
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001325 n_blocks = (p_src[0] << 8) | p_src[1];
1326 p_src += sizeof(u16);
Oliver Endrissebc7de22011-07-03 13:49:44 -03001327 offset += sizeof(u16);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001328
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001329 for (i = 0; i < n_blocks; i += 1) {
1330 address = (p_src[0] << 24) | (p_src[1] << 16) |
1331 (p_src[2] << 8) | p_src[3];
1332 p_src += sizeof(u32);
Oliver Endrissebc7de22011-07-03 13:49:44 -03001333 offset += sizeof(u32);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001334
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001335 block_size = ((p_src[0] << 8) | p_src[1]) * sizeof(u16);
1336 p_src += sizeof(u16);
Oliver Endrissebc7de22011-07-03 13:49:44 -03001337 offset += sizeof(u16);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001338
Hans Verkuil5becbc52012-05-14 10:22:58 -03001339#if 0
1340 /* For future reference */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001341 flags = (p_src[0] << 8) | p_src[1];
Hans Verkuil5becbc52012-05-14 10:22:58 -03001342#endif
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001343 p_src += sizeof(u16);
Oliver Endrissebc7de22011-07-03 13:49:44 -03001344 offset += sizeof(u16);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001345
Hans Verkuil5becbc52012-05-14 10:22:58 -03001346#if 0
1347 /* For future reference */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001348 block_crc = (p_src[0] << 8) | p_src[1];
Hans Verkuil5becbc52012-05-14 10:22:58 -03001349#endif
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001350 p_src += sizeof(u16);
Oliver Endrissebc7de22011-07-03 13:49:44 -03001351 offset += sizeof(u16);
Mauro Carvalho Chehabbcd2ebb2011-07-09 18:57:54 -03001352
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001353 if (offset + block_size > length) {
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03001354 pr_err("Firmware is corrupted.\n");
Mauro Carvalho Chehabbcd2ebb2011-07-09 18:57:54 -03001355 return -EINVAL;
1356 }
1357
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001358 status = write_block(state, address, block_size, p_src);
Mauro Carvalho Chehab39624f72011-07-09 19:23:44 -03001359 if (status < 0) {
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03001360 pr_err("Error %d while loading firmware\n", status);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001361 break;
Mauro Carvalho Chehab39624f72011-07-09 19:23:44 -03001362 }
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001363 p_src += block_size;
1364 offset += block_size;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001365 }
1366 return status;
1367}
1368
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001369static int dvbt_enable_ofdm_token_ring(struct drxk_state *state, bool enable)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001370{
1371 int status;
Oliver Endrissebc7de22011-07-03 13:49:44 -03001372 u16 data = 0;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001373 u16 desired_ctrl = SIO_OFDM_SH_OFDM_RING_ENABLE_ON;
1374 u16 desired_status = SIO_OFDM_SH_OFDM_RING_STATUS_ENABLED;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001375 unsigned long end;
1376
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03001377 dprintk(1, "\n");
1378
Mauro Carvalho Chehab5a7f7b72014-09-03 15:23:57 -03001379 if (!enable) {
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001380 desired_ctrl = SIO_OFDM_SH_OFDM_RING_ENABLE_OFF;
1381 desired_status = SIO_OFDM_SH_OFDM_RING_STATUS_DOWN;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001382 }
1383
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001384 status = read16(state, SIO_OFDM_SH_OFDM_RING_STATUS__A, &data);
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001385 if (status >= 0 && data == desired_status) {
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001386 /* tokenring already has correct status */
1387 return status;
1388 }
1389 /* Disable/enable dvbt tokenring bridge */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001390 status = write16(state, SIO_OFDM_SH_OFDM_RING_ENABLE__A, desired_ctrl);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001391
Oliver Endrissebc7de22011-07-03 13:49:44 -03001392 end = jiffies + msecs_to_jiffies(DRXK_OFDM_TR_SHUTDOWN_TIMEOUT);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03001393 do {
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -03001394 status = read16(state, SIO_OFDM_SH_OFDM_RING_STATUS__A, &data);
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03001395 if ((status >= 0 && data == desired_status)
1396 || time_is_after_jiffies(end))
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03001397 break;
Mauro Carvalho Chehabb72852b2013-04-28 11:47:47 -03001398 usleep_range(1000, 2000);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001399 } while (1);
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001400 if (data != desired_status) {
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03001401 pr_err("SIO not ready\n");
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001402 return -EINVAL;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001403 }
1404 return status;
1405}
1406
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001407static int mpegts_stop(struct drxk_state *state)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001408{
1409 int status = 0;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001410 u16 fec_oc_snc_mode = 0;
1411 u16 fec_oc_ipr_mode = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001412
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03001413 dprintk(1, "\n");
1414
Jonathan McCrohan39c1cb22013-10-20 21:34:01 -03001415 /* Graceful shutdown (byte boundaries) */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001416 status = read16(state, FEC_OC_SNC_MODE__A, &fec_oc_snc_mode);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001417 if (status < 0)
1418 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001419 fec_oc_snc_mode |= FEC_OC_SNC_MODE_SHUTDOWN__M;
1420 status = write16(state, FEC_OC_SNC_MODE__A, fec_oc_snc_mode);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001421 if (status < 0)
1422 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001423
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001424 /* Suppress MCLK during absence of data */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001425 status = read16(state, FEC_OC_IPR_MODE__A, &fec_oc_ipr_mode);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001426 if (status < 0)
1427 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001428 fec_oc_ipr_mode |= FEC_OC_IPR_MODE_MCLK_DIS_DAT_ABS__M;
1429 status = write16(state, FEC_OC_IPR_MODE__A, fec_oc_ipr_mode);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001430
1431error:
1432 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03001433 pr_err("Error %d on %s\n", status, __func__);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001434
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001435 return status;
1436}
1437
1438static int scu_command(struct drxk_state *state,
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001439 u16 cmd, u8 parameter_len,
1440 u16 *parameter, u8 result_len, u16 *result)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001441{
1442#if (SCU_RAM_PARAM_0__A - SCU_RAM_PARAM_15__A) != 15
1443#error DRXK register mapping no longer compatible with this routine!
1444#endif
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001445 u16 cur_cmd = 0;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001446 int status = -EINVAL;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001447 unsigned long end;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001448 u8 buffer[34];
1449 int cnt = 0, ii;
Mauro Carvalho Chehab75589772011-07-10 13:25:48 -03001450 const char *p;
1451 char errname[30];
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001452
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03001453 dprintk(1, "\n");
1454
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001455 if ((cmd == 0) || ((parameter_len > 0) && (parameter == NULL)) ||
1456 ((result_len > 0) && (result == NULL))) {
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03001457 pr_err("Error %d on %s\n", status, __func__);
Alexey Khoroshilove4459e12012-04-05 18:53:20 -03001458 return status;
1459 }
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001460
1461 mutex_lock(&state->mutex);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001462
1463 /* assume that the command register is ready
1464 since it is checked afterwards */
Mauro Carvalho Chehabdaad52c2018-12-07 08:07:55 -05001465 if (parameter) {
1466 for (ii = parameter_len - 1; ii >= 0; ii -= 1) {
1467 buffer[cnt++] = (parameter[ii] & 0xFF);
1468 buffer[cnt++] = ((parameter[ii] >> 8) & 0xFF);
1469 }
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001470 }
1471 buffer[cnt++] = (cmd & 0xFF);
1472 buffer[cnt++] = ((cmd >> 8) & 0xFF);
1473
1474 write_block(state, SCU_RAM_PARAM_0__A -
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001475 (parameter_len - 1), cnt, buffer);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001476 /* Wait until SCU has processed command */
1477 end = jiffies + msecs_to_jiffies(DRXK_MAX_WAITTIME);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001478 do {
Mauro Carvalho Chehabb72852b2013-04-28 11:47:47 -03001479 usleep_range(1000, 2000);
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001480 status = read16(state, SCU_RAM_COMMAND__A, &cur_cmd);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001481 if (status < 0)
1482 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001483 } while (!(cur_cmd == DRX_SCU_READY) && (time_is_after_jiffies(end)));
1484 if (cur_cmd != DRX_SCU_READY) {
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03001485 pr_err("SCU not ready\n");
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001486 status = -EIO;
1487 goto error2;
1488 }
1489 /* read results */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001490 if ((result_len > 0) && (result != NULL)) {
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001491 s16 err;
1492 int ii;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001493
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001494 for (ii = result_len - 1; ii >= 0; ii -= 1) {
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03001495 status = read16(state, SCU_RAM_PARAM_0__A - ii,
1496 &result[ii]);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03001497 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001498 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001499 }
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001500
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001501 /* Check if an error was reported by SCU */
1502 err = (s16)result[0];
Mauro Carvalho Chehab75589772011-07-10 13:25:48 -03001503 if (err >= 0)
1504 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001505
Mauro Carvalho Chehab75589772011-07-10 13:25:48 -03001506 /* check for the known error codes */
1507 switch (err) {
1508 case SCU_RESULT_UNKCMD:
1509 p = "SCU_RESULT_UNKCMD";
1510 break;
1511 case SCU_RESULT_UNKSTD:
1512 p = "SCU_RESULT_UNKSTD";
1513 break;
1514 case SCU_RESULT_SIZE:
1515 p = "SCU_RESULT_SIZE";
1516 break;
1517 case SCU_RESULT_INVPAR:
1518 p = "SCU_RESULT_INVPAR";
1519 break;
1520 default: /* Other negative values are errors */
1521 sprintf(errname, "ERROR: %d\n", err);
1522 p = errname;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001523 }
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03001524 pr_err("%s while sending cmd 0x%04x with params:", p, cmd);
Mauro Carvalho Chehab75589772011-07-10 13:25:48 -03001525 print_hex_dump_bytes("drxk: ", DUMP_PREFIX_NONE, buffer, cnt);
1526 status = -EINVAL;
1527 goto error2;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001528 }
1529
1530error:
Oliver Endrissebc7de22011-07-03 13:49:44 -03001531 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03001532 pr_err("Error %d on %s\n", status, __func__);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001533error2:
1534 mutex_unlock(&state->mutex);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001535 return status;
1536}
1537
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001538static int set_iqm_af(struct drxk_state *state, bool active)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001539{
1540 u16 data = 0;
1541 int status;
1542
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03001543 dprintk(1, "\n");
1544
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001545 /* Configure IQM */
1546 status = read16(state, IQM_AF_STDBY__A, &data);
1547 if (status < 0)
1548 goto error;
Oliver Endrissebc7de22011-07-03 13:49:44 -03001549
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001550 if (!active) {
1551 data |= (IQM_AF_STDBY_STDBY_ADC_STANDBY
1552 | IQM_AF_STDBY_STDBY_AMP_STANDBY
1553 | IQM_AF_STDBY_STDBY_PD_STANDBY
1554 | IQM_AF_STDBY_STDBY_TAGC_IF_STANDBY
1555 | IQM_AF_STDBY_STDBY_TAGC_RF_STANDBY);
1556 } else {
1557 data &= ((~IQM_AF_STDBY_STDBY_ADC_STANDBY)
1558 & (~IQM_AF_STDBY_STDBY_AMP_STANDBY)
1559 & (~IQM_AF_STDBY_STDBY_PD_STANDBY)
1560 & (~IQM_AF_STDBY_STDBY_TAGC_IF_STANDBY)
1561 & (~IQM_AF_STDBY_STDBY_TAGC_RF_STANDBY)
1562 );
1563 }
1564 status = write16(state, IQM_AF_STDBY__A, data);
1565
1566error:
1567 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03001568 pr_err("Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001569 return status;
1570}
1571
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001572static int ctrl_power_mode(struct drxk_state *state, enum drx_power_mode *mode)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001573{
1574 int status = 0;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001575 u16 sio_cc_pwd_mode = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001576
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03001577 dprintk(1, "\n");
1578
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001579 /* Check arguments */
1580 if (mode == NULL)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001581 return -EINVAL;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001582
1583 switch (*mode) {
1584 case DRX_POWER_UP:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001585 sio_cc_pwd_mode = SIO_CC_PWD_MODE_LEVEL_NONE;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001586 break;
1587 case DRXK_POWER_DOWN_OFDM:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001588 sio_cc_pwd_mode = SIO_CC_PWD_MODE_LEVEL_OFDM;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001589 break;
1590 case DRXK_POWER_DOWN_CORE:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001591 sio_cc_pwd_mode = SIO_CC_PWD_MODE_LEVEL_CLOCK;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001592 break;
1593 case DRXK_POWER_DOWN_PLL:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001594 sio_cc_pwd_mode = SIO_CC_PWD_MODE_LEVEL_PLL;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001595 break;
1596 case DRX_POWER_DOWN:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001597 sio_cc_pwd_mode = SIO_CC_PWD_MODE_LEVEL_OSC;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001598 break;
1599 default:
1600 /* Unknow sleep mode */
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001601 return -EINVAL;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001602 }
1603
1604 /* If already in requested power mode, do nothing */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001605 if (state->m_current_power_mode == *mode)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001606 return 0;
1607
1608 /* For next steps make sure to start from DRX_POWER_UP mode */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001609 if (state->m_current_power_mode != DRX_POWER_UP) {
1610 status = power_up_device(state);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001611 if (status < 0)
1612 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001613 status = dvbt_enable_ofdm_token_ring(state, true);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001614 if (status < 0)
1615 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001616 }
1617
1618 if (*mode == DRX_POWER_UP) {
Masahiro Yamada2c149602017-02-27 14:29:31 -08001619 /* Restore analog & pin configuration */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001620 } else {
1621 /* Power down to requested mode */
1622 /* Backup some register settings */
1623 /* Set pins with possible pull-ups connected
1624 to them in input mode */
1625 /* Analog power down */
1626 /* ADC power down */
1627 /* Power down device */
1628 /* stop all comm_exec */
1629 /* Stop and power down previous standard */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001630 switch (state->m_operation_mode) {
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001631 case OM_DVBT:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001632 status = mpegts_stop(state);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03001633 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001634 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001635 status = power_down_dvbt(state, false);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03001636 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001637 goto error;
1638 break;
1639 case OM_QAM_ITU_A:
1640 case OM_QAM_ITU_C:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001641 status = mpegts_stop(state);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03001642 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001643 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001644 status = power_down_qam(state);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001645 if (status < 0)
1646 goto error;
1647 break;
1648 default:
1649 break;
1650 }
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001651 status = dvbt_enable_ofdm_token_ring(state, false);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001652 if (status < 0)
1653 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001654 status = write16(state, SIO_CC_PWD_MODE__A, sio_cc_pwd_mode);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001655 if (status < 0)
1656 goto error;
1657 status = write16(state, SIO_CC_UPDATE__A, SIO_CC_UPDATE_KEY);
1658 if (status < 0)
1659 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001660
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001661 if (*mode != DRXK_POWER_DOWN_OFDM) {
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001662 state->m_hi_cfg_ctrl |=
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001663 SIO_HI_RA_RAM_PAR_5_CFG_SLEEP_ZZZ;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001664 status = hi_cfg_command(state);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001665 if (status < 0)
1666 goto error;
1667 }
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001668 }
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001669 state->m_current_power_mode = *mode;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001670
1671error:
1672 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03001673 pr_err("Error %d on %s\n", status, __func__);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001674
Oliver Endrissebc7de22011-07-03 13:49:44 -03001675 return status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001676}
1677
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001678static int power_down_dvbt(struct drxk_state *state, bool set_power_mode)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001679{
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001680 enum drx_power_mode power_mode = DRXK_POWER_DOWN_OFDM;
1681 u16 cmd_result = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001682 u16 data = 0;
1683 int status;
1684
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03001685 dprintk(1, "\n");
1686
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001687 status = read16(state, SCU_COMM_EXEC__A, &data);
1688 if (status < 0)
1689 goto error;
1690 if (data == SCU_COMM_EXEC_ACTIVE) {
1691 /* Send OFDM stop command */
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03001692 status = scu_command(state,
1693 SCU_RAM_COMMAND_STANDARD_OFDM
1694 | SCU_RAM_COMMAND_CMD_DEMOD_STOP,
1695 0, NULL, 1, &cmd_result);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03001696 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001697 goto error;
1698 /* Send OFDM reset command */
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03001699 status = scu_command(state,
1700 SCU_RAM_COMMAND_STANDARD_OFDM
1701 | SCU_RAM_COMMAND_CMD_DEMOD_RESET,
1702 0, NULL, 1, &cmd_result);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001703 if (status < 0)
1704 goto error;
1705 }
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001706
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001707 /* Reset datapath for OFDM, processors first */
1708 status = write16(state, OFDM_SC_COMM_EXEC__A, OFDM_SC_COMM_EXEC_STOP);
1709 if (status < 0)
1710 goto error;
1711 status = write16(state, OFDM_LC_COMM_EXEC__A, OFDM_LC_COMM_EXEC_STOP);
1712 if (status < 0)
1713 goto error;
1714 status = write16(state, IQM_COMM_EXEC__A, IQM_COMM_EXEC_B_STOP);
1715 if (status < 0)
1716 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001717
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001718 /* powerdown AFE */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001719 status = set_iqm_af(state, false);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001720 if (status < 0)
1721 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001722
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001723 /* powerdown to OFDM mode */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001724 if (set_power_mode) {
1725 status = ctrl_power_mode(state, &power_mode);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001726 if (status < 0)
1727 goto error;
1728 }
1729error:
1730 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03001731 pr_err("Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001732 return status;
1733}
1734
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001735static int setoperation_mode(struct drxk_state *state,
1736 enum operation_mode o_mode)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001737{
1738 int status = 0;
1739
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03001740 dprintk(1, "\n");
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001741 /*
Oliver Endrissebc7de22011-07-03 13:49:44 -03001742 Stop and power down previous standard
1743 TODO investigate total power down instead of partial
1744 power down depending on "previous" standard.
1745 */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001746
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001747 /* disable HW lock indicator */
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03001748 status = write16(state, SCU_RAM_GPIO__A,
1749 SCU_RAM_GPIO_HW_LOCK_IND_DISABLE);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001750 if (status < 0)
1751 goto error;
1752
Mauro Carvalho Chehabf1b82972011-07-10 13:08:44 -03001753 /* Device is already at the required mode */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001754 if (state->m_operation_mode == o_mode)
Mauro Carvalho Chehabf1b82972011-07-10 13:08:44 -03001755 return 0;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001756
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001757 switch (state->m_operation_mode) {
Mauro Carvalho Chehabf1b82972011-07-10 13:08:44 -03001758 /* OM_NONE was added for start up */
1759 case OM_NONE:
1760 break;
1761 case OM_DVBT:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001762 status = mpegts_stop(state);
Mauro Carvalho Chehabf1b82972011-07-10 13:08:44 -03001763 if (status < 0)
1764 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001765 status = power_down_dvbt(state, true);
Mauro Carvalho Chehabf1b82972011-07-10 13:08:44 -03001766 if (status < 0)
1767 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001768 state->m_operation_mode = OM_NONE;
Mauro Carvalho Chehabf1b82972011-07-10 13:08:44 -03001769 break;
1770 case OM_QAM_ITU_A: /* fallthrough */
1771 case OM_QAM_ITU_C:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001772 status = mpegts_stop(state);
Mauro Carvalho Chehabf1b82972011-07-10 13:08:44 -03001773 if (status < 0)
1774 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001775 status = power_down_qam(state);
Mauro Carvalho Chehabf1b82972011-07-10 13:08:44 -03001776 if (status < 0)
1777 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001778 state->m_operation_mode = OM_NONE;
Mauro Carvalho Chehabf1b82972011-07-10 13:08:44 -03001779 break;
1780 case OM_QAM_ITU_B:
1781 default:
1782 status = -EINVAL;
1783 goto error;
1784 }
1785
1786 /*
1787 Power up new standard
1788 */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001789 switch (o_mode) {
Mauro Carvalho Chehabf1b82972011-07-10 13:08:44 -03001790 case OM_DVBT:
Mauro Carvalho Chehab48763e22011-12-09 08:53:36 -02001791 dprintk(1, ": DVB-T\n");
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001792 state->m_operation_mode = o_mode;
1793 status = set_dvbt_standard(state, o_mode);
Mauro Carvalho Chehabf1b82972011-07-10 13:08:44 -03001794 if (status < 0)
1795 goto error;
1796 break;
1797 case OM_QAM_ITU_A: /* fallthrough */
1798 case OM_QAM_ITU_C:
Mauro Carvalho Chehab48763e22011-12-09 08:53:36 -02001799 dprintk(1, ": DVB-C Annex %c\n",
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001800 (state->m_operation_mode == OM_QAM_ITU_A) ? 'A' : 'C');
1801 state->m_operation_mode = o_mode;
1802 status = set_qam_standard(state, o_mode);
Mauro Carvalho Chehabf1b82972011-07-10 13:08:44 -03001803 if (status < 0)
1804 goto error;
1805 break;
1806 case OM_QAM_ITU_B:
1807 default:
1808 status = -EINVAL;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001809 }
1810error:
1811 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03001812 pr_err("Error %d on %s\n", status, __func__);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001813 return status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001814}
1815
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001816static int start(struct drxk_state *state, s32 offset_freq,
1817 s32 intermediate_frequency)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001818{
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001819 int status = -EINVAL;
1820
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001821 u16 i_freqk_hz;
1822 s32 offsetk_hz = offset_freq / 1000;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001823
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03001824 dprintk(1, "\n");
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001825 if (state->m_drxk_state != DRXK_STOPPED &&
1826 state->m_drxk_state != DRXK_DTV_STARTED)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001827 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001828
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001829 state->m_b_mirror_freq_spect = (state->props.inversion == INVERSION_ON);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001830
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001831 if (intermediate_frequency < 0) {
1832 state->m_b_mirror_freq_spect = !state->m_b_mirror_freq_spect;
1833 intermediate_frequency = -intermediate_frequency;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001834 }
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001835
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001836 switch (state->m_operation_mode) {
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001837 case OM_QAM_ITU_A:
1838 case OM_QAM_ITU_C:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001839 i_freqk_hz = (intermediate_frequency / 1000);
1840 status = set_qam(state, i_freqk_hz, offsetk_hz);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001841 if (status < 0)
1842 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001843 state->m_drxk_state = DRXK_DTV_STARTED;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001844 break;
1845 case OM_DVBT:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001846 i_freqk_hz = (intermediate_frequency / 1000);
1847 status = mpegts_stop(state);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001848 if (status < 0)
1849 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001850 status = set_dvbt(state, i_freqk_hz, offsetk_hz);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001851 if (status < 0)
1852 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001853 status = dvbt_start(state);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001854 if (status < 0)
1855 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001856 state->m_drxk_state = DRXK_DTV_STARTED;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001857 break;
1858 default:
1859 break;
1860 }
1861error:
1862 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03001863 pr_err("Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001864 return status;
1865}
1866
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001867static int shut_down(struct drxk_state *state)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001868{
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03001869 dprintk(1, "\n");
1870
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001871 mpegts_stop(state);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001872 return 0;
1873}
1874
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001875static int get_lock_status(struct drxk_state *state, u32 *p_lock_status)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001876{
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001877 int status = -EINVAL;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001878
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03001879 dprintk(1, "\n");
1880
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001881 if (p_lock_status == NULL)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001882 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001883
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001884 *p_lock_status = NOT_LOCKED;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001885
1886 /* define the SCU command code */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001887 switch (state->m_operation_mode) {
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001888 case OM_QAM_ITU_A:
1889 case OM_QAM_ITU_B:
1890 case OM_QAM_ITU_C:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001891 status = get_qam_lock_status(state, p_lock_status);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001892 break;
1893 case OM_DVBT:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001894 status = get_dvbt_lock_status(state, p_lock_status);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001895 break;
1896 default:
Daniel Schellerb73bb2a2017-03-14 19:22:37 -03001897 pr_debug("Unsupported operation mode %d in %s\n",
1898 state->m_operation_mode, __func__);
1899 return 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001900 }
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001901error:
1902 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03001903 pr_err("Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001904 return status;
1905}
1906
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001907static int mpegts_start(struct drxk_state *state)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001908{
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001909 int status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001910
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001911 u16 fec_oc_snc_mode = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001912
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001913 /* Allow OC to sync again */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001914 status = read16(state, FEC_OC_SNC_MODE__A, &fec_oc_snc_mode);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001915 if (status < 0)
1916 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001917 fec_oc_snc_mode &= ~FEC_OC_SNC_MODE_SHUTDOWN__M;
1918 status = write16(state, FEC_OC_SNC_MODE__A, fec_oc_snc_mode);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001919 if (status < 0)
1920 goto error;
1921 status = write16(state, FEC_OC_SNC_UNLOCK__A, 1);
1922error:
1923 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03001924 pr_err("Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001925 return status;
1926}
1927
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001928static int mpegts_dto_init(struct drxk_state *state)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001929{
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001930 int status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001931
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03001932 dprintk(1, "\n");
1933
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001934 /* Rate integration settings */
1935 status = write16(state, FEC_OC_RCN_CTL_STEP_LO__A, 0x0000);
1936 if (status < 0)
1937 goto error;
1938 status = write16(state, FEC_OC_RCN_CTL_STEP_HI__A, 0x000C);
1939 if (status < 0)
1940 goto error;
1941 status = write16(state, FEC_OC_RCN_GAIN__A, 0x000A);
1942 if (status < 0)
1943 goto error;
1944 status = write16(state, FEC_OC_AVR_PARM_A__A, 0x0008);
1945 if (status < 0)
1946 goto error;
1947 status = write16(state, FEC_OC_AVR_PARM_B__A, 0x0006);
1948 if (status < 0)
1949 goto error;
1950 status = write16(state, FEC_OC_TMD_HI_MARGIN__A, 0x0680);
1951 if (status < 0)
1952 goto error;
1953 status = write16(state, FEC_OC_TMD_LO_MARGIN__A, 0x0080);
1954 if (status < 0)
1955 goto error;
1956 status = write16(state, FEC_OC_TMD_COUNT__A, 0x03F4);
1957 if (status < 0)
1958 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001959
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001960 /* Additional configuration */
1961 status = write16(state, FEC_OC_OCR_INVERT__A, 0);
1962 if (status < 0)
1963 goto error;
1964 status = write16(state, FEC_OC_SNC_LWM__A, 2);
1965 if (status < 0)
1966 goto error;
1967 status = write16(state, FEC_OC_SNC_HWM__A, 12);
1968error:
1969 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03001970 pr_err("Error %d on %s\n", status, __func__);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001971
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001972 return status;
1973}
1974
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001975static int mpegts_dto_setup(struct drxk_state *state,
1976 enum operation_mode o_mode)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001977{
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001978 int status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001979
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001980 u16 fec_oc_reg_mode = 0; /* FEC_OC_MODE register value */
1981 u16 fec_oc_reg_ipr_mode = 0; /* FEC_OC_IPR_MODE register value */
1982 u16 fec_oc_dto_mode = 0; /* FEC_OC_IPR_INVERT register value */
1983 u16 fec_oc_fct_mode = 0; /* FEC_OC_IPR_INVERT register value */
1984 u16 fec_oc_dto_period = 2; /* FEC_OC_IPR_INVERT register value */
1985 u16 fec_oc_dto_burst_len = 188; /* FEC_OC_IPR_INVERT register value */
1986 u32 fec_oc_rcn_ctl_rate = 0; /* FEC_OC_IPR_INVERT register value */
1987 u16 fec_oc_tmd_mode = 0;
1988 u16 fec_oc_tmd_int_upd_rate = 0;
1989 u32 max_bit_rate = 0;
1990 bool static_clk = false;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03001991
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03001992 dprintk(1, "\n");
1993
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001994 /* Check insertion of the Reed-Solomon parity bytes */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001995 status = read16(state, FEC_OC_MODE__A, &fec_oc_reg_mode);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001996 if (status < 0)
1997 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03001998 status = read16(state, FEC_OC_IPR_MODE__A, &fec_oc_reg_ipr_mode);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03001999 if (status < 0)
2000 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002001 fec_oc_reg_mode &= (~FEC_OC_MODE_PARITY__M);
2002 fec_oc_reg_ipr_mode &= (~FEC_OC_IPR_MODE_MVAL_DIS_PAR__M);
Mauro Carvalho Chehab5a7f7b72014-09-03 15:23:57 -03002003 if (state->m_insert_rs_byte) {
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002004 /* enable parity symbol forward */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002005 fec_oc_reg_mode |= FEC_OC_MODE_PARITY__M;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002006 /* MVAL disable during parity bytes */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002007 fec_oc_reg_ipr_mode |= FEC_OC_IPR_MODE_MVAL_DIS_PAR__M;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002008 /* TS burst length to 204 */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002009 fec_oc_dto_burst_len = 204;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002010 }
2011
Jonathan McCrohan39c1cb22013-10-20 21:34:01 -03002012 /* Check serial or parallel output */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002013 fec_oc_reg_ipr_mode &= (~(FEC_OC_IPR_MODE_SERIAL__M));
Mauro Carvalho Chehab5a7f7b72014-09-03 15:23:57 -03002014 if (!state->m_enable_parallel) {
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002015 /* MPEG data output is serial -> set ipr_mode[0] */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002016 fec_oc_reg_ipr_mode |= FEC_OC_IPR_MODE_SERIAL__M;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002017 }
2018
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002019 switch (o_mode) {
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002020 case OM_DVBT:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002021 max_bit_rate = state->m_dvbt_bitrate;
2022 fec_oc_tmd_mode = 3;
2023 fec_oc_rcn_ctl_rate = 0xC00000;
2024 static_clk = state->m_dvbt_static_clk;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002025 break;
2026 case OM_QAM_ITU_A: /* fallthrough */
2027 case OM_QAM_ITU_C:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002028 fec_oc_tmd_mode = 0x0004;
2029 fec_oc_rcn_ctl_rate = 0xD2B4EE; /* good for >63 Mb/s */
2030 max_bit_rate = state->m_dvbc_bitrate;
2031 static_clk = state->m_dvbc_static_clk;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002032 break;
2033 default:
2034 status = -EINVAL;
2035 } /* switch (standard) */
2036 if (status < 0)
2037 goto error;
2038
2039 /* Configure DTO's */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002040 if (static_clk) {
2041 u32 bit_rate = 0;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002042
2043 /* Rational DTO for MCLK source (static MCLK rate),
2044 Dynamic DTO for optimal grouping
2045 (avoid intra-packet gaps),
2046 DTO offset enable to sync TS burst with MSTRT */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002047 fec_oc_dto_mode = (FEC_OC_DTO_MODE_DYNAMIC__M |
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002048 FEC_OC_DTO_MODE_OFFSET_ENABLE__M);
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002049 fec_oc_fct_mode = (FEC_OC_FCT_MODE_RAT_ENA__M |
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002050 FEC_OC_FCT_MODE_VIRT_ENA__M);
2051
2052 /* Check user defined bitrate */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002053 bit_rate = max_bit_rate;
2054 if (bit_rate > 75900000UL) { /* max is 75.9 Mb/s */
2055 bit_rate = 75900000UL;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002056 }
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002057 /* Rational DTO period:
2058 dto_period = (Fsys / bitrate) - 2
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002059
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002060 result should be floored,
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002061 to make sure >= requested bitrate
2062 */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002063 fec_oc_dto_period = (u16) (((state->m_sys_clock_freq)
2064 * 1000) / bit_rate);
2065 if (fec_oc_dto_period <= 2)
2066 fec_oc_dto_period = 0;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002067 else
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002068 fec_oc_dto_period -= 2;
2069 fec_oc_tmd_int_upd_rate = 8;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002070 } else {
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002071 /* (commonAttr->static_clk == false) => dynamic mode */
2072 fec_oc_dto_mode = FEC_OC_DTO_MODE_DYNAMIC__M;
2073 fec_oc_fct_mode = FEC_OC_FCT_MODE__PRE;
2074 fec_oc_tmd_int_upd_rate = 5;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002075 }
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002076
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002077 /* Write appropriate registers with requested configuration */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002078 status = write16(state, FEC_OC_DTO_BURST_LEN__A, fec_oc_dto_burst_len);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002079 if (status < 0)
2080 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002081 status = write16(state, FEC_OC_DTO_PERIOD__A, fec_oc_dto_period);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002082 if (status < 0)
2083 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002084 status = write16(state, FEC_OC_DTO_MODE__A, fec_oc_dto_mode);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002085 if (status < 0)
2086 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002087 status = write16(state, FEC_OC_FCT_MODE__A, fec_oc_fct_mode);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002088 if (status < 0)
2089 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002090 status = write16(state, FEC_OC_MODE__A, fec_oc_reg_mode);
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_IPR_MODE__A, fec_oc_reg_ipr_mode);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002094 if (status < 0)
2095 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002096
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002097 /* Rate integration settings */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002098 status = write32(state, FEC_OC_RCN_CTL_RATE_LO__A, fec_oc_rcn_ctl_rate);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002099 if (status < 0)
2100 goto error;
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03002101 status = write16(state, FEC_OC_TMD_INT_UPD_RATE__A,
2102 fec_oc_tmd_int_upd_rate);
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_TMD_MODE__A, fec_oc_tmd_mode);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002106error:
2107 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03002108 pr_err("Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002109 return status;
2110}
2111
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002112static int mpegts_configure_polarity(struct drxk_state *state)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002113{
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002114 u16 fec_oc_reg_ipr_invert = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002115
2116 /* Data mask for the output data byte */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002117 u16 invert_data_mask =
Oliver Endrissebc7de22011-07-03 13:49:44 -03002118 FEC_OC_IPR_INVERT_MD7__M | FEC_OC_IPR_INVERT_MD6__M |
2119 FEC_OC_IPR_INVERT_MD5__M | FEC_OC_IPR_INVERT_MD4__M |
2120 FEC_OC_IPR_INVERT_MD3__M | FEC_OC_IPR_INVERT_MD2__M |
2121 FEC_OC_IPR_INVERT_MD1__M | FEC_OC_IPR_INVERT_MD0__M;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002122
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002123 dprintk(1, "\n");
2124
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002125 /* Control selective inversion of output bits */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002126 fec_oc_reg_ipr_invert &= (~(invert_data_mask));
Mauro Carvalho Chehab5a7f7b72014-09-03 15:23:57 -03002127 if (state->m_invert_data)
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002128 fec_oc_reg_ipr_invert |= invert_data_mask;
2129 fec_oc_reg_ipr_invert &= (~(FEC_OC_IPR_INVERT_MERR__M));
Mauro Carvalho Chehab5a7f7b72014-09-03 15:23:57 -03002130 if (state->m_invert_err)
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002131 fec_oc_reg_ipr_invert |= FEC_OC_IPR_INVERT_MERR__M;
2132 fec_oc_reg_ipr_invert &= (~(FEC_OC_IPR_INVERT_MSTRT__M));
Mauro Carvalho Chehab5a7f7b72014-09-03 15:23:57 -03002133 if (state->m_invert_str)
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002134 fec_oc_reg_ipr_invert |= FEC_OC_IPR_INVERT_MSTRT__M;
2135 fec_oc_reg_ipr_invert &= (~(FEC_OC_IPR_INVERT_MVAL__M));
Mauro Carvalho Chehab5a7f7b72014-09-03 15:23:57 -03002136 if (state->m_invert_val)
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002137 fec_oc_reg_ipr_invert |= FEC_OC_IPR_INVERT_MVAL__M;
2138 fec_oc_reg_ipr_invert &= (~(FEC_OC_IPR_INVERT_MCLK__M));
Mauro Carvalho Chehab5a7f7b72014-09-03 15:23:57 -03002139 if (state->m_invert_clk)
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002140 fec_oc_reg_ipr_invert |= FEC_OC_IPR_INVERT_MCLK__M;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002141
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002142 return write16(state, FEC_OC_IPR_INVERT__A, fec_oc_reg_ipr_invert);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002143}
2144
2145#define SCU_RAM_AGC_KI_INV_RF_POL__M 0x4000
2146
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002147static int set_agc_rf(struct drxk_state *state,
2148 struct s_cfg_agc *p_agc_cfg, bool is_dtv)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002149{
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002150 int status = -EINVAL;
2151 u16 data = 0;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002152 struct s_cfg_agc *p_if_agc_settings;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002153
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03002154 dprintk(1, "\n");
2155
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002156 if (p_agc_cfg == NULL)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002157 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002158
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002159 switch (p_agc_cfg->ctrl_mode) {
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002160 case DRXK_AGC_CTRL_AUTO:
2161 /* Enable RF AGC DAC */
2162 status = read16(state, IQM_AF_STDBY__A, &data);
2163 if (status < 0)
2164 goto error;
2165 data &= ~IQM_AF_STDBY_STDBY_TAGC_RF_STANDBY;
2166 status = write16(state, IQM_AF_STDBY__A, data);
2167 if (status < 0)
2168 goto error;
2169 status = read16(state, SCU_RAM_AGC_CONFIG__A, &data);
2170 if (status < 0)
2171 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002172
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002173 /* Enable SCU RF AGC loop */
2174 data &= ~SCU_RAM_AGC_CONFIG_DISABLE_RF_AGC__M;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002175
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002176 /* Polarity */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002177 if (state->m_rf_agc_pol)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002178 data |= SCU_RAM_AGC_CONFIG_INV_RF_POL__M;
2179 else
2180 data &= ~SCU_RAM_AGC_CONFIG_INV_RF_POL__M;
2181 status = write16(state, SCU_RAM_AGC_CONFIG__A, data);
2182 if (status < 0)
2183 goto error;
2184
2185 /* Set speed (using complementary reduction value) */
2186 status = read16(state, SCU_RAM_AGC_KI_RED__A, &data);
2187 if (status < 0)
2188 goto error;
2189
2190 data &= ~SCU_RAM_AGC_KI_RED_RAGC_RED__M;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002191 data |= (~(p_agc_cfg->speed <<
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002192 SCU_RAM_AGC_KI_RED_RAGC_RED__B)
2193 & SCU_RAM_AGC_KI_RED_RAGC_RED__M);
2194
2195 status = write16(state, SCU_RAM_AGC_KI_RED__A, data);
2196 if (status < 0)
2197 goto error;
2198
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002199 if (is_dvbt(state))
2200 p_if_agc_settings = &state->m_dvbt_if_agc_cfg;
2201 else if (is_qam(state))
2202 p_if_agc_settings = &state->m_qam_if_agc_cfg;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002203 else
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002204 p_if_agc_settings = &state->m_atv_if_agc_cfg;
2205 if (p_if_agc_settings == NULL) {
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002206 status = -EINVAL;
2207 goto error;
2208 }
2209
2210 /* Set TOP, only if IF-AGC is in AUTO mode */
Mauro Carvalho Chehab89fffac2014-09-03 19:11:45 -03002211 if (p_if_agc_settings->ctrl_mode == DRXK_AGC_CTRL_AUTO) {
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03002212 status = write16(state,
2213 SCU_RAM_AGC_IF_IACCU_HI_TGT_MAX__A,
2214 p_agc_cfg->top);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03002215 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002216 goto error;
Mauro Carvalho Chehab89fffac2014-09-03 19:11:45 -03002217 }
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002218
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002219 /* Cut-Off current */
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03002220 status = write16(state, SCU_RAM_AGC_RF_IACCU_HI_CO__A,
2221 p_agc_cfg->cut_off_current);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002222 if (status < 0)
2223 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002224
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002225 /* Max. output level */
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03002226 status = write16(state, SCU_RAM_AGC_RF_MAX__A,
2227 p_agc_cfg->max_output_level);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002228 if (status < 0)
2229 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002230
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002231 break;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002232
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002233 case DRXK_AGC_CTRL_USER:
2234 /* Enable RF AGC DAC */
2235 status = read16(state, IQM_AF_STDBY__A, &data);
2236 if (status < 0)
2237 goto error;
2238 data &= ~IQM_AF_STDBY_STDBY_TAGC_RF_STANDBY;
2239 status = write16(state, IQM_AF_STDBY__A, data);
2240 if (status < 0)
2241 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002242
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002243 /* Disable SCU RF AGC loop */
2244 status = read16(state, SCU_RAM_AGC_CONFIG__A, &data);
2245 if (status < 0)
2246 goto error;
2247 data |= SCU_RAM_AGC_CONFIG_DISABLE_RF_AGC__M;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002248 if (state->m_rf_agc_pol)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002249 data |= SCU_RAM_AGC_CONFIG_INV_RF_POL__M;
2250 else
2251 data &= ~SCU_RAM_AGC_CONFIG_INV_RF_POL__M;
2252 status = write16(state, SCU_RAM_AGC_CONFIG__A, data);
2253 if (status < 0)
2254 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002255
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002256 /* SCU c.o.c. to 0, enabling full control range */
2257 status = write16(state, SCU_RAM_AGC_RF_IACCU_HI_CO__A, 0);
2258 if (status < 0)
2259 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002260
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002261 /* Write value to output pin */
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03002262 status = write16(state, SCU_RAM_AGC_RF_IACCU_HI__A,
2263 p_agc_cfg->output_level);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002264 if (status < 0)
2265 goto error;
2266 break;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002267
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002268 case DRXK_AGC_CTRL_OFF:
2269 /* Disable RF AGC DAC */
2270 status = read16(state, IQM_AF_STDBY__A, &data);
2271 if (status < 0)
2272 goto error;
2273 data |= IQM_AF_STDBY_STDBY_TAGC_RF_STANDBY;
2274 status = write16(state, IQM_AF_STDBY__A, data);
2275 if (status < 0)
2276 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002277
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002278 /* Disable SCU RF AGC loop */
2279 status = read16(state, SCU_RAM_AGC_CONFIG__A, &data);
2280 if (status < 0)
2281 goto error;
2282 data |= SCU_RAM_AGC_CONFIG_DISABLE_RF_AGC__M;
2283 status = write16(state, SCU_RAM_AGC_CONFIG__A, data);
2284 if (status < 0)
2285 goto error;
2286 break;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002287
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002288 default:
2289 status = -EINVAL;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002290
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002291 }
2292error:
2293 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03002294 pr_err("Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002295 return status;
2296}
2297
2298#define SCU_RAM_AGC_KI_INV_IF_POL__M 0x2000
2299
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002300static int set_agc_if(struct drxk_state *state,
2301 struct s_cfg_agc *p_agc_cfg, bool is_dtv)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002302{
2303 u16 data = 0;
2304 int status = 0;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002305 struct s_cfg_agc *p_rf_agc_settings;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002306
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03002307 dprintk(1, "\n");
2308
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002309 switch (p_agc_cfg->ctrl_mode) {
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002310 case DRXK_AGC_CTRL_AUTO:
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002311
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002312 /* Enable IF AGC DAC */
2313 status = read16(state, IQM_AF_STDBY__A, &data);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03002314 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002315 goto error;
2316 data &= ~IQM_AF_STDBY_STDBY_TAGC_IF_STANDBY;
2317 status = write16(state, IQM_AF_STDBY__A, data);
2318 if (status < 0)
2319 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002320
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002321 status = read16(state, SCU_RAM_AGC_CONFIG__A, &data);
2322 if (status < 0)
2323 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002324
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002325 /* Enable SCU IF AGC loop */
2326 data &= ~SCU_RAM_AGC_CONFIG_DISABLE_IF_AGC__M;
2327
2328 /* Polarity */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002329 if (state->m_if_agc_pol)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002330 data |= SCU_RAM_AGC_CONFIG_INV_IF_POL__M;
2331 else
2332 data &= ~SCU_RAM_AGC_CONFIG_INV_IF_POL__M;
2333 status = write16(state, SCU_RAM_AGC_CONFIG__A, data);
2334 if (status < 0)
2335 goto error;
2336
2337 /* Set speed (using complementary reduction value) */
2338 status = read16(state, SCU_RAM_AGC_KI_RED__A, &data);
2339 if (status < 0)
2340 goto error;
2341 data &= ~SCU_RAM_AGC_KI_RED_IAGC_RED__M;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002342 data |= (~(p_agc_cfg->speed <<
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002343 SCU_RAM_AGC_KI_RED_IAGC_RED__B)
2344 & SCU_RAM_AGC_KI_RED_IAGC_RED__M);
2345
2346 status = write16(state, SCU_RAM_AGC_KI_RED__A, data);
2347 if (status < 0)
2348 goto error;
2349
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002350 if (is_qam(state))
2351 p_rf_agc_settings = &state->m_qam_rf_agc_cfg;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002352 else
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002353 p_rf_agc_settings = &state->m_atv_rf_agc_cfg;
2354 if (p_rf_agc_settings == NULL)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002355 return -1;
2356 /* Restore TOP */
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03002357 status = write16(state, SCU_RAM_AGC_IF_IACCU_HI_TGT_MAX__A,
2358 p_rf_agc_settings->top);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002359 if (status < 0)
2360 goto error;
2361 break;
2362
2363 case DRXK_AGC_CTRL_USER:
2364
2365 /* Enable IF AGC DAC */
2366 status = read16(state, IQM_AF_STDBY__A, &data);
2367 if (status < 0)
2368 goto error;
2369 data &= ~IQM_AF_STDBY_STDBY_TAGC_IF_STANDBY;
2370 status = write16(state, IQM_AF_STDBY__A, data);
2371 if (status < 0)
2372 goto error;
2373
2374 status = read16(state, SCU_RAM_AGC_CONFIG__A, &data);
2375 if (status < 0)
2376 goto error;
2377
2378 /* Disable SCU IF AGC loop */
2379 data |= SCU_RAM_AGC_CONFIG_DISABLE_IF_AGC__M;
2380
2381 /* Polarity */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002382 if (state->m_if_agc_pol)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002383 data |= SCU_RAM_AGC_CONFIG_INV_IF_POL__M;
2384 else
2385 data &= ~SCU_RAM_AGC_CONFIG_INV_IF_POL__M;
2386 status = write16(state, SCU_RAM_AGC_CONFIG__A, data);
2387 if (status < 0)
2388 goto error;
2389
2390 /* Write value to output pin */
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03002391 status = write16(state, SCU_RAM_AGC_IF_IACCU_HI_TGT_MAX__A,
2392 p_agc_cfg->output_level);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002393 if (status < 0)
2394 goto error;
2395 break;
2396
2397 case DRXK_AGC_CTRL_OFF:
2398
2399 /* Disable If AGC DAC */
2400 status = read16(state, IQM_AF_STDBY__A, &data);
2401 if (status < 0)
2402 goto error;
2403 data |= IQM_AF_STDBY_STDBY_TAGC_IF_STANDBY;
2404 status = write16(state, IQM_AF_STDBY__A, data);
2405 if (status < 0)
2406 goto error;
2407
2408 /* Disable SCU IF AGC loop */
2409 status = read16(state, SCU_RAM_AGC_CONFIG__A, &data);
2410 if (status < 0)
2411 goto error;
2412 data |= SCU_RAM_AGC_CONFIG_DISABLE_IF_AGC__M;
2413 status = write16(state, SCU_RAM_AGC_CONFIG__A, data);
2414 if (status < 0)
2415 goto error;
2416 break;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002417 } /* switch (agcSettingsIf->ctrl_mode) */
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002418
2419 /* always set the top to support
2420 configurations without if-loop */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002421 status = write16(state, SCU_RAM_AGC_INGAIN_TGT_MIN__A, p_agc_cfg->top);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002422error:
2423 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03002424 pr_err("Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002425 return status;
2426}
2427
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002428static int get_qam_signal_to_noise(struct drxk_state *state,
2429 s32 *p_signal_to_noise)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002430{
2431 int status = 0;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002432 u16 qam_sl_err_power = 0; /* accum. error between
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002433 raw and sliced symbols */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002434 u32 qam_sl_sig_power = 0; /* used for MER, depends of
Mauro Carvalho Chehabed5452a2011-12-26 09:57:11 -03002435 QAM modulation */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002436 u32 qam_sl_mer = 0; /* QAM MER */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002437
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03002438 dprintk(1, "\n");
2439
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002440 /* MER calculation */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002441
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002442 /* get the register value needed for MER */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002443 status = read16(state, QAM_SL_ERR_POWER__A, &qam_sl_err_power);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002444 if (status < 0) {
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03002445 pr_err("Error %d on %s\n", status, __func__);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002446 return -EINVAL;
2447 }
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002448
Mauro Carvalho Chehabed5452a2011-12-26 09:57:11 -03002449 switch (state->props.modulation) {
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002450 case QAM_16:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002451 qam_sl_sig_power = DRXK_QAM_SL_SIG_POWER_QAM16 << 2;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002452 break;
2453 case QAM_32:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002454 qam_sl_sig_power = DRXK_QAM_SL_SIG_POWER_QAM32 << 2;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002455 break;
2456 case QAM_64:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002457 qam_sl_sig_power = DRXK_QAM_SL_SIG_POWER_QAM64 << 2;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002458 break;
2459 case QAM_128:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002460 qam_sl_sig_power = DRXK_QAM_SL_SIG_POWER_QAM128 << 2;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002461 break;
2462 default:
2463 case QAM_256:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002464 qam_sl_sig_power = DRXK_QAM_SL_SIG_POWER_QAM256 << 2;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002465 break;
2466 }
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002467
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002468 if (qam_sl_err_power > 0) {
2469 qam_sl_mer = log10times100(qam_sl_sig_power) -
2470 log10times100((u32) qam_sl_err_power);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002471 }
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002472 *p_signal_to_noise = qam_sl_mer;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002473
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002474 return status;
2475}
2476
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002477static int get_dvbt_signal_to_noise(struct drxk_state *state,
2478 s32 *p_signal_to_noise)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002479{
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002480 int status;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002481 u16 reg_data = 0;
2482 u32 eq_reg_td_sqr_err_i = 0;
2483 u32 eq_reg_td_sqr_err_q = 0;
2484 u16 eq_reg_td_sqr_err_exp = 0;
2485 u16 eq_reg_td_tps_pwr_ofs = 0;
2486 u16 eq_reg_td_req_smb_cnt = 0;
2487 u32 tps_cnt = 0;
2488 u32 sqr_err_iq = 0;
Oliver Endrissebc7de22011-07-03 13:49:44 -03002489 u32 a = 0;
2490 u32 b = 0;
2491 u32 c = 0;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002492 u32 i_mer = 0;
2493 u16 transmission_params = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002494
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03002495 dprintk(1, "\n");
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002496
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03002497 status = read16(state, OFDM_EQ_TOP_TD_TPS_PWR_OFS__A,
2498 &eq_reg_td_tps_pwr_ofs);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002499 if (status < 0)
2500 goto error;
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03002501 status = read16(state, OFDM_EQ_TOP_TD_REQ_SMB_CNT__A,
2502 &eq_reg_td_req_smb_cnt);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002503 if (status < 0)
2504 goto error;
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03002505 status = read16(state, OFDM_EQ_TOP_TD_SQR_ERR_EXP__A,
2506 &eq_reg_td_sqr_err_exp);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002507 if (status < 0)
2508 goto error;
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03002509 status = read16(state, OFDM_EQ_TOP_TD_SQR_ERR_I__A,
2510 &reg_data);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002511 if (status < 0)
2512 goto error;
2513 /* Extend SQR_ERR_I operational range */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002514 eq_reg_td_sqr_err_i = (u32) reg_data;
2515 if ((eq_reg_td_sqr_err_exp > 11) &&
2516 (eq_reg_td_sqr_err_i < 0x00000FFFUL)) {
2517 eq_reg_td_sqr_err_i += 0x00010000UL;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002518 }
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002519 status = read16(state, OFDM_EQ_TOP_TD_SQR_ERR_Q__A, &reg_data);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002520 if (status < 0)
2521 goto error;
2522 /* Extend SQR_ERR_Q operational range */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002523 eq_reg_td_sqr_err_q = (u32) reg_data;
2524 if ((eq_reg_td_sqr_err_exp > 11) &&
2525 (eq_reg_td_sqr_err_q < 0x00000FFFUL))
2526 eq_reg_td_sqr_err_q += 0x00010000UL;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002527
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03002528 status = read16(state, OFDM_SC_RA_RAM_OP_PARAM__A,
2529 &transmission_params);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002530 if (status < 0)
2531 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002532
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002533 /* Check input data for MER */
2534
2535 /* MER calculation (in 0.1 dB) without math.h */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002536 if ((eq_reg_td_tps_pwr_ofs == 0) || (eq_reg_td_req_smb_cnt == 0))
2537 i_mer = 0;
2538 else if ((eq_reg_td_sqr_err_i + eq_reg_td_sqr_err_q) == 0) {
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002539 /* No error at all, this must be the HW reset value
2540 * Apparently no first measurement yet
2541 * Set MER to 0.0 */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002542 i_mer = 0;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002543 } else {
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002544 sqr_err_iq = (eq_reg_td_sqr_err_i + eq_reg_td_sqr_err_q) <<
2545 eq_reg_td_sqr_err_exp;
2546 if ((transmission_params &
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002547 OFDM_SC_RA_RAM_OP_PARAM_MODE__M)
2548 == OFDM_SC_RA_RAM_OP_PARAM_MODE_2K)
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002549 tps_cnt = 17;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002550 else
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002551 tps_cnt = 68;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002552
2553 /* IMER = 100 * log10 (x)
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002554 where x = (eq_reg_td_tps_pwr_ofs^2 *
2555 eq_reg_td_req_smb_cnt * tps_cnt)/sqr_err_iq
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002556
2557 => IMER = a + b -c
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002558 where a = 100 * log10 (eq_reg_td_tps_pwr_ofs^2)
2559 b = 100 * log10 (eq_reg_td_req_smb_cnt * tps_cnt)
2560 c = 100 * log10 (sqr_err_iq)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002561 */
2562
2563 /* log(x) x = 9bits * 9bits->18 bits */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002564 a = log10times100(eq_reg_td_tps_pwr_ofs *
2565 eq_reg_td_tps_pwr_ofs);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002566 /* log(x) x = 16bits * 7bits->23 bits */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002567 b = log10times100(eq_reg_td_req_smb_cnt * tps_cnt);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002568 /* log(x) x = (16bits + 16bits) << 15 ->32 bits */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002569 c = log10times100(sqr_err_iq);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002570
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002571 i_mer = a + b - c;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002572 }
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002573 *p_signal_to_noise = i_mer;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002574
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002575error:
2576 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03002577 pr_err("Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002578 return status;
2579}
2580
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002581static int get_signal_to_noise(struct drxk_state *state, s32 *p_signal_to_noise)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002582{
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03002583 dprintk(1, "\n");
2584
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002585 *p_signal_to_noise = 0;
2586 switch (state->m_operation_mode) {
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002587 case OM_DVBT:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002588 return get_dvbt_signal_to_noise(state, p_signal_to_noise);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002589 case OM_QAM_ITU_A:
2590 case OM_QAM_ITU_C:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002591 return get_qam_signal_to_noise(state, p_signal_to_noise);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002592 default:
2593 break;
2594 }
2595 return 0;
2596}
2597
2598#if 0
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002599static int get_dvbt_quality(struct drxk_state *state, s32 *p_quality)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002600{
2601 /* SNR Values for quasi errorfree reception rom Nordig 2.2 */
2602 int status = 0;
2603
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03002604 dprintk(1, "\n");
2605
Oliver Endrissebc7de22011-07-03 13:49:44 -03002606 static s32 QE_SN[] = {
2607 51, /* QPSK 1/2 */
2608 69, /* QPSK 2/3 */
2609 79, /* QPSK 3/4 */
2610 89, /* QPSK 5/6 */
2611 97, /* QPSK 7/8 */
2612 108, /* 16-QAM 1/2 */
2613 131, /* 16-QAM 2/3 */
2614 146, /* 16-QAM 3/4 */
2615 156, /* 16-QAM 5/6 */
2616 160, /* 16-QAM 7/8 */
2617 165, /* 64-QAM 1/2 */
2618 187, /* 64-QAM 2/3 */
2619 202, /* 64-QAM 3/4 */
2620 216, /* 64-QAM 5/6 */
2621 225, /* 64-QAM 7/8 */
2622 };
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002623
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002624 *p_quality = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002625
2626 do {
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002627 s32 signal_to_noise = 0;
2628 u16 constellation = 0;
2629 u16 code_rate = 0;
2630 u32 signal_to_noise_rel;
2631 u32 ber_quality;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002632
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002633 status = get_dvbt_signal_to_noise(state, &signal_to_noise);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03002634 if (status < 0)
2635 break;
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03002636 status = read16(state, OFDM_EQ_TOP_TD_TPS_CONST__A,
2637 &constellation);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03002638 if (status < 0)
2639 break;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002640 constellation &= OFDM_EQ_TOP_TD_TPS_CONST__M;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002641
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03002642 status = read16(state, OFDM_EQ_TOP_TD_TPS_CODE_HP__A,
2643 &code_rate);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03002644 if (status < 0)
2645 break;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002646 code_rate &= OFDM_EQ_TOP_TD_TPS_CODE_HP__M;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002647
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002648 if (constellation > OFDM_EQ_TOP_TD_TPS_CONST_64QAM ||
2649 code_rate > OFDM_EQ_TOP_TD_TPS_CODE_LP_7_8)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002650 break;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002651 signal_to_noise_rel = signal_to_noise -
2652 QE_SN[constellation * 5 + code_rate];
2653 ber_quality = 100;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002654
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002655 if (signal_to_noise_rel < -70)
2656 *p_quality = 0;
2657 else if (signal_to_noise_rel < 30)
2658 *p_quality = ((signal_to_noise_rel + 70) *
2659 ber_quality) / 100;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002660 else
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002661 *p_quality = ber_quality;
Oliver Endrissebc7de22011-07-03 13:49:44 -03002662 } while (0);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002663 return 0;
2664};
2665
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002666static int get_dvbc_quality(struct drxk_state *state, s32 *p_quality)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002667{
2668 int status = 0;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002669 *p_quality = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002670
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03002671 dprintk(1, "\n");
2672
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002673 do {
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002674 u32 signal_to_noise = 0;
2675 u32 ber_quality = 100;
2676 u32 signal_to_noise_rel = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002677
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002678 status = get_qam_signal_to_noise(state, &signal_to_noise);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03002679 if (status < 0)
2680 break;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002681
Mauro Carvalho Chehabed5452a2011-12-26 09:57:11 -03002682 switch (state->props.modulation) {
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002683 case QAM_16:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002684 signal_to_noise_rel = signal_to_noise - 200;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002685 break;
2686 case QAM_32:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002687 signal_to_noise_rel = signal_to_noise - 230;
Oliver Endrissebc7de22011-07-03 13:49:44 -03002688 break; /* Not in NorDig */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002689 case QAM_64:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002690 signal_to_noise_rel = signal_to_noise - 260;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002691 break;
2692 case QAM_128:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002693 signal_to_noise_rel = signal_to_noise - 290;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002694 break;
2695 default:
2696 case QAM_256:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002697 signal_to_noise_rel = signal_to_noise - 320;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002698 break;
2699 }
2700
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002701 if (signal_to_noise_rel < -70)
2702 *p_quality = 0;
2703 else if (signal_to_noise_rel < 30)
2704 *p_quality = ((signal_to_noise_rel + 70) *
2705 ber_quality) / 100;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002706 else
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002707 *p_quality = ber_quality;
Oliver Endrissebc7de22011-07-03 13:49:44 -03002708 } while (0);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002709
2710 return status;
2711}
2712
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002713static int get_quality(struct drxk_state *state, s32 *p_quality)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002714{
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03002715 dprintk(1, "\n");
2716
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002717 switch (state->m_operation_mode) {
Oliver Endrissebc7de22011-07-03 13:49:44 -03002718 case OM_DVBT:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002719 return get_dvbt_quality(state, p_quality);
Oliver Endrissebc7de22011-07-03 13:49:44 -03002720 case OM_QAM_ITU_A:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002721 return get_dvbc_quality(state, p_quality);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002722 default:
2723 break;
2724 }
2725
2726 return 0;
2727}
2728#endif
2729
2730/* Free data ram in SIO HI */
2731#define SIO_HI_RA_RAM_USR_BEGIN__A 0x420040
2732#define SIO_HI_RA_RAM_USR_END__A 0x420060
2733
2734#define DRXK_HI_ATOMIC_BUF_START (SIO_HI_RA_RAM_USR_BEGIN__A)
2735#define DRXK_HI_ATOMIC_BUF_END (SIO_HI_RA_RAM_USR_BEGIN__A + 7)
2736#define DRXK_HI_ATOMIC_READ SIO_HI_RA_RAM_PAR_3_ACP_RW_READ
2737#define DRXK_HI_ATOMIC_WRITE SIO_HI_RA_RAM_PAR_3_ACP_RW_WRITE
2738
2739#define DRXDAP_FASI_ADDR2BLOCK(addr) (((addr) >> 22) & 0x3F)
2740#define DRXDAP_FASI_ADDR2BANK(addr) (((addr) >> 16) & 0x3F)
2741#define DRXDAP_FASI_ADDR2OFFSET(addr) ((addr) & 0x7FFF)
2742
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002743static int ConfigureI2CBridge(struct drxk_state *state, bool b_enable_bridge)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002744{
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002745 int status = -EINVAL;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002746
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03002747 dprintk(1, "\n");
2748
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002749 if (state->m_drxk_state == DRXK_UNINITIALIZED)
Mauro Carvalho Chehab704a28e2012-06-29 15:45:04 -03002750 return 0;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002751 if (state->m_drxk_state == DRXK_POWERED_DOWN)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002752 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002753
Mauro Carvalho Chehabf1fe1b72011-07-09 21:59:33 -03002754 if (state->no_i2c_bridge)
2755 return 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002756
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03002757 status = write16(state, SIO_HI_RA_RAM_PAR_1__A,
2758 SIO_HI_RA_RAM_PAR_1_PAR1_SEC_KEY);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002759 if (status < 0)
2760 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002761 if (b_enable_bridge) {
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03002762 status = write16(state, SIO_HI_RA_RAM_PAR_2__A,
2763 SIO_HI_RA_RAM_PAR_2_BRD_CFG_CLOSED);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03002764 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002765 goto error;
2766 } else {
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03002767 status = write16(state, SIO_HI_RA_RAM_PAR_2__A,
2768 SIO_HI_RA_RAM_PAR_2_BRD_CFG_OPEN);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002769 if (status < 0)
2770 goto error;
2771 }
2772
Hans Verkuilb1cf2012013-10-04 11:01:45 -03002773 status = hi_command(state, SIO_HI_RA_RAM_CMD_BRDCTRL, NULL);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002774
2775error:
2776 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03002777 pr_err("Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002778 return status;
2779}
2780
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002781static int set_pre_saw(struct drxk_state *state,
2782 struct s_cfg_pre_saw *p_pre_saw_cfg)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002783{
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002784 int status = -EINVAL;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002785
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03002786 dprintk(1, "\n");
2787
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002788 if ((p_pre_saw_cfg == NULL)
2789 || (p_pre_saw_cfg->reference > IQM_AF_PDREF__M))
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002790 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002791
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002792 status = write16(state, IQM_AF_PDREF__A, p_pre_saw_cfg->reference);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002793error:
2794 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03002795 pr_err("Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002796 return status;
2797}
2798
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002799static int bl_direct_cmd(struct drxk_state *state, u32 target_addr,
2800 u16 rom_offset, u16 nr_of_elements, u32 time_out)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002801{
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002802 u16 bl_status = 0;
2803 u16 offset = (u16) ((target_addr >> 0) & 0x00FFFF);
2804 u16 blockbank = (u16) ((target_addr >> 16) & 0x000FFF);
Oliver Endrissebc7de22011-07-03 13:49:44 -03002805 int status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002806 unsigned long end;
2807
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03002808 dprintk(1, "\n");
2809
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002810 mutex_lock(&state->mutex);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002811 status = write16(state, SIO_BL_MODE__A, SIO_BL_MODE_DIRECT);
2812 if (status < 0)
2813 goto error;
2814 status = write16(state, SIO_BL_TGT_HDR__A, blockbank);
2815 if (status < 0)
2816 goto error;
2817 status = write16(state, SIO_BL_TGT_ADDR__A, offset);
2818 if (status < 0)
2819 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002820 status = write16(state, SIO_BL_SRC_ADDR__A, rom_offset);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002821 if (status < 0)
2822 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002823 status = write16(state, SIO_BL_SRC_LEN__A, nr_of_elements);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002824 if (status < 0)
2825 goto error;
2826 status = write16(state, SIO_BL_ENABLE__A, SIO_BL_ENABLE_ON);
2827 if (status < 0)
2828 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002829
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002830 end = jiffies + msecs_to_jiffies(time_out);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002831 do {
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002832 status = read16(state, SIO_BL_STATUS__A, &bl_status);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002833 if (status < 0)
2834 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002835 } while ((bl_status == 0x1) && time_is_after_jiffies(end));
2836 if (bl_status == 0x1) {
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03002837 pr_err("SIO not ready\n");
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002838 status = -EINVAL;
2839 goto error2;
2840 }
2841error:
2842 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03002843 pr_err("Error %d on %s\n", status, __func__);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002844error2:
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002845 mutex_unlock(&state->mutex);
2846 return status;
2847
2848}
2849
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002850static int adc_sync_measurement(struct drxk_state *state, u16 *count)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002851{
2852 u16 data = 0;
2853 int status;
2854
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03002855 dprintk(1, "\n");
2856
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002857 /* start measurement */
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002858 status = write16(state, IQM_AF_COMM_EXEC__A, IQM_AF_COMM_EXEC_ACTIVE);
2859 if (status < 0)
2860 goto error;
2861 status = write16(state, IQM_AF_START_LOCK__A, 1);
2862 if (status < 0)
2863 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002864
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002865 *count = 0;
2866 status = read16(state, IQM_AF_PHASE0__A, &data);
2867 if (status < 0)
2868 goto error;
2869 if (data == 127)
2870 *count = *count + 1;
2871 status = read16(state, IQM_AF_PHASE1__A, &data);
2872 if (status < 0)
2873 goto error;
2874 if (data == 127)
2875 *count = *count + 1;
2876 status = read16(state, IQM_AF_PHASE2__A, &data);
2877 if (status < 0)
2878 goto error;
2879 if (data == 127)
2880 *count = *count + 1;
2881
2882error:
2883 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03002884 pr_err("Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002885 return status;
2886}
2887
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002888static int adc_synchronization(struct drxk_state *state)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002889{
2890 u16 count = 0;
2891 int status;
2892
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03002893 dprintk(1, "\n");
2894
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002895 status = adc_sync_measurement(state, &count);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002896 if (status < 0)
2897 goto error;
2898
2899 if (count == 1) {
Jonathan McCrohan39c1cb22013-10-20 21:34:01 -03002900 /* Try sampling on a different edge */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002901 u16 clk_neg = 0;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002902
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002903 status = read16(state, IQM_AF_CLKNEG__A, &clk_neg);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002904 if (status < 0)
2905 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002906 if ((clk_neg & IQM_AF_CLKNEG_CLKNEGDATA__M) ==
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002907 IQM_AF_CLKNEG_CLKNEGDATA_CLK_ADC_DATA_POS) {
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002908 clk_neg &= (~(IQM_AF_CLKNEG_CLKNEGDATA__M));
2909 clk_neg |=
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002910 IQM_AF_CLKNEG_CLKNEGDATA_CLK_ADC_DATA_NEG;
2911 } else {
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002912 clk_neg &= (~(IQM_AF_CLKNEG_CLKNEGDATA__M));
2913 clk_neg |=
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002914 IQM_AF_CLKNEG_CLKNEGDATA_CLK_ADC_DATA_POS;
2915 }
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002916 status = write16(state, IQM_AF_CLKNEG__A, clk_neg);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002917 if (status < 0)
2918 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002919 status = adc_sync_measurement(state, &count);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03002920 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002921 goto error;
2922 }
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002923
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002924 if (count < 2)
2925 status = -EINVAL;
2926error:
2927 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03002928 pr_err("Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002929 return status;
2930}
2931
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002932static int set_frequency_shifter(struct drxk_state *state,
2933 u16 intermediate_freqk_hz,
2934 s32 tuner_freq_offset, bool is_dtv)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002935{
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002936 bool select_pos_image = false;
2937 u32 rf_freq_residual = tuner_freq_offset;
2938 u32 fm_frequency_shift = 0;
2939 bool tuner_mirror = !state->m_b_mirror_freq_spect;
2940 u32 adc_freq;
2941 bool adc_flip;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002942 int status;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002943 u32 if_freq_actual;
2944 u32 sampling_frequency = (u32) (state->m_sys_clock_freq / 3);
2945 u32 frequency_shift;
2946 bool image_to_select;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002947
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03002948 dprintk(1, "\n");
2949
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002950 /*
Oliver Endrissebc7de22011-07-03 13:49:44 -03002951 Program frequency shifter
2952 No need to account for mirroring on RF
2953 */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002954 if (is_dtv) {
2955 if ((state->m_operation_mode == OM_QAM_ITU_A) ||
2956 (state->m_operation_mode == OM_QAM_ITU_C) ||
2957 (state->m_operation_mode == OM_DVBT))
2958 select_pos_image = true;
Oliver Endrissebc7de22011-07-03 13:49:44 -03002959 else
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002960 select_pos_image = false;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002961 }
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002962 if (tuner_mirror)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002963 /* tuner doesn't mirror */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002964 if_freq_actual = intermediate_freqk_hz +
2965 rf_freq_residual + fm_frequency_shift;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002966 else
2967 /* tuner mirrors */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002968 if_freq_actual = intermediate_freqk_hz -
2969 rf_freq_residual - fm_frequency_shift;
2970 if (if_freq_actual > sampling_frequency / 2) {
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002971 /* adc mirrors */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002972 adc_freq = sampling_frequency - if_freq_actual;
2973 adc_flip = true;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002974 } else {
2975 /* adc doesn't mirror */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002976 adc_freq = if_freq_actual;
2977 adc_flip = false;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002978 }
2979
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002980 frequency_shift = adc_freq;
2981 image_to_select = state->m_rfmirror ^ tuner_mirror ^
2982 adc_flip ^ select_pos_image;
2983 state->m_iqm_fs_rate_ofs =
2984 Frac28a((frequency_shift), sampling_frequency);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002985
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002986 if (image_to_select)
2987 state->m_iqm_fs_rate_ofs = ~state->m_iqm_fs_rate_ofs + 1;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002988
2989 /* Program frequency shifter with tuner offset compensation */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002990 /* frequency_shift += tuner_freq_offset; TODO */
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -03002991 status = write32(state, IQM_FS_RATE_OFS_LO__A,
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002992 state->m_iqm_fs_rate_ofs);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03002993 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03002994 pr_err("Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002995 return status;
2996}
2997
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03002998static int init_agc(struct drxk_state *state, bool is_dtv)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03002999{
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003000 u16 ingain_tgt = 0;
3001 u16 ingain_tgt_min = 0;
3002 u16 ingain_tgt_max = 0;
3003 u16 clp_cyclen = 0;
3004 u16 clp_sum_min = 0;
3005 u16 clp_dir_to = 0;
3006 u16 sns_sum_min = 0;
3007 u16 sns_sum_max = 0;
3008 u16 clp_sum_max = 0;
3009 u16 sns_dir_to = 0;
3010 u16 ki_innergain_min = 0;
3011 u16 if_iaccu_hi_tgt = 0;
3012 u16 if_iaccu_hi_tgt_min = 0;
3013 u16 if_iaccu_hi_tgt_max = 0;
Oliver Endrissebc7de22011-07-03 13:49:44 -03003014 u16 data = 0;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003015 u16 fast_clp_ctrl_delay = 0;
3016 u16 clp_ctrl_mode = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003017 int status = 0;
3018
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03003019 dprintk(1, "\n");
3020
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003021 /* Common settings */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003022 sns_sum_max = 1023;
3023 if_iaccu_hi_tgt_min = 2047;
3024 clp_cyclen = 500;
3025 clp_sum_max = 1023;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003026
Mauro Carvalho Chehabf1b82972011-07-10 13:08:44 -03003027 /* AGCInit() not available for DVBT; init done in microcode */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003028 if (!is_qam(state)) {
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03003029 pr_err("%s: mode %d is not DVB-C\n",
3030 __func__, state->m_operation_mode);
Mauro Carvalho Chehabf1b82972011-07-10 13:08:44 -03003031 return -EINVAL;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003032 }
Mauro Carvalho Chehabf1b82972011-07-10 13:08:44 -03003033
3034 /* FIXME: Analog TV AGC require different settings */
3035
3036 /* Standard specific settings */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003037 clp_sum_min = 8;
3038 clp_dir_to = (u16) -9;
3039 clp_ctrl_mode = 0;
3040 sns_sum_min = 8;
3041 sns_dir_to = (u16) -9;
3042 ki_innergain_min = (u16) -1030;
3043 if_iaccu_hi_tgt_max = 0x2380;
3044 if_iaccu_hi_tgt = 0x2380;
3045 ingain_tgt_min = 0x0511;
3046 ingain_tgt = 0x0511;
3047 ingain_tgt_max = 5119;
3048 fast_clp_ctrl_delay = state->m_qam_if_agc_cfg.fast_clip_ctrl_delay;
Mauro Carvalho Chehabf1b82972011-07-10 13:08:44 -03003049
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03003050 status = write16(state, SCU_RAM_AGC_FAST_CLP_CTRL_DELAY__A,
3051 fast_clp_ctrl_delay);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003052 if (status < 0)
3053 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003054
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003055 status = write16(state, SCU_RAM_AGC_CLP_CTRL_MODE__A, clp_ctrl_mode);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003056 if (status < 0)
3057 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003058 status = write16(state, SCU_RAM_AGC_INGAIN_TGT__A, ingain_tgt);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003059 if (status < 0)
3060 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003061 status = write16(state, SCU_RAM_AGC_INGAIN_TGT_MIN__A, ingain_tgt_min);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003062 if (status < 0)
3063 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003064 status = write16(state, SCU_RAM_AGC_INGAIN_TGT_MAX__A, ingain_tgt_max);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003065 if (status < 0)
3066 goto error;
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03003067 status = write16(state, SCU_RAM_AGC_IF_IACCU_HI_TGT_MIN__A,
3068 if_iaccu_hi_tgt_min);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003069 if (status < 0)
3070 goto error;
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03003071 status = write16(state, SCU_RAM_AGC_IF_IACCU_HI_TGT_MAX__A,
3072 if_iaccu_hi_tgt_max);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003073 if (status < 0)
3074 goto error;
3075 status = write16(state, SCU_RAM_AGC_IF_IACCU_HI__A, 0);
3076 if (status < 0)
3077 goto error;
3078 status = write16(state, SCU_RAM_AGC_IF_IACCU_LO__A, 0);
3079 if (status < 0)
3080 goto error;
3081 status = write16(state, SCU_RAM_AGC_RF_IACCU_HI__A, 0);
3082 if (status < 0)
3083 goto error;
3084 status = write16(state, SCU_RAM_AGC_RF_IACCU_LO__A, 0);
3085 if (status < 0)
3086 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003087 status = write16(state, SCU_RAM_AGC_CLP_SUM_MAX__A, clp_sum_max);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003088 if (status < 0)
3089 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003090 status = write16(state, SCU_RAM_AGC_SNS_SUM_MAX__A, sns_sum_max);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003091 if (status < 0)
3092 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003093
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03003094 status = write16(state, SCU_RAM_AGC_KI_INNERGAIN_MIN__A,
3095 ki_innergain_min);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003096 if (status < 0)
3097 goto error;
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03003098 status = write16(state, SCU_RAM_AGC_IF_IACCU_HI_TGT__A,
3099 if_iaccu_hi_tgt);
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_CLP_CYCLEN__A, clp_cyclen);
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 Chehabbe44eb22011-07-10 01:49:53 -03003106 status = write16(state, SCU_RAM_AGC_RF_SNS_DEV_MAX__A, 1023);
3107 if (status < 0)
3108 goto error;
3109 status = write16(state, SCU_RAM_AGC_RF_SNS_DEV_MIN__A, (u16) -1023);
3110 if (status < 0)
3111 goto error;
3112 status = write16(state, SCU_RAM_AGC_FAST_SNS_CTRL_DELAY__A, 50);
3113 if (status < 0)
3114 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003115
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003116 status = write16(state, SCU_RAM_AGC_KI_MAXMINGAIN_TH__A, 20);
3117 if (status < 0)
3118 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003119 status = write16(state, SCU_RAM_AGC_CLP_SUM_MIN__A, clp_sum_min);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003120 if (status < 0)
3121 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003122 status = write16(state, SCU_RAM_AGC_SNS_SUM_MIN__A, sns_sum_min);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003123 if (status < 0)
3124 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003125 status = write16(state, SCU_RAM_AGC_CLP_DIR_TO__A, clp_dir_to);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003126 if (status < 0)
3127 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003128 status = write16(state, SCU_RAM_AGC_SNS_DIR_TO__A, sns_dir_to);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003129 if (status < 0)
3130 goto error;
3131 status = write16(state, SCU_RAM_AGC_KI_MINGAIN__A, 0x7fff);
3132 if (status < 0)
3133 goto error;
3134 status = write16(state, SCU_RAM_AGC_KI_MAXGAIN__A, 0x0);
3135 if (status < 0)
3136 goto error;
3137 status = write16(state, SCU_RAM_AGC_KI_MIN__A, 0x0117);
3138 if (status < 0)
3139 goto error;
3140 status = write16(state, SCU_RAM_AGC_KI_MAX__A, 0x0657);
3141 if (status < 0)
3142 goto error;
3143 status = write16(state, SCU_RAM_AGC_CLP_SUM__A, 0);
3144 if (status < 0)
3145 goto error;
3146 status = write16(state, SCU_RAM_AGC_CLP_CYCCNT__A, 0);
3147 if (status < 0)
3148 goto error;
3149 status = write16(state, SCU_RAM_AGC_CLP_DIR_WD__A, 0);
3150 if (status < 0)
3151 goto error;
3152 status = write16(state, SCU_RAM_AGC_CLP_DIR_STP__A, 1);
3153 if (status < 0)
3154 goto error;
3155 status = write16(state, SCU_RAM_AGC_SNS_SUM__A, 0);
3156 if (status < 0)
3157 goto error;
3158 status = write16(state, SCU_RAM_AGC_SNS_CYCCNT__A, 0);
3159 if (status < 0)
3160 goto error;
3161 status = write16(state, SCU_RAM_AGC_SNS_DIR_WD__A, 0);
3162 if (status < 0)
3163 goto error;
3164 status = write16(state, SCU_RAM_AGC_SNS_DIR_STP__A, 1);
3165 if (status < 0)
3166 goto error;
3167 status = write16(state, SCU_RAM_AGC_SNS_CYCLEN__A, 500);
3168 if (status < 0)
3169 goto error;
3170 status = write16(state, SCU_RAM_AGC_KI_CYCLEN__A, 500);
3171 if (status < 0)
3172 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003173
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003174 /* Initialize inner-loop KI gain factors */
3175 status = read16(state, SCU_RAM_AGC_KI__A, &data);
3176 if (status < 0)
3177 goto error;
Mauro Carvalho Chehabf1b82972011-07-10 13:08:44 -03003178
3179 data = 0x0657;
3180 data &= ~SCU_RAM_AGC_KI_RF__M;
3181 data |= (DRXK_KI_RAGC_QAM << SCU_RAM_AGC_KI_RF__B);
3182 data &= ~SCU_RAM_AGC_KI_IF__M;
3183 data |= (DRXK_KI_IAGC_QAM << SCU_RAM_AGC_KI_IF__B);
3184
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003185 status = write16(state, SCU_RAM_AGC_KI__A, data);
3186error:
3187 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03003188 pr_err("Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003189 return status;
3190}
3191
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003192static int dvbtqam_get_acc_pkt_err(struct drxk_state *state, u16 *packet_err)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003193{
3194 int status;
3195
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03003196 dprintk(1, "\n");
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003197 if (packet_err == NULL)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003198 status = write16(state, SCU_RAM_FEC_ACCUM_PKT_FAILURES__A, 0);
3199 else
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03003200 status = read16(state, SCU_RAM_FEC_ACCUM_PKT_FAILURES__A,
3201 packet_err);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003202 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03003203 pr_err("Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003204 return status;
3205}
3206
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003207static int dvbt_sc_command(struct drxk_state *state,
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003208 u16 cmd, u16 subcmd,
3209 u16 param0, u16 param1, u16 param2,
3210 u16 param3, u16 param4)
3211{
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003212 u16 cur_cmd = 0;
3213 u16 err_code = 0;
3214 u16 retry_cnt = 0;
3215 u16 sc_exec = 0;
Oliver Endrissebc7de22011-07-03 13:49:44 -03003216 int status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003217
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03003218 dprintk(1, "\n");
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003219 status = read16(state, OFDM_SC_COMM_EXEC__A, &sc_exec);
3220 if (sc_exec != 1) {
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003221 /* SC is not running */
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003222 status = -EINVAL;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003223 }
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003224 if (status < 0)
3225 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003226
3227 /* Wait until sc is ready to receive command */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003228 retry_cnt = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003229 do {
Mauro Carvalho Chehabb72852b2013-04-28 11:47:47 -03003230 usleep_range(1000, 2000);
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003231 status = read16(state, OFDM_SC_RA_RAM_CMD__A, &cur_cmd);
3232 retry_cnt++;
3233 } while ((cur_cmd != 0) && (retry_cnt < DRXK_MAX_RETRIES));
3234 if (retry_cnt >= DRXK_MAX_RETRIES && (status < 0))
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003235 goto error;
3236
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003237 /* Write sub-command */
3238 switch (cmd) {
3239 /* All commands using sub-cmd */
3240 case OFDM_SC_RA_RAM_CMD_PROC_START:
3241 case OFDM_SC_RA_RAM_CMD_SET_PREF_PARAM:
3242 case OFDM_SC_RA_RAM_CMD_PROGRAM_PARAM:
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003243 status = write16(state, OFDM_SC_RA_RAM_CMD_ADDR__A, subcmd);
3244 if (status < 0)
3245 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003246 break;
3247 default:
3248 /* Do nothing */
3249 break;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003250 }
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003251
3252 /* Write needed parameters and the command */
Mauro Carvalho Chehab2f60f132015-06-05 07:58:52 -03003253 status = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003254 switch (cmd) {
3255 /* All commands using 5 parameters */
3256 /* All commands using 4 parameters */
3257 /* All commands using 3 parameters */
3258 /* All commands using 2 parameters */
3259 case OFDM_SC_RA_RAM_CMD_PROC_START:
3260 case OFDM_SC_RA_RAM_CMD_SET_PREF_PARAM:
3261 case OFDM_SC_RA_RAM_CMD_PROGRAM_PARAM:
Mauro Carvalho Chehab2f60f132015-06-05 07:58:52 -03003262 status |= write16(state, OFDM_SC_RA_RAM_PARAM1__A, param1);
Mauro Carvalho Chehab40e43112018-08-07 07:59:20 -04003263 /* fall through - All commands using 1 parameters */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003264 case OFDM_SC_RA_RAM_CMD_SET_ECHO_TIMING:
3265 case OFDM_SC_RA_RAM_CMD_USER_IO:
Mauro Carvalho Chehab2f60f132015-06-05 07:58:52 -03003266 status |= write16(state, OFDM_SC_RA_RAM_PARAM0__A, param0);
Mauro Carvalho Chehab40e43112018-08-07 07:59:20 -04003267 /* fall through - All commands using 0 parameters */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003268 case OFDM_SC_RA_RAM_CMD_GET_OP_PARAM:
3269 case OFDM_SC_RA_RAM_CMD_NULL:
3270 /* Write command */
Mauro Carvalho Chehab2f60f132015-06-05 07:58:52 -03003271 status |= write16(state, OFDM_SC_RA_RAM_CMD__A, cmd);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003272 break;
3273 default:
3274 /* Unknown command */
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003275 status = -EINVAL;
3276 }
3277 if (status < 0)
3278 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003279
3280 /* Wait until sc is ready processing command */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003281 retry_cnt = 0;
Oliver Endrissebc7de22011-07-03 13:49:44 -03003282 do {
Mauro Carvalho Chehabb72852b2013-04-28 11:47:47 -03003283 usleep_range(1000, 2000);
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003284 status = read16(state, OFDM_SC_RA_RAM_CMD__A, &cur_cmd);
3285 retry_cnt++;
3286 } while ((cur_cmd != 0) && (retry_cnt < DRXK_MAX_RETRIES));
3287 if (retry_cnt >= DRXK_MAX_RETRIES && (status < 0))
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003288 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003289
3290 /* Check for illegal cmd */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003291 status = read16(state, OFDM_SC_RA_RAM_CMD_ADDR__A, &err_code);
3292 if (err_code == 0xFFFF) {
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003293 /* illegal command */
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003294 status = -EINVAL;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003295 }
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003296 if (status < 0)
3297 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003298
Jonathan McCrohan39c1cb22013-10-20 21:34:01 -03003299 /* Retrieve results parameters from SC */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003300 switch (cmd) {
3301 /* All commands yielding 5 results */
3302 /* All commands yielding 4 results */
3303 /* All commands yielding 3 results */
3304 /* All commands yielding 2 results */
3305 /* All commands yielding 1 result */
3306 case OFDM_SC_RA_RAM_CMD_USER_IO:
3307 case OFDM_SC_RA_RAM_CMD_GET_OP_PARAM:
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003308 status = read16(state, OFDM_SC_RA_RAM_PARAM0__A, &(param0));
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003309 /* All commands yielding 0 results */
3310 case OFDM_SC_RA_RAM_CMD_SET_ECHO_TIMING:
3311 case OFDM_SC_RA_RAM_CMD_SET_TIMER:
3312 case OFDM_SC_RA_RAM_CMD_PROC_START:
3313 case OFDM_SC_RA_RAM_CMD_SET_PREF_PARAM:
3314 case OFDM_SC_RA_RAM_CMD_PROGRAM_PARAM:
3315 case OFDM_SC_RA_RAM_CMD_NULL:
3316 break;
3317 default:
3318 /* Unknown command */
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003319 status = -EINVAL;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003320 break;
Oliver Endrissebc7de22011-07-03 13:49:44 -03003321 } /* switch (cmd->cmd) */
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003322error:
3323 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03003324 pr_err("Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003325 return status;
3326}
3327
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003328static int power_up_dvbt(struct drxk_state *state)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003329{
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003330 enum drx_power_mode power_mode = DRX_POWER_UP;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003331 int status;
3332
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03003333 dprintk(1, "\n");
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003334 status = ctrl_power_mode(state, &power_mode);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003335 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 dvbt_ctrl_set_inc_enable(struct drxk_state *state, bool *enabled)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003341{
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003342 int status;
Oliver Endrissebc7de22011-07-03 13:49:44 -03003343
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03003344 dprintk(1, "\n");
Mauro Carvalho Chehab5a7f7b72014-09-03 15:23:57 -03003345 if (*enabled)
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -03003346 status = write16(state, IQM_CF_BYPASSDET__A, 0);
Oliver Endrissebc7de22011-07-03 13:49:44 -03003347 else
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -03003348 status = write16(state, IQM_CF_BYPASSDET__A, 1);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003349 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03003350 pr_err("Error %d on %s\n", status, __func__);
Oliver Endrissebc7de22011-07-03 13:49:44 -03003351 return status;
3352}
3353
3354#define DEFAULT_FR_THRES_8K 4000
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003355static int dvbt_ctrl_set_fr_enable(struct drxk_state *state, bool *enabled)
Oliver Endrissebc7de22011-07-03 13:49:44 -03003356{
3357
3358 int status;
3359
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03003360 dprintk(1, "\n");
Mauro Carvalho Chehab5a7f7b72014-09-03 15:23:57 -03003361 if (*enabled) {
Oliver Endrissebc7de22011-07-03 13:49:44 -03003362 /* write mask to 1 */
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -03003363 status = write16(state, OFDM_SC_RA_RAM_FR_THRES_8K__A,
Oliver Endrissebc7de22011-07-03 13:49:44 -03003364 DEFAULT_FR_THRES_8K);
3365 } else {
3366 /* write mask to 0 */
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -03003367 status = write16(state, OFDM_SC_RA_RAM_FR_THRES_8K__A, 0);
Oliver Endrissebc7de22011-07-03 13:49:44 -03003368 }
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003369 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03003370 pr_err("Error %d on %s\n", status, __func__);
Oliver Endrissebc7de22011-07-03 13:49:44 -03003371
3372 return status;
3373}
3374
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003375static int dvbt_ctrl_set_echo_threshold(struct drxk_state *state,
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03003376 struct drxk_cfg_dvbt_echo_thres_t *echo_thres)
Oliver Endrissebc7de22011-07-03 13:49:44 -03003377{
3378 u16 data = 0;
3379 int status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003380
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03003381 dprintk(1, "\n");
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003382 status = read16(state, OFDM_SC_RA_RAM_ECHO_THRES__A, &data);
3383 if (status < 0)
3384 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003385
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003386 switch (echo_thres->fft_mode) {
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003387 case DRX_FFTMODE_2K:
3388 data &= ~OFDM_SC_RA_RAM_ECHO_THRES_2K__M;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003389 data |= ((echo_thres->threshold <<
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003390 OFDM_SC_RA_RAM_ECHO_THRES_2K__B)
3391 & (OFDM_SC_RA_RAM_ECHO_THRES_2K__M));
Mauro Carvalho Chehab320ed232011-07-15 01:14:17 -03003392 break;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003393 case DRX_FFTMODE_8K:
3394 data &= ~OFDM_SC_RA_RAM_ECHO_THRES_8K__M;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003395 data |= ((echo_thres->threshold <<
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003396 OFDM_SC_RA_RAM_ECHO_THRES_8K__B)
3397 & (OFDM_SC_RA_RAM_ECHO_THRES_8K__M));
Mauro Carvalho Chehab320ed232011-07-15 01:14:17 -03003398 break;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003399 default:
3400 return -EINVAL;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003401 }
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003402
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003403 status = write16(state, OFDM_SC_RA_RAM_ECHO_THRES__A, data);
3404error:
3405 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03003406 pr_err("Error %d on %s\n", status, __func__);
Oliver Endrissebc7de22011-07-03 13:49:44 -03003407 return status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003408}
3409
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003410static int dvbt_ctrl_set_sqi_speed(struct drxk_state *state,
3411 enum drxk_cfg_dvbt_sqi_speed *speed)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003412{
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003413 int status = -EINVAL;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003414
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03003415 dprintk(1, "\n");
3416
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003417 switch (*speed) {
3418 case DRXK_DVBT_SQI_SPEED_FAST:
3419 case DRXK_DVBT_SQI_SPEED_MEDIUM:
3420 case DRXK_DVBT_SQI_SPEED_SLOW:
3421 break;
3422 default:
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003423 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003424 }
Mauro Carvalho Chehab5e66b872011-07-09 09:50:21 -03003425 status = write16(state, SCU_RAM_FEC_PRE_RS_BER_FILTER_SH__A,
Oliver Endrissebc7de22011-07-03 13:49:44 -03003426 (u16) *speed);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003427error:
3428 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03003429 pr_err("Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003430 return status;
3431}
3432
3433/*============================================================================*/
3434
Mauro Carvalho Chehab34eb9752017-11-27 10:10:28 -05003435/*
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003436* \brief Activate DVBT specific presets
3437* \param demod instance of demodulator.
3438* \return DRXStatus_t.
3439*
3440* Called in DVBTSetStandard
3441*
3442*/
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003443static int dvbt_activate_presets(struct drxk_state *state)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003444{
Oliver Endrissebc7de22011-07-03 13:49:44 -03003445 int status;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003446 bool setincenable = false;
3447 bool setfrenable = true;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003448
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003449 struct drxk_cfg_dvbt_echo_thres_t echo_thres2k = { 0, DRX_FFTMODE_2K };
3450 struct drxk_cfg_dvbt_echo_thres_t echo_thres8k = { 0, DRX_FFTMODE_8K };
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003451
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03003452 dprintk(1, "\n");
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003453 status = dvbt_ctrl_set_inc_enable(state, &setincenable);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003454 if (status < 0)
3455 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003456 status = dvbt_ctrl_set_fr_enable(state, &setfrenable);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003457 if (status < 0)
3458 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003459 status = dvbt_ctrl_set_echo_threshold(state, &echo_thres2k);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003460 if (status < 0)
3461 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003462 status = dvbt_ctrl_set_echo_threshold(state, &echo_thres8k);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003463 if (status < 0)
3464 goto error;
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03003465 status = write16(state, SCU_RAM_AGC_INGAIN_TGT_MAX__A,
3466 state->m_dvbt_if_agc_cfg.ingain_tgt_max);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003467error:
3468 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03003469 pr_err("Error %d on %s\n", status, __func__);
Oliver Endrissebc7de22011-07-03 13:49:44 -03003470 return status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003471}
Oliver Endrissebc7de22011-07-03 13:49:44 -03003472
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003473/*============================================================================*/
3474
Mauro Carvalho Chehab34eb9752017-11-27 10:10:28 -05003475/*
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003476* \brief Initialize channelswitch-independent settings for DVBT.
3477* \param demod instance of demodulator.
3478* \return DRXStatus_t.
3479*
3480* For ROM code channel filter taps are loaded from the bootloader. For microcode
3481* the DVB-T taps from the drxk_filters.h are used.
3482*/
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003483static int set_dvbt_standard(struct drxk_state *state,
3484 enum operation_mode o_mode)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003485{
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003486 u16 cmd_result = 0;
Oliver Endrissebc7de22011-07-03 13:49:44 -03003487 u16 data = 0;
3488 int status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003489
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03003490 dprintk(1, "\n");
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003491
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003492 power_up_dvbt(state);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003493 /* added antenna switch */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003494 switch_antenna_to_dvbt(state);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003495 /* send OFDM reset command */
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03003496 status = scu_command(state,
3497 SCU_RAM_COMMAND_STANDARD_OFDM
3498 | SCU_RAM_COMMAND_CMD_DEMOD_RESET,
3499 0, NULL, 1, &cmd_result);
Oliver Endrissebc7de22011-07-03 13:49:44 -03003500 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003501 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003502
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003503 /* send OFDM setenv command */
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03003504 status = scu_command(state, SCU_RAM_COMMAND_STANDARD_OFDM
3505 | SCU_RAM_COMMAND_CMD_DEMOD_SET_ENV,
3506 0, NULL, 1, &cmd_result);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003507 if (status < 0)
3508 goto error;
3509
3510 /* reset datapath for OFDM, processors first */
3511 status = write16(state, OFDM_SC_COMM_EXEC__A, OFDM_SC_COMM_EXEC_STOP);
3512 if (status < 0)
3513 goto error;
3514 status = write16(state, OFDM_LC_COMM_EXEC__A, OFDM_LC_COMM_EXEC_STOP);
3515 if (status < 0)
3516 goto error;
3517 status = write16(state, IQM_COMM_EXEC__A, IQM_COMM_EXEC_B_STOP);
3518 if (status < 0)
3519 goto error;
3520
3521 /* IQM setup */
3522 /* synchronize on ofdstate->m_festart */
3523 status = write16(state, IQM_AF_UPD_SEL__A, 1);
3524 if (status < 0)
3525 goto error;
3526 /* window size for clipping ADC detection */
3527 status = write16(state, IQM_AF_CLP_LEN__A, 0);
3528 if (status < 0)
3529 goto error;
3530 /* window size for for sense pre-SAW detection */
3531 status = write16(state, IQM_AF_SNS_LEN__A, 0);
3532 if (status < 0)
3533 goto error;
3534 /* sense threshold for sense pre-SAW detection */
3535 status = write16(state, IQM_AF_AMUX__A, IQM_AF_AMUX_SIGNAL2ADC);
3536 if (status < 0)
3537 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003538 status = set_iqm_af(state, true);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003539 if (status < 0)
3540 goto error;
3541
3542 status = write16(state, IQM_AF_AGC_RF__A, 0);
3543 if (status < 0)
3544 goto error;
3545
3546 /* Impulse noise cruncher setup */
3547 status = write16(state, IQM_AF_INC_LCT__A, 0); /* crunch in IQM_CF */
3548 if (status < 0)
3549 goto error;
3550 status = write16(state, IQM_CF_DET_LCT__A, 0); /* detect in IQM_CF */
3551 if (status < 0)
3552 goto error;
3553 status = write16(state, IQM_CF_WND_LEN__A, 3); /* peak detector window length */
3554 if (status < 0)
3555 goto error;
3556
3557 status = write16(state, IQM_RC_STRETCH__A, 16);
3558 if (status < 0)
3559 goto error;
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03003560 status = write16(state, IQM_CF_OUT_ENA__A, 0x4); /* enable output 2 */
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003561 if (status < 0)
3562 goto error;
3563 status = write16(state, IQM_CF_DS_ENA__A, 0x4); /* decimate output 2 */
3564 if (status < 0)
3565 goto error;
3566 status = write16(state, IQM_CF_SCALE__A, 1600);
3567 if (status < 0)
3568 goto error;
3569 status = write16(state, IQM_CF_SCALE_SH__A, 0);
3570 if (status < 0)
3571 goto error;
3572
3573 /* virtual clipping threshold for clipping ADC detection */
3574 status = write16(state, IQM_AF_CLP_TH__A, 448);
3575 if (status < 0)
3576 goto error;
3577 status = write16(state, IQM_CF_DATATH__A, 495); /* crunching threshold */
3578 if (status < 0)
3579 goto error;
3580
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03003581 status = bl_chain_cmd(state, DRXK_BL_ROM_OFFSET_TAPS_DVBT,
3582 DRXK_BLCC_NR_ELEMENTS_TAPS, DRXK_BLC_TIMEOUT);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003583 if (status < 0)
3584 goto error;
3585
3586 status = write16(state, IQM_CF_PKDTH__A, 2); /* peak detector threshold */
3587 if (status < 0)
3588 goto error;
3589 status = write16(state, IQM_CF_POW_MEAS_LEN__A, 2);
3590 if (status < 0)
3591 goto error;
3592 /* enable power measurement interrupt */
3593 status = write16(state, IQM_CF_COMM_INT_MSK__A, 1);
3594 if (status < 0)
3595 goto error;
3596 status = write16(state, IQM_COMM_EXEC__A, IQM_COMM_EXEC_B_ACTIVE);
3597 if (status < 0)
3598 goto error;
3599
3600 /* IQM will not be reset from here, sync ADC and update/init AGC */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003601 status = adc_synchronization(state);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003602 if (status < 0)
3603 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003604 status = set_pre_saw(state, &state->m_dvbt_pre_saw_cfg);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003605 if (status < 0)
3606 goto error;
3607
3608 /* Halt SCU to enable safe non-atomic accesses */
3609 status = write16(state, SCU_COMM_EXEC__A, SCU_COMM_EXEC_HOLD);
3610 if (status < 0)
3611 goto error;
3612
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003613 status = set_agc_rf(state, &state->m_dvbt_rf_agc_cfg, true);
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_agc_if(state, &state->m_dvbt_if_agc_cfg, true);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003617 if (status < 0)
3618 goto error;
3619
3620 /* Set Noise Estimation notch width and enable DC fix */
3621 status = read16(state, OFDM_SC_RA_RAM_CONFIG__A, &data);
3622 if (status < 0)
3623 goto error;
3624 data |= OFDM_SC_RA_RAM_CONFIG_NE_FIX_ENABLE__M;
3625 status = write16(state, OFDM_SC_RA_RAM_CONFIG__A, data);
3626 if (status < 0)
3627 goto error;
3628
3629 /* Activate SCU to enable SCU commands */
3630 status = write16(state, SCU_COMM_EXEC__A, SCU_COMM_EXEC_ACTIVE);
3631 if (status < 0)
3632 goto error;
3633
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003634 if (!state->m_drxk_a3_rom_code) {
3635 /* AGCInit() is not done for DVBT, so set agcfast_clip_ctrl_delay */
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03003636 status = write16(state, SCU_RAM_AGC_FAST_CLP_CTRL_DELAY__A,
3637 state->m_dvbt_if_agc_cfg.fast_clip_ctrl_delay);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003638 if (status < 0)
3639 goto error;
3640 }
3641
3642 /* OFDM_SC setup */
3643#ifdef COMPILE_FOR_NONRT
3644 status = write16(state, OFDM_SC_RA_RAM_BE_OPT_DELAY__A, 1);
3645 if (status < 0)
3646 goto error;
3647 status = write16(state, OFDM_SC_RA_RAM_BE_OPT_INIT_DELAY__A, 2);
3648 if (status < 0)
3649 goto error;
3650#endif
3651
3652 /* FEC setup */
3653 status = write16(state, FEC_DI_INPUT_CTL__A, 1); /* OFDM input */
3654 if (status < 0)
3655 goto error;
3656
3657
3658#ifdef COMPILE_FOR_NONRT
3659 status = write16(state, FEC_RS_MEASUREMENT_PERIOD__A, 0x400);
3660 if (status < 0)
3661 goto error;
3662#else
3663 status = write16(state, FEC_RS_MEASUREMENT_PERIOD__A, 0x1000);
3664 if (status < 0)
3665 goto error;
3666#endif
3667 status = write16(state, FEC_RS_MEASUREMENT_PRESCALE__A, 0x0001);
3668 if (status < 0)
3669 goto error;
3670
3671 /* Setup MPEG bus */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003672 status = mpegts_dto_setup(state, OM_DVBT);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003673 if (status < 0)
3674 goto error;
3675 /* Set DVBT Presets */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003676 status = dvbt_activate_presets(state);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003677 if (status < 0)
3678 goto error;
3679
3680error:
3681 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03003682 pr_err("Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003683 return status;
3684}
3685
3686/*============================================================================*/
Mauro Carvalho Chehab34eb9752017-11-27 10:10:28 -05003687/*
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003688* \brief start dvbt demodulating for channel.
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003689* \param demod instance of demodulator.
3690* \return DRXStatus_t.
3691*/
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003692static int dvbt_start(struct drxk_state *state)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003693{
Oliver Endrissebc7de22011-07-03 13:49:44 -03003694 u16 param1;
3695 int status;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003696 /* drxk_ofdm_sc_cmd_t scCmd; */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003697
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03003698 dprintk(1, "\n");
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003699 /* start correct processes to get in lock */
Oliver Endrissebc7de22011-07-03 13:49:44 -03003700 /* DRXK: OFDM_SC_RA_RAM_PROC_LOCKTRACK is no longer in mapfile! */
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003701 param1 = OFDM_SC_RA_RAM_LOCKTRACK_MIN;
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03003702 status = dvbt_sc_command(state, OFDM_SC_RA_RAM_CMD_PROC_START, 0,
3703 OFDM_SC_RA_RAM_SW_EVENT_RUN_NMASK__M, param1,
3704 0, 0, 0);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003705 if (status < 0)
3706 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003707 /* start FEC OC */
3708 status = mpegts_start(state);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003709 if (status < 0)
3710 goto error;
3711 status = write16(state, FEC_COMM_EXEC__A, FEC_COMM_EXEC_ACTIVE);
3712 if (status < 0)
3713 goto error;
3714error:
3715 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03003716 pr_err("Error %d on %s\n", status, __func__);
Oliver Endrissebc7de22011-07-03 13:49:44 -03003717 return status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003718}
3719
3720
3721/*============================================================================*/
3722
Mauro Carvalho Chehab34eb9752017-11-27 10:10:28 -05003723/*
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003724* \brief Set up dvbt demodulator for channel.
3725* \param demod instance of demodulator.
3726* \return DRXStatus_t.
3727* // original DVBTSetChannel()
3728*/
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003729static int set_dvbt(struct drxk_state *state, u16 intermediate_freqk_hz,
3730 s32 tuner_freq_offset)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003731{
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003732 u16 cmd_result = 0;
3733 u16 transmission_params = 0;
3734 u16 operation_mode = 0;
3735 u32 iqm_rc_rate_ofs = 0;
Oliver Endrissebc7de22011-07-03 13:49:44 -03003736 u32 bandwidth = 0;
3737 u16 param1;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003738 int status;
3739
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03003740 dprintk(1, "IF =%d, TFO = %d\n",
3741 intermediate_freqk_hz, tuner_freq_offset);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003742
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03003743 status = scu_command(state, SCU_RAM_COMMAND_STANDARD_OFDM
3744 | SCU_RAM_COMMAND_CMD_DEMOD_STOP,
3745 0, NULL, 1, &cmd_result);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003746 if (status < 0)
3747 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003748
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003749 /* Halt SCU to enable safe non-atomic accesses */
3750 status = write16(state, SCU_COMM_EXEC__A, SCU_COMM_EXEC_HOLD);
3751 if (status < 0)
3752 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003753
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003754 /* Stop processors */
3755 status = write16(state, OFDM_SC_COMM_EXEC__A, OFDM_SC_COMM_EXEC_STOP);
3756 if (status < 0)
3757 goto error;
3758 status = write16(state, OFDM_LC_COMM_EXEC__A, OFDM_LC_COMM_EXEC_STOP);
3759 if (status < 0)
3760 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003761
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003762 /* Mandatory fix, always stop CP, required to set spl offset back to
3763 hardware default (is set to 0 by ucode during pilot detection */
3764 status = write16(state, OFDM_CP_COMM_EXEC__A, OFDM_CP_COMM_EXEC_STOP);
3765 if (status < 0)
3766 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003767
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03003768 /*== Write channel settings to device ================================*/
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003769
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003770 /* mode */
Mauro Carvalho Chehabed5452a2011-12-26 09:57:11 -03003771 switch (state->props.transmission_mode) {
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003772 case TRANSMISSION_MODE_AUTO:
3773 default:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003774 operation_mode |= OFDM_SC_RA_RAM_OP_AUTO_MODE__M;
Mauro Carvalho Chehab40e43112018-08-07 07:59:20 -04003775 /* fall through - try first guess DRX_FFTMODE_8K */
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003776 case TRANSMISSION_MODE_8K:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003777 transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_MODE_8K;
Mauro Carvalho Chehab320ed232011-07-15 01:14:17 -03003778 break;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003779 case TRANSMISSION_MODE_2K:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003780 transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_MODE_2K;
Mauro Carvalho Chehab320ed232011-07-15 01:14:17 -03003781 break;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003782 }
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003783
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003784 /* guard */
Mauro Carvalho Chehabed5452a2011-12-26 09:57:11 -03003785 switch (state->props.guard_interval) {
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003786 default:
3787 case GUARD_INTERVAL_AUTO:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003788 operation_mode |= OFDM_SC_RA_RAM_OP_AUTO_GUARD__M;
Mauro Carvalho Chehab40e43112018-08-07 07:59:20 -04003789 /* fall through - try first guess DRX_GUARD_1DIV4 */
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003790 case GUARD_INTERVAL_1_4:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003791 transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_GUARD_4;
Mauro Carvalho Chehab320ed232011-07-15 01:14:17 -03003792 break;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003793 case GUARD_INTERVAL_1_32:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003794 transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_GUARD_32;
Mauro Carvalho Chehab320ed232011-07-15 01:14:17 -03003795 break;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003796 case GUARD_INTERVAL_1_16:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003797 transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_GUARD_16;
Mauro Carvalho Chehab320ed232011-07-15 01:14:17 -03003798 break;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003799 case GUARD_INTERVAL_1_8:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003800 transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_GUARD_8;
Mauro Carvalho Chehab320ed232011-07-15 01:14:17 -03003801 break;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003802 }
3803
3804 /* hierarchy */
Mauro Carvalho Chehabed5452a2011-12-26 09:57:11 -03003805 switch (state->props.hierarchy) {
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003806 case HIERARCHY_AUTO:
3807 case HIERARCHY_NONE:
3808 default:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003809 operation_mode |= OFDM_SC_RA_RAM_OP_AUTO_HIER__M;
Mauro Carvalho Chehab06eeefe2017-05-18 08:13:28 -03003810 /* try first guess SC_RA_RAM_OP_PARAM_HIER_NO */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003811 /* transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_HIER_NO; */
Mauro Carvalho Chehab06eeefe2017-05-18 08:13:28 -03003812 /* fall through */
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003813 case HIERARCHY_1:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003814 transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_HIER_A1;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003815 break;
3816 case HIERARCHY_2:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003817 transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_HIER_A2;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003818 break;
3819 case HIERARCHY_4:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003820 transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_HIER_A4;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003821 break;
3822 }
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003823
3824
Mauro Carvalho Chehabed5452a2011-12-26 09:57:11 -03003825 /* modulation */
3826 switch (state->props.modulation) {
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003827 case QAM_AUTO:
3828 default:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003829 operation_mode |= OFDM_SC_RA_RAM_OP_AUTO_CONST__M;
Mauro Carvalho Chehab40e43112018-08-07 07:59:20 -04003830 /* fall through - try first guess DRX_CONSTELLATION_QAM64 */
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003831 case QAM_64:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003832 transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_CONST_QAM64;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003833 break;
3834 case QPSK:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003835 transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_CONST_QPSK;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003836 break;
3837 case QAM_16:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003838 transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_CONST_QAM16;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003839 break;
3840 }
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003841#if 0
Jonathan McCrohan39c1cb22013-10-20 21:34:01 -03003842 /* No hierarchical channels support in BDA */
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003843 /* Priority (only for hierarchical channels) */
3844 switch (channel->priority) {
3845 case DRX_PRIORITY_LOW:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003846 transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_PRIO_LO;
3847 WR16(dev_addr, OFDM_EC_SB_PRIOR__A,
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003848 OFDM_EC_SB_PRIOR_LO);
3849 break;
3850 case DRX_PRIORITY_HIGH:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003851 transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_PRIO_HI;
3852 WR16(dev_addr, OFDM_EC_SB_PRIOR__A,
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003853 OFDM_EC_SB_PRIOR_HI));
3854 break;
3855 case DRX_PRIORITY_UNKNOWN: /* fall through */
3856 default:
3857 status = -EINVAL;
3858 goto error;
3859 }
3860#else
Mauro Carvalho Chehab868c9a12019-02-18 14:28:55 -05003861 /* Set Priority high */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003862 transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_PRIO_HI;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003863 status = write16(state, OFDM_EC_SB_PRIOR__A, OFDM_EC_SB_PRIOR_HI);
3864 if (status < 0)
3865 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003866#endif
3867
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003868 /* coderate */
Mauro Carvalho Chehabed5452a2011-12-26 09:57:11 -03003869 switch (state->props.code_rate_HP) {
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003870 case FEC_AUTO:
3871 default:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003872 operation_mode |= OFDM_SC_RA_RAM_OP_AUTO_RATE__M;
Mauro Carvalho Chehab40e43112018-08-07 07:59:20 -04003873 /* fall through - try first guess DRX_CODERATE_2DIV3 */
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003874 case FEC_2_3:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003875 transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_RATE_2_3;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003876 break;
3877 case FEC_1_2:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003878 transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_RATE_1_2;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003879 break;
3880 case FEC_3_4:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003881 transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_RATE_3_4;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003882 break;
3883 case FEC_5_6:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003884 transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_RATE_5_6;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003885 break;
3886 case FEC_7_8:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003887 transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_RATE_7_8;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003888 break;
3889 }
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003890
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03003891 /*
Mauro Carvalho Chehab868c9a12019-02-18 14:28:55 -05003892 * SAW filter selection: normally not necessary, but if wanted
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03003893 * the application can select a SAW filter via the driver by
3894 * using UIOs
3895 */
3896
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003897 /* First determine real bandwidth (Hz) */
3898 /* Also set delay for impulse noise cruncher */
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03003899 /*
3900 * Also set parameters for EC_OC fix, note EC_OC_REG_TMD_HIL_MAR is
3901 * changed by SC for fix for some 8K,1/8 guard but is restored by
3902 * InitEC and ResetEC functions
3903 */
Mauro Carvalho Chehabed5452a2011-12-26 09:57:11 -03003904 switch (state->props.bandwidth_hz) {
3905 case 0:
3906 state->props.bandwidth_hz = 8000000;
Mauro Carvalho Chehab06eeefe2017-05-18 08:13:28 -03003907 /* fall through */
Mauro Carvalho Chehabed5452a2011-12-26 09:57:11 -03003908 case 8000000:
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003909 bandwidth = DRXK_BANDWIDTH_8MHZ_IN_HZ;
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03003910 status = write16(state, OFDM_SC_RA_RAM_SRMM_FIX_FACT_8K__A,
3911 3052);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03003912 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003913 goto error;
3914 /* cochannel protection for PAL 8 MHz */
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03003915 status = write16(state, OFDM_SC_RA_RAM_NI_INIT_8K_PER_LEFT__A,
3916 7);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003917 if (status < 0)
3918 goto error;
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03003919 status = write16(state, OFDM_SC_RA_RAM_NI_INIT_8K_PER_RIGHT__A,
3920 7);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003921 if (status < 0)
3922 goto error;
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03003923 status = write16(state, OFDM_SC_RA_RAM_NI_INIT_2K_PER_LEFT__A,
3924 7);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003925 if (status < 0)
3926 goto error;
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03003927 status = write16(state, OFDM_SC_RA_RAM_NI_INIT_2K_PER_RIGHT__A,
3928 1);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003929 if (status < 0)
3930 goto error;
3931 break;
Mauro Carvalho Chehabed5452a2011-12-26 09:57:11 -03003932 case 7000000:
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003933 bandwidth = DRXK_BANDWIDTH_7MHZ_IN_HZ;
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03003934 status = write16(state, OFDM_SC_RA_RAM_SRMM_FIX_FACT_8K__A,
3935 3491);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003936 if (status < 0)
3937 goto error;
3938 /* cochannel protection for PAL 7 MHz */
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03003939 status = write16(state, OFDM_SC_RA_RAM_NI_INIT_8K_PER_LEFT__A,
3940 8);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003941 if (status < 0)
3942 goto error;
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03003943 status = write16(state, OFDM_SC_RA_RAM_NI_INIT_8K_PER_RIGHT__A,
3944 8);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003945 if (status < 0)
3946 goto error;
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03003947 status = write16(state, OFDM_SC_RA_RAM_NI_INIT_2K_PER_LEFT__A,
3948 4);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003949 if (status < 0)
3950 goto error;
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03003951 status = write16(state, OFDM_SC_RA_RAM_NI_INIT_2K_PER_RIGHT__A,
3952 1);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003953 if (status < 0)
3954 goto error;
3955 break;
Mauro Carvalho Chehabed5452a2011-12-26 09:57:11 -03003956 case 6000000:
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003957 bandwidth = DRXK_BANDWIDTH_6MHZ_IN_HZ;
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03003958 status = write16(state, OFDM_SC_RA_RAM_SRMM_FIX_FACT_8K__A,
3959 4073);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003960 if (status < 0)
3961 goto error;
3962 /* cochannel protection for NTSC 6 MHz */
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03003963 status = write16(state, OFDM_SC_RA_RAM_NI_INIT_8K_PER_LEFT__A,
3964 19);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003965 if (status < 0)
3966 goto error;
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03003967 status = write16(state, OFDM_SC_RA_RAM_NI_INIT_8K_PER_RIGHT__A,
3968 19);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003969 if (status < 0)
3970 goto error;
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03003971 status = write16(state, OFDM_SC_RA_RAM_NI_INIT_2K_PER_LEFT__A,
3972 14);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003973 if (status < 0)
3974 goto error;
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03003975 status = write16(state, OFDM_SC_RA_RAM_NI_INIT_2K_PER_RIGHT__A,
3976 1);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003977 if (status < 0)
3978 goto error;
3979 break;
3980 default:
3981 status = -EINVAL;
3982 goto error;
3983 }
Ralph Metzler43dd07f2011-07-03 13:42:18 -03003984
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003985 if (iqm_rc_rate_ofs == 0) {
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03003986 /* Now compute IQM_RC_RATE_OFS
3987 (((SysFreq/BandWidth)/2)/2) -1) * 2^23)
3988 =>
3989 ((SysFreq / BandWidth) * (2^21)) - (2^23)
3990 */
3991 /* (SysFreq / BandWidth) * (2^28) */
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03003992 /*
3993 * assert (MAX(sysClk)/MIN(bandwidth) < 16)
3994 * => assert(MAX(sysClk) < 16*MIN(bandwidth))
3995 * => assert(109714272 > 48000000) = true
3996 * so Frac 28 can be used
3997 */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03003998 iqm_rc_rate_ofs = Frac28a((u32)
3999 ((state->m_sys_clock_freq *
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004000 1000) / 3), bandwidth);
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03004001 /* (SysFreq / BandWidth) * (2^21), rounding before truncating */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03004002 if ((iqm_rc_rate_ofs & 0x7fL) >= 0x40)
4003 iqm_rc_rate_ofs += 0x80L;
4004 iqm_rc_rate_ofs = iqm_rc_rate_ofs >> 7;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004005 /* ((SysFreq / BandWidth) * (2^21)) - (2^23) */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03004006 iqm_rc_rate_ofs = iqm_rc_rate_ofs - (1 << 23);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004007 }
4008
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03004009 iqm_rc_rate_ofs &=
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004010 ((((u32) IQM_RC_RATE_OFS_HI__M) <<
4011 IQM_RC_RATE_OFS_LO__W) | IQM_RC_RATE_OFS_LO__M);
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03004012 status = write32(state, IQM_RC_RATE_OFS_LO__A, iqm_rc_rate_ofs);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004013 if (status < 0)
4014 goto error;
4015
4016 /* Bandwidth setting done */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004017
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03004018#if 0
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03004019 status = dvbt_set_frequency_shift(demod, channel, tuner_offset);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004020 if (status < 0)
4021 goto error;
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03004022#endif
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03004023 status = set_frequency_shifter(state, intermediate_freqk_hz,
4024 tuner_freq_offset, true);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004025 if (status < 0)
4026 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004027
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03004028 /*== start SC, write channel settings to SC ==========================*/
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004029
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004030 /* Activate SCU to enable SCU commands */
4031 status = write16(state, SCU_COMM_EXEC__A, SCU_COMM_EXEC_ACTIVE);
4032 if (status < 0)
4033 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004034
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004035 /* Enable SC after setting all other parameters */
4036 status = write16(state, OFDM_SC_COMM_STATE__A, 0);
4037 if (status < 0)
4038 goto error;
4039 status = write16(state, OFDM_SC_COMM_EXEC__A, 1);
4040 if (status < 0)
4041 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004042
4043
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03004044 status = scu_command(state, SCU_RAM_COMMAND_STANDARD_OFDM
4045 | SCU_RAM_COMMAND_CMD_DEMOD_START,
4046 0, NULL, 1, &cmd_result);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004047 if (status < 0)
4048 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004049
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004050 /* Write SC parameter registers, set all AUTO flags in operation mode */
4051 param1 = (OFDM_SC_RA_RAM_OP_AUTO_MODE__M |
4052 OFDM_SC_RA_RAM_OP_AUTO_GUARD__M |
4053 OFDM_SC_RA_RAM_OP_AUTO_CONST__M |
4054 OFDM_SC_RA_RAM_OP_AUTO_HIER__M |
4055 OFDM_SC_RA_RAM_OP_AUTO_RATE__M);
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03004056 status = dvbt_sc_command(state, OFDM_SC_RA_RAM_CMD_SET_PREF_PARAM,
4057 0, transmission_params, param1, 0, 0, 0);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004058 if (status < 0)
4059 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004060
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03004061 if (!state->m_drxk_a3_rom_code)
4062 status = dvbt_ctrl_set_sqi_speed(state, &state->m_sqi_speed);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004063error:
4064 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03004065 pr_err("Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004066
4067 return status;
4068}
4069
4070
4071/*============================================================================*/
4072
Mauro Carvalho Chehab34eb9752017-11-27 10:10:28 -05004073/*
Jonathan McCrohan39c1cb22013-10-20 21:34:01 -03004074* \brief Retrieve lock status .
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004075* \param demod Pointer to demodulator instance.
4076* \param lockStat Pointer to lock status structure.
4077* \return DRXStatus_t.
4078*
4079*/
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03004080static int get_dvbt_lock_status(struct drxk_state *state, u32 *p_lock_status)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004081{
Oliver Endrissebc7de22011-07-03 13:49:44 -03004082 int status;
4083 const u16 mpeg_lock_mask = (OFDM_SC_RA_RAM_LOCK_MPEG__M |
4084 OFDM_SC_RA_RAM_LOCK_FEC__M);
4085 const u16 fec_lock_mask = (OFDM_SC_RA_RAM_LOCK_FEC__M);
4086 const u16 demod_lock_mask = OFDM_SC_RA_RAM_LOCK_DEMOD__M;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004087
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03004088 u16 sc_ra_ram_lock = 0;
4089 u16 sc_comm_exec = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004090
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03004091 dprintk(1, "\n");
4092
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03004093 *p_lock_status = NOT_LOCKED;
Oliver Endrissebc7de22011-07-03 13:49:44 -03004094 /* driver 0.9.0 */
4095 /* Check if SC is running */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03004096 status = read16(state, OFDM_SC_COMM_EXEC__A, &sc_comm_exec);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004097 if (status < 0)
4098 goto end;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03004099 if (sc_comm_exec == OFDM_SC_COMM_EXEC_STOP)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004100 goto end;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004101
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03004102 status = read16(state, OFDM_SC_RA_RAM_LOCK__A, &sc_ra_ram_lock);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004103 if (status < 0)
4104 goto end;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004105
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03004106 if ((sc_ra_ram_lock & mpeg_lock_mask) == mpeg_lock_mask)
4107 *p_lock_status = MPEG_LOCK;
4108 else if ((sc_ra_ram_lock & fec_lock_mask) == fec_lock_mask)
4109 *p_lock_status = FEC_LOCK;
4110 else if ((sc_ra_ram_lock & demod_lock_mask) == demod_lock_mask)
4111 *p_lock_status = DEMOD_LOCK;
4112 else if (sc_ra_ram_lock & OFDM_SC_RA_RAM_LOCK_NODVBT__M)
4113 *p_lock_status = NEVER_LOCK;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004114end:
4115 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03004116 pr_err("Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004117
Oliver Endrissebc7de22011-07-03 13:49:44 -03004118 return status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004119}
4120
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03004121static int power_up_qam(struct drxk_state *state)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004122{
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03004123 enum drx_power_mode power_mode = DRXK_POWER_DOWN_OFDM;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004124 int status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004125
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03004126 dprintk(1, "\n");
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03004127 status = ctrl_power_mode(state, &power_mode);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004128 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03004129 pr_err("Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004130
Oliver Endrissebc7de22011-07-03 13:49:44 -03004131 return status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004132}
4133
4134
Mauro Carvalho Chehab34eb9752017-11-27 10:10:28 -05004135/* Power Down QAM */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03004136static int power_down_qam(struct drxk_state *state)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004137{
Oliver Endrissebc7de22011-07-03 13:49:44 -03004138 u16 data = 0;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03004139 u16 cmd_result;
Oliver Endrissebc7de22011-07-03 13:49:44 -03004140 int status = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004141
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03004142 dprintk(1, "\n");
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004143 status = read16(state, SCU_COMM_EXEC__A, &data);
4144 if (status < 0)
4145 goto error;
4146 if (data == SCU_COMM_EXEC_ACTIVE) {
4147 /*
4148 STOP demodulator
4149 QAM and HW blocks
4150 */
4151 /* stop all comstate->m_exec */
4152 status = write16(state, QAM_COMM_EXEC__A, QAM_COMM_EXEC_STOP);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03004153 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004154 goto error;
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03004155 status = scu_command(state, SCU_RAM_COMMAND_STANDARD_QAM
4156 | SCU_RAM_COMMAND_CMD_DEMOD_STOP,
4157 0, NULL, 1, &cmd_result);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03004158 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004159 goto error;
4160 }
4161 /* powerdown AFE */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03004162 status = set_iqm_af(state, false);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004163
4164error:
4165 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03004166 pr_err("Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004167
Oliver Endrissebc7de22011-07-03 13:49:44 -03004168 return status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004169}
Oliver Endrissebc7de22011-07-03 13:49:44 -03004170
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004171/*============================================================================*/
4172
Mauro Carvalho Chehab34eb9752017-11-27 10:10:28 -05004173/*
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004174* \brief Setup of the QAM Measurement intervals for signal quality
4175* \param demod instance of demod.
Mauro Carvalho Chehabed5452a2011-12-26 09:57:11 -03004176* \param modulation current modulation.
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004177* \return DRXStatus_t.
4178*
4179* NOTE:
4180* Take into account that for certain settings the errorcounters can overflow.
4181* The implementation does not check this.
4182*
4183*/
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03004184static int set_qam_measurement(struct drxk_state *state,
4185 enum e_drxk_constellation modulation,
4186 u32 symbol_rate)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004187{
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03004188 u32 fec_bits_desired = 0; /* BER accounting period */
4189 u32 fec_rs_period_total = 0; /* Total period */
4190 u16 fec_rs_prescale = 0; /* ReedSolomon Measurement Prescale */
4191 u16 fec_rs_period = 0; /* Value for corresponding I2C register */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004192 int status = 0;
4193
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03004194 dprintk(1, "\n");
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004195
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03004196 fec_rs_prescale = 1;
4197 /* fec_bits_desired = symbol_rate [kHz] *
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004198 FrameLenght [ms] *
Mauro Carvalho Chehabed5452a2011-12-26 09:57:11 -03004199 (modulation + 1) *
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004200 SyncLoss (== 1) *
4201 ViterbiLoss (==1)
4202 */
Mauro Carvalho Chehabed5452a2011-12-26 09:57:11 -03004203 switch (modulation) {
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004204 case DRX_CONSTELLATION_QAM16:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03004205 fec_bits_desired = 4 * symbol_rate;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004206 break;
4207 case DRX_CONSTELLATION_QAM32:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03004208 fec_bits_desired = 5 * symbol_rate;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004209 break;
4210 case DRX_CONSTELLATION_QAM64:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03004211 fec_bits_desired = 6 * symbol_rate;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004212 break;
4213 case DRX_CONSTELLATION_QAM128:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03004214 fec_bits_desired = 7 * symbol_rate;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004215 break;
4216 case DRX_CONSTELLATION_QAM256:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03004217 fec_bits_desired = 8 * symbol_rate;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004218 break;
4219 default:
4220 status = -EINVAL;
4221 }
Oliver Endrissebc7de22011-07-03 13:49:44 -03004222 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004223 goto error;
Oliver Endrissebc7de22011-07-03 13:49:44 -03004224
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03004225 fec_bits_desired /= 1000; /* symbol_rate [Hz] -> symbol_rate [kHz] */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03004226 fec_bits_desired *= 500; /* meas. period [ms] */
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004227
4228 /* Annex A/C: bits/RsPeriod = 204 * 8 = 1632 */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03004229 /* fec_rs_period_total = fec_bits_desired / 1632 */
4230 fec_rs_period_total = (fec_bits_desired / 1632UL) + 1; /* roughly ceil */
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004231
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03004232 /* fec_rs_period_total = fec_rs_prescale * fec_rs_period */
4233 fec_rs_prescale = 1 + (u16) (fec_rs_period_total >> 16);
4234 if (fec_rs_prescale == 0) {
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004235 /* Divide by zero (though impossible) */
4236 status = -EINVAL;
4237 if (status < 0)
4238 goto error;
4239 }
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03004240 fec_rs_period =
4241 ((u16) fec_rs_period_total +
4242 (fec_rs_prescale >> 1)) / fec_rs_prescale;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004243
4244 /* write corresponding registers */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03004245 status = write16(state, FEC_RS_MEASUREMENT_PERIOD__A, fec_rs_period);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004246 if (status < 0)
4247 goto error;
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03004248 status = write16(state, FEC_RS_MEASUREMENT_PRESCALE__A,
4249 fec_rs_prescale);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004250 if (status < 0)
4251 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03004252 status = write16(state, FEC_OC_SNC_FAIL_PERIOD__A, fec_rs_period);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004253error:
4254 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03004255 pr_err("Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004256 return status;
4257}
4258
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03004259static int set_qam16(struct drxk_state *state)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004260{
Oliver Endrissebc7de22011-07-03 13:49:44 -03004261 int status = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004262
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03004263 dprintk(1, "\n");
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004264 /* QAM Equalizer Setup */
4265 /* Equalizer */
4266 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD0__A, 13517);
4267 if (status < 0)
4268 goto error;
4269 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD1__A, 13517);
4270 if (status < 0)
4271 goto error;
4272 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD2__A, 13517);
4273 if (status < 0)
4274 goto error;
4275 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD3__A, 13517);
4276 if (status < 0)
4277 goto error;
4278 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD4__A, 13517);
4279 if (status < 0)
4280 goto error;
4281 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD5__A, 13517);
4282 if (status < 0)
4283 goto error;
4284 /* Decision Feedback Equalizer */
4285 status = write16(state, QAM_DQ_QUAL_FUN0__A, 2);
4286 if (status < 0)
4287 goto error;
4288 status = write16(state, QAM_DQ_QUAL_FUN1__A, 2);
4289 if (status < 0)
4290 goto error;
4291 status = write16(state, QAM_DQ_QUAL_FUN2__A, 2);
4292 if (status < 0)
4293 goto error;
4294 status = write16(state, QAM_DQ_QUAL_FUN3__A, 2);
4295 if (status < 0)
4296 goto error;
4297 status = write16(state, QAM_DQ_QUAL_FUN4__A, 2);
4298 if (status < 0)
4299 goto error;
4300 status = write16(state, QAM_DQ_QUAL_FUN5__A, 0);
4301 if (status < 0)
4302 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004303
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004304 status = write16(state, QAM_SY_SYNC_HWM__A, 5);
4305 if (status < 0)
4306 goto error;
4307 status = write16(state, QAM_SY_SYNC_AWM__A, 4);
4308 if (status < 0)
4309 goto error;
4310 status = write16(state, QAM_SY_SYNC_LWM__A, 3);
4311 if (status < 0)
4312 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004313
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004314 /* QAM Slicer Settings */
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03004315 status = write16(state, SCU_RAM_QAM_SL_SIG_POWER__A,
4316 DRXK_QAM_SL_SIG_POWER_QAM16);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004317 if (status < 0)
4318 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004319
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004320 /* QAM Loop Controller Coeficients */
4321 status = write16(state, SCU_RAM_QAM_LC_CA_FINE__A, 15);
4322 if (status < 0)
4323 goto error;
4324 status = write16(state, SCU_RAM_QAM_LC_CA_COARSE__A, 40);
4325 if (status < 0)
4326 goto error;
4327 status = write16(state, SCU_RAM_QAM_LC_EP_FINE__A, 12);
4328 if (status < 0)
4329 goto error;
4330 status = write16(state, SCU_RAM_QAM_LC_EP_MEDIUM__A, 24);
4331 if (status < 0)
4332 goto error;
4333 status = write16(state, SCU_RAM_QAM_LC_EP_COARSE__A, 24);
4334 if (status < 0)
4335 goto error;
4336 status = write16(state, SCU_RAM_QAM_LC_EI_FINE__A, 12);
4337 if (status < 0)
4338 goto error;
4339 status = write16(state, SCU_RAM_QAM_LC_EI_MEDIUM__A, 16);
4340 if (status < 0)
4341 goto error;
4342 status = write16(state, SCU_RAM_QAM_LC_EI_COARSE__A, 16);
4343 if (status < 0)
4344 goto error;
Oliver Endrissebc7de22011-07-03 13:49:44 -03004345
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004346 status = write16(state, SCU_RAM_QAM_LC_CP_FINE__A, 5);
4347 if (status < 0)
4348 goto error;
4349 status = write16(state, SCU_RAM_QAM_LC_CP_MEDIUM__A, 20);
4350 if (status < 0)
4351 goto error;
4352 status = write16(state, SCU_RAM_QAM_LC_CP_COARSE__A, 80);
4353 if (status < 0)
4354 goto error;
4355 status = write16(state, SCU_RAM_QAM_LC_CI_FINE__A, 5);
4356 if (status < 0)
4357 goto error;
4358 status = write16(state, SCU_RAM_QAM_LC_CI_MEDIUM__A, 20);
4359 if (status < 0)
4360 goto error;
4361 status = write16(state, SCU_RAM_QAM_LC_CI_COARSE__A, 50);
4362 if (status < 0)
4363 goto error;
4364 status = write16(state, SCU_RAM_QAM_LC_CF_FINE__A, 16);
4365 if (status < 0)
4366 goto error;
4367 status = write16(state, SCU_RAM_QAM_LC_CF_MEDIUM__A, 16);
4368 if (status < 0)
4369 goto error;
4370 status = write16(state, SCU_RAM_QAM_LC_CF_COARSE__A, 32);
4371 if (status < 0)
4372 goto error;
4373 status = write16(state, SCU_RAM_QAM_LC_CF1_FINE__A, 5);
4374 if (status < 0)
4375 goto error;
4376 status = write16(state, SCU_RAM_QAM_LC_CF1_MEDIUM__A, 10);
4377 if (status < 0)
4378 goto error;
4379 status = write16(state, SCU_RAM_QAM_LC_CF1_COARSE__A, 10);
4380 if (status < 0)
4381 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004382
4383
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004384 /* QAM State Machine (FSM) Thresholds */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004385
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004386 status = write16(state, SCU_RAM_QAM_FSM_RTH__A, 140);
4387 if (status < 0)
4388 goto error;
4389 status = write16(state, SCU_RAM_QAM_FSM_FTH__A, 50);
4390 if (status < 0)
4391 goto error;
4392 status = write16(state, SCU_RAM_QAM_FSM_CTH__A, 95);
4393 if (status < 0)
4394 goto error;
4395 status = write16(state, SCU_RAM_QAM_FSM_PTH__A, 120);
4396 if (status < 0)
4397 goto error;
4398 status = write16(state, SCU_RAM_QAM_FSM_QTH__A, 230);
4399 if (status < 0)
4400 goto error;
4401 status = write16(state, SCU_RAM_QAM_FSM_MTH__A, 105);
4402 if (status < 0)
4403 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004404
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004405 status = write16(state, SCU_RAM_QAM_FSM_RATE_LIM__A, 40);
4406 if (status < 0)
4407 goto error;
4408 status = write16(state, SCU_RAM_QAM_FSM_COUNT_LIM__A, 4);
4409 if (status < 0)
4410 goto error;
4411 status = write16(state, SCU_RAM_QAM_FSM_FREQ_LIM__A, 24);
4412 if (status < 0)
4413 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004414
4415
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004416 /* QAM FSM Tracking Parameters */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004417
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004418 status = write16(state, SCU_RAM_QAM_FSM_MEDIAN_AV_MULT__A, (u16) 16);
4419 if (status < 0)
4420 goto error;
4421 status = write16(state, SCU_RAM_QAM_FSM_RADIUS_AV_LIMIT__A, (u16) 220);
4422 if (status < 0)
4423 goto error;
4424 status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET1__A, (u16) 25);
4425 if (status < 0)
4426 goto error;
4427 status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET2__A, (u16) 6);
4428 if (status < 0)
4429 goto error;
4430 status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET3__A, (u16) -24);
4431 if (status < 0)
4432 goto error;
4433 status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET4__A, (u16) -65);
4434 if (status < 0)
4435 goto error;
4436 status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET5__A, (u16) -127);
4437 if (status < 0)
4438 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004439
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004440error:
4441 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03004442 pr_err("Error %d on %s\n", status, __func__);
Oliver Endrissebc7de22011-07-03 13:49:44 -03004443 return status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004444}
4445
4446/*============================================================================*/
4447
Mauro Carvalho Chehab34eb9752017-11-27 10:10:28 -05004448/*
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004449* \brief QAM32 specific setup
4450* \param demod instance of demod.
4451* \return DRXStatus_t.
4452*/
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03004453static int set_qam32(struct drxk_state *state)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004454{
Oliver Endrissebc7de22011-07-03 13:49:44 -03004455 int status = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004456
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03004457 dprintk(1, "\n");
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004458
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004459 /* QAM Equalizer Setup */
4460 /* Equalizer */
4461 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD0__A, 6707);
4462 if (status < 0)
4463 goto error;
4464 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD1__A, 6707);
4465 if (status < 0)
4466 goto error;
4467 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD2__A, 6707);
4468 if (status < 0)
4469 goto error;
4470 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD3__A, 6707);
4471 if (status < 0)
4472 goto error;
4473 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD4__A, 6707);
4474 if (status < 0)
4475 goto error;
4476 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD5__A, 6707);
4477 if (status < 0)
4478 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004479
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004480 /* Decision Feedback Equalizer */
4481 status = write16(state, QAM_DQ_QUAL_FUN0__A, 3);
4482 if (status < 0)
4483 goto error;
4484 status = write16(state, QAM_DQ_QUAL_FUN1__A, 3);
4485 if (status < 0)
4486 goto error;
4487 status = write16(state, QAM_DQ_QUAL_FUN2__A, 3);
4488 if (status < 0)
4489 goto error;
4490 status = write16(state, QAM_DQ_QUAL_FUN3__A, 3);
4491 if (status < 0)
4492 goto error;
4493 status = write16(state, QAM_DQ_QUAL_FUN4__A, 3);
4494 if (status < 0)
4495 goto error;
4496 status = write16(state, QAM_DQ_QUAL_FUN5__A, 0);
4497 if (status < 0)
4498 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004499
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004500 status = write16(state, QAM_SY_SYNC_HWM__A, 6);
4501 if (status < 0)
4502 goto error;
4503 status = write16(state, QAM_SY_SYNC_AWM__A, 5);
4504 if (status < 0)
4505 goto error;
4506 status = write16(state, QAM_SY_SYNC_LWM__A, 3);
4507 if (status < 0)
4508 goto error;
Oliver Endrissebc7de22011-07-03 13:49:44 -03004509
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004510 /* QAM Slicer Settings */
4511
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03004512 status = write16(state, SCU_RAM_QAM_SL_SIG_POWER__A,
4513 DRXK_QAM_SL_SIG_POWER_QAM32);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004514 if (status < 0)
4515 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004516
4517
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004518 /* QAM Loop Controller Coeficients */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004519
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004520 status = write16(state, SCU_RAM_QAM_LC_CA_FINE__A, 15);
4521 if (status < 0)
4522 goto error;
4523 status = write16(state, SCU_RAM_QAM_LC_CA_COARSE__A, 40);
4524 if (status < 0)
4525 goto error;
4526 status = write16(state, SCU_RAM_QAM_LC_EP_FINE__A, 12);
4527 if (status < 0)
4528 goto error;
4529 status = write16(state, SCU_RAM_QAM_LC_EP_MEDIUM__A, 24);
4530 if (status < 0)
4531 goto error;
4532 status = write16(state, SCU_RAM_QAM_LC_EP_COARSE__A, 24);
4533 if (status < 0)
4534 goto error;
4535 status = write16(state, SCU_RAM_QAM_LC_EI_FINE__A, 12);
4536 if (status < 0)
4537 goto error;
4538 status = write16(state, SCU_RAM_QAM_LC_EI_MEDIUM__A, 16);
4539 if (status < 0)
4540 goto error;
4541 status = write16(state, SCU_RAM_QAM_LC_EI_COARSE__A, 16);
4542 if (status < 0)
4543 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004544
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004545 status = write16(state, SCU_RAM_QAM_LC_CP_FINE__A, 5);
4546 if (status < 0)
4547 goto error;
4548 status = write16(state, SCU_RAM_QAM_LC_CP_MEDIUM__A, 20);
4549 if (status < 0)
4550 goto error;
4551 status = write16(state, SCU_RAM_QAM_LC_CP_COARSE__A, 80);
4552 if (status < 0)
4553 goto error;
4554 status = write16(state, SCU_RAM_QAM_LC_CI_FINE__A, 5);
4555 if (status < 0)
4556 goto error;
4557 status = write16(state, SCU_RAM_QAM_LC_CI_MEDIUM__A, 20);
4558 if (status < 0)
4559 goto error;
4560 status = write16(state, SCU_RAM_QAM_LC_CI_COARSE__A, 50);
4561 if (status < 0)
4562 goto error;
4563 status = write16(state, SCU_RAM_QAM_LC_CF_FINE__A, 16);
4564 if (status < 0)
4565 goto error;
4566 status = write16(state, SCU_RAM_QAM_LC_CF_MEDIUM__A, 16);
4567 if (status < 0)
4568 goto error;
4569 status = write16(state, SCU_RAM_QAM_LC_CF_COARSE__A, 16);
4570 if (status < 0)
4571 goto error;
4572 status = write16(state, SCU_RAM_QAM_LC_CF1_FINE__A, 5);
4573 if (status < 0)
4574 goto error;
4575 status = write16(state, SCU_RAM_QAM_LC_CF1_MEDIUM__A, 10);
4576 if (status < 0)
4577 goto error;
4578 status = write16(state, SCU_RAM_QAM_LC_CF1_COARSE__A, 0);
4579 if (status < 0)
4580 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004581
4582
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004583 /* QAM State Machine (FSM) Thresholds */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004584
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004585 status = write16(state, SCU_RAM_QAM_FSM_RTH__A, 90);
4586 if (status < 0)
4587 goto error;
4588 status = write16(state, SCU_RAM_QAM_FSM_FTH__A, 50);
4589 if (status < 0)
4590 goto error;
4591 status = write16(state, SCU_RAM_QAM_FSM_CTH__A, 80);
4592 if (status < 0)
4593 goto error;
4594 status = write16(state, SCU_RAM_QAM_FSM_PTH__A, 100);
4595 if (status < 0)
4596 goto error;
4597 status = write16(state, SCU_RAM_QAM_FSM_QTH__A, 170);
4598 if (status < 0)
4599 goto error;
4600 status = write16(state, SCU_RAM_QAM_FSM_MTH__A, 100);
4601 if (status < 0)
4602 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004603
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004604 status = write16(state, SCU_RAM_QAM_FSM_RATE_LIM__A, 40);
4605 if (status < 0)
4606 goto error;
4607 status = write16(state, SCU_RAM_QAM_FSM_COUNT_LIM__A, 4);
4608 if (status < 0)
4609 goto error;
4610 status = write16(state, SCU_RAM_QAM_FSM_FREQ_LIM__A, 10);
4611 if (status < 0)
4612 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004613
4614
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004615 /* QAM FSM Tracking Parameters */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004616
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004617 status = write16(state, SCU_RAM_QAM_FSM_MEDIAN_AV_MULT__A, (u16) 12);
4618 if (status < 0)
4619 goto error;
4620 status = write16(state, SCU_RAM_QAM_FSM_RADIUS_AV_LIMIT__A, (u16) 140);
4621 if (status < 0)
4622 goto error;
4623 status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET1__A, (u16) -8);
4624 if (status < 0)
4625 goto error;
4626 status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET2__A, (u16) -16);
4627 if (status < 0)
4628 goto error;
4629 status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET3__A, (u16) -26);
4630 if (status < 0)
4631 goto error;
4632 status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET4__A, (u16) -56);
4633 if (status < 0)
4634 goto error;
4635 status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET5__A, (u16) -86);
4636error:
4637 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03004638 pr_err("Error %d on %s\n", status, __func__);
Oliver Endrissebc7de22011-07-03 13:49:44 -03004639 return status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004640}
4641
4642/*============================================================================*/
4643
Mauro Carvalho Chehab34eb9752017-11-27 10:10:28 -05004644/*
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004645* \brief QAM64 specific setup
4646* \param demod instance of demod.
4647* \return DRXStatus_t.
4648*/
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03004649static int set_qam64(struct drxk_state *state)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004650{
Oliver Endrissebc7de22011-07-03 13:49:44 -03004651 int status = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004652
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03004653 dprintk(1, "\n");
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004654 /* QAM Equalizer Setup */
4655 /* Equalizer */
4656 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD0__A, 13336);
4657 if (status < 0)
4658 goto error;
4659 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD1__A, 12618);
4660 if (status < 0)
4661 goto error;
4662 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD2__A, 11988);
4663 if (status < 0)
4664 goto error;
4665 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD3__A, 13809);
4666 if (status < 0)
4667 goto error;
4668 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD4__A, 13809);
4669 if (status < 0)
4670 goto error;
4671 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD5__A, 15609);
4672 if (status < 0)
4673 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004674
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004675 /* Decision Feedback Equalizer */
4676 status = write16(state, QAM_DQ_QUAL_FUN0__A, 4);
4677 if (status < 0)
4678 goto error;
4679 status = write16(state, QAM_DQ_QUAL_FUN1__A, 4);
4680 if (status < 0)
4681 goto error;
4682 status = write16(state, QAM_DQ_QUAL_FUN2__A, 4);
4683 if (status < 0)
4684 goto error;
4685 status = write16(state, QAM_DQ_QUAL_FUN3__A, 4);
4686 if (status < 0)
4687 goto error;
4688 status = write16(state, QAM_DQ_QUAL_FUN4__A, 3);
4689 if (status < 0)
4690 goto error;
4691 status = write16(state, QAM_DQ_QUAL_FUN5__A, 0);
4692 if (status < 0)
4693 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004694
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004695 status = write16(state, QAM_SY_SYNC_HWM__A, 5);
4696 if (status < 0)
4697 goto error;
4698 status = write16(state, QAM_SY_SYNC_AWM__A, 4);
4699 if (status < 0)
4700 goto error;
4701 status = write16(state, QAM_SY_SYNC_LWM__A, 3);
4702 if (status < 0)
4703 goto error;
Oliver Endrissebc7de22011-07-03 13:49:44 -03004704
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004705 /* QAM Slicer Settings */
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03004706 status = write16(state, SCU_RAM_QAM_SL_SIG_POWER__A,
4707 DRXK_QAM_SL_SIG_POWER_QAM64);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004708 if (status < 0)
4709 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004710
4711
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004712 /* QAM Loop Controller Coeficients */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004713
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004714 status = write16(state, SCU_RAM_QAM_LC_CA_FINE__A, 15);
4715 if (status < 0)
4716 goto error;
4717 status = write16(state, SCU_RAM_QAM_LC_CA_COARSE__A, 40);
4718 if (status < 0)
4719 goto error;
4720 status = write16(state, SCU_RAM_QAM_LC_EP_FINE__A, 12);
4721 if (status < 0)
4722 goto error;
4723 status = write16(state, SCU_RAM_QAM_LC_EP_MEDIUM__A, 24);
4724 if (status < 0)
4725 goto error;
4726 status = write16(state, SCU_RAM_QAM_LC_EP_COARSE__A, 24);
4727 if (status < 0)
4728 goto error;
4729 status = write16(state, SCU_RAM_QAM_LC_EI_FINE__A, 12);
4730 if (status < 0)
4731 goto error;
4732 status = write16(state, SCU_RAM_QAM_LC_EI_MEDIUM__A, 16);
4733 if (status < 0)
4734 goto error;
4735 status = write16(state, SCU_RAM_QAM_LC_EI_COARSE__A, 16);
4736 if (status < 0)
4737 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004738
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004739 status = write16(state, SCU_RAM_QAM_LC_CP_FINE__A, 5);
4740 if (status < 0)
4741 goto error;
4742 status = write16(state, SCU_RAM_QAM_LC_CP_MEDIUM__A, 30);
4743 if (status < 0)
4744 goto error;
4745 status = write16(state, SCU_RAM_QAM_LC_CP_COARSE__A, 100);
4746 if (status < 0)
4747 goto error;
4748 status = write16(state, SCU_RAM_QAM_LC_CI_FINE__A, 5);
4749 if (status < 0)
4750 goto error;
4751 status = write16(state, SCU_RAM_QAM_LC_CI_MEDIUM__A, 30);
4752 if (status < 0)
4753 goto error;
4754 status = write16(state, SCU_RAM_QAM_LC_CI_COARSE__A, 50);
4755 if (status < 0)
4756 goto error;
4757 status = write16(state, SCU_RAM_QAM_LC_CF_FINE__A, 16);
4758 if (status < 0)
4759 goto error;
4760 status = write16(state, SCU_RAM_QAM_LC_CF_MEDIUM__A, 25);
4761 if (status < 0)
4762 goto error;
4763 status = write16(state, SCU_RAM_QAM_LC_CF_COARSE__A, 48);
4764 if (status < 0)
4765 goto error;
4766 status = write16(state, SCU_RAM_QAM_LC_CF1_FINE__A, 5);
4767 if (status < 0)
4768 goto error;
4769 status = write16(state, SCU_RAM_QAM_LC_CF1_MEDIUM__A, 10);
4770 if (status < 0)
4771 goto error;
4772 status = write16(state, SCU_RAM_QAM_LC_CF1_COARSE__A, 10);
4773 if (status < 0)
4774 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004775
4776
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004777 /* QAM State Machine (FSM) Thresholds */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004778
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004779 status = write16(state, SCU_RAM_QAM_FSM_RTH__A, 100);
4780 if (status < 0)
4781 goto error;
4782 status = write16(state, SCU_RAM_QAM_FSM_FTH__A, 60);
4783 if (status < 0)
4784 goto error;
4785 status = write16(state, SCU_RAM_QAM_FSM_CTH__A, 80);
4786 if (status < 0)
4787 goto error;
4788 status = write16(state, SCU_RAM_QAM_FSM_PTH__A, 110);
4789 if (status < 0)
4790 goto error;
4791 status = write16(state, SCU_RAM_QAM_FSM_QTH__A, 200);
4792 if (status < 0)
4793 goto error;
4794 status = write16(state, SCU_RAM_QAM_FSM_MTH__A, 95);
4795 if (status < 0)
4796 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004797
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004798 status = write16(state, SCU_RAM_QAM_FSM_RATE_LIM__A, 40);
4799 if (status < 0)
4800 goto error;
4801 status = write16(state, SCU_RAM_QAM_FSM_COUNT_LIM__A, 4);
4802 if (status < 0)
4803 goto error;
4804 status = write16(state, SCU_RAM_QAM_FSM_FREQ_LIM__A, 15);
4805 if (status < 0)
4806 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004807
4808
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004809 /* QAM FSM Tracking Parameters */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004810
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004811 status = write16(state, SCU_RAM_QAM_FSM_MEDIAN_AV_MULT__A, (u16) 12);
4812 if (status < 0)
4813 goto error;
4814 status = write16(state, SCU_RAM_QAM_FSM_RADIUS_AV_LIMIT__A, (u16) 141);
4815 if (status < 0)
4816 goto error;
4817 status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET1__A, (u16) 7);
4818 if (status < 0)
4819 goto error;
4820 status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET2__A, (u16) 0);
4821 if (status < 0)
4822 goto error;
4823 status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET3__A, (u16) -15);
4824 if (status < 0)
4825 goto error;
4826 status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET4__A, (u16) -45);
4827 if (status < 0)
4828 goto error;
4829 status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET5__A, (u16) -80);
4830error:
4831 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03004832 pr_err("Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004833
Oliver Endrissebc7de22011-07-03 13:49:44 -03004834 return status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004835}
4836
4837/*============================================================================*/
4838
Mauro Carvalho Chehab34eb9752017-11-27 10:10:28 -05004839/*
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004840* \brief QAM128 specific setup
4841* \param demod: instance of demod.
4842* \return DRXStatus_t.
4843*/
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03004844static int set_qam128(struct drxk_state *state)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004845{
Oliver Endrissebc7de22011-07-03 13:49:44 -03004846 int status = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004847
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03004848 dprintk(1, "\n");
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004849 /* QAM Equalizer Setup */
4850 /* Equalizer */
4851 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD0__A, 6564);
4852 if (status < 0)
4853 goto error;
4854 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD1__A, 6598);
4855 if (status < 0)
4856 goto error;
4857 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD2__A, 6394);
4858 if (status < 0)
4859 goto error;
4860 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD3__A, 6409);
4861 if (status < 0)
4862 goto error;
4863 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD4__A, 6656);
4864 if (status < 0)
4865 goto error;
4866 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD5__A, 7238);
4867 if (status < 0)
4868 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004869
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004870 /* Decision Feedback Equalizer */
4871 status = write16(state, QAM_DQ_QUAL_FUN0__A, 6);
4872 if (status < 0)
4873 goto error;
4874 status = write16(state, QAM_DQ_QUAL_FUN1__A, 6);
4875 if (status < 0)
4876 goto error;
4877 status = write16(state, QAM_DQ_QUAL_FUN2__A, 6);
4878 if (status < 0)
4879 goto error;
4880 status = write16(state, QAM_DQ_QUAL_FUN3__A, 6);
4881 if (status < 0)
4882 goto error;
4883 status = write16(state, QAM_DQ_QUAL_FUN4__A, 5);
4884 if (status < 0)
4885 goto error;
4886 status = write16(state, QAM_DQ_QUAL_FUN5__A, 0);
4887 if (status < 0)
4888 goto error;
Oliver Endrissebc7de22011-07-03 13:49:44 -03004889
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004890 status = write16(state, QAM_SY_SYNC_HWM__A, 6);
4891 if (status < 0)
4892 goto error;
4893 status = write16(state, QAM_SY_SYNC_AWM__A, 5);
4894 if (status < 0)
4895 goto error;
4896 status = write16(state, QAM_SY_SYNC_LWM__A, 3);
4897 if (status < 0)
4898 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004899
4900
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004901 /* QAM Slicer Settings */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004902
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03004903 status = write16(state, SCU_RAM_QAM_SL_SIG_POWER__A,
4904 DRXK_QAM_SL_SIG_POWER_QAM128);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004905 if (status < 0)
4906 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004907
4908
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004909 /* QAM Loop Controller Coeficients */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004910
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004911 status = write16(state, SCU_RAM_QAM_LC_CA_FINE__A, 15);
4912 if (status < 0)
4913 goto error;
4914 status = write16(state, SCU_RAM_QAM_LC_CA_COARSE__A, 40);
4915 if (status < 0)
4916 goto error;
4917 status = write16(state, SCU_RAM_QAM_LC_EP_FINE__A, 12);
4918 if (status < 0)
4919 goto error;
4920 status = write16(state, SCU_RAM_QAM_LC_EP_MEDIUM__A, 24);
4921 if (status < 0)
4922 goto error;
4923 status = write16(state, SCU_RAM_QAM_LC_EP_COARSE__A, 24);
4924 if (status < 0)
4925 goto error;
4926 status = write16(state, SCU_RAM_QAM_LC_EI_FINE__A, 12);
4927 if (status < 0)
4928 goto error;
4929 status = write16(state, SCU_RAM_QAM_LC_EI_MEDIUM__A, 16);
4930 if (status < 0)
4931 goto error;
4932 status = write16(state, SCU_RAM_QAM_LC_EI_COARSE__A, 16);
4933 if (status < 0)
4934 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004935
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004936 status = write16(state, SCU_RAM_QAM_LC_CP_FINE__A, 5);
4937 if (status < 0)
4938 goto error;
4939 status = write16(state, SCU_RAM_QAM_LC_CP_MEDIUM__A, 40);
4940 if (status < 0)
4941 goto error;
4942 status = write16(state, SCU_RAM_QAM_LC_CP_COARSE__A, 120);
4943 if (status < 0)
4944 goto error;
4945 status = write16(state, SCU_RAM_QAM_LC_CI_FINE__A, 5);
4946 if (status < 0)
4947 goto error;
4948 status = write16(state, SCU_RAM_QAM_LC_CI_MEDIUM__A, 40);
4949 if (status < 0)
4950 goto error;
4951 status = write16(state, SCU_RAM_QAM_LC_CI_COARSE__A, 60);
4952 if (status < 0)
4953 goto error;
4954 status = write16(state, SCU_RAM_QAM_LC_CF_FINE__A, 16);
4955 if (status < 0)
4956 goto error;
4957 status = write16(state, SCU_RAM_QAM_LC_CF_MEDIUM__A, 25);
4958 if (status < 0)
4959 goto error;
4960 status = write16(state, SCU_RAM_QAM_LC_CF_COARSE__A, 64);
4961 if (status < 0)
4962 goto error;
4963 status = write16(state, SCU_RAM_QAM_LC_CF1_FINE__A, 5);
4964 if (status < 0)
4965 goto error;
4966 status = write16(state, SCU_RAM_QAM_LC_CF1_MEDIUM__A, 10);
4967 if (status < 0)
4968 goto error;
4969 status = write16(state, SCU_RAM_QAM_LC_CF1_COARSE__A, 0);
4970 if (status < 0)
4971 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004972
4973
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004974 /* QAM State Machine (FSM) Thresholds */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004975
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004976 status = write16(state, SCU_RAM_QAM_FSM_RTH__A, 50);
4977 if (status < 0)
4978 goto error;
4979 status = write16(state, SCU_RAM_QAM_FSM_FTH__A, 60);
4980 if (status < 0)
4981 goto error;
4982 status = write16(state, SCU_RAM_QAM_FSM_CTH__A, 80);
4983 if (status < 0)
4984 goto error;
4985 status = write16(state, SCU_RAM_QAM_FSM_PTH__A, 100);
4986 if (status < 0)
4987 goto error;
4988 status = write16(state, SCU_RAM_QAM_FSM_QTH__A, 140);
4989 if (status < 0)
4990 goto error;
4991 status = write16(state, SCU_RAM_QAM_FSM_MTH__A, 100);
4992 if (status < 0)
4993 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03004994
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03004995 status = write16(state, SCU_RAM_QAM_FSM_RATE_LIM__A, 40);
4996 if (status < 0)
4997 goto error;
4998 status = write16(state, SCU_RAM_QAM_FSM_COUNT_LIM__A, 5);
4999 if (status < 0)
5000 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005001
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005002 status = write16(state, SCU_RAM_QAM_FSM_FREQ_LIM__A, 12);
5003 if (status < 0)
5004 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005005
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005006 /* QAM FSM Tracking Parameters */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005007
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005008 status = write16(state, SCU_RAM_QAM_FSM_MEDIAN_AV_MULT__A, (u16) 8);
5009 if (status < 0)
5010 goto error;
5011 status = write16(state, SCU_RAM_QAM_FSM_RADIUS_AV_LIMIT__A, (u16) 65);
5012 if (status < 0)
5013 goto error;
5014 status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET1__A, (u16) 5);
5015 if (status < 0)
5016 goto error;
5017 status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET2__A, (u16) 3);
5018 if (status < 0)
5019 goto error;
5020 status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET3__A, (u16) -1);
5021 if (status < 0)
5022 goto error;
5023 status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET4__A, (u16) -12);
5024 if (status < 0)
5025 goto error;
5026 status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET5__A, (u16) -23);
5027error:
5028 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03005029 pr_err("Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005030
Oliver Endrissebc7de22011-07-03 13:49:44 -03005031 return status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005032}
5033
5034/*============================================================================*/
5035
Mauro Carvalho Chehab34eb9752017-11-27 10:10:28 -05005036/*
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005037* \brief QAM256 specific setup
5038* \param demod: instance of demod.
5039* \return DRXStatus_t.
5040*/
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005041static int set_qam256(struct drxk_state *state)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005042{
Oliver Endrissebc7de22011-07-03 13:49:44 -03005043 int status = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005044
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03005045 dprintk(1, "\n");
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005046 /* QAM Equalizer Setup */
5047 /* Equalizer */
5048 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD0__A, 11502);
5049 if (status < 0)
5050 goto error;
5051 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD1__A, 12084);
5052 if (status < 0)
5053 goto error;
5054 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD2__A, 12543);
5055 if (status < 0)
5056 goto error;
5057 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD3__A, 12931);
5058 if (status < 0)
5059 goto error;
5060 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD4__A, 13629);
5061 if (status < 0)
5062 goto error;
5063 status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD5__A, 15385);
5064 if (status < 0)
5065 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005066
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005067 /* Decision Feedback Equalizer */
5068 status = write16(state, QAM_DQ_QUAL_FUN0__A, 8);
5069 if (status < 0)
5070 goto error;
5071 status = write16(state, QAM_DQ_QUAL_FUN1__A, 8);
5072 if (status < 0)
5073 goto error;
5074 status = write16(state, QAM_DQ_QUAL_FUN2__A, 8);
5075 if (status < 0)
5076 goto error;
5077 status = write16(state, QAM_DQ_QUAL_FUN3__A, 8);
5078 if (status < 0)
5079 goto error;
5080 status = write16(state, QAM_DQ_QUAL_FUN4__A, 6);
5081 if (status < 0)
5082 goto error;
5083 status = write16(state, QAM_DQ_QUAL_FUN5__A, 0);
5084 if (status < 0)
5085 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005086
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005087 status = write16(state, QAM_SY_SYNC_HWM__A, 5);
5088 if (status < 0)
5089 goto error;
5090 status = write16(state, QAM_SY_SYNC_AWM__A, 4);
5091 if (status < 0)
5092 goto error;
5093 status = write16(state, QAM_SY_SYNC_LWM__A, 3);
5094 if (status < 0)
5095 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005096
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005097 /* QAM Slicer Settings */
Oliver Endrissebc7de22011-07-03 13:49:44 -03005098
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03005099 status = write16(state, SCU_RAM_QAM_SL_SIG_POWER__A,
5100 DRXK_QAM_SL_SIG_POWER_QAM256);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005101 if (status < 0)
5102 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005103
5104
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005105 /* QAM Loop Controller Coeficients */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005106
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005107 status = write16(state, SCU_RAM_QAM_LC_CA_FINE__A, 15);
5108 if (status < 0)
5109 goto error;
5110 status = write16(state, SCU_RAM_QAM_LC_CA_COARSE__A, 40);
5111 if (status < 0)
5112 goto error;
5113 status = write16(state, SCU_RAM_QAM_LC_EP_FINE__A, 12);
5114 if (status < 0)
5115 goto error;
5116 status = write16(state, SCU_RAM_QAM_LC_EP_MEDIUM__A, 24);
5117 if (status < 0)
5118 goto error;
5119 status = write16(state, SCU_RAM_QAM_LC_EP_COARSE__A, 24);
5120 if (status < 0)
5121 goto error;
5122 status = write16(state, SCU_RAM_QAM_LC_EI_FINE__A, 12);
5123 if (status < 0)
5124 goto error;
5125 status = write16(state, SCU_RAM_QAM_LC_EI_MEDIUM__A, 16);
5126 if (status < 0)
5127 goto error;
5128 status = write16(state, SCU_RAM_QAM_LC_EI_COARSE__A, 16);
5129 if (status < 0)
5130 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005131
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005132 status = write16(state, SCU_RAM_QAM_LC_CP_FINE__A, 5);
5133 if (status < 0)
5134 goto error;
5135 status = write16(state, SCU_RAM_QAM_LC_CP_MEDIUM__A, 50);
5136 if (status < 0)
5137 goto error;
5138 status = write16(state, SCU_RAM_QAM_LC_CP_COARSE__A, 250);
5139 if (status < 0)
5140 goto error;
5141 status = write16(state, SCU_RAM_QAM_LC_CI_FINE__A, 5);
5142 if (status < 0)
5143 goto error;
5144 status = write16(state, SCU_RAM_QAM_LC_CI_MEDIUM__A, 50);
5145 if (status < 0)
5146 goto error;
5147 status = write16(state, SCU_RAM_QAM_LC_CI_COARSE__A, 125);
5148 if (status < 0)
5149 goto error;
5150 status = write16(state, SCU_RAM_QAM_LC_CF_FINE__A, 16);
5151 if (status < 0)
5152 goto error;
5153 status = write16(state, SCU_RAM_QAM_LC_CF_MEDIUM__A, 25);
5154 if (status < 0)
5155 goto error;
5156 status = write16(state, SCU_RAM_QAM_LC_CF_COARSE__A, 48);
5157 if (status < 0)
5158 goto error;
5159 status = write16(state, SCU_RAM_QAM_LC_CF1_FINE__A, 5);
5160 if (status < 0)
5161 goto error;
5162 status = write16(state, SCU_RAM_QAM_LC_CF1_MEDIUM__A, 10);
5163 if (status < 0)
5164 goto error;
5165 status = write16(state, SCU_RAM_QAM_LC_CF1_COARSE__A, 10);
5166 if (status < 0)
5167 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005168
5169
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005170 /* QAM State Machine (FSM) Thresholds */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005171
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005172 status = write16(state, SCU_RAM_QAM_FSM_RTH__A, 50);
5173 if (status < 0)
5174 goto error;
5175 status = write16(state, SCU_RAM_QAM_FSM_FTH__A, 60);
5176 if (status < 0)
5177 goto error;
5178 status = write16(state, SCU_RAM_QAM_FSM_CTH__A, 80);
5179 if (status < 0)
5180 goto error;
5181 status = write16(state, SCU_RAM_QAM_FSM_PTH__A, 100);
5182 if (status < 0)
5183 goto error;
5184 status = write16(state, SCU_RAM_QAM_FSM_QTH__A, 150);
5185 if (status < 0)
5186 goto error;
5187 status = write16(state, SCU_RAM_QAM_FSM_MTH__A, 110);
5188 if (status < 0)
5189 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005190
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005191 status = write16(state, SCU_RAM_QAM_FSM_RATE_LIM__A, 40);
5192 if (status < 0)
5193 goto error;
5194 status = write16(state, SCU_RAM_QAM_FSM_COUNT_LIM__A, 4);
5195 if (status < 0)
5196 goto error;
5197 status = write16(state, SCU_RAM_QAM_FSM_FREQ_LIM__A, 12);
5198 if (status < 0)
5199 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005200
5201
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005202 /* QAM FSM Tracking Parameters */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005203
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005204 status = write16(state, SCU_RAM_QAM_FSM_MEDIAN_AV_MULT__A, (u16) 8);
5205 if (status < 0)
5206 goto error;
5207 status = write16(state, SCU_RAM_QAM_FSM_RADIUS_AV_LIMIT__A, (u16) 74);
5208 if (status < 0)
5209 goto error;
5210 status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET1__A, (u16) 18);
5211 if (status < 0)
5212 goto error;
5213 status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET2__A, (u16) 13);
5214 if (status < 0)
5215 goto error;
5216 status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET3__A, (u16) 7);
5217 if (status < 0)
5218 goto error;
5219 status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET4__A, (u16) 0);
5220 if (status < 0)
5221 goto error;
5222 status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET5__A, (u16) -8);
5223error:
5224 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03005225 pr_err("Error %d on %s\n", status, __func__);
Oliver Endrissebc7de22011-07-03 13:49:44 -03005226 return status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005227}
5228
5229
5230/*============================================================================*/
Mauro Carvalho Chehab34eb9752017-11-27 10:10:28 -05005231/*
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005232* \brief Reset QAM block.
5233* \param demod: instance of demod.
5234* \param channel: pointer to channel data.
5235* \return DRXStatus_t.
5236*/
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005237static int qam_reset_qam(struct drxk_state *state)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005238{
Oliver Endrissebc7de22011-07-03 13:49:44 -03005239 int status;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005240 u16 cmd_result;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005241
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03005242 dprintk(1, "\n");
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005243 /* Stop QAM comstate->m_exec */
5244 status = write16(state, QAM_COMM_EXEC__A, QAM_COMM_EXEC_STOP);
5245 if (status < 0)
5246 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005247
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03005248 status = scu_command(state, SCU_RAM_COMMAND_STANDARD_QAM
5249 | SCU_RAM_COMMAND_CMD_DEMOD_RESET,
5250 0, NULL, 1, &cmd_result);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005251error:
5252 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03005253 pr_err("Error %d on %s\n", status, __func__);
Oliver Endrissebc7de22011-07-03 13:49:44 -03005254 return status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005255}
5256
5257/*============================================================================*/
5258
Mauro Carvalho Chehab34eb9752017-11-27 10:10:28 -05005259/*
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005260* \brief Set QAM symbolrate.
5261* \param demod: instance of demod.
5262* \param channel: pointer to channel data.
5263* \return DRXStatus_t.
5264*/
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005265static int qam_set_symbolrate(struct drxk_state *state)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005266{
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005267 u32 adc_frequency = 0;
5268 u32 symb_freq = 0;
5269 u32 iqm_rc_rate = 0;
Oliver Endrissebc7de22011-07-03 13:49:44 -03005270 u16 ratesel = 0;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005271 u32 lc_symb_rate = 0;
Oliver Endrissebc7de22011-07-03 13:49:44 -03005272 int status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005273
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03005274 dprintk(1, "\n");
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005275 /* Select & calculate correct IQM rate */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005276 adc_frequency = (state->m_sys_clock_freq * 1000) / 3;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005277 ratesel = 0;
Mauro Carvalho Chehabed5452a2011-12-26 09:57:11 -03005278 if (state->props.symbol_rate <= 1188750)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005279 ratesel = 3;
Mauro Carvalho Chehabed5452a2011-12-26 09:57:11 -03005280 else if (state->props.symbol_rate <= 2377500)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005281 ratesel = 2;
Mauro Carvalho Chehabed5452a2011-12-26 09:57:11 -03005282 else if (state->props.symbol_rate <= 4755000)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005283 ratesel = 1;
5284 status = write16(state, IQM_FD_RATESEL__A, ratesel);
5285 if (status < 0)
5286 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005287
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005288 /*
5289 IqmRcRate = ((Fadc / (symbolrate * (4<<ratesel))) - 1) * (1<<23)
5290 */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005291 symb_freq = state->props.symbol_rate * (1 << ratesel);
5292 if (symb_freq == 0) {
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005293 /* Divide by zero */
5294 status = -EINVAL;
5295 goto error;
5296 }
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005297 iqm_rc_rate = (adc_frequency / symb_freq) * (1 << 21) +
5298 (Frac28a((adc_frequency % symb_freq), symb_freq) >> 7) -
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005299 (1 << 23);
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005300 status = write32(state, IQM_RC_RATE_OFS_LO__A, iqm_rc_rate);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005301 if (status < 0)
5302 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005303 state->m_iqm_rc_rate = iqm_rc_rate;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005304 /*
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005305 LcSymbFreq = round (.125 * symbolrate / adc_freq * (1<<15))
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005306 */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005307 symb_freq = state->props.symbol_rate;
5308 if (adc_frequency == 0) {
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005309 /* Divide by zero */
5310 status = -EINVAL;
5311 goto error;
5312 }
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005313 lc_symb_rate = (symb_freq / adc_frequency) * (1 << 12) +
5314 (Frac28a((symb_freq % adc_frequency), adc_frequency) >>
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005315 16);
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005316 if (lc_symb_rate > 511)
5317 lc_symb_rate = 511;
5318 status = write16(state, QAM_LC_SYMBOL_FREQ__A, (u16) lc_symb_rate);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005319
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005320error:
5321 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03005322 pr_err("Error %d on %s\n", status, __func__);
Oliver Endrissebc7de22011-07-03 13:49:44 -03005323 return status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005324}
5325
5326/*============================================================================*/
5327
Mauro Carvalho Chehab34eb9752017-11-27 10:10:28 -05005328/*
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005329* \brief Get QAM lock status.
5330* \param demod: instance of demod.
5331* \param channel: pointer to channel data.
5332* \return DRXStatus_t.
5333*/
5334
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005335static int get_qam_lock_status(struct drxk_state *state, u32 *p_lock_status)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005336{
5337 int status;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005338 u16 result[2] = { 0, 0 };
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005339
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03005340 dprintk(1, "\n");
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005341 *p_lock_status = NOT_LOCKED;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005342 status = scu_command(state,
Oliver Endrissebc7de22011-07-03 13:49:44 -03005343 SCU_RAM_COMMAND_STANDARD_QAM |
5344 SCU_RAM_COMMAND_CMD_DEMOD_GET_LOCK, 0, NULL, 2,
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005345 result);
Oliver Endrissebc7de22011-07-03 13:49:44 -03005346 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03005347 pr_err("Error %d on %s\n", status, __func__);
Oliver Endrissebc7de22011-07-03 13:49:44 -03005348
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005349 if (result[1] < SCU_RAM_QAM_LOCKED_LOCKED_DEMOD_LOCKED) {
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005350 /* 0x0000 NOT LOCKED */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005351 } else if (result[1] < SCU_RAM_QAM_LOCKED_LOCKED_LOCKED) {
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005352 /* 0x4000 DEMOD LOCKED */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005353 *p_lock_status = DEMOD_LOCK;
5354 } else if (result[1] < SCU_RAM_QAM_LOCKED_LOCKED_NEVER_LOCK) {
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005355 /* 0x8000 DEMOD + FEC LOCKED (system lock) */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005356 *p_lock_status = MPEG_LOCK;
Oliver Endrissebc7de22011-07-03 13:49:44 -03005357 } else {
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005358 /* 0xC000 NEVER LOCKED */
5359 /* (system will never be able to lock to the signal) */
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03005360 /*
5361 * TODO: check this, intermediate & standard specific lock
5362 * states are not taken into account here
5363 */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005364 *p_lock_status = NEVER_LOCK;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005365 }
5366 return status;
5367}
5368
5369#define QAM_MIRROR__M 0x03
5370#define QAM_MIRROR_NORMAL 0x00
5371#define QAM_MIRRORED 0x01
5372#define QAM_MIRROR_AUTO_ON 0x02
5373#define QAM_LOCKRANGE__M 0x10
5374#define QAM_LOCKRANGE_NORMAL 0x10
5375
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005376static int qam_demodulator_command(struct drxk_state *state,
5377 int number_of_parameters)
Martin Blumenstingl9e23f50a2012-07-04 17:36:55 -03005378{
5379 int status;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005380 u16 cmd_result;
5381 u16 set_param_parameters[4] = { 0, 0, 0, 0 };
Martin Blumenstingl9e23f50a2012-07-04 17:36:55 -03005382
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005383 set_param_parameters[0] = state->m_constellation; /* modulation */
5384 set_param_parameters[1] = DRXK_QAM_I12_J17; /* interleave mode */
Martin Blumenstingl9e23f50a2012-07-04 17:36:55 -03005385
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005386 if (number_of_parameters == 2) {
5387 u16 set_env_parameters[1] = { 0 };
Martin Blumenstingl9e23f50a2012-07-04 17:36:55 -03005388
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005389 if (state->m_operation_mode == OM_QAM_ITU_C)
5390 set_env_parameters[0] = QAM_TOP_ANNEX_C;
Martin Blumenstingl9e23f50a2012-07-04 17:36:55 -03005391 else
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005392 set_env_parameters[0] = QAM_TOP_ANNEX_A;
Martin Blumenstingl9e23f50a2012-07-04 17:36:55 -03005393
5394 status = scu_command(state,
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03005395 SCU_RAM_COMMAND_STANDARD_QAM
5396 | SCU_RAM_COMMAND_CMD_DEMOD_SET_ENV,
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005397 1, set_env_parameters, 1, &cmd_result);
Martin Blumenstingl9e23f50a2012-07-04 17:36:55 -03005398 if (status < 0)
5399 goto error;
5400
5401 status = scu_command(state,
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03005402 SCU_RAM_COMMAND_STANDARD_QAM
5403 | SCU_RAM_COMMAND_CMD_DEMOD_SET_PARAM,
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005404 number_of_parameters, set_param_parameters,
5405 1, &cmd_result);
5406 } else if (number_of_parameters == 4) {
5407 if (state->m_operation_mode == OM_QAM_ITU_C)
5408 set_param_parameters[2] = QAM_TOP_ANNEX_C;
Martin Blumenstingl9e23f50a2012-07-04 17:36:55 -03005409 else
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005410 set_param_parameters[2] = QAM_TOP_ANNEX_A;
Martin Blumenstingl9e23f50a2012-07-04 17:36:55 -03005411
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005412 set_param_parameters[3] |= (QAM_MIRROR_AUTO_ON);
Martin Blumenstingl9e23f50a2012-07-04 17:36:55 -03005413 /* Env parameters */
Mauro Carvalho Chehab868c9a12019-02-18 14:28:55 -05005414 /* check for LOCKRANGE Extended */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005415 /* set_param_parameters[3] |= QAM_LOCKRANGE_NORMAL; */
Martin Blumenstingl9e23f50a2012-07-04 17:36:55 -03005416
5417 status = scu_command(state,
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03005418 SCU_RAM_COMMAND_STANDARD_QAM
5419 | SCU_RAM_COMMAND_CMD_DEMOD_SET_PARAM,
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005420 number_of_parameters, set_param_parameters,
5421 1, &cmd_result);
Martin Blumenstingl9e23f50a2012-07-04 17:36:55 -03005422 } else {
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03005423 pr_warn("Unknown QAM demodulator parameter count %d\n",
5424 number_of_parameters);
Mauro Carvalho Chehab94af1b62012-10-29 07:58:59 -02005425 status = -EINVAL;
Martin Blumenstingl9e23f50a2012-07-04 17:36:55 -03005426 }
5427
5428error:
5429 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03005430 pr_warn("Warning %d on %s\n", status, __func__);
Martin Blumenstingl9e23f50a2012-07-04 17:36:55 -03005431 return status;
5432}
5433
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005434static int set_qam(struct drxk_state *state, u16 intermediate_freqk_hz,
5435 s32 tuner_freq_offset)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005436{
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005437 int status;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005438 u16 cmd_result;
5439 int qam_demod_param_count = state->qam_demod_parameter_count;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005440
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03005441 dprintk(1, "\n");
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005442 /*
Mauro Carvalho Chehab119faf92011-07-24 09:11:36 -03005443 * STEP 1: reset demodulator
5444 * resets FEC DI and FEC RS
5445 * resets QAM block
5446 * resets SCU variables
5447 */
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005448 status = write16(state, FEC_DI_COMM_EXEC__A, FEC_DI_COMM_EXEC_STOP);
Oliver Endrissebc7de22011-07-03 13:49:44 -03005449 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005450 goto error;
5451 status = write16(state, FEC_RS_COMM_EXEC__A, FEC_RS_COMM_EXEC_STOP);
5452 if (status < 0)
5453 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005454 status = qam_reset_qam(state);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005455 if (status < 0)
5456 goto error;
Oliver Endrissebc7de22011-07-03 13:49:44 -03005457
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005458 /*
Mauro Carvalho Chehab119faf92011-07-24 09:11:36 -03005459 * STEP 2: configure demodulator
5460 * -set params; resets IQM,QAM,FEC HW; initializes some
5461 * SCU variables
5462 */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005463 status = qam_set_symbolrate(state);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005464 if (status < 0)
5465 goto error;
5466
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005467 /* Set params */
Mauro Carvalho Chehabed5452a2011-12-26 09:57:11 -03005468 switch (state->props.modulation) {
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005469 case QAM_256:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005470 state->m_constellation = DRX_CONSTELLATION_QAM256;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005471 break;
5472 case QAM_AUTO:
5473 case QAM_64:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005474 state->m_constellation = DRX_CONSTELLATION_QAM64;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005475 break;
5476 case QAM_16:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005477 state->m_constellation = DRX_CONSTELLATION_QAM16;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005478 break;
5479 case QAM_32:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005480 state->m_constellation = DRX_CONSTELLATION_QAM32;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005481 break;
5482 case QAM_128:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005483 state->m_constellation = DRX_CONSTELLATION_QAM128;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005484 break;
5485 default:
5486 status = -EINVAL;
5487 break;
5488 }
5489 if (status < 0)
5490 goto error;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005491
Martin Blumenstingl9e23f50a2012-07-04 17:36:55 -03005492 /* Use the 4-parameter if it's requested or we're probing for
5493 * the correct command. */
5494 if (state->qam_demod_parameter_count == 4
5495 || !state->qam_demod_parameter_count) {
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005496 qam_demod_param_count = 4;
5497 status = qam_demodulator_command(state, qam_demod_param_count);
Mauro Carvalho Chehab5eee2bb2011-07-10 14:33:29 -03005498 }
Martin Blumenstingl9e23f50a2012-07-04 17:36:55 -03005499
5500 /* Use the 2-parameter command if it was requested or if we're
5501 * probing for the correct command and the 4-parameter command
5502 * failed. */
5503 if (state->qam_demod_parameter_count == 2
5504 || (!state->qam_demod_parameter_count && status < 0)) {
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005505 qam_demod_param_count = 2;
5506 status = qam_demodulator_command(state, qam_demod_param_count);
Martin Blumenstingl9e23f50a2012-07-04 17:36:55 -03005507 }
5508
5509 if (status < 0) {
Mauro Carvalho Chehab0fb220f2013-04-28 11:47:46 -03005510 dprintk(1, "Could not set demodulator parameters.\n");
5511 dprintk(1,
5512 "Make sure qam_demod_parameter_count (%d) is correct for your firmware (%s).\n",
Martin Blumenstingl9e23f50a2012-07-04 17:36:55 -03005513 state->qam_demod_parameter_count,
5514 state->microcode_name);
Mauro Carvalho Chehab5eee2bb2011-07-10 14:33:29 -03005515 goto error;
Martin Blumenstingl9e23f50a2012-07-04 17:36:55 -03005516 } else if (!state->qam_demod_parameter_count) {
Mauro Carvalho Chehab0fb220f2013-04-28 11:47:46 -03005517 dprintk(1,
5518 "Auto-probing the QAM command parameters was successful - using %d parameters.\n",
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005519 qam_demod_param_count);
Martin Blumenstingl9e23f50a2012-07-04 17:36:55 -03005520
Mauro Carvalho Chehab7eaf7182012-07-06 14:53:51 -03005521 /*
5522 * One of our commands was successful. We don't need to
5523 * auto-probe anymore, now that we got the correct command.
5524 */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005525 state->qam_demod_parameter_count = qam_demod_param_count;
Martin Blumenstingl9e23f50a2012-07-04 17:36:55 -03005526 }
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005527
Mauro Carvalho Chehab119faf92011-07-24 09:11:36 -03005528 /*
5529 * STEP 3: enable the system in a mode where the ADC provides valid
Mauro Carvalho Chehabed5452a2011-12-26 09:57:11 -03005530 * signal setup modulation independent registers
Mauro Carvalho Chehab119faf92011-07-24 09:11:36 -03005531 */
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005532#if 0
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005533 status = set_frequency(channel, tuner_freq_offset));
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005534 if (status < 0)
5535 goto error;
5536#endif
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03005537 status = set_frequency_shifter(state, intermediate_freqk_hz,
5538 tuner_freq_offset, true);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005539 if (status < 0)
5540 goto error;
5541
5542 /* Setup BER measurement */
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03005543 status = set_qam_measurement(state, state->m_constellation,
5544 state->props.symbol_rate);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005545 if (status < 0)
5546 goto error;
5547
5548 /* Reset default values */
5549 status = write16(state, IQM_CF_SCALE_SH__A, IQM_CF_SCALE_SH__PRE);
5550 if (status < 0)
5551 goto error;
5552 status = write16(state, QAM_SY_TIMEOUT__A, QAM_SY_TIMEOUT__PRE);
5553 if (status < 0)
5554 goto error;
5555
5556 /* Reset default LC values */
5557 status = write16(state, QAM_LC_RATE_LIMIT__A, 3);
5558 if (status < 0)
5559 goto error;
5560 status = write16(state, QAM_LC_LPF_FACTORP__A, 4);
5561 if (status < 0)
5562 goto error;
5563 status = write16(state, QAM_LC_LPF_FACTORI__A, 4);
5564 if (status < 0)
5565 goto error;
5566 status = write16(state, QAM_LC_MODE__A, 7);
5567 if (status < 0)
5568 goto error;
5569
5570 status = write16(state, QAM_LC_QUAL_TAB0__A, 1);
5571 if (status < 0)
5572 goto error;
5573 status = write16(state, QAM_LC_QUAL_TAB1__A, 1);
5574 if (status < 0)
5575 goto error;
5576 status = write16(state, QAM_LC_QUAL_TAB2__A, 1);
5577 if (status < 0)
5578 goto error;
5579 status = write16(state, QAM_LC_QUAL_TAB3__A, 1);
5580 if (status < 0)
5581 goto error;
5582 status = write16(state, QAM_LC_QUAL_TAB4__A, 2);
5583 if (status < 0)
5584 goto error;
5585 status = write16(state, QAM_LC_QUAL_TAB5__A, 2);
5586 if (status < 0)
5587 goto error;
5588 status = write16(state, QAM_LC_QUAL_TAB6__A, 2);
5589 if (status < 0)
5590 goto error;
5591 status = write16(state, QAM_LC_QUAL_TAB8__A, 2);
5592 if (status < 0)
5593 goto error;
5594 status = write16(state, QAM_LC_QUAL_TAB9__A, 2);
5595 if (status < 0)
5596 goto error;
5597 status = write16(state, QAM_LC_QUAL_TAB10__A, 2);
5598 if (status < 0)
5599 goto error;
5600 status = write16(state, QAM_LC_QUAL_TAB12__A, 2);
5601 if (status < 0)
5602 goto error;
5603 status = write16(state, QAM_LC_QUAL_TAB15__A, 3);
5604 if (status < 0)
5605 goto error;
5606 status = write16(state, QAM_LC_QUAL_TAB16__A, 3);
5607 if (status < 0)
5608 goto error;
5609 status = write16(state, QAM_LC_QUAL_TAB20__A, 4);
5610 if (status < 0)
5611 goto error;
5612 status = write16(state, QAM_LC_QUAL_TAB25__A, 4);
5613 if (status < 0)
5614 goto error;
5615
5616 /* Mirroring, QAM-block starting point not inverted */
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03005617 status = write16(state, QAM_SY_SP_INV__A,
5618 QAM_SY_SP_INV_SPECTRUM_INV_DIS);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005619 if (status < 0)
5620 goto error;
5621
5622 /* Halt SCU to enable safe non-atomic accesses */
5623 status = write16(state, SCU_COMM_EXEC__A, SCU_COMM_EXEC_HOLD);
5624 if (status < 0)
5625 goto error;
5626
Mauro Carvalho Chehabed5452a2011-12-26 09:57:11 -03005627 /* STEP 4: modulation specific setup */
5628 switch (state->props.modulation) {
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005629 case QAM_16:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005630 status = set_qam16(state);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005631 break;
5632 case QAM_32:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005633 status = set_qam32(state);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005634 break;
5635 case QAM_AUTO:
5636 case QAM_64:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005637 status = set_qam64(state);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005638 break;
5639 case QAM_128:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005640 status = set_qam128(state);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005641 break;
5642 case QAM_256:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005643 status = set_qam256(state);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005644 break;
5645 default:
5646 status = -EINVAL;
5647 break;
5648 }
5649 if (status < 0)
5650 goto error;
5651
5652 /* Activate SCU to enable SCU commands */
5653 status = write16(state, SCU_COMM_EXEC__A, SCU_COMM_EXEC_ACTIVE);
5654 if (status < 0)
5655 goto error;
5656
5657 /* Re-configure MPEG output, requires knowledge of channel bitrate */
Mauro Carvalho Chehabed5452a2011-12-26 09:57:11 -03005658 /* extAttr->currentChannel.modulation = channel->modulation; */
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005659 /* extAttr->currentChannel.symbolrate = channel->symbolrate; */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005660 status = mpegts_dto_setup(state, state->m_operation_mode);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005661 if (status < 0)
5662 goto error;
5663
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005664 /* start processes */
5665 status = mpegts_start(state);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005666 if (status < 0)
5667 goto error;
5668 status = write16(state, FEC_COMM_EXEC__A, FEC_COMM_EXEC_ACTIVE);
5669 if (status < 0)
5670 goto error;
5671 status = write16(state, QAM_COMM_EXEC__A, QAM_COMM_EXEC_ACTIVE);
5672 if (status < 0)
5673 goto error;
5674 status = write16(state, IQM_COMM_EXEC__A, IQM_COMM_EXEC_B_ACTIVE);
5675 if (status < 0)
5676 goto error;
5677
5678 /* STEP 5: start QAM demodulator (starts FEC, QAM and IQM HW) */
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03005679 status = scu_command(state, SCU_RAM_COMMAND_STANDARD_QAM
5680 | SCU_RAM_COMMAND_CMD_DEMOD_START,
5681 0, NULL, 1, &cmd_result);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005682 if (status < 0)
5683 goto error;
5684
5685 /* update global DRXK data container */
5686/*? extAttr->qamInterleaveMode = DRXK_QAM_I12_J17; */
5687
5688error:
5689 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03005690 pr_err("Error %d on %s\n", status, __func__);
Oliver Endrissebc7de22011-07-03 13:49:44 -03005691 return status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005692}
5693
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005694static int set_qam_standard(struct drxk_state *state,
5695 enum operation_mode o_mode)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005696{
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005697 int status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005698#ifdef DRXK_QAM_TAPS
5699#define DRXK_QAMA_TAPS_SELECT
5700#include "drxk_filters.h"
5701#undef DRXK_QAMA_TAPS_SELECT
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005702#endif
5703
Mauro Carvalho Chehabf1b82972011-07-10 13:08:44 -03005704 dprintk(1, "\n");
5705
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005706 /* added antenna switch */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005707 switch_antenna_to_qam(state);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005708
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005709 /* Ensure correct power-up mode */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005710 status = power_up_qam(state);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005711 if (status < 0)
5712 goto error;
5713 /* Reset QAM block */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005714 status = qam_reset_qam(state);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005715 if (status < 0)
5716 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005717
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005718 /* Setup IQM */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005719
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005720 status = write16(state, IQM_COMM_EXEC__A, IQM_COMM_EXEC_B_STOP);
5721 if (status < 0)
5722 goto error;
5723 status = write16(state, IQM_AF_AMUX__A, IQM_AF_AMUX_SIGNAL2ADC);
5724 if (status < 0)
5725 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005726
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005727 /* Upload IQM Channel Filter settings by
5728 boot loader from ROM table */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005729 switch (o_mode) {
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005730 case OM_QAM_ITU_A:
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03005731 status = bl_chain_cmd(state, DRXK_BL_ROM_OFFSET_TAPS_ITU_A,
5732 DRXK_BLCC_NR_ELEMENTS_TAPS,
5733 DRXK_BLC_TIMEOUT);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005734 break;
5735 case OM_QAM_ITU_C:
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03005736 status = bl_direct_cmd(state, IQM_CF_TAP_RE0__A,
5737 DRXK_BL_ROM_OFFSET_TAPS_ITU_C,
5738 DRXK_BLDC_NR_ELEMENTS_TAPS,
5739 DRXK_BLC_TIMEOUT);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03005740 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005741 goto error;
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03005742 status = bl_direct_cmd(state,
5743 IQM_CF_TAP_IM0__A,
5744 DRXK_BL_ROM_OFFSET_TAPS_ITU_C,
5745 DRXK_BLDC_NR_ELEMENTS_TAPS,
5746 DRXK_BLC_TIMEOUT);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005747 break;
5748 default:
5749 status = -EINVAL;
5750 }
5751 if (status < 0)
5752 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005753
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03005754 status = write16(state, IQM_CF_OUT_ENA__A, 1 << IQM_CF_OUT_ENA_QAM__B);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005755 if (status < 0)
5756 goto error;
5757 status = write16(state, IQM_CF_SYMMETRIC__A, 0);
5758 if (status < 0)
5759 goto error;
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03005760 status = write16(state, IQM_CF_MIDTAP__A,
5761 ((1 << IQM_CF_MIDTAP_RE__B) | (1 << IQM_CF_MIDTAP_IM__B)));
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005762 if (status < 0)
5763 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005764
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005765 status = write16(state, IQM_RC_STRETCH__A, 21);
5766 if (status < 0)
5767 goto error;
5768 status = write16(state, IQM_AF_CLP_LEN__A, 0);
5769 if (status < 0)
5770 goto error;
5771 status = write16(state, IQM_AF_CLP_TH__A, 448);
5772 if (status < 0)
5773 goto error;
5774 status = write16(state, IQM_AF_SNS_LEN__A, 0);
5775 if (status < 0)
5776 goto error;
5777 status = write16(state, IQM_CF_POW_MEAS_LEN__A, 0);
5778 if (status < 0)
5779 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005780
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005781 status = write16(state, IQM_FS_ADJ_SEL__A, 1);
5782 if (status < 0)
5783 goto error;
5784 status = write16(state, IQM_RC_ADJ_SEL__A, 1);
5785 if (status < 0)
5786 goto error;
5787 status = write16(state, IQM_CF_ADJ_SEL__A, 1);
5788 if (status < 0)
5789 goto error;
5790 status = write16(state, IQM_AF_UPD_SEL__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 /* IQM Impulse Noise Processing Unit */
5795 status = write16(state, IQM_CF_CLP_VAL__A, 500);
5796 if (status < 0)
5797 goto error;
5798 status = write16(state, IQM_CF_DATATH__A, 1000);
5799 if (status < 0)
5800 goto error;
5801 status = write16(state, IQM_CF_BYPASSDET__A, 1);
5802 if (status < 0)
5803 goto error;
5804 status = write16(state, IQM_CF_DET_LCT__A, 0);
5805 if (status < 0)
5806 goto error;
5807 status = write16(state, IQM_CF_WND_LEN__A, 1);
5808 if (status < 0)
5809 goto error;
5810 status = write16(state, IQM_CF_PKDTH__A, 1);
5811 if (status < 0)
5812 goto error;
5813 status = write16(state, IQM_AF_INC_BYPASS__A, 1);
5814 if (status < 0)
5815 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005816
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005817 /* turn on IQMAF. Must be done before setAgc**() */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005818 status = set_iqm_af(state, true);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005819 if (status < 0)
5820 goto error;
5821 status = write16(state, IQM_AF_START_LOCK__A, 0x01);
5822 if (status < 0)
5823 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005824
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005825 /* IQM will not be reset from here, sync ADC and update/init AGC */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005826 status = adc_synchronization(state);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005827 if (status < 0)
5828 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005829
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005830 /* Set the FSM step period */
5831 status = write16(state, SCU_RAM_QAM_FSM_STEP_PERIOD__A, 2000);
5832 if (status < 0)
5833 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005834
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005835 /* Halt SCU to enable safe non-atomic accesses */
5836 status = write16(state, SCU_COMM_EXEC__A, SCU_COMM_EXEC_HOLD);
5837 if (status < 0)
5838 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005839
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005840 /* No more resets of the IQM, current standard correctly set =>
5841 now AGCs can be configured. */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005842
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005843 status = init_agc(state, true);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005844 if (status < 0)
5845 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005846 status = set_pre_saw(state, &(state->m_qam_pre_saw_cfg));
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005847 if (status < 0)
5848 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005849
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005850 /* Configure AGC's */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005851 status = set_agc_rf(state, &(state->m_qam_rf_agc_cfg), true);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005852 if (status < 0)
5853 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005854 status = set_agc_if(state, &(state->m_qam_if_agc_cfg), true);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005855 if (status < 0)
5856 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005857
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005858 /* Activate SCU to enable SCU commands */
5859 status = write16(state, SCU_COMM_EXEC__A, SCU_COMM_EXEC_ACTIVE);
5860error:
5861 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03005862 pr_err("Error %d on %s\n", status, __func__);
Oliver Endrissebc7de22011-07-03 13:49:44 -03005863 return status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005864}
5865
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005866static int write_gpio(struct drxk_state *state)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005867{
Oliver Endrissebc7de22011-07-03 13:49:44 -03005868 int status;
5869 u16 value = 0;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005870
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03005871 dprintk(1, "\n");
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005872 /* stop lock indicator process */
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03005873 status = write16(state, SCU_RAM_GPIO__A,
5874 SCU_RAM_GPIO_HW_LOCK_IND_DISABLE);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005875 if (status < 0)
5876 goto error;
5877
5878 /* Write magic word to enable pdr reg write */
5879 status = write16(state, SIO_TOP_COMM_KEY__A, SIO_TOP_COMM_KEY_KEY);
5880 if (status < 0)
5881 goto error;
5882
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005883 if (state->m_has_sawsw) {
5884 if (state->uio_mask & 0x0001) { /* UIO-1 */
Mauro Carvalho Chehab90796ac2011-07-10 09:36:30 -03005885 /* write to io pad configuration register - output mode */
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03005886 status = write16(state, SIO_PDR_SMA_TX_CFG__A,
5887 state->m_gpio_cfg);
Mauro Carvalho Chehab90796ac2011-07-10 09:36:30 -03005888 if (status < 0)
5889 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005890
Mauro Carvalho Chehab90796ac2011-07-10 09:36:30 -03005891 /* use corresponding bit in io data output registar */
5892 status = read16(state, SIO_PDR_UIO_OUT_LO__A, &value);
5893 if (status < 0)
5894 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005895 if ((state->m_gpio & 0x0001) == 0)
Mauro Carvalho Chehab90796ac2011-07-10 09:36:30 -03005896 value &= 0x7FFF; /* write zero to 15th bit - 1st UIO */
5897 else
5898 value |= 0x8000; /* write one to 15th bit - 1st UIO */
5899 /* write back to io data output register */
5900 status = write16(state, SIO_PDR_UIO_OUT_LO__A, value);
5901 if (status < 0)
5902 goto error;
5903 }
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005904 if (state->uio_mask & 0x0002) { /* UIO-2 */
Mauro Carvalho Chehab90796ac2011-07-10 09:36:30 -03005905 /* write to io pad configuration register - output mode */
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03005906 status = write16(state, SIO_PDR_SMA_RX_CFG__A,
5907 state->m_gpio_cfg);
Mauro Carvalho Chehab90796ac2011-07-10 09:36:30 -03005908 if (status < 0)
5909 goto error;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005910
Mauro Carvalho Chehab90796ac2011-07-10 09:36:30 -03005911 /* use corresponding bit in io data output registar */
5912 status = read16(state, SIO_PDR_UIO_OUT_LO__A, &value);
5913 if (status < 0)
5914 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005915 if ((state->m_gpio & 0x0002) == 0)
Mauro Carvalho Chehab90796ac2011-07-10 09:36:30 -03005916 value &= 0xBFFF; /* write zero to 14th bit - 2st UIO */
5917 else
5918 value |= 0x4000; /* write one to 14th bit - 2st UIO */
5919 /* write back to io data output register */
5920 status = write16(state, SIO_PDR_UIO_OUT_LO__A, value);
5921 if (status < 0)
5922 goto error;
5923 }
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005924 if (state->uio_mask & 0x0004) { /* UIO-3 */
Mauro Carvalho Chehab90796ac2011-07-10 09:36:30 -03005925 /* write to io pad configuration register - output mode */
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03005926 status = write16(state, SIO_PDR_GPIO_CFG__A,
5927 state->m_gpio_cfg);
Mauro Carvalho Chehab90796ac2011-07-10 09:36:30 -03005928 if (status < 0)
5929 goto error;
5930
5931 /* use corresponding bit in io data output registar */
5932 status = read16(state, SIO_PDR_UIO_OUT_LO__A, &value);
5933 if (status < 0)
5934 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005935 if ((state->m_gpio & 0x0004) == 0)
Mauro Carvalho Chehab90796ac2011-07-10 09:36:30 -03005936 value &= 0xFFFB; /* write zero to 2nd bit - 3rd UIO */
5937 else
5938 value |= 0x0004; /* write one to 2nd bit - 3rd UIO */
5939 /* write back to io data output register */
5940 status = write16(state, SIO_PDR_UIO_OUT_LO__A, value);
5941 if (status < 0)
5942 goto error;
5943 }
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005944 }
5945 /* Write magic word to disable pdr reg write */
5946 status = write16(state, SIO_TOP_COMM_KEY__A, 0x0000);
5947error:
5948 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03005949 pr_err("Error %d on %s\n", status, __func__);
Oliver Endrissebc7de22011-07-03 13:49:44 -03005950 return status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005951}
5952
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005953static int switch_antenna_to_qam(struct drxk_state *state)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005954{
Mauro Carvalho Chehab147e1102011-07-10 08:24:26 -03005955 int status = 0;
Mauro Carvalho Chehab90796ac2011-07-10 09:36:30 -03005956 bool gpio_state;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005957
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03005958 dprintk(1, "\n");
Mauro Carvalho Chehab147e1102011-07-10 08:24:26 -03005959
Mauro Carvalho Chehab90796ac2011-07-10 09:36:30 -03005960 if (!state->antenna_gpio)
5961 return 0;
5962
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005963 gpio_state = state->m_gpio & state->antenna_gpio;
Mauro Carvalho Chehab90796ac2011-07-10 09:36:30 -03005964
5965 if (state->antenna_dvbt ^ gpio_state) {
5966 /* Antenna is on DVB-T mode. Switch */
5967 if (state->antenna_dvbt)
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005968 state->m_gpio &= ~state->antenna_gpio;
Mauro Carvalho Chehab90796ac2011-07-10 09:36:30 -03005969 else
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005970 state->m_gpio |= state->antenna_gpio;
5971 status = write_gpio(state);
Oliver Endrissebc7de22011-07-03 13:49:44 -03005972 }
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005973 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03005974 pr_err("Error %d on %s\n", status, __func__);
Oliver Endrissebc7de22011-07-03 13:49:44 -03005975 return status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005976}
5977
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005978static int switch_antenna_to_dvbt(struct drxk_state *state)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005979{
Mauro Carvalho Chehab147e1102011-07-10 08:24:26 -03005980 int status = 0;
Mauro Carvalho Chehab90796ac2011-07-10 09:36:30 -03005981 bool gpio_state;
Oliver Endrissebc7de22011-07-03 13:49:44 -03005982
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03005983 dprintk(1, "\n");
Mauro Carvalho Chehab90796ac2011-07-10 09:36:30 -03005984
5985 if (!state->antenna_gpio)
5986 return 0;
5987
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005988 gpio_state = state->m_gpio & state->antenna_gpio;
Mauro Carvalho Chehab90796ac2011-07-10 09:36:30 -03005989
5990 if (!(state->antenna_dvbt ^ gpio_state)) {
5991 /* Antenna is on DVB-C mode. Switch */
5992 if (state->antenna_dvbt)
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005993 state->m_gpio |= state->antenna_gpio;
Mauro Carvalho Chehab90796ac2011-07-10 09:36:30 -03005994 else
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03005995 state->m_gpio &= ~state->antenna_gpio;
5996 status = write_gpio(state);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03005997 }
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03005998 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03005999 pr_err("Error %d on %s\n", status, __func__);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006000 return status;
6001}
6002
6003
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006004static int power_down_device(struct drxk_state *state)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006005{
6006 /* Power down to requested mode */
6007 /* Backup some register settings */
6008 /* Set pins with possible pull-ups connected to them in input mode */
6009 /* Analog power down */
6010 /* ADC power down */
6011 /* Power down device */
6012 int status;
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03006013
6014 dprintk(1, "\n");
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006015 if (state->m_b_p_down_open_bridge) {
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006016 /* Open I2C bridge before power down of DRXK */
6017 status = ConfigureI2CBridge(state, true);
Mauro Carvalho Chehabea90f012011-07-03 18:06:07 -03006018 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006019 goto error;
6020 }
6021 /* driver 0.9.0 */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006022 status = dvbt_enable_ofdm_token_ring(state, false);
Oliver Endrissebc7de22011-07-03 13:49:44 -03006023 if (status < 0)
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006024 goto error;
Oliver Endrissebc7de22011-07-03 13:49:44 -03006025
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03006026 status = write16(state, SIO_CC_PWD_MODE__A,
6027 SIO_CC_PWD_MODE_LEVEL_CLOCK);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006028 if (status < 0)
6029 goto error;
6030 status = write16(state, SIO_CC_UPDATE__A, SIO_CC_UPDATE_KEY);
6031 if (status < 0)
6032 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006033 state->m_hi_cfg_ctrl |= SIO_HI_RA_RAM_PAR_5_CFG_SLEEP_ZZZ;
6034 status = hi_cfg_command(state);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006035error:
6036 if (status < 0)
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03006037 pr_err("Error %d on %s\n", status, __func__);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006038
6039 return status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006040}
6041
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006042static int init_drxk(struct drxk_state *state)
6043{
Mauro Carvalho Chehab177bc7d2012-06-21 09:36:38 -03006044 int status = 0, n = 0;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006045 enum drx_power_mode power_mode = DRXK_POWER_DOWN_OFDM;
6046 u16 driver_version;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006047
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03006048 dprintk(1, "\n");
Nick Desaulniers7369bbf2017-12-18 12:31:52 -05006049 if (state->m_drxk_state == DRXK_UNINITIALIZED) {
Mauro Carvalho Chehab20bfe7a2012-06-29 14:43:32 -03006050 drxk_i2c_lock(state);
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006051 status = power_up_device(state);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006052 if (status < 0)
6053 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006054 status = drxx_open(state);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006055 if (status < 0)
6056 goto error;
6057 /* Soft reset of OFDM-, sys- and osc-clockdomain */
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03006058 status = write16(state, SIO_CC_SOFT_RST__A,
6059 SIO_CC_SOFT_RST_OFDM__M
6060 | SIO_CC_SOFT_RST_SYS__M
6061 | SIO_CC_SOFT_RST_OSC__M);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006062 if (status < 0)
6063 goto error;
6064 status = write16(state, SIO_CC_UPDATE__A, SIO_CC_UPDATE_KEY);
6065 if (status < 0)
6066 goto error;
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03006067 /*
6068 * TODO is this needed? If yes, how much delay in
6069 * worst case scenario
6070 */
Mauro Carvalho Chehabb72852b2013-04-28 11:47:47 -03006071 usleep_range(1000, 2000);
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006072 state->m_drxk_a3_patch_code = true;
6073 status = get_device_capabilities(state);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006074 if (status < 0)
6075 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006076
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006077 /* Bridge delay, uses oscilator clock */
6078 /* Delay = (delay (nano seconds) * oscclk (kHz))/ 1000 */
6079 /* SDA brdige delay */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006080 state->m_hi_cfg_bridge_delay =
6081 (u16) ((state->m_osc_clock_freq / 1000) *
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006082 HI_I2C_BRIDGE_DELAY) / 1000;
6083 /* Clipping */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006084 if (state->m_hi_cfg_bridge_delay >
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006085 SIO_HI_RA_RAM_PAR_3_CFG_DBL_SDA__M) {
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006086 state->m_hi_cfg_bridge_delay =
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006087 SIO_HI_RA_RAM_PAR_3_CFG_DBL_SDA__M;
6088 }
6089 /* SCL bridge delay, same as SDA for now */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006090 state->m_hi_cfg_bridge_delay +=
6091 state->m_hi_cfg_bridge_delay <<
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006092 SIO_HI_RA_RAM_PAR_3_CFG_DBL_SCL__B;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006093
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006094 status = init_hi(state);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006095 if (status < 0)
6096 goto error;
6097 /* disable various processes */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006098#if NOA1ROM
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006099 if (!(state->m_DRXK_A1_ROM_CODE)
6100 && !(state->m_DRXK_A2_ROM_CODE))
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006101#endif
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006102 {
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03006103 status = write16(state, SCU_RAM_GPIO__A,
6104 SCU_RAM_GPIO_HW_LOCK_IND_DISABLE);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006105 if (status < 0)
6106 goto error;
6107 }
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006108
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006109 /* disable MPEG port */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006110 status = mpegts_disable(state);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006111 if (status < 0)
6112 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006113
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006114 /* Stop AUD and SCU */
6115 status = write16(state, AUD_COMM_EXEC__A, AUD_COMM_EXEC_STOP);
6116 if (status < 0)
6117 goto error;
6118 status = write16(state, SCU_COMM_EXEC__A, SCU_COMM_EXEC_STOP);
6119 if (status < 0)
6120 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006121
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006122 /* enable token-ring bus through OFDM block for possible ucode upload */
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03006123 status = write16(state, SIO_OFDM_SH_OFDM_RING_ENABLE__A,
6124 SIO_OFDM_SH_OFDM_RING_ENABLE_ON);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006125 if (status < 0)
6126 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006127
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006128 /* include boot loader section */
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03006129 status = write16(state, SIO_BL_COMM_EXEC__A,
6130 SIO_BL_COMM_EXEC_ACTIVE);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006131 if (status < 0)
6132 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006133 status = bl_chain_cmd(state, 0, 6, 100);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006134 if (status < 0)
6135 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006136
Mauro Carvalho Chehab177bc7d2012-06-21 09:36:38 -03006137 if (state->fw) {
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006138 status = download_microcode(state, state->fw->data,
Mauro Carvalho Chehab177bc7d2012-06-21 09:36:38 -03006139 state->fw->size);
6140 if (status < 0)
6141 goto error;
6142 }
Mauro Carvalho Chehabe4f4f872011-07-09 17:35:26 -03006143
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006144 /* disable token-ring bus through OFDM block for possible ucode upload */
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03006145 status = write16(state, SIO_OFDM_SH_OFDM_RING_ENABLE__A,
6146 SIO_OFDM_SH_OFDM_RING_ENABLE_OFF);
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 Chehabbe44eb22011-07-10 01:49:53 -03006150 /* Run SCU for a little while to initialize microcode version numbers */
6151 status = write16(state, SCU_COMM_EXEC__A, SCU_COMM_EXEC_ACTIVE);
6152 if (status < 0)
6153 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006154 status = drxx_open(state);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006155 if (status < 0)
6156 goto error;
6157 /* added for test */
6158 msleep(30);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006159
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006160 power_mode = DRXK_POWER_DOWN_OFDM;
6161 status = ctrl_power_mode(state, &power_mode);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006162 if (status < 0)
6163 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006164
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006165 /* Stamp driver version number in SCU data RAM in BCD code
Jonathan McCrohan39c1cb22013-10-20 21:34:01 -03006166 Done to enable field application engineers to retrieve drxdriver version
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006167 via I2C from SCU RAM.
6168 Not using SCU command interface for SCU register access since no
6169 microcode may be present.
6170 */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006171 driver_version =
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006172 (((DRXK_VERSION_MAJOR / 100) % 10) << 12) +
6173 (((DRXK_VERSION_MAJOR / 10) % 10) << 8) +
6174 ((DRXK_VERSION_MAJOR % 10) << 4) +
6175 (DRXK_VERSION_MINOR % 10);
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03006176 status = write16(state, SCU_RAM_DRIVER_VER_HI__A,
6177 driver_version);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006178 if (status < 0)
6179 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006180 driver_version =
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006181 (((DRXK_VERSION_PATCH / 1000) % 10) << 12) +
6182 (((DRXK_VERSION_PATCH / 100) % 10) << 8) +
6183 (((DRXK_VERSION_PATCH / 10) % 10) << 4) +
6184 (DRXK_VERSION_PATCH % 10);
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03006185 status = write16(state, SCU_RAM_DRIVER_VER_LO__A,
6186 driver_version);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006187 if (status < 0)
6188 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006189
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03006190 pr_info("DRXK driver version %d.%d.%d\n",
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006191 DRXK_VERSION_MAJOR, DRXK_VERSION_MINOR,
6192 DRXK_VERSION_PATCH);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006193
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03006194 /*
6195 * Dirty fix of default values for ROM/PATCH microcode
6196 * Dirty because this fix makes it impossible to setup
6197 * suitable values before calling DRX_Open. This solution
6198 * requires changes to RF AGC speed to be done via the CTRL
6199 * function after calling DRX_Open
6200 */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006201
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006202 /* m_dvbt_rf_agc_cfg.speed = 3; */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006203
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006204 /* Reset driver debug flags to 0 */
6205 status = write16(state, SCU_RAM_DRIVER_DEBUG__A, 0);
6206 if (status < 0)
6207 goto error;
6208 /* driver 0.9.0 */
6209 /* Setup FEC OC:
6210 NOTE: No more full FEC resets allowed afterwards!! */
6211 status = write16(state, FEC_COMM_EXEC__A, FEC_COMM_EXEC_STOP);
6212 if (status < 0)
6213 goto error;
6214 /* MPEGTS functions are still the same */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006215 status = mpegts_dto_init(state);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006216 if (status < 0)
6217 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006218 status = mpegts_stop(state);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006219 if (status < 0)
6220 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006221 status = mpegts_configure_polarity(state);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006222 if (status < 0)
6223 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006224 status = mpegts_configure_pins(state, state->m_enable_mpeg_output);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006225 if (status < 0)
6226 goto error;
6227 /* added: configure GPIO */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006228 status = write_gpio(state);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006229 if (status < 0)
6230 goto error;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006231
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006232 state->m_drxk_state = DRXK_STOPPED;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006233
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006234 if (state->m_b_power_down) {
6235 status = power_down_device(state);
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006236 if (status < 0)
6237 goto error;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006238 state->m_drxk_state = DRXK_POWERED_DOWN;
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006239 } else
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006240 state->m_drxk_state = DRXK_STOPPED;
Mauro Carvalho Chehab177bc7d2012-06-21 09:36:38 -03006241
6242 /* Initialize the supported delivery systems */
6243 n = 0;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006244 if (state->m_has_dvbc) {
Mauro Carvalho Chehab177bc7d2012-06-21 09:36:38 -03006245 state->frontend.ops.delsys[n++] = SYS_DVBC_ANNEX_A;
6246 state->frontend.ops.delsys[n++] = SYS_DVBC_ANNEX_C;
6247 strlcat(state->frontend.ops.info.name, " DVB-C",
6248 sizeof(state->frontend.ops.info.name));
6249 }
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006250 if (state->m_has_dvbt) {
Mauro Carvalho Chehab177bc7d2012-06-21 09:36:38 -03006251 state->frontend.ops.delsys[n++] = SYS_DVBT;
6252 strlcat(state->frontend.ops.info.name, " DVB-T",
6253 sizeof(state->frontend.ops.info.name));
6254 }
Mauro Carvalho Chehab20bfe7a2012-06-29 14:43:32 -03006255 drxk_i2c_unlock(state);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006256 }
Mauro Carvalho Chehabbe44eb22011-07-10 01:49:53 -03006257error:
Mauro Carvalho Chehab20bfe7a2012-06-29 14:43:32 -03006258 if (status < 0) {
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006259 state->m_drxk_state = DRXK_NO_DEV;
Mauro Carvalho Chehab20bfe7a2012-06-29 14:43:32 -03006260 drxk_i2c_unlock(state);
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03006261 pr_err("Error %d on %s\n", status, __func__);
Mauro Carvalho Chehab20bfe7a2012-06-29 14:43:32 -03006262 }
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006263
Mauro Carvalho Chehabe716ada2011-07-21 19:35:04 -03006264 return status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006265}
6266
Mauro Carvalho Chehab177bc7d2012-06-21 09:36:38 -03006267static void load_firmware_cb(const struct firmware *fw,
6268 void *context)
6269{
6270 struct drxk_state *state = context;
6271
Mauro Carvalho Chehab704a28e2012-06-29 15:45:04 -03006272 dprintk(1, ": %s\n", fw ? "firmware loaded" : "firmware not loaded");
Mauro Carvalho Chehab177bc7d2012-06-21 09:36:38 -03006273 if (!fw) {
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03006274 pr_err("Could not load firmware file %s.\n",
Mauro Carvalho Chehab177bc7d2012-06-21 09:36:38 -03006275 state->microcode_name);
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03006276 pr_info("Copy %s to your hotplug directory!\n",
Mauro Carvalho Chehab177bc7d2012-06-21 09:36:38 -03006277 state->microcode_name);
6278 state->microcode_name = NULL;
6279
6280 /*
6281 * As firmware is now load asynchronous, it is not possible
6282 * anymore to fail at frontend attach. We might silently
6283 * return here, and hope that the driver won't crash.
6284 * We might also change all DVB callbacks to return -ENODEV
6285 * if the device is not initialized.
6286 * As the DRX-K devices have their own internal firmware,
6287 * let's just hope that it will match a firmware revision
6288 * compatible with this driver and proceed.
6289 */
6290 }
6291 state->fw = fw;
6292
6293 init_drxk(state);
6294}
6295
Mauro Carvalho Chehabfa4b2a12012-01-05 08:07:32 -02006296static void drxk_release(struct dvb_frontend *fe)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006297{
Oliver Endrissebc7de22011-07-03 13:49:44 -03006298 struct drxk_state *state = fe->demodulator_priv;
6299
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03006300 dprintk(1, "\n");
Markus Elfring9bc2dd72014-11-19 18:27:24 -03006301 release_firmware(state->fw);
Mauro Carvalho Chehab177bc7d2012-06-21 09:36:38 -03006302
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006303 kfree(state);
6304}
6305
Mauro Carvalho Chehabfa4b2a12012-01-05 08:07:32 -02006306static int drxk_sleep(struct dvb_frontend *fe)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006307{
Oliver Endrissebc7de22011-07-03 13:49:44 -03006308 struct drxk_state *state = fe->demodulator_priv;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006309
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03006310 dprintk(1, "\n");
Mauro Carvalho Chehab704a28e2012-06-29 15:45:04 -03006311
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006312 if (state->m_drxk_state == DRXK_NO_DEV)
Mauro Carvalho Chehab704a28e2012-06-29 15:45:04 -03006313 return -ENODEV;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006314 if (state->m_drxk_state == DRXK_UNINITIALIZED)
Mauro Carvalho Chehab704a28e2012-06-29 15:45:04 -03006315 return 0;
6316
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006317 shut_down(state);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006318 return 0;
6319}
6320
Oliver Endrissebc7de22011-07-03 13:49:44 -03006321static int drxk_gate_ctrl(struct dvb_frontend *fe, int enable)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006322{
6323 struct drxk_state *state = fe->demodulator_priv;
6324
Martin Blumenstingl257ee972012-07-04 17:38:23 -03006325 dprintk(1, ": %s\n", enable ? "enable" : "disable");
Mauro Carvalho Chehab704a28e2012-06-29 15:45:04 -03006326
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006327 if (state->m_drxk_state == DRXK_NO_DEV)
Mauro Carvalho Chehab704a28e2012-06-29 15:45:04 -03006328 return -ENODEV;
6329
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006330 return ConfigureI2CBridge(state, enable ? true : false);
6331}
6332
Mauro Carvalho Chehabed5452a2011-12-26 09:57:11 -03006333static int drxk_set_parameters(struct dvb_frontend *fe)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006334{
Mauro Carvalho Chehabed5452a2011-12-26 09:57:11 -03006335 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
Mauro Carvalho Chehab6cb393c2012-01-05 09:26:40 -02006336 u32 delsys = p->delivery_system, old_delsys;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006337 struct drxk_state *state = fe->demodulator_priv;
6338 u32 IF;
6339
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03006340 dprintk(1, "\n");
Mauro Carvalho Chehab8513e142011-09-03 11:40:02 -03006341
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006342 if (state->m_drxk_state == DRXK_NO_DEV)
Mauro Carvalho Chehab704a28e2012-06-29 15:45:04 -03006343 return -ENODEV;
6344
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006345 if (state->m_drxk_state == DRXK_UNINITIALIZED)
Mauro Carvalho Chehab704a28e2012-06-29 15:45:04 -03006346 return -EAGAIN;
6347
Mauro Carvalho Chehab8513e142011-09-03 11:40:02 -03006348 if (!fe->ops.tuner_ops.get_if_frequency) {
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03006349 pr_err("Error: get_if_frequency() not defined at tuner. Can't work without it!\n");
Mauro Carvalho Chehab8513e142011-09-03 11:40:02 -03006350 return -EINVAL;
6351 }
6352
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006353 if (fe->ops.i2c_gate_ctrl)
6354 fe->ops.i2c_gate_ctrl(fe, 1);
6355 if (fe->ops.tuner_ops.set_params)
Mauro Carvalho Chehab14d24d12011-12-24 12:24:33 -03006356 fe->ops.tuner_ops.set_params(fe);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006357 if (fe->ops.i2c_gate_ctrl)
6358 fe->ops.i2c_gate_ctrl(fe, 0);
Mauro Carvalho Chehab6cb393c2012-01-05 09:26:40 -02006359
6360 old_delsys = state->props.delivery_system;
Mauro Carvalho Chehabed5452a2011-12-26 09:57:11 -03006361 state->props = *p;
Mauro Carvalho Chehabfa4b2a12012-01-05 08:07:32 -02006362
Mauro Carvalho Chehab6cb393c2012-01-05 09:26:40 -02006363 if (old_delsys != delsys) {
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006364 shut_down(state);
Mauro Carvalho Chehab6cb393c2012-01-05 09:26:40 -02006365 switch (delsys) {
6366 case SYS_DVBC_ANNEX_A:
6367 case SYS_DVBC_ANNEX_C:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006368 if (!state->m_has_dvbc)
Mauro Carvalho Chehab6cb393c2012-01-05 09:26:40 -02006369 return -EINVAL;
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03006370 state->m_itut_annex_c = (delsys == SYS_DVBC_ANNEX_C) ?
6371 true : false;
Mauro Carvalho Chehab6cb393c2012-01-05 09:26:40 -02006372 if (state->m_itut_annex_c)
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006373 setoperation_mode(state, OM_QAM_ITU_C);
Mauro Carvalho Chehab6cb393c2012-01-05 09:26:40 -02006374 else
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006375 setoperation_mode(state, OM_QAM_ITU_A);
Mauro Carvalho Chehabfa4b2a12012-01-05 08:07:32 -02006376 break;
Mauro Carvalho Chehab6cb393c2012-01-05 09:26:40 -02006377 case SYS_DVBT:
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006378 if (!state->m_has_dvbt)
Mauro Carvalho Chehab6cb393c2012-01-05 09:26:40 -02006379 return -EINVAL;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006380 setoperation_mode(state, OM_DVBT);
Mauro Carvalho Chehab6cb393c2012-01-05 09:26:40 -02006381 break;
6382 default:
Mauro Carvalho Chehabfa4b2a12012-01-05 08:07:32 -02006383 return -EINVAL;
Mauro Carvalho Chehab6cb393c2012-01-05 09:26:40 -02006384 }
Mauro Carvalho Chehabfa4b2a12012-01-05 08:07:32 -02006385 }
6386
Mauro Carvalho Chehab8513e142011-09-03 11:40:02 -03006387 fe->ops.tuner_ops.get_if_frequency(fe, &IF);
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006388 start(state, 0, IF);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006389
Jonathan McCrohan39c1cb22013-10-20 21:34:01 -03006390 /* After set_frontend, stats aren't available */
Mauro Carvalho Chehab8f3741e2013-03-20 06:15:45 -03006391 p->strength.stat[0].scale = FE_SCALE_RELATIVE;
6392 p->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
6393 p->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
6394 p->block_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
6395 p->pre_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
6396 p->pre_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
6397 p->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
6398 p->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
6399
Mauro Carvalho Chehabe0e6eca2011-07-04 08:27:47 -03006400 /* printk(KERN_DEBUG "drxk: %s IF=%d done\n", __func__, IF); */
Oliver Endrissebc7de22011-07-03 13:49:44 -03006401
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006402 return 0;
6403}
6404
Mauro Carvalho Chehab59a7a232013-03-20 08:21:52 -03006405static int get_strength(struct drxk_state *state, u64 *strength)
6406{
6407 int status;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006408 struct s_cfg_agc rf_agc, if_agc;
6409 u32 total_gain = 0;
Mauro Carvalho Chehab59a7a232013-03-20 08:21:52 -03006410 u32 atten = 0;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006411 u32 agc_range = 0;
Mauro Carvalho Chehab59a7a232013-03-20 08:21:52 -03006412 u16 scu_lvl = 0;
6413 u16 scu_coc = 0;
6414 /* FIXME: those are part of the tuner presets */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006415 u16 tuner_rf_gain = 50; /* Default value on az6007 driver */
6416 u16 tuner_if_gain = 40; /* Default value on az6007 driver */
Mauro Carvalho Chehab59a7a232013-03-20 08:21:52 -03006417
6418 *strength = 0;
6419
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006420 if (is_dvbt(state)) {
6421 rf_agc = state->m_dvbt_rf_agc_cfg;
6422 if_agc = state->m_dvbt_if_agc_cfg;
6423 } else if (is_qam(state)) {
6424 rf_agc = state->m_qam_rf_agc_cfg;
6425 if_agc = state->m_qam_if_agc_cfg;
Mauro Carvalho Chehab59a7a232013-03-20 08:21:52 -03006426 } else {
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006427 rf_agc = state->m_atv_rf_agc_cfg;
6428 if_agc = state->m_atv_if_agc_cfg;
Mauro Carvalho Chehab59a7a232013-03-20 08:21:52 -03006429 }
6430
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006431 if (rf_agc.ctrl_mode == DRXK_AGC_CTRL_AUTO) {
6432 /* SCU output_level */
Mauro Carvalho Chehab59a7a232013-03-20 08:21:52 -03006433 status = read16(state, SCU_RAM_AGC_RF_IACCU_HI__A, &scu_lvl);
6434 if (status < 0)
6435 return status;
6436
6437 /* SCU c.o.c. */
Christophe JAILLETd259a5e2016-08-10 02:54:41 -03006438 status = read16(state, SCU_RAM_AGC_RF_IACCU_HI_CO__A, &scu_coc);
Mauro Carvalho Chehab59a7a232013-03-20 08:21:52 -03006439 if (status < 0)
6440 return status;
6441
6442 if (((u32) scu_lvl + (u32) scu_coc) < 0xffff)
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006443 rf_agc.output_level = scu_lvl + scu_coc;
Mauro Carvalho Chehab59a7a232013-03-20 08:21:52 -03006444 else
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006445 rf_agc.output_level = 0xffff;
Mauro Carvalho Chehab59a7a232013-03-20 08:21:52 -03006446
6447 /* Take RF gain into account */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006448 total_gain += tuner_rf_gain;
Mauro Carvalho Chehab59a7a232013-03-20 08:21:52 -03006449
6450 /* clip output value */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006451 if (rf_agc.output_level < rf_agc.min_output_level)
6452 rf_agc.output_level = rf_agc.min_output_level;
6453 if (rf_agc.output_level > rf_agc.max_output_level)
6454 rf_agc.output_level = rf_agc.max_output_level;
Mauro Carvalho Chehab59a7a232013-03-20 08:21:52 -03006455
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006456 agc_range = (u32) (rf_agc.max_output_level - rf_agc.min_output_level);
6457 if (agc_range > 0) {
Mauro Carvalho Chehab59a7a232013-03-20 08:21:52 -03006458 atten += 100UL *
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006459 ((u32)(tuner_rf_gain)) *
6460 ((u32)(rf_agc.output_level - rf_agc.min_output_level))
6461 / agc_range;
Mauro Carvalho Chehab59a7a232013-03-20 08:21:52 -03006462 }
6463 }
6464
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006465 if (if_agc.ctrl_mode == DRXK_AGC_CTRL_AUTO) {
Mauro Carvalho Chehab59a7a232013-03-20 08:21:52 -03006466 status = read16(state, SCU_RAM_AGC_IF_IACCU_HI__A,
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006467 &if_agc.output_level);
Mauro Carvalho Chehab59a7a232013-03-20 08:21:52 -03006468 if (status < 0)
6469 return status;
6470
6471 status = read16(state, SCU_RAM_AGC_INGAIN_TGT_MIN__A,
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006472 &if_agc.top);
Mauro Carvalho Chehab59a7a232013-03-20 08:21:52 -03006473 if (status < 0)
6474 return status;
6475
6476 /* Take IF gain into account */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006477 total_gain += (u32) tuner_if_gain;
Mauro Carvalho Chehab59a7a232013-03-20 08:21:52 -03006478
6479 /* clip output value */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006480 if (if_agc.output_level < if_agc.min_output_level)
6481 if_agc.output_level = if_agc.min_output_level;
6482 if (if_agc.output_level > if_agc.max_output_level)
6483 if_agc.output_level = if_agc.max_output_level;
Mauro Carvalho Chehab59a7a232013-03-20 08:21:52 -03006484
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03006485 agc_range = (u32)(if_agc.max_output_level - if_agc.min_output_level);
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006486 if (agc_range > 0) {
Mauro Carvalho Chehab59a7a232013-03-20 08:21:52 -03006487 atten += 100UL *
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006488 ((u32)(tuner_if_gain)) *
6489 ((u32)(if_agc.output_level - if_agc.min_output_level))
6490 / agc_range;
Mauro Carvalho Chehab59a7a232013-03-20 08:21:52 -03006491 }
6492 }
6493
6494 /*
6495 * Convert to 0..65535 scale.
6496 * If it can't be measured (AGC is disabled), just show 100%.
6497 */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006498 if (total_gain > 0)
6499 *strength = (65535UL * atten / total_gain / 100);
Mauro Carvalho Chehab59a7a232013-03-20 08:21:52 -03006500 else
6501 *strength = 65535;
6502
6503 return 0;
6504}
6505
Mauro Carvalho Chehab8f3741e2013-03-20 06:15:45 -03006506static int drxk_get_stats(struct dvb_frontend *fe)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006507{
Mauro Carvalho Chehab8f3741e2013-03-20 06:15:45 -03006508 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006509 struct drxk_state *state = fe->demodulator_priv;
Mauro Carvalho Chehab8f3741e2013-03-20 06:15:45 -03006510 int status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006511 u32 stat;
Mauro Carvalho Chehab8f3741e2013-03-20 06:15:45 -03006512 u16 reg16;
6513 u32 post_bit_count;
6514 u32 post_bit_err_count;
6515 u32 post_bit_error_scale;
6516 u32 pre_bit_err_count;
6517 u32 pre_bit_count;
6518 u32 pkt_count;
6519 u32 pkt_error_count;
Mauro Carvalho Chehab59a7a232013-03-20 08:21:52 -03006520 s32 cnr;
Mauro Carvalho Chehab704a28e2012-06-29 15:45:04 -03006521
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006522 if (state->m_drxk_state == DRXK_NO_DEV)
Mauro Carvalho Chehab704a28e2012-06-29 15:45:04 -03006523 return -ENODEV;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006524 if (state->m_drxk_state == DRXK_UNINITIALIZED)
Mauro Carvalho Chehab704a28e2012-06-29 15:45:04 -03006525 return -EAGAIN;
6526
Mauro Carvalho Chehab8f3741e2013-03-20 06:15:45 -03006527 /* get status */
6528 state->fe_status = 0;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006529 get_lock_status(state, &stat);
Oliver Endrissebc7de22011-07-03 13:49:44 -03006530 if (stat == MPEG_LOCK)
Mauro Carvalho Chehab8f3741e2013-03-20 06:15:45 -03006531 state->fe_status |= 0x1f;
Oliver Endrissebc7de22011-07-03 13:49:44 -03006532 if (stat == FEC_LOCK)
Mauro Carvalho Chehab8f3741e2013-03-20 06:15:45 -03006533 state->fe_status |= 0x0f;
Oliver Endrissebc7de22011-07-03 13:49:44 -03006534 if (stat == DEMOD_LOCK)
Mauro Carvalho Chehab8f3741e2013-03-20 06:15:45 -03006535 state->fe_status |= 0x07;
6536
Mauro Carvalho Chehab59a7a232013-03-20 08:21:52 -03006537 /*
6538 * Estimate signal strength from AGC
6539 */
6540 get_strength(state, &c->strength.stat[0].uvalue);
6541 c->strength.stat[0].scale = FE_SCALE_RELATIVE;
6542
6543
Mauro Carvalho Chehab8f3741e2013-03-20 06:15:45 -03006544 if (stat >= DEMOD_LOCK) {
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006545 get_signal_to_noise(state, &cnr);
Mauro Carvalho Chehab8f3741e2013-03-20 06:15:45 -03006546 c->cnr.stat[0].svalue = cnr * 100;
6547 c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
6548 } else {
6549 c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
6550 }
6551
6552 if (stat < FEC_LOCK) {
6553 c->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
6554 c->block_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
6555 c->pre_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
6556 c->pre_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
6557 c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
6558 c->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
6559 return 0;
6560 }
6561
6562 /* Get post BER */
6563
6564 /* BER measurement is valid if at least FEC lock is achieved */
6565
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03006566 /*
6567 * OFDM_EC_VD_REQ_SMB_CNT__A and/or OFDM_EC_VD_REQ_BIT_CNT can be
6568 * written to set nr of symbols or bits over which to measure
6569 * EC_VD_REG_ERR_BIT_CNT__A . See CtrlSetCfg().
6570 */
Mauro Carvalho Chehab8f3741e2013-03-20 06:15:45 -03006571
6572 /* Read registers for post/preViterbi BER calculation */
6573 status = read16(state, OFDM_EC_VD_ERR_BIT_CNT__A, &reg16);
6574 if (status < 0)
6575 goto error;
6576 pre_bit_err_count = reg16;
6577
6578 status = read16(state, OFDM_EC_VD_IN_BIT_CNT__A , &reg16);
6579 if (status < 0)
6580 goto error;
6581 pre_bit_count = reg16;
6582
6583 /* Number of bit-errors */
6584 status = read16(state, FEC_RS_NR_BIT_ERRORS__A, &reg16);
6585 if (status < 0)
6586 goto error;
6587 post_bit_err_count = reg16;
6588
6589 status = read16(state, FEC_RS_MEASUREMENT_PRESCALE__A, &reg16);
6590 if (status < 0)
6591 goto error;
6592 post_bit_error_scale = reg16;
6593
6594 status = read16(state, FEC_RS_MEASUREMENT_PERIOD__A, &reg16);
6595 if (status < 0)
6596 goto error;
6597 pkt_count = reg16;
6598
6599 status = read16(state, SCU_RAM_FEC_ACCUM_PKT_FAILURES__A, &reg16);
6600 if (status < 0)
6601 goto error;
6602 pkt_error_count = reg16;
6603 write16(state, SCU_RAM_FEC_ACCUM_PKT_FAILURES__A, 0);
6604
6605 post_bit_err_count *= post_bit_error_scale;
6606
6607 post_bit_count = pkt_count * 204 * 8;
6608
6609 /* Store the results */
6610 c->block_error.stat[0].scale = FE_SCALE_COUNTER;
6611 c->block_error.stat[0].uvalue += pkt_error_count;
6612 c->block_count.stat[0].scale = FE_SCALE_COUNTER;
6613 c->block_count.stat[0].uvalue += pkt_count;
6614
6615 c->pre_bit_error.stat[0].scale = FE_SCALE_COUNTER;
6616 c->pre_bit_error.stat[0].uvalue += pre_bit_err_count;
6617 c->pre_bit_count.stat[0].scale = FE_SCALE_COUNTER;
6618 c->pre_bit_count.stat[0].uvalue += pre_bit_count;
6619
6620 c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
6621 c->post_bit_error.stat[0].uvalue += post_bit_err_count;
6622 c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
6623 c->post_bit_count.stat[0].uvalue += post_bit_count;
6624
Mauro Carvalho Chehab8f3741e2013-03-20 06:15:45 -03006625error:
6626 return status;
6627}
6628
6629
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -03006630static int drxk_read_status(struct dvb_frontend *fe, enum fe_status *status)
Mauro Carvalho Chehab8f3741e2013-03-20 06:15:45 -03006631{
6632 struct drxk_state *state = fe->demodulator_priv;
6633 int rc;
6634
6635 dprintk(1, "\n");
6636
6637 rc = drxk_get_stats(fe);
6638 if (rc < 0)
6639 return rc;
6640
6641 *status = state->fe_status;
6642
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006643 return 0;
6644}
6645
Oliver Endrissebc7de22011-07-03 13:49:44 -03006646static int drxk_read_signal_strength(struct dvb_frontend *fe,
6647 u16 *strength)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006648{
6649 struct drxk_state *state = fe->demodulator_priv;
Mauro Carvalho Chehab340e7692013-03-20 08:57:42 -03006650 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006651
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03006652 dprintk(1, "\n");
Mauro Carvalho Chehab704a28e2012-06-29 15:45:04 -03006653
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006654 if (state->m_drxk_state == DRXK_NO_DEV)
Mauro Carvalho Chehab704a28e2012-06-29 15:45:04 -03006655 return -ENODEV;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006656 if (state->m_drxk_state == DRXK_UNINITIALIZED)
Mauro Carvalho Chehab704a28e2012-06-29 15:45:04 -03006657 return -EAGAIN;
6658
Mauro Carvalho Chehab340e7692013-03-20 08:57:42 -03006659 *strength = c->strength.stat[0].uvalue;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006660 return 0;
6661}
6662
6663static int drxk_read_snr(struct dvb_frontend *fe, u16 *snr)
6664{
6665 struct drxk_state *state = fe->demodulator_priv;
6666 s32 snr2;
6667
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03006668 dprintk(1, "\n");
Mauro Carvalho Chehab704a28e2012-06-29 15:45:04 -03006669
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006670 if (state->m_drxk_state == DRXK_NO_DEV)
Mauro Carvalho Chehab704a28e2012-06-29 15:45:04 -03006671 return -ENODEV;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006672 if (state->m_drxk_state == DRXK_UNINITIALIZED)
Mauro Carvalho Chehab704a28e2012-06-29 15:45:04 -03006673 return -EAGAIN;
6674
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006675 get_signal_to_noise(state, &snr2);
Mauro Carvalho Chehab8f3741e2013-03-20 06:15:45 -03006676
6677 /* No negative SNR, clip to zero */
6678 if (snr2 < 0)
6679 snr2 = 0;
Oliver Endrissebc7de22011-07-03 13:49:44 -03006680 *snr = snr2 & 0xffff;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006681 return 0;
6682}
6683
6684static int drxk_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
6685{
6686 struct drxk_state *state = fe->demodulator_priv;
6687 u16 err;
6688
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03006689 dprintk(1, "\n");
Mauro Carvalho Chehab704a28e2012-06-29 15:45:04 -03006690
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006691 if (state->m_drxk_state == DRXK_NO_DEV)
Mauro Carvalho Chehab704a28e2012-06-29 15:45:04 -03006692 return -ENODEV;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006693 if (state->m_drxk_state == DRXK_UNINITIALIZED)
Mauro Carvalho Chehab704a28e2012-06-29 15:45:04 -03006694 return -EAGAIN;
6695
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006696 dvbtqam_get_acc_pkt_err(state, &err);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006697 *ucblocks = (u32) err;
6698 return 0;
6699}
6700
Mauro Carvalho Chehabab5060c2013-04-28 11:47:51 -03006701static int drxk_get_tune_settings(struct dvb_frontend *fe,
6702 struct dvb_frontend_tune_settings *sets)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006703{
Mauro Carvalho Chehab704a28e2012-06-29 15:45:04 -03006704 struct drxk_state *state = fe->demodulator_priv;
Mauro Carvalho Chehabfa4b2a12012-01-05 08:07:32 -02006705 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03006706
6707 dprintk(1, "\n");
Mauro Carvalho Chehab704a28e2012-06-29 15:45:04 -03006708
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006709 if (state->m_drxk_state == DRXK_NO_DEV)
Mauro Carvalho Chehab704a28e2012-06-29 15:45:04 -03006710 return -ENODEV;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006711 if (state->m_drxk_state == DRXK_UNINITIALIZED)
Mauro Carvalho Chehab704a28e2012-06-29 15:45:04 -03006712 return -EAGAIN;
6713
Mauro Carvalho Chehabfa4b2a12012-01-05 08:07:32 -02006714 switch (p->delivery_system) {
6715 case SYS_DVBC_ANNEX_A:
6716 case SYS_DVBC_ANNEX_C:
Jose Alberto Reguerodc66d7f2012-01-27 18:34:49 -03006717 case SYS_DVBT:
Mauro Carvalho Chehabfa4b2a12012-01-05 08:07:32 -02006718 sets->min_delay_ms = 3000;
6719 sets->max_drift = 0;
6720 sets->step_size = 0;
6721 return 0;
6722 default:
Mauro Carvalho Chehabfa4b2a12012-01-05 08:07:32 -02006723 return -EINVAL;
6724 }
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006725}
6726
Max Kellermannbd336e62016-08-09 18:32:21 -03006727static const struct dvb_frontend_ops drxk_ops = {
Mauro Carvalho Chehabfa4b2a12012-01-05 08:07:32 -02006728 /* .delsys will be filled dynamically */
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006729 .info = {
Mauro Carvalho Chehabfa4b2a12012-01-05 08:07:32 -02006730 .name = "DRXK",
Mauro Carvalho Chehabf1b1eab2018-07-05 18:59:36 -04006731 .frequency_min_hz = 47 * MHz,
6732 .frequency_max_hz = 865 * MHz,
Mauro Carvalho Chehabfa4b2a12012-01-05 08:07:32 -02006733 /* For DVB-C */
Mauro Carvalho Chehabf1b1eab2018-07-05 18:59:36 -04006734 .symbol_rate_min = 870000,
Mauro Carvalho Chehabfa4b2a12012-01-05 08:07:32 -02006735 .symbol_rate_max = 11700000,
6736 /* For DVB-T */
Mauro Carvalho Chehabf1b1eab2018-07-05 18:59:36 -04006737 .frequency_stepsize_hz = 166667,
Mauro Carvalho Chehabfa4b2a12012-01-05 08:07:32 -02006738
6739 .caps = FE_CAN_QAM_16 | FE_CAN_QAM_32 | FE_CAN_QAM_64 |
6740 FE_CAN_QAM_128 | FE_CAN_QAM_256 | FE_CAN_FEC_AUTO |
6741 FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
6742 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_MUTE_TS |
6743 FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_RECOVER |
6744 FE_CAN_GUARD_INTERVAL_AUTO | FE_CAN_HIERARCHY_AUTO
6745 },
6746
6747 .release = drxk_release,
6748 .sleep = drxk_sleep,
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006749 .i2c_gate_ctrl = drxk_gate_ctrl,
6750
Mauro Carvalho Chehabed5452a2011-12-26 09:57:11 -03006751 .set_frontend = drxk_set_parameters,
Mauro Carvalho Chehabfa4b2a12012-01-05 08:07:32 -02006752 .get_tune_settings = drxk_get_tune_settings,
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006753
6754 .read_status = drxk_read_status,
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006755 .read_signal_strength = drxk_read_signal_strength,
6756 .read_snr = drxk_read_snr,
6757 .read_ucblocks = drxk_read_ucblocks,
6758};
6759
Mauro Carvalho Chehab0fc55e82011-07-09 12:36:58 -03006760struct dvb_frontend *drxk_attach(const struct drxk_config *config,
Mauro Carvalho Chehabfa4b2a12012-01-05 08:07:32 -02006761 struct i2c_adapter *i2c)
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006762{
Mauro Carvalho Chehab8f3741e2013-03-20 06:15:45 -03006763 struct dtv_frontend_properties *p;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006764 struct drxk_state *state = NULL;
Mauro Carvalho Chehab0fc55e82011-07-09 12:36:58 -03006765 u8 adr = config->adr;
Mauro Carvalho Chehab177bc7d2012-06-21 09:36:38 -03006766 int status;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006767
Mauro Carvalho Chehab2da67502011-07-04 17:39:21 -03006768 dprintk(1, "\n");
Oliver Endrissebc7de22011-07-03 13:49:44 -03006769 state = kzalloc(sizeof(struct drxk_state), GFP_KERNEL);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006770 if (!state)
6771 return NULL;
6772
Oliver Endrissebc7de22011-07-03 13:49:44 -03006773 state->i2c = i2c;
6774 state->demod_address = adr;
Mauro Carvalho Chehabe076c922011-07-09 13:06:12 -03006775 state->single_master = config->single_master;
Mauro Carvalho Chehabe4f4f872011-07-09 17:35:26 -03006776 state->microcode_name = config->microcode_name;
Martin Blumenstingl9e23f50a2012-07-04 17:36:55 -03006777 state->qam_demod_parameter_count = config->qam_demod_parameter_count;
Mauro Carvalho Chehabf1fe1b72011-07-09 21:59:33 -03006778 state->no_i2c_bridge = config->no_i2c_bridge;
Mauro Carvalho Chehab90796ac2011-07-10 09:36:30 -03006779 state->antenna_gpio = config->antenna_gpio;
6780 state->antenna_dvbt = config->antenna_dvbt;
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006781 state->m_chunk_size = config->chunk_size;
Mauro Carvalho Chehabd5856812012-01-21 07:57:06 -03006782 state->enable_merr_cfg = config->enable_merr_cfg;
Mauro Carvalho Chehab90796ac2011-07-10 09:36:30 -03006783
Mauro Carvalho Chehab67f04612012-01-20 18:30:58 -03006784 if (config->dynamic_clk) {
Mauro Carvalho Chehab5a7f7b72014-09-03 15:23:57 -03006785 state->m_dvbt_static_clk = false;
6786 state->m_dvbc_static_clk = false;
Mauro Carvalho Chehab67f04612012-01-20 18:30:58 -03006787 } else {
Mauro Carvalho Chehab5a7f7b72014-09-03 15:23:57 -03006788 state->m_dvbt_static_clk = true;
6789 state->m_dvbc_static_clk = true;
Mauro Carvalho Chehab67f04612012-01-20 18:30:58 -03006790 }
6791
Mauro Carvalho Chehab6fb65a62012-01-20 19:13:07 -03006792
6793 if (config->mpeg_out_clk_strength)
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006794 state->m_ts_clockk_strength = config->mpeg_out_clk_strength & 0x07;
Mauro Carvalho Chehab6fb65a62012-01-20 19:13:07 -03006795 else
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006796 state->m_ts_clockk_strength = 0x06;
Mauro Carvalho Chehab6fb65a62012-01-20 19:13:07 -03006797
Mauro Carvalho Chehab534e0482011-07-24 14:59:20 -03006798 if (config->parallel_ts)
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006799 state->m_enable_parallel = true;
Mauro Carvalho Chehab534e0482011-07-24 14:59:20 -03006800 else
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006801 state->m_enable_parallel = false;
Mauro Carvalho Chehab534e0482011-07-24 14:59:20 -03006802
Mauro Carvalho Chehab90796ac2011-07-10 09:36:30 -03006803 /* NOTE: as more UIO bits will be used, add them to the mask */
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006804 state->uio_mask = config->antenna_gpio;
Mauro Carvalho Chehab90796ac2011-07-10 09:36:30 -03006805
6806 /* Default gpio to DVB-C */
6807 if (!state->antenna_dvbt && state->antenna_gpio)
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006808 state->m_gpio |= state->antenna_gpio;
Mauro Carvalho Chehab90796ac2011-07-10 09:36:30 -03006809 else
Mauro Carvalho Chehabcd7a67a2013-04-28 11:47:44 -03006810 state->m_gpio &= ~state->antenna_gpio;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006811
6812 mutex_init(&state->mutex);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006813
Mauro Carvalho Chehabfa4b2a12012-01-05 08:07:32 -02006814 memcpy(&state->frontend.ops, &drxk_ops, sizeof(drxk_ops));
6815 state->frontend.demodulator_priv = state;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006816
6817 init_state(state);
Mauro Carvalho Chehabfa4b2a12012-01-05 08:07:32 -02006818
Mauro Carvalho Chehab177bc7d2012-06-21 09:36:38 -03006819 /* Load firmware and initialize DRX-K */
6820 if (state->microcode_name) {
Mauro Carvalho Chehab4b819722014-01-13 04:31:31 -03006821 const struct firmware *fw = NULL;
Mauro Carvalho Chehab8e307832012-10-02 16:01:15 -03006822
Mauro Carvalho Chehab4b819722014-01-13 04:31:31 -03006823 status = request_firmware(&fw, state->microcode_name,
6824 state->i2c->dev.parent);
6825 if (status < 0)
6826 fw = NULL;
6827 load_firmware_cb(fw, state);
Mauro Carvalho Chehab177bc7d2012-06-21 09:36:38 -03006828 } else if (init_drxk(state) < 0)
6829 goto error;
Mauro Carvalho Chehabcf694b12011-07-10 10:26:06 -03006830
Mauro Carvalho Chehab8f3741e2013-03-20 06:15:45 -03006831
6832 /* Initialize stats */
6833 p = &state->frontend.dtv_property_cache;
6834 p->strength.len = 1;
6835 p->cnr.len = 1;
6836 p->block_error.len = 1;
6837 p->block_count.len = 1;
6838 p->pre_bit_error.len = 1;
6839 p->pre_bit_count.len = 1;
6840 p->post_bit_error.len = 1;
6841 p->post_bit_count.len = 1;
6842
6843 p->strength.stat[0].scale = FE_SCALE_RELATIVE;
6844 p->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
6845 p->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
6846 p->block_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
6847 p->pre_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
6848 p->pre_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
6849 p->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
6850 p->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
6851
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03006852 pr_info("frontend initialized.\n");
Mauro Carvalho Chehabfa4b2a12012-01-05 08:07:32 -02006853 return &state->frontend;
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006854
6855error:
Mauro Carvalho Chehab3a4398f2013-04-28 11:47:45 -03006856 pr_err("not found\n");
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006857 kfree(state);
6858 return NULL;
6859}
Oliver Endrissebc7de22011-07-03 13:49:44 -03006860EXPORT_SYMBOL(drxk_attach);
Ralph Metzler43dd07f2011-07-03 13:42:18 -03006861
6862MODULE_DESCRIPTION("DRX-K driver");
6863MODULE_AUTHOR("Ralph Metzler");
6864MODULE_LICENSE("GPL");