blob: 26e65a66719a75efa24c0f35280dc805fff1bf46 [file] [log] [blame]
Steven Toth0d467482008-09-04 01:14:43 -03001/*
2 Conexant cx24116/cx24118 - DVBS/S2 Satellite demod/tuner driver
3
4 Copyright (C) 2006-2008 Steven Toth <stoth@hauppauge.com>
Darron Broad490c8682008-09-13 19:42:16 -03005 Copyright (C) 2006-2007 Georg Acher
6 Copyright (C) 2007-2008 Darron Broad
7 March 2007
8 Fixed some bugs.
9 Added diseqc support.
10 Added corrected signal strength support.
11 August 2007
12 Sync with legacy version.
13 Some clean ups.
14 Copyright (C) 2008 Igor Liplianin
15 September, 9th 2008
Igor M. Liplianinc063a482008-09-14 07:43:53 -030016 Fixed locking on high symbol rates (>30000).
17 Implement MPEG initialization parameter.
Igor M. Liplianinc7bdcd02009-01-17 10:16:36 -030018 January, 17th 2009
19 Fill set_voltage with actually control voltage code.
20 Correct set tone to not affect voltage.
Steven Toth0d467482008-09-04 01:14:43 -030021
22 This program is free software; you can redistribute it and/or modify
23 it under the terms of the GNU General Public License as published by
24 the Free Software Foundation; either version 2 of the License, or
25 (at your option) any later version.
26
27 This program is distributed in the hope that it will be useful,
28 but WITHOUT ANY WARRANTY; without even the implied warranty of
29 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 GNU General Public License for more details.
31
32 You should have received a copy of the GNU General Public License
33 along with this program; if not, write to the Free Software
34 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35*/
36
Steven Toth0d467482008-09-04 01:14:43 -030037#include <linux/slab.h>
38#include <linux/kernel.h>
39#include <linux/module.h>
40#include <linux/moduleparam.h>
41#include <linux/init.h>
42#include <linux/firmware.h>
43
44#include "dvb_frontend.h"
45#include "cx24116.h"
46
Steven Tothf11ec7d2008-10-16 20:22:01 -030047static int debug;
48module_param(debug, int, 0644);
49MODULE_PARM_DESC(debug, "Activates frontend debugging (default:0)");
50
Darron Broad490c8682008-09-13 19:42:16 -030051#define dprintk(args...) \
52 do { \
Steven Tothf11ec7d2008-10-16 20:22:01 -030053 if (debug) \
Steven Toth98c94822008-10-16 20:24:42 -030054 printk(KERN_INFO "cx24116: " args); \
Darron Broad490c8682008-09-13 19:42:16 -030055 } while (0)
56
Steven Toth0d467482008-09-04 01:14:43 -030057#define CX24116_DEFAULT_FIRMWARE "dvb-fe-cx24116.fw"
58#define CX24116_SEARCH_RANGE_KHZ 5000
59
Darron Broad490c8682008-09-13 19:42:16 -030060/* known registers */
61#define CX24116_REG_COMMAND (0x00) /* command args 0x00..0x1e */
62#define CX24116_REG_EXECUTE (0x1f) /* execute command */
63#define CX24116_REG_MAILBOX (0x96) /* FW or multipurpose mailbox? */
64#define CX24116_REG_RESET (0x20) /* reset status > 0 */
65#define CX24116_REG_SIGNAL (0x9e) /* signal low */
66#define CX24116_REG_SSTATUS (0x9d) /* signal high / status */
Steven Toth8953db72008-10-06 21:20:21 -030067#define CX24116_REG_QUALITY8 (0xa3)
Darron Broad490c8682008-09-13 19:42:16 -030068#define CX24116_REG_QSTATUS (0xbc)
Steven Toth8953db72008-10-06 21:20:21 -030069#define CX24116_REG_QUALITY0 (0xd5)
Darron Broad490c8682008-09-13 19:42:16 -030070#define CX24116_REG_BER0 (0xc9)
71#define CX24116_REG_BER8 (0xc8)
72#define CX24116_REG_BER16 (0xc7)
73#define CX24116_REG_BER24 (0xc6)
74#define CX24116_REG_UCB0 (0xcb)
75#define CX24116_REG_UCB8 (0xca)
76#define CX24116_REG_CLKDIV (0xf3)
77#define CX24116_REG_RATEDIV (0xf9)
Steven Toth98c94822008-10-16 20:24:42 -030078
79/* configured fec (not tuned) or actual FEC (tuned) 1=1/2 2=2/3 etc */
80#define CX24116_REG_FECSTATUS (0x9c)
Darron Broad681faa02008-09-22 00:47:20 -030081
82/* FECSTATUS bits */
Steven Toth98c94822008-10-16 20:24:42 -030083/* mask to determine configured fec (not tuned) or actual fec (tuned) */
84#define CX24116_FEC_FECMASK (0x1f)
85
86/* Select DVB-S demodulator, else DVB-S2 */
87#define CX24116_FEC_DVBS (0x20)
Darron Broad681faa02008-09-22 00:47:20 -030088#define CX24116_FEC_UNKNOWN (0x40) /* Unknown/unused */
Steven Toth98c94822008-10-16 20:24:42 -030089
90/* Pilot mode requested when tuning else always reset when tuned */
91#define CX24116_FEC_PILOT (0x80)
Steven Toth0d467482008-09-04 01:14:43 -030092
93/* arg buffer size */
94#define CX24116_ARGLEN (0x1e)
95
Darron Broad490c8682008-09-13 19:42:16 -030096/* rolloff */
97#define CX24116_ROLLOFF_020 (0x00)
98#define CX24116_ROLLOFF_025 (0x01)
99#define CX24116_ROLLOFF_035 (0x02)
100
101/* pilot bit */
Darron Broad01a8f032008-10-03 11:47:46 -0300102#define CX24116_PILOT_OFF (0x00)
103#define CX24116_PILOT_ON (0x40)
Darron Broad490c8682008-09-13 19:42:16 -0300104
105/* signal status */
106#define CX24116_HAS_SIGNAL (0x01)
107#define CX24116_HAS_CARRIER (0x02)
108#define CX24116_HAS_VITERBI (0x04)
109#define CX24116_HAS_SYNCLOCK (0x08)
110#define CX24116_HAS_UNKNOWN1 (0x10)
111#define CX24116_HAS_UNKNOWN2 (0x20)
Darron Broad6639f1e2008-12-18 06:28:18 -0300112#define CX24116_STATUS_MASK (0x0f)
Darron Broad490c8682008-09-13 19:42:16 -0300113#define CX24116_SIGNAL_MASK (0xc0)
114
115#define CX24116_DISEQC_TONEOFF (0) /* toneburst never sent */
116#define CX24116_DISEQC_TONECACHE (1) /* toneburst cached */
117#define CX24116_DISEQC_MESGCACHE (2) /* message cached */
118
Steven Toth0d467482008-09-04 01:14:43 -0300119/* arg offset for DiSEqC */
120#define CX24116_DISEQC_BURST (1)
121#define CX24116_DISEQC_ARG2_2 (2) /* unknown value=2 */
122#define CX24116_DISEQC_ARG3_0 (3) /* unknown value=0 */
123#define CX24116_DISEQC_ARG4_0 (4) /* unknown value=0 */
124#define CX24116_DISEQC_MSGLEN (5)
125#define CX24116_DISEQC_MSGOFS (6)
126
127/* DiSEqC burst */
128#define CX24116_DISEQC_MINI_A (0)
129#define CX24116_DISEQC_MINI_B (1)
130
Darron Broad490c8682008-09-13 19:42:16 -0300131/* DiSEqC tone burst */
132static int toneburst = 1;
Steven Tothf11ec7d2008-10-16 20:22:01 -0300133module_param(toneburst, int, 0644);
Steven Toth98c94822008-10-16 20:24:42 -0300134MODULE_PARM_DESC(toneburst, "DiSEqC toneburst 0=OFF, 1=TONE CACHE, "\
135 "2=MESSAGE CACHE (default:1)");
Darron Broad490c8682008-09-13 19:42:16 -0300136
Steven Toth8953db72008-10-06 21:20:21 -0300137/* SNR measurements */
Steven Tothf11ec7d2008-10-16 20:22:01 -0300138static int esno_snr;
139module_param(esno_snr, int, 0644);
Hans Petter Selasky53f1fe92011-03-28 14:37:36 -0300140MODULE_PARM_DESC(esno_snr, "SNR return units, 0=PERCENTAGE 0-100, "\
Steven Toth98c94822008-10-16 20:24:42 -0300141 "1=ESNO(db * 10) (default:0)");
Steven Toth8953db72008-10-06 21:20:21 -0300142
Steven Tothf11ec7d2008-10-16 20:22:01 -0300143enum cmds {
Darron Broad490c8682008-09-13 19:42:16 -0300144 CMD_SET_VCO = 0x10,
Steven Toth0d467482008-09-04 01:14:43 -0300145 CMD_TUNEREQUEST = 0x11,
Darron Broad490c8682008-09-13 19:42:16 -0300146 CMD_MPEGCONFIG = 0x13,
147 CMD_TUNERINIT = 0x14,
148 CMD_BANDWIDTH = 0x15,
149 CMD_GETAGC = 0x19,
150 CMD_LNBCONFIG = 0x20,
151 CMD_LNBSEND = 0x21, /* Formerly CMD_SEND_DISEQC */
Igor M. Liplianinc7bdcd02009-01-17 10:16:36 -0300152 CMD_LNBDCLEVEL = 0x22,
Steven Toth0d467482008-09-04 01:14:43 -0300153 CMD_SET_TONE = 0x23,
Darron Broad490c8682008-09-13 19:42:16 -0300154 CMD_UPDFWVERS = 0x35,
155 CMD_TUNERSLEEP = 0x36,
156 CMD_AGCCONTROL = 0x3b, /* Unknown */
Steven Toth0d467482008-09-04 01:14:43 -0300157};
158
159/* The Demod/Tuner can't easily provide these, we cache them */
Steven Tothf11ec7d2008-10-16 20:22:01 -0300160struct cx24116_tuning {
Steven Toth0d467482008-09-04 01:14:43 -0300161 u32 frequency;
162 u32 symbol_rate;
163 fe_spectral_inversion_t inversion;
164 fe_code_rate_t fec;
165
Darron Broad35694762008-12-18 06:21:51 -0300166 fe_delivery_system_t delsys;
Steven Toth0d467482008-09-04 01:14:43 -0300167 fe_modulation_t modulation;
Darron Broad490c8682008-09-13 19:42:16 -0300168 fe_pilot_t pilot;
169 fe_rolloff_t rolloff;
Steven Toth0d467482008-09-04 01:14:43 -0300170
171 /* Demod values */
172 u8 fec_val;
173 u8 fec_mask;
174 u8 inversion_val;
Darron Broad01a8f032008-10-03 11:47:46 -0300175 u8 pilot_val;
Darron Broad490c8682008-09-13 19:42:16 -0300176 u8 rolloff_val;
Steven Toth0d467482008-09-04 01:14:43 -0300177};
178
179/* Basic commands that are sent to the firmware */
Steven Tothf11ec7d2008-10-16 20:22:01 -0300180struct cx24116_cmd {
Steven Toth0d467482008-09-04 01:14:43 -0300181 u8 len;
182 u8 args[CX24116_ARGLEN];
183};
184
Steven Tothf11ec7d2008-10-16 20:22:01 -0300185struct cx24116_state {
186 struct i2c_adapter *i2c;
187 const struct cx24116_config *config;
Steven Toth0d467482008-09-04 01:14:43 -0300188
189 struct dvb_frontend frontend;
190
191 struct cx24116_tuning dcur;
192 struct cx24116_tuning dnxt;
193
194 u8 skip_fw_load;
195 u8 burst;
Darron Broad490c8682008-09-13 19:42:16 -0300196 struct cx24116_cmd dsec_cmd;
Steven Toth0d467482008-09-04 01:14:43 -0300197};
198
Steven Tothf11ec7d2008-10-16 20:22:01 -0300199static int cx24116_writereg(struct cx24116_state *state, int reg, int data)
Steven Toth0d467482008-09-04 01:14:43 -0300200{
201 u8 buf[] = { reg, data };
202 struct i2c_msg msg = { .addr = state->config->demod_address,
203 .flags = 0, .buf = buf, .len = 2 };
204 int err;
205
Steven Tothf11ec7d2008-10-16 20:22:01 -0300206 if (debug > 1)
Steven Toth0d467482008-09-04 01:14:43 -0300207 printk("cx24116: %s: write reg 0x%02x, value 0x%02x\n",
Steven Tothf11ec7d2008-10-16 20:22:01 -0300208 __func__, reg, data);
Steven Toth0d467482008-09-04 01:14:43 -0300209
Steven Tothf11ec7d2008-10-16 20:22:01 -0300210 err = i2c_transfer(state->i2c, &msg, 1);
211 if (err != 1) {
Steven Toth98c94822008-10-16 20:24:42 -0300212 printk(KERN_ERR "%s: writereg error(err == %i, reg == 0x%02x,"
Steven Toth0d467482008-09-04 01:14:43 -0300213 " value == 0x%02x)\n", __func__, err, reg, data);
214 return -EREMOTEIO;
215 }
216
217 return 0;
218}
219
220/* Bulk byte writes to a single I2C address, for 32k firmware load */
Steven Tothf11ec7d2008-10-16 20:22:01 -0300221static int cx24116_writeregN(struct cx24116_state *state, int reg,
Geert Uytterhoeven64decbf2008-10-16 21:04:35 -0300222 const u8 *data, u16 len)
Steven Toth0d467482008-09-04 01:14:43 -0300223{
224 int ret = -EREMOTEIO;
225 struct i2c_msg msg;
226 u8 *buf;
227
228 buf = kmalloc(len + 1, GFP_KERNEL);
229 if (buf == NULL) {
230 printk("Unable to kmalloc\n");
231 ret = -ENOMEM;
232 goto error;
233 }
234
235 *(buf) = reg;
236 memcpy(buf + 1, data, len);
237
238 msg.addr = state->config->demod_address;
239 msg.flags = 0;
240 msg.buf = buf;
241 msg.len = len + 1;
242
Steven Tothf11ec7d2008-10-16 20:22:01 -0300243 if (debug > 1)
Steven Toth98c94822008-10-16 20:24:42 -0300244 printk(KERN_INFO "cx24116: %s: write regN 0x%02x, len = %d\n",
Steven Tothf11ec7d2008-10-16 20:22:01 -0300245 __func__, reg, len);
Steven Toth0d467482008-09-04 01:14:43 -0300246
Steven Tothf11ec7d2008-10-16 20:22:01 -0300247 ret = i2c_transfer(state->i2c, &msg, 1);
248 if (ret != 1) {
Steven Toth98c94822008-10-16 20:24:42 -0300249 printk(KERN_ERR "%s: writereg error(err == %i, reg == 0x%02x\n",
Steven Toth0d467482008-09-04 01:14:43 -0300250 __func__, ret, reg);
251 ret = -EREMOTEIO;
252 }
253
254error:
255 kfree(buf);
256
257 return ret;
258}
259
Steven Tothf11ec7d2008-10-16 20:22:01 -0300260static int cx24116_readreg(struct cx24116_state *state, u8 reg)
Steven Toth0d467482008-09-04 01:14:43 -0300261{
262 int ret;
263 u8 b0[] = { reg };
264 u8 b1[] = { 0 };
265 struct i2c_msg msg[] = {
Steven Tothf11ec7d2008-10-16 20:22:01 -0300266 { .addr = state->config->demod_address, .flags = 0,
267 .buf = b0, .len = 1 },
268 { .addr = state->config->demod_address, .flags = I2C_M_RD,
269 .buf = b1, .len = 1 }
Steven Toth0d467482008-09-04 01:14:43 -0300270 };
271
272 ret = i2c_transfer(state->i2c, msg, 2);
273
274 if (ret != 2) {
Steven Toth98c94822008-10-16 20:24:42 -0300275 printk(KERN_ERR "%s: reg=0x%x (error=%d)\n",
276 __func__, reg, ret);
Steven Toth0d467482008-09-04 01:14:43 -0300277 return ret;
278 }
279
Steven Tothf11ec7d2008-10-16 20:22:01 -0300280 if (debug > 1)
Steven Toth98c94822008-10-16 20:24:42 -0300281 printk(KERN_INFO "cx24116: read reg 0x%02x, value 0x%02x\n",
Steven Tothf11ec7d2008-10-16 20:22:01 -0300282 reg, b1[0]);
Steven Toth0d467482008-09-04 01:14:43 -0300283
284 return b1[0];
285}
286
Steven Toth98c94822008-10-16 20:24:42 -0300287static int cx24116_set_inversion(struct cx24116_state *state,
288 fe_spectral_inversion_t inversion)
Steven Toth0d467482008-09-04 01:14:43 -0300289{
290 dprintk("%s(%d)\n", __func__, inversion);
291
292 switch (inversion) {
293 case INVERSION_OFF:
294 state->dnxt.inversion_val = 0x00;
295 break;
296 case INVERSION_ON:
297 state->dnxt.inversion_val = 0x04;
298 break;
299 case INVERSION_AUTO:
300 state->dnxt.inversion_val = 0x0C;
301 break;
302 default:
303 return -EINVAL;
304 }
305
306 state->dnxt.inversion = inversion;
307
308 return 0;
309}
310
Darron Broad490c8682008-09-13 19:42:16 -0300311/*
312 * modfec (modulation and FEC)
313 * ===========================
314 *
315 * MOD FEC mask/val standard
316 * ---- -------- ----------- --------
317 * QPSK FEC_1_2 0x02 0x02+X DVB-S
318 * QPSK FEC_2_3 0x04 0x02+X DVB-S
319 * QPSK FEC_3_4 0x08 0x02+X DVB-S
320 * QPSK FEC_4_5 0x10 0x02+X DVB-S (?)
321 * QPSK FEC_5_6 0x20 0x02+X DVB-S
322 * QPSK FEC_6_7 0x40 0x02+X DVB-S
323 * QPSK FEC_7_8 0x80 0x02+X DVB-S
324 * QPSK FEC_8_9 0x01 0x02+X DVB-S (?) (NOT SUPPORTED?)
325 * QPSK AUTO 0xff 0x02+X DVB-S
326 *
327 * For DVB-S high byte probably represents FEC
328 * and low byte selects the modulator. The high
329 * byte is search range mask. Bit 5 may turn
330 * on DVB-S and remaining bits represent some
331 * kind of calibration (how/what i do not know).
332 *
333 * Eg.(2/3) szap "Zone Horror"
334 *
335 * mask/val = 0x04, 0x20
Steven Toth98c94822008-10-16 20:24:42 -0300336 * status 1f | signal c3c0 | snr a333 | ber 00000098 | unc 0 | FE_HAS_LOCK
Darron Broad490c8682008-09-13 19:42:16 -0300337 *
338 * mask/val = 0x04, 0x30
Steven Toth98c94822008-10-16 20:24:42 -0300339 * status 1f | signal c3c0 | snr a333 | ber 00000000 | unc 0 | FE_HAS_LOCK
Darron Broad490c8682008-09-13 19:42:16 -0300340 *
341 * After tuning FECSTATUS contains actual FEC
342 * in use numbered 1 through to 8 for 1/2 .. 2/3 etc
343 *
344 * NBC=NOT/NON BACKWARD COMPATIBLE WITH DVB-S (DVB-S2 only)
345 *
346 * NBC-QPSK FEC_1_2 0x00, 0x04 DVB-S2
347 * NBC-QPSK FEC_3_5 0x00, 0x05 DVB-S2
348 * NBC-QPSK FEC_2_3 0x00, 0x06 DVB-S2
349 * NBC-QPSK FEC_3_4 0x00, 0x07 DVB-S2
350 * NBC-QPSK FEC_4_5 0x00, 0x08 DVB-S2
351 * NBC-QPSK FEC_5_6 0x00, 0x09 DVB-S2
352 * NBC-QPSK FEC_8_9 0x00, 0x0a DVB-S2
353 * NBC-QPSK FEC_9_10 0x00, 0x0b DVB-S2
354 *
355 * NBC-8PSK FEC_3_5 0x00, 0x0c DVB-S2
356 * NBC-8PSK FEC_2_3 0x00, 0x0d DVB-S2
357 * NBC-8PSK FEC_3_4 0x00, 0x0e DVB-S2
358 * NBC-8PSK FEC_5_6 0x00, 0x0f DVB-S2
359 * NBC-8PSK FEC_8_9 0x00, 0x10 DVB-S2
360 * NBC-8PSK FEC_9_10 0x00, 0x11 DVB-S2
361 *
362 * For DVB-S2 low bytes selects both modulator
363 * and FEC. High byte is meaningless here. To
364 * set pilot, bit 6 (0x40) is set. When inspecting
365 * FECSTATUS bit 7 (0x80) represents the pilot
366 * selection whilst not tuned. When tuned, actual FEC
367 * in use is found in FECSTATUS as per above. Pilot
368 * value is reset.
369 */
370
Steven Toth0d467482008-09-04 01:14:43 -0300371/* A table of modulation, fec and configuration bytes for the demod.
372 * Not all S2 mmodulation schemes are support and not all rates with
373 * a scheme are support. Especially, no auto detect when in S2 mode.
374 */
Mauro Carvalho Chehabffbc5f82009-01-05 01:34:20 -0300375static struct cx24116_modfec {
Steven Toth0a6393a2008-10-06 21:06:48 -0300376 fe_delivery_system_t delivery_system;
Steven Toth0d467482008-09-04 01:14:43 -0300377 fe_modulation_t modulation;
378 fe_code_rate_t fec;
379 u8 mask; /* In DVBS mode this is used to autodetect */
380 u8 val; /* Passed to the firmware to indicate mode selection */
381} CX24116_MODFEC_MODES[] = {
382 /* QPSK. For unknown rates we set hardware to auto detect 0xfe 0x30 */
Darron Broad490c8682008-09-13 19:42:16 -0300383
384 /*mod fec mask val */
Steven Toth0a6393a2008-10-06 21:06:48 -0300385 { SYS_DVBS, QPSK, FEC_NONE, 0xfe, 0x30 },
386 { SYS_DVBS, QPSK, FEC_1_2, 0x02, 0x2e }, /* 00000010 00101110 */
387 { SYS_DVBS, QPSK, FEC_2_3, 0x04, 0x2f }, /* 00000100 00101111 */
388 { SYS_DVBS, QPSK, FEC_3_4, 0x08, 0x30 }, /* 00001000 00110000 */
389 { SYS_DVBS, QPSK, FEC_4_5, 0xfe, 0x30 }, /* 000?0000 ? */
390 { SYS_DVBS, QPSK, FEC_5_6, 0x20, 0x31 }, /* 00100000 00110001 */
391 { SYS_DVBS, QPSK, FEC_6_7, 0xfe, 0x30 }, /* 0?000000 ? */
392 { SYS_DVBS, QPSK, FEC_7_8, 0x80, 0x32 }, /* 10000000 00110010 */
393 { SYS_DVBS, QPSK, FEC_8_9, 0xfe, 0x30 }, /* 0000000? ? */
394 { SYS_DVBS, QPSK, FEC_AUTO, 0xfe, 0x30 },
Steven Toth0d467482008-09-04 01:14:43 -0300395 /* NBC-QPSK */
Steven Toth0a6393a2008-10-06 21:06:48 -0300396 { SYS_DVBS2, QPSK, FEC_1_2, 0x00, 0x04 },
397 { SYS_DVBS2, QPSK, FEC_3_5, 0x00, 0x05 },
398 { SYS_DVBS2, QPSK, FEC_2_3, 0x00, 0x06 },
399 { SYS_DVBS2, QPSK, FEC_3_4, 0x00, 0x07 },
400 { SYS_DVBS2, QPSK, FEC_4_5, 0x00, 0x08 },
401 { SYS_DVBS2, QPSK, FEC_5_6, 0x00, 0x09 },
402 { SYS_DVBS2, QPSK, FEC_8_9, 0x00, 0x0a },
403 { SYS_DVBS2, QPSK, FEC_9_10, 0x00, 0x0b },
Steven Toth0d467482008-09-04 01:14:43 -0300404 /* 8PSK */
Steven Toth0a6393a2008-10-06 21:06:48 -0300405 { SYS_DVBS2, PSK_8, FEC_3_5, 0x00, 0x0c },
406 { SYS_DVBS2, PSK_8, FEC_2_3, 0x00, 0x0d },
407 { SYS_DVBS2, PSK_8, FEC_3_4, 0x00, 0x0e },
408 { SYS_DVBS2, PSK_8, FEC_5_6, 0x00, 0x0f },
409 { SYS_DVBS2, PSK_8, FEC_8_9, 0x00, 0x10 },
410 { SYS_DVBS2, PSK_8, FEC_9_10, 0x00, 0x11 },
Darron Broad490c8682008-09-13 19:42:16 -0300411 /*
412 * `val' can be found in the FECSTATUS register when tuning.
413 * FECSTATUS will give the actual FEC in use if tuning was successful.
414 */
Steven Toth0d467482008-09-04 01:14:43 -0300415};
416
Steven Tothf11ec7d2008-10-16 20:22:01 -0300417static int cx24116_lookup_fecmod(struct cx24116_state *state,
Darron Broad35694762008-12-18 06:21:51 -0300418 fe_delivery_system_t d, fe_modulation_t m, fe_code_rate_t f)
Steven Toth0d467482008-09-04 01:14:43 -0300419{
420 int i, ret = -EOPNOTSUPP;
421
Darron Broad490c8682008-09-13 19:42:16 -0300422 dprintk("%s(0x%02x,0x%02x)\n", __func__, m, f);
423
Steven Tothf11ec7d2008-10-16 20:22:01 -0300424 for (i = 0; i < ARRAY_SIZE(CX24116_MODFEC_MODES); i++) {
Darron Broad35694762008-12-18 06:21:51 -0300425 if ((d == CX24116_MODFEC_MODES[i].delivery_system) &&
426 (m == CX24116_MODFEC_MODES[i].modulation) &&
Steven Tothf11ec7d2008-10-16 20:22:01 -0300427 (f == CX24116_MODFEC_MODES[i].fec)) {
Steven Toth0d467482008-09-04 01:14:43 -0300428 ret = i;
429 break;
430 }
431 }
432
433 return ret;
434}
435
Steven Toth98c94822008-10-16 20:24:42 -0300436static int cx24116_set_fec(struct cx24116_state *state,
Darron Broad35694762008-12-18 06:21:51 -0300437 fe_delivery_system_t delsys, fe_modulation_t mod, fe_code_rate_t fec)
Steven Toth0d467482008-09-04 01:14:43 -0300438{
439 int ret = 0;
Darron Broad490c8682008-09-13 19:42:16 -0300440
441 dprintk("%s(0x%02x,0x%02x)\n", __func__, mod, fec);
Steven Toth0d467482008-09-04 01:14:43 -0300442
Darron Broad35694762008-12-18 06:21:51 -0300443 ret = cx24116_lookup_fecmod(state, delsys, mod, fec);
Steven Toth0d467482008-09-04 01:14:43 -0300444
Steven Tothf11ec7d2008-10-16 20:22:01 -0300445 if (ret < 0)
Steven Toth0d467482008-09-04 01:14:43 -0300446 return ret;
447
Darron Broad490c8682008-09-13 19:42:16 -0300448 state->dnxt.fec = fec;
Steven Toth0d467482008-09-04 01:14:43 -0300449 state->dnxt.fec_val = CX24116_MODFEC_MODES[ret].val;
450 state->dnxt.fec_mask = CX24116_MODFEC_MODES[ret].mask;
Darron Broad490c8682008-09-13 19:42:16 -0300451 dprintk("%s() mask/val = 0x%02x/0x%02x\n", __func__,
452 state->dnxt.fec_mask, state->dnxt.fec_val);
Steven Toth0d467482008-09-04 01:14:43 -0300453
454 return 0;
455}
456
Steven Tothf11ec7d2008-10-16 20:22:01 -0300457static int cx24116_set_symbolrate(struct cx24116_state *state, u32 rate)
Steven Toth0d467482008-09-04 01:14:43 -0300458{
Darron Broad490c8682008-09-13 19:42:16 -0300459 dprintk("%s(%d)\n", __func__, rate);
Steven Toth0d467482008-09-04 01:14:43 -0300460
461 /* check if symbol rate is within limits */
Darron Broad490c8682008-09-13 19:42:16 -0300462 if ((rate > state->frontend.ops.info.symbol_rate_max) ||
463 (rate < state->frontend.ops.info.symbol_rate_min)) {
464 dprintk("%s() unsupported symbol_rate = %d\n", __func__, rate);
465 return -EOPNOTSUPP;
466 }
Steven Toth0d467482008-09-04 01:14:43 -0300467
Darron Broad490c8682008-09-13 19:42:16 -0300468 state->dnxt.symbol_rate = rate;
469 dprintk("%s() symbol_rate = %d\n", __func__, rate);
470
471 return 0;
Steven Toth0d467482008-09-04 01:14:43 -0300472}
473
Steven Tothf11ec7d2008-10-16 20:22:01 -0300474static int cx24116_load_firmware(struct dvb_frontend *fe,
475 const struct firmware *fw);
Steven Toth0d467482008-09-04 01:14:43 -0300476
Steven Tothf11ec7d2008-10-16 20:22:01 -0300477static int cx24116_firmware_ondemand(struct dvb_frontend *fe)
Steven Toth0d467482008-09-04 01:14:43 -0300478{
479 struct cx24116_state *state = fe->demodulator_priv;
480 const struct firmware *fw;
481 int ret = 0;
482
Steven Tothf11ec7d2008-10-16 20:22:01 -0300483 dprintk("%s()\n", __func__);
Steven Toth0d467482008-09-04 01:14:43 -0300484
Steven Tothf11ec7d2008-10-16 20:22:01 -0300485 if (cx24116_readreg(state, 0x20) > 0) {
Steven Toth0d467482008-09-04 01:14:43 -0300486
487 if (state->skip_fw_load)
488 return 0;
489
490 /* Load firmware */
Steven Toth98c94822008-10-16 20:24:42 -0300491 /* request the firmware, this will block until loaded */
492 printk(KERN_INFO "%s: Waiting for firmware upload (%s)...\n",
493 __func__, CX24116_DEFAULT_FIRMWARE);
494 ret = request_firmware(&fw, CX24116_DEFAULT_FIRMWARE,
Jean Delvaree9785252009-04-26 05:43:59 -0300495 state->i2c->dev.parent);
Steven Toth98c94822008-10-16 20:24:42 -0300496 printk(KERN_INFO "%s: Waiting for firmware upload(2)...\n",
497 __func__);
Steven Toth0d467482008-09-04 01:14:43 -0300498 if (ret) {
Steven Toth98c94822008-10-16 20:24:42 -0300499 printk(KERN_ERR "%s: No firmware uploaded "
500 "(timeout or file not found?)\n", __func__);
Steven Toth0d467482008-09-04 01:14:43 -0300501 return ret;
502 }
503
Steven Toth98c94822008-10-16 20:24:42 -0300504 /* Make sure we don't recurse back through here
505 * during loading */
Steven Toth0d467482008-09-04 01:14:43 -0300506 state->skip_fw_load = 1;
507
508 ret = cx24116_load_firmware(fe, fw);
509 if (ret)
Steven Toth98c94822008-10-16 20:24:42 -0300510 printk(KERN_ERR "%s: Writing firmware to device failed\n",
511 __func__);
Steven Toth0d467482008-09-04 01:14:43 -0300512
513 release_firmware(fw);
514
Steven Toth98c94822008-10-16 20:24:42 -0300515 printk(KERN_INFO "%s: Firmware upload %s\n", __func__,
516 ret == 0 ? "complete" : "failed");
Steven Toth0d467482008-09-04 01:14:43 -0300517
518 /* Ensure firmware is always loaded if required */
519 state->skip_fw_load = 0;
520 }
521
522 return ret;
523}
524
Steven Toth98c94822008-10-16 20:24:42 -0300525/* Take a basic firmware command structure, format it
526 * and forward it for processing
527 */
Steven Tothf11ec7d2008-10-16 20:22:01 -0300528static int cx24116_cmd_execute(struct dvb_frontend *fe, struct cx24116_cmd *cmd)
Steven Toth0d467482008-09-04 01:14:43 -0300529{
530 struct cx24116_state *state = fe->demodulator_priv;
531 int i, ret;
532
533 dprintk("%s()\n", __func__);
534
535 /* Load the firmware if required */
Steven Tothf11ec7d2008-10-16 20:22:01 -0300536 ret = cx24116_firmware_ondemand(fe);
537 if (ret != 0) {
Steven Toth98c94822008-10-16 20:24:42 -0300538 printk(KERN_ERR "%s(): Unable initialise the firmware\n",
539 __func__);
Steven Toth0d467482008-09-04 01:14:43 -0300540 return ret;
541 }
542
543 /* Write the command */
Steven Tothf11ec7d2008-10-16 20:22:01 -0300544 for (i = 0; i < cmd->len ; i++) {
Steven Toth0d467482008-09-04 01:14:43 -0300545 dprintk("%s: 0x%02x == 0x%02x\n", __func__, i, cmd->args[i]);
546 cx24116_writereg(state, i, cmd->args[i]);
547 }
548
549 /* Start execution and wait for cmd to terminate */
Darron Broad490c8682008-09-13 19:42:16 -0300550 cx24116_writereg(state, CX24116_REG_EXECUTE, 0x01);
Steven Tothf11ec7d2008-10-16 20:22:01 -0300551 while (cx24116_readreg(state, CX24116_REG_EXECUTE)) {
Steven Toth0d467482008-09-04 01:14:43 -0300552 msleep(10);
Steven Tothf11ec7d2008-10-16 20:22:01 -0300553 if (i++ > 64) {
Steven Toth98c94822008-10-16 20:24:42 -0300554 /* Avoid looping forever if the firmware does
555 not respond */
556 printk(KERN_WARNING "%s() Firmware not responding\n",
557 __func__);
Steven Toth0d467482008-09-04 01:14:43 -0300558 return -EREMOTEIO;
559 }
560 }
561 return 0;
562}
563
Steven Tothf11ec7d2008-10-16 20:22:01 -0300564static int cx24116_load_firmware(struct dvb_frontend *fe,
565 const struct firmware *fw)
Steven Toth0d467482008-09-04 01:14:43 -0300566{
Steven Tothf11ec7d2008-10-16 20:22:01 -0300567 struct cx24116_state *state = fe->demodulator_priv;
Steven Toth0d467482008-09-04 01:14:43 -0300568 struct cx24116_cmd cmd;
Antti Palosaari107d7b12011-04-27 21:03:07 -0300569 int i, ret, len, remaining;
Darron Broad490c8682008-09-13 19:42:16 -0300570 unsigned char vers[4];
Steven Toth0d467482008-09-04 01:14:43 -0300571
572 dprintk("%s\n", __func__);
Steven Tothf11ec7d2008-10-16 20:22:01 -0300573 dprintk("Firmware is %zu bytes (%02x %02x .. %02x %02x)\n",
574 fw->size,
575 fw->data[0],
576 fw->data[1],
577 fw->data[fw->size-2],
578 fw->data[fw->size-1]);
Steven Toth0d467482008-09-04 01:14:43 -0300579
580 /* Toggle 88x SRST pin to reset demod */
581 if (state->config->reset_device)
582 state->config->reset_device(fe);
583
584 /* Begin the firmware load process */
585 /* Prepare the demod, load the firmware, cleanup after load */
Steven Toth0d467482008-09-04 01:14:43 -0300586
Darron Broad490c8682008-09-13 19:42:16 -0300587 /* Init PLL */
588 cx24116_writereg(state, 0xE5, 0x00);
589 cx24116_writereg(state, 0xF1, 0x08);
590 cx24116_writereg(state, 0xF2, 0x13);
591
592 /* Start PLL */
593 cx24116_writereg(state, 0xe0, 0x03);
594 cx24116_writereg(state, 0xe0, 0x00);
595
596 /* Unknown */
597 cx24116_writereg(state, CX24116_REG_CLKDIV, 0x46);
598 cx24116_writereg(state, CX24116_REG_RATEDIV, 0x00);
599
600 /* Unknown */
Steven Toth0d467482008-09-04 01:14:43 -0300601 cx24116_writereg(state, 0xF0, 0x03);
602 cx24116_writereg(state, 0xF4, 0x81);
603 cx24116_writereg(state, 0xF5, 0x00);
604 cx24116_writereg(state, 0xF6, 0x00);
605
Antti Palosaari107d7b12011-04-27 21:03:07 -0300606 /* Split firmware to the max I2C write len and write.
607 * This overflows 16 bit intentionally in order to get max write
608 * len when i2c_wr_max is set to 0. */
609 for (remaining = fw->size; remaining > 0;
610 remaining -= (u16) (state->config->i2c_wr_max - 1)) {
611
612 len = remaining;
613 if (len > (u16) (state->config->i2c_wr_max - 1))
614 len = (u16) (state->config->i2c_wr_max - 1);
615
616 cx24116_writeregN(state, 0xF7, &fw->data[fw->size - remaining],
617 len);
618 }
Steven Toth0d467482008-09-04 01:14:43 -0300619
620 cx24116_writereg(state, 0xF4, 0x10);
621 cx24116_writereg(state, 0xF0, 0x00);
622 cx24116_writereg(state, 0xF8, 0x06);
623
Darron Broad490c8682008-09-13 19:42:16 -0300624 /* Firmware CMD 10: VCO config */
625 cmd.args[0x00] = CMD_SET_VCO;
Steven Toth0d467482008-09-04 01:14:43 -0300626 cmd.args[0x01] = 0x05;
627 cmd.args[0x02] = 0xdc;
628 cmd.args[0x03] = 0xda;
629 cmd.args[0x04] = 0xae;
630 cmd.args[0x05] = 0xaa;
631 cmd.args[0x06] = 0x04;
632 cmd.args[0x07] = 0x9d;
633 cmd.args[0x08] = 0xfc;
634 cmd.args[0x09] = 0x06;
Steven Tothf11ec7d2008-10-16 20:22:01 -0300635 cmd.len = 0x0a;
Steven Toth0d467482008-09-04 01:14:43 -0300636 ret = cx24116_cmd_execute(fe, &cmd);
637 if (ret != 0)
638 return ret;
639
Darron Broad490c8682008-09-13 19:42:16 -0300640 cx24116_writereg(state, CX24116_REG_SSTATUS, 0x00);
Steven Toth0d467482008-09-04 01:14:43 -0300641
Darron Broad490c8682008-09-13 19:42:16 -0300642 /* Firmware CMD 14: Tuner config */
643 cmd.args[0x00] = CMD_TUNERINIT;
Steven Toth0d467482008-09-04 01:14:43 -0300644 cmd.args[0x01] = 0x00;
645 cmd.args[0x02] = 0x00;
Steven Tothf11ec7d2008-10-16 20:22:01 -0300646 cmd.len = 0x03;
Steven Toth0d467482008-09-04 01:14:43 -0300647 ret = cx24116_cmd_execute(fe, &cmd);
648 if (ret != 0)
649 return ret;
650
651 cx24116_writereg(state, 0xe5, 0x00);
652
Darron Broad490c8682008-09-13 19:42:16 -0300653 /* Firmware CMD 13: MPEG config */
654 cmd.args[0x00] = CMD_MPEGCONFIG;
Steven Toth0d467482008-09-04 01:14:43 -0300655 cmd.args[0x01] = 0x01;
656 cmd.args[0x02] = 0x75;
657 cmd.args[0x03] = 0x00;
Igor M. Liplianincc8c4f32008-09-09 13:57:47 -0300658 if (state->config->mpg_clk_pos_pol)
659 cmd.args[0x04] = state->config->mpg_clk_pos_pol;
660 else
661 cmd.args[0x04] = 0x02;
Steven Toth0d467482008-09-04 01:14:43 -0300662 cmd.args[0x05] = 0x00;
Steven Tothf11ec7d2008-10-16 20:22:01 -0300663 cmd.len = 0x06;
Steven Toth0d467482008-09-04 01:14:43 -0300664 ret = cx24116_cmd_execute(fe, &cmd);
665 if (ret != 0)
666 return ret;
667
Darron Broad490c8682008-09-13 19:42:16 -0300668 /* Firmware CMD 35: Get firmware version */
669 cmd.args[0x00] = CMD_UPDFWVERS;
Steven Tothf11ec7d2008-10-16 20:22:01 -0300670 cmd.len = 0x02;
671 for (i = 0; i < 4; i++) {
Darron Broad490c8682008-09-13 19:42:16 -0300672 cmd.args[0x01] = i;
673 ret = cx24116_cmd_execute(fe, &cmd);
674 if (ret != 0)
675 return ret;
Steven Tothf11ec7d2008-10-16 20:22:01 -0300676 vers[i] = cx24116_readreg(state, CX24116_REG_MAILBOX);
Darron Broad490c8682008-09-13 19:42:16 -0300677 }
Steven Toth98c94822008-10-16 20:24:42 -0300678 printk(KERN_INFO "%s: FW version %i.%i.%i.%i\n", __func__,
Darron Broad490c8682008-09-13 19:42:16 -0300679 vers[0], vers[1], vers[2], vers[3]);
680
Steven Toth0d467482008-09-04 01:14:43 -0300681 return 0;
682}
683
Steven Tothf11ec7d2008-10-16 20:22:01 -0300684static int cx24116_read_status(struct dvb_frontend *fe, fe_status_t *status)
Steven Toth0d467482008-09-04 01:14:43 -0300685{
686 struct cx24116_state *state = fe->demodulator_priv;
687
Darron Broad6639f1e2008-12-18 06:28:18 -0300688 int lock = cx24116_readreg(state, CX24116_REG_SSTATUS) &
689 CX24116_STATUS_MASK;
Steven Toth0d467482008-09-04 01:14:43 -0300690
691 dprintk("%s: status = 0x%02x\n", __func__, lock);
692
693 *status = 0;
694
Darron Broad490c8682008-09-13 19:42:16 -0300695 if (lock & CX24116_HAS_SIGNAL)
Steven Toth0d467482008-09-04 01:14:43 -0300696 *status |= FE_HAS_SIGNAL;
Darron Broad490c8682008-09-13 19:42:16 -0300697 if (lock & CX24116_HAS_CARRIER)
Steven Toth0d467482008-09-04 01:14:43 -0300698 *status |= FE_HAS_CARRIER;
Darron Broad490c8682008-09-13 19:42:16 -0300699 if (lock & CX24116_HAS_VITERBI)
Steven Toth0d467482008-09-04 01:14:43 -0300700 *status |= FE_HAS_VITERBI;
Darron Broad490c8682008-09-13 19:42:16 -0300701 if (lock & CX24116_HAS_SYNCLOCK)
Steven Toth0d467482008-09-04 01:14:43 -0300702 *status |= FE_HAS_SYNC | FE_HAS_LOCK;
703
704 return 0;
705}
706
Steven Tothf11ec7d2008-10-16 20:22:01 -0300707static int cx24116_read_ber(struct dvb_frontend *fe, u32 *ber)
Steven Toth0d467482008-09-04 01:14:43 -0300708{
Darron Broad490c8682008-09-13 19:42:16 -0300709 struct cx24116_state *state = fe->demodulator_priv;
710
Steven Toth0d467482008-09-04 01:14:43 -0300711 dprintk("%s()\n", __func__);
Darron Broad490c8682008-09-13 19:42:16 -0300712
Steven Tothf11ec7d2008-10-16 20:22:01 -0300713 *ber = (cx24116_readreg(state, CX24116_REG_BER24) << 24) |
714 (cx24116_readreg(state, CX24116_REG_BER16) << 16) |
715 (cx24116_readreg(state, CX24116_REG_BER8) << 8) |
716 cx24116_readreg(state, CX24116_REG_BER0);
Steven Toth0d467482008-09-04 01:14:43 -0300717
718 return 0;
719}
720
Darron Broad490c8682008-09-13 19:42:16 -0300721/* TODO Determine function and scale appropriately */
Steven Tothf11ec7d2008-10-16 20:22:01 -0300722static int cx24116_read_signal_strength(struct dvb_frontend *fe,
723 u16 *signal_strength)
Steven Toth0d467482008-09-04 01:14:43 -0300724{
725 struct cx24116_state *state = fe->demodulator_priv;
Darron Broad490c8682008-09-13 19:42:16 -0300726 struct cx24116_cmd cmd;
727 int ret;
728 u16 sig_reading;
729
730 dprintk("%s()\n", __func__);
731
732 /* Firmware CMD 19: Get AGC */
733 cmd.args[0x00] = CMD_GETAGC;
Steven Tothf11ec7d2008-10-16 20:22:01 -0300734 cmd.len = 0x01;
Darron Broad490c8682008-09-13 19:42:16 -0300735 ret = cx24116_cmd_execute(fe, &cmd);
736 if (ret != 0)
737 return ret;
738
Steven Tothf11ec7d2008-10-16 20:22:01 -0300739 sig_reading =
740 (cx24116_readreg(state,
741 CX24116_REG_SSTATUS) & CX24116_SIGNAL_MASK) |
742 (cx24116_readreg(state, CX24116_REG_SIGNAL) << 6);
743 *signal_strength = 0 - sig_reading;
Darron Broad490c8682008-09-13 19:42:16 -0300744
Steven Tothf11ec7d2008-10-16 20:22:01 -0300745 dprintk("%s: raw / cooked = 0x%04x / 0x%04x\n",
746 __func__, sig_reading, *signal_strength);
Darron Broad490c8682008-09-13 19:42:16 -0300747
748 return 0;
749}
750
751/* SNR (0..100)% = (sig & 0xf0) * 10 + (sig & 0x0f) * 10 / 16 */
Steven Tothf11ec7d2008-10-16 20:22:01 -0300752static int cx24116_read_snr_pct(struct dvb_frontend *fe, u16 *snr)
Darron Broad490c8682008-09-13 19:42:16 -0300753{
754 struct cx24116_state *state = fe->demodulator_priv;
755 u8 snr_reading;
756 static const u32 snr_tab[] = { /* 10 x Table (rounded up) */
Steven Tothf11ec7d2008-10-16 20:22:01 -0300757 0x00000, 0x0199A, 0x03333, 0x04ccD, 0x06667,
758 0x08000, 0x0999A, 0x0b333, 0x0cccD, 0x0e667,
759 0x10000, 0x1199A, 0x13333, 0x14ccD, 0x16667,
760 0x18000 };
Steven Toth0d467482008-09-04 01:14:43 -0300761
762 dprintk("%s()\n", __func__);
763
Steven Toth8953db72008-10-06 21:20:21 -0300764 snr_reading = cx24116_readreg(state, CX24116_REG_QUALITY0);
Steven Toth0d467482008-09-04 01:14:43 -0300765
Steven Tothf11ec7d2008-10-16 20:22:01 -0300766 if (snr_reading >= 0xa0 /* 100% */)
Darron Broad490c8682008-09-13 19:42:16 -0300767 *snr = 0xffff;
Steven Toth0d467482008-09-04 01:14:43 -0300768 else
Steven Tothf11ec7d2008-10-16 20:22:01 -0300769 *snr = snr_tab[(snr_reading & 0xf0) >> 4] +
770 (snr_tab[(snr_reading & 0x0f)] >> 4);
Steven Toth0d467482008-09-04 01:14:43 -0300771
Darron Broad490c8682008-09-13 19:42:16 -0300772 dprintk("%s: raw / cooked = 0x%02x / 0x%04x\n", __func__,
773 snr_reading, *snr);
Steven Toth0d467482008-09-04 01:14:43 -0300774
775 return 0;
776}
777
Steven Toth8953db72008-10-06 21:20:21 -0300778/* The reelbox patches show the value in the registers represents
779 * ESNO, from 0->30db (values 0->300). We provide this value by
780 * default.
781 */
Steven Tothf11ec7d2008-10-16 20:22:01 -0300782static int cx24116_read_snr_esno(struct dvb_frontend *fe, u16 *snr)
Steven Toth8953db72008-10-06 21:20:21 -0300783{
784 struct cx24116_state *state = fe->demodulator_priv;
785
786 dprintk("%s()\n", __func__);
787
788 *snr = cx24116_readreg(state, CX24116_REG_QUALITY8) << 8 |
789 cx24116_readreg(state, CX24116_REG_QUALITY0);
790
791 dprintk("%s: raw 0x%04x\n", __func__, *snr);
792
793 return 0;
794}
795
Steven Tothf11ec7d2008-10-16 20:22:01 -0300796static int cx24116_read_snr(struct dvb_frontend *fe, u16 *snr)
Steven Toth8953db72008-10-06 21:20:21 -0300797{
798 if (esno_snr == 1)
799 return cx24116_read_snr_esno(fe, snr);
800 else
801 return cx24116_read_snr_pct(fe, snr);
802}
803
Steven Tothf11ec7d2008-10-16 20:22:01 -0300804static int cx24116_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
Steven Toth0d467482008-09-04 01:14:43 -0300805{
Darron Broad490c8682008-09-13 19:42:16 -0300806 struct cx24116_state *state = fe->demodulator_priv;
807
Steven Toth0d467482008-09-04 01:14:43 -0300808 dprintk("%s()\n", __func__);
Darron Broad490c8682008-09-13 19:42:16 -0300809
Steven Tothf11ec7d2008-10-16 20:22:01 -0300810 *ucblocks = (cx24116_readreg(state, CX24116_REG_UCB8) << 8) |
Darron Broad490c8682008-09-13 19:42:16 -0300811 cx24116_readreg(state, CX24116_REG_UCB0);
Steven Toth0d467482008-09-04 01:14:43 -0300812
813 return 0;
814}
815
816/* Overwrite the current tuning params, we are about to tune */
Steven Tothf11ec7d2008-10-16 20:22:01 -0300817static void cx24116_clone_params(struct dvb_frontend *fe)
Steven Toth0d467482008-09-04 01:14:43 -0300818{
819 struct cx24116_state *state = fe->demodulator_priv;
820 memcpy(&state->dcur, &state->dnxt, sizeof(state->dcur));
821}
822
Darron Broad490c8682008-09-13 19:42:16 -0300823/* Wait for LNB */
Steven Tothf11ec7d2008-10-16 20:22:01 -0300824static int cx24116_wait_for_lnb(struct dvb_frontend *fe)
Darron Broad490c8682008-09-13 19:42:16 -0300825{
826 struct cx24116_state *state = fe->demodulator_priv;
827 int i;
828
829 dprintk("%s() qstatus = 0x%02x\n", __func__,
830 cx24116_readreg(state, CX24116_REG_QSTATUS));
831
832 /* Wait for up to 300 ms */
Steven Tothf11ec7d2008-10-16 20:22:01 -0300833 for (i = 0; i < 30 ; i++) {
Darron Broad490c8682008-09-13 19:42:16 -0300834 if (cx24116_readreg(state, CX24116_REG_QSTATUS) & 0x20)
835 return 0;
836 msleep(10);
837 }
838
839 dprintk("%s(): LNB not ready\n", __func__);
840
841 return -ETIMEDOUT; /* -EBUSY ? */
842}
843
Igor M. Liplianinc7bdcd02009-01-17 10:16:36 -0300844static int cx24116_set_voltage(struct dvb_frontend *fe,
845 fe_sec_voltage_t voltage)
846{
847 struct cx24116_cmd cmd;
848 int ret;
849
850 dprintk("%s: %s\n", __func__,
851 voltage == SEC_VOLTAGE_13 ? "SEC_VOLTAGE_13" :
852 voltage == SEC_VOLTAGE_18 ? "SEC_VOLTAGE_18" : "??");
853
854 /* Wait for LNB ready */
855 ret = cx24116_wait_for_lnb(fe);
856 if (ret != 0)
857 return ret;
858
859 /* Wait for voltage/min repeat delay */
860 msleep(100);
861
862 cmd.args[0x00] = CMD_LNBDCLEVEL;
863 cmd.args[0x01] = (voltage == SEC_VOLTAGE_18 ? 0x01 : 0x00);
864 cmd.len = 0x02;
865
866 /* Min delay time before DiSEqC send */
867 msleep(15);
868
869 return cx24116_cmd_execute(fe, &cmd);
870}
871
Steven Tothf11ec7d2008-10-16 20:22:01 -0300872static int cx24116_set_tone(struct dvb_frontend *fe,
873 fe_sec_tone_mode_t tone)
Steven Toth0d467482008-09-04 01:14:43 -0300874{
875 struct cx24116_cmd cmd;
876 int ret;
877
878 dprintk("%s(%d)\n", __func__, tone);
Steven Tothf11ec7d2008-10-16 20:22:01 -0300879 if ((tone != SEC_TONE_ON) && (tone != SEC_TONE_OFF)) {
Steven Toth98c94822008-10-16 20:24:42 -0300880 printk(KERN_ERR "%s: Invalid, tone=%d\n", __func__, tone);
Steven Toth0d467482008-09-04 01:14:43 -0300881 return -EINVAL;
882 }
883
Darron Broad490c8682008-09-13 19:42:16 -0300884 /* Wait for LNB ready */
885 ret = cx24116_wait_for_lnb(fe);
Steven Tothf11ec7d2008-10-16 20:22:01 -0300886 if (ret != 0)
Darron Broad490c8682008-09-13 19:42:16 -0300887 return ret;
888
889 /* Min delay time after DiSEqC send */
890 msleep(15); /* XXX determine is FW does this, see send_diseqc/burst */
891
Steven Toth0d467482008-09-04 01:14:43 -0300892 /* Now we set the tone */
893 cmd.args[0x00] = CMD_SET_TONE;
894 cmd.args[0x01] = 0x00;
895 cmd.args[0x02] = 0x00;
896
897 switch (tone) {
898 case SEC_TONE_ON:
899 dprintk("%s: setting tone on\n", __func__);
900 cmd.args[0x03] = 0x01;
901 break;
902 case SEC_TONE_OFF:
Steven Tothf11ec7d2008-10-16 20:22:01 -0300903 dprintk("%s: setting tone off\n", __func__);
Steven Toth0d467482008-09-04 01:14:43 -0300904 cmd.args[0x03] = 0x00;
905 break;
906 }
Steven Tothf11ec7d2008-10-16 20:22:01 -0300907 cmd.len = 0x04;
Steven Toth0d467482008-09-04 01:14:43 -0300908
Darron Broad490c8682008-09-13 19:42:16 -0300909 /* Min delay time before DiSEqC send */
910 msleep(15); /* XXX determine is FW does this, see send_diseqc/burst */
911
Steven Toth0d467482008-09-04 01:14:43 -0300912 return cx24116_cmd_execute(fe, &cmd);
913}
914
915/* Initialise DiSEqC */
Steven Tothf11ec7d2008-10-16 20:22:01 -0300916static int cx24116_diseqc_init(struct dvb_frontend *fe)
Steven Toth0d467482008-09-04 01:14:43 -0300917{
918 struct cx24116_state *state = fe->demodulator_priv;
Darron Broad490c8682008-09-13 19:42:16 -0300919 struct cx24116_cmd cmd;
920 int ret;
Steven Toth0d467482008-09-04 01:14:43 -0300921
Darron Broad490c8682008-09-13 19:42:16 -0300922 /* Firmware CMD 20: LNB/DiSEqC config */
923 cmd.args[0x00] = CMD_LNBCONFIG;
924 cmd.args[0x01] = 0x00;
925 cmd.args[0x02] = 0x10;
926 cmd.args[0x03] = 0x00;
927 cmd.args[0x04] = 0x8f;
928 cmd.args[0x05] = 0x28;
929 cmd.args[0x06] = (toneburst == CX24116_DISEQC_TONEOFF) ? 0x00 : 0x01;
930 cmd.args[0x07] = 0x01;
Steven Tothf11ec7d2008-10-16 20:22:01 -0300931 cmd.len = 0x08;
Darron Broad490c8682008-09-13 19:42:16 -0300932 ret = cx24116_cmd_execute(fe, &cmd);
933 if (ret != 0)
934 return ret;
935
936 /* Prepare a DiSEqC command */
937 state->dsec_cmd.args[0x00] = CMD_LNBSEND;
938
939 /* DiSEqC burst */
940 state->dsec_cmd.args[CX24116_DISEQC_BURST] = CX24116_DISEQC_MINI_A;
941
942 /* Unknown */
943 state->dsec_cmd.args[CX24116_DISEQC_ARG2_2] = 0x02;
944 state->dsec_cmd.args[CX24116_DISEQC_ARG3_0] = 0x00;
Steven Toth98c94822008-10-16 20:24:42 -0300945 /* Continuation flag? */
946 state->dsec_cmd.args[CX24116_DISEQC_ARG4_0] = 0x00;
Darron Broad490c8682008-09-13 19:42:16 -0300947
948 /* DiSEqC message length */
949 state->dsec_cmd.args[CX24116_DISEQC_MSGLEN] = 0x00;
950
951 /* Command length */
Steven Tothf11ec7d2008-10-16 20:22:01 -0300952 state->dsec_cmd.len = CX24116_DISEQC_MSGOFS;
Steven Toth0d467482008-09-04 01:14:43 -0300953
954 return 0;
955}
956
957/* Send DiSEqC message with derived burst (hack) || previous burst */
Steven Tothf11ec7d2008-10-16 20:22:01 -0300958static int cx24116_send_diseqc_msg(struct dvb_frontend *fe,
959 struct dvb_diseqc_master_cmd *d)
Steven Toth0d467482008-09-04 01:14:43 -0300960{
961 struct cx24116_state *state = fe->demodulator_priv;
Steven Toth0d467482008-09-04 01:14:43 -0300962 int i, ret;
963
964 /* Dump DiSEqC message */
965 if (debug) {
Steven Toth98c94822008-10-16 20:24:42 -0300966 printk(KERN_INFO "cx24116: %s(", __func__);
Steven Tothf11ec7d2008-10-16 20:22:01 -0300967 for (i = 0 ; i < d->msg_len ;) {
Steven Toth98c94822008-10-16 20:24:42 -0300968 printk(KERN_INFO "0x%02x", d->msg[i]);
Steven Tothf11ec7d2008-10-16 20:22:01 -0300969 if (++i < d->msg_len)
Steven Toth98c94822008-10-16 20:24:42 -0300970 printk(KERN_INFO ", ");
Steven Tothf11ec7d2008-10-16 20:22:01 -0300971 }
Darron Broad490c8682008-09-13 19:42:16 -0300972 printk(") toneburst=%d\n", toneburst);
Steven Toth0d467482008-09-04 01:14:43 -0300973 }
974
Darron Broad490c8682008-09-13 19:42:16 -0300975 /* Validate length */
Steven Tothf11ec7d2008-10-16 20:22:01 -0300976 if (d->msg_len > (CX24116_ARGLEN - CX24116_DISEQC_MSGOFS))
Steven Toth0d467482008-09-04 01:14:43 -0300977 return -EINVAL;
978
Steven Toth0d467482008-09-04 01:14:43 -0300979 /* DiSEqC message */
980 for (i = 0; i < d->msg_len; i++)
Darron Broad490c8682008-09-13 19:42:16 -0300981 state->dsec_cmd.args[CX24116_DISEQC_MSGOFS + i] = d->msg[i];
Steven Toth0d467482008-09-04 01:14:43 -0300982
Darron Broad490c8682008-09-13 19:42:16 -0300983 /* DiSEqC message length */
984 state->dsec_cmd.args[CX24116_DISEQC_MSGLEN] = d->msg_len;
Steven Toth0d467482008-09-04 01:14:43 -0300985
Darron Broad490c8682008-09-13 19:42:16 -0300986 /* Command length */
Steven Tothf11ec7d2008-10-16 20:22:01 -0300987 state->dsec_cmd.len = CX24116_DISEQC_MSGOFS +
988 state->dsec_cmd.args[CX24116_DISEQC_MSGLEN];
Steven Toth0d467482008-09-04 01:14:43 -0300989
Darron Broad490c8682008-09-13 19:42:16 -0300990 /* DiSEqC toneburst */
Steven Tothf11ec7d2008-10-16 20:22:01 -0300991 if (toneburst == CX24116_DISEQC_MESGCACHE)
Darron Broad490c8682008-09-13 19:42:16 -0300992 /* Message is cached */
993 return 0;
Steven Toth0d467482008-09-04 01:14:43 -0300994
Steven Tothf11ec7d2008-10-16 20:22:01 -0300995 else if (toneburst == CX24116_DISEQC_TONEOFF)
Darron Broad490c8682008-09-13 19:42:16 -0300996 /* Message is sent without burst */
997 state->dsec_cmd.args[CX24116_DISEQC_BURST] = 0;
998
Steven Tothf11ec7d2008-10-16 20:22:01 -0300999 else if (toneburst == CX24116_DISEQC_TONECACHE) {
Darron Broad490c8682008-09-13 19:42:16 -03001000 /*
1001 * Message is sent with derived else cached burst
1002 *
1003 * WRITE PORT GROUP COMMAND 38
1004 *
1005 * 0/A/A: E0 10 38 F0..F3
1006 * 1/B/B: E0 10 38 F4..F7
1007 * 2/C/A: E0 10 38 F8..FB
1008 * 3/D/B: E0 10 38 FC..FF
1009 *
Darron Broad7396d3e2008-09-14 10:45:58 -03001010 * databyte[3]= 8421:8421
Darron Broad490c8682008-09-13 19:42:16 -03001011 * ABCD:WXYZ
1012 * CLR :SET
1013 *
1014 * WX= PORT SELECT 0..3 (X=TONEBURST)
1015 * Y = VOLTAGE (0=13V, 1=18V)
1016 * Z = BAND (0=LOW, 1=HIGH(22K))
1017 */
Steven Tothf11ec7d2008-10-16 20:22:01 -03001018 if (d->msg_len >= 4 && d->msg[2] == 0x38)
1019 state->dsec_cmd.args[CX24116_DISEQC_BURST] =
1020 ((d->msg[3] & 4) >> 2);
1021 if (debug)
1022 dprintk("%s burst=%d\n", __func__,
1023 state->dsec_cmd.args[CX24116_DISEQC_BURST]);
Darron Broad490c8682008-09-13 19:42:16 -03001024 }
1025
1026 /* Wait for LNB ready */
1027 ret = cx24116_wait_for_lnb(fe);
Steven Tothf11ec7d2008-10-16 20:22:01 -03001028 if (ret != 0)
Darron Broad490c8682008-09-13 19:42:16 -03001029 return ret;
1030
1031 /* Wait for voltage/min repeat delay */
1032 msleep(100);
1033
1034 /* Command */
1035 ret = cx24116_cmd_execute(fe, &state->dsec_cmd);
Steven Tothf11ec7d2008-10-16 20:22:01 -03001036 if (ret != 0)
Darron Broad490c8682008-09-13 19:42:16 -03001037 return ret;
1038 /*
1039 * Wait for send
Steven Toth0d467482008-09-04 01:14:43 -03001040 *
1041 * Eutelsat spec:
Darron Broad490c8682008-09-13 19:42:16 -03001042 * >15ms delay + (XXX determine if FW does this, see set_tone)
1043 * 13.5ms per byte +
1044 * >15ms delay +
1045 * 12.5ms burst +
1046 * >15ms delay (XXX determine if FW does this, see set_tone)
Steven Toth0d467482008-09-04 01:14:43 -03001047 */
Steven Tothf11ec7d2008-10-16 20:22:01 -03001048 msleep((state->dsec_cmd.args[CX24116_DISEQC_MSGLEN] << 4) +
1049 ((toneburst == CX24116_DISEQC_TONEOFF) ? 30 : 60));
Steven Toth0d467482008-09-04 01:14:43 -03001050
Darron Broad490c8682008-09-13 19:42:16 -03001051 return 0;
Steven Toth0d467482008-09-04 01:14:43 -03001052}
1053
1054/* Send DiSEqC burst */
Steven Tothf11ec7d2008-10-16 20:22:01 -03001055static int cx24116_diseqc_send_burst(struct dvb_frontend *fe,
1056 fe_sec_mini_cmd_t burst)
Steven Toth0d467482008-09-04 01:14:43 -03001057{
1058 struct cx24116_state *state = fe->demodulator_priv;
Steven Toth0d467482008-09-04 01:14:43 -03001059 int ret;
1060
Steven Tothf11ec7d2008-10-16 20:22:01 -03001061 dprintk("%s(%d) toneburst=%d\n", __func__, burst, toneburst);
Steven Toth0d467482008-09-04 01:14:43 -03001062
Darron Broad490c8682008-09-13 19:42:16 -03001063 /* DiSEqC burst */
Steven Toth0d467482008-09-04 01:14:43 -03001064 if (burst == SEC_MINI_A)
Steven Tothf11ec7d2008-10-16 20:22:01 -03001065 state->dsec_cmd.args[CX24116_DISEQC_BURST] =
1066 CX24116_DISEQC_MINI_A;
1067 else if (burst == SEC_MINI_B)
1068 state->dsec_cmd.args[CX24116_DISEQC_BURST] =
1069 CX24116_DISEQC_MINI_B;
Steven Toth0d467482008-09-04 01:14:43 -03001070 else
1071 return -EINVAL;
1072
Darron Broad490c8682008-09-13 19:42:16 -03001073 /* DiSEqC toneburst */
Steven Tothf11ec7d2008-10-16 20:22:01 -03001074 if (toneburst != CX24116_DISEQC_MESGCACHE)
Darron Broad490c8682008-09-13 19:42:16 -03001075 /* Burst is cached */
1076 return 0;
Steven Toth0d467482008-09-04 01:14:43 -03001077
Darron Broad490c8682008-09-13 19:42:16 -03001078 /* Burst is to be sent with cached message */
Steven Toth0d467482008-09-04 01:14:43 -03001079
Darron Broad490c8682008-09-13 19:42:16 -03001080 /* Wait for LNB ready */
1081 ret = cx24116_wait_for_lnb(fe);
Steven Tothf11ec7d2008-10-16 20:22:01 -03001082 if (ret != 0)
Darron Broad490c8682008-09-13 19:42:16 -03001083 return ret;
Steven Toth0d467482008-09-04 01:14:43 -03001084
Darron Broad490c8682008-09-13 19:42:16 -03001085 /* Wait for voltage/min repeat delay */
1086 msleep(100);
Steven Toth0d467482008-09-04 01:14:43 -03001087
Darron Broad490c8682008-09-13 19:42:16 -03001088 /* Command */
1089 ret = cx24116_cmd_execute(fe, &state->dsec_cmd);
Steven Tothf11ec7d2008-10-16 20:22:01 -03001090 if (ret != 0)
Darron Broad490c8682008-09-13 19:42:16 -03001091 return ret;
1092
1093 /*
1094 * Wait for send
1095 *
1096 * Eutelsat spec:
1097 * >15ms delay + (XXX determine if FW does this, see set_tone)
1098 * 13.5ms per byte +
1099 * >15ms delay +
1100 * 12.5ms burst +
1101 * >15ms delay (XXX determine if FW does this, see set_tone)
1102 */
Steven Tothf11ec7d2008-10-16 20:22:01 -03001103 msleep((state->dsec_cmd.args[CX24116_DISEQC_MSGLEN] << 4) + 60);
Darron Broad490c8682008-09-13 19:42:16 -03001104
1105 return 0;
Steven Toth0d467482008-09-04 01:14:43 -03001106}
1107
Steven Tothf11ec7d2008-10-16 20:22:01 -03001108static void cx24116_release(struct dvb_frontend *fe)
Steven Toth0d467482008-09-04 01:14:43 -03001109{
Steven Tothf11ec7d2008-10-16 20:22:01 -03001110 struct cx24116_state *state = fe->demodulator_priv;
1111 dprintk("%s\n", __func__);
Steven Toth0d467482008-09-04 01:14:43 -03001112 kfree(state);
1113}
1114
1115static struct dvb_frontend_ops cx24116_ops;
1116
Steven Tothf11ec7d2008-10-16 20:22:01 -03001117struct dvb_frontend *cx24116_attach(const struct cx24116_config *config,
1118 struct i2c_adapter *i2c)
Steven Toth0d467482008-09-04 01:14:43 -03001119{
Steven Tothf11ec7d2008-10-16 20:22:01 -03001120 struct cx24116_state *state = NULL;
Steven Toth0d467482008-09-04 01:14:43 -03001121 int ret;
1122
Steven Tothf11ec7d2008-10-16 20:22:01 -03001123 dprintk("%s\n", __func__);
Steven Toth0d467482008-09-04 01:14:43 -03001124
1125 /* allocate memory for the internal state */
Matthias Schwarzott8420fa72009-02-23 12:26:38 -03001126 state = kzalloc(sizeof(struct cx24116_state), GFP_KERNEL);
Steven Toth98c94822008-10-16 20:24:42 -03001127 if (state == NULL)
Darron Broad7396d3e2008-09-14 10:45:58 -03001128 goto error1;
Steven Toth0d467482008-09-04 01:14:43 -03001129
Steven Toth0d467482008-09-04 01:14:43 -03001130 state->config = config;
1131 state->i2c = i2c;
1132
1133 /* check if the demod is present */
Steven Toth98c94822008-10-16 20:24:42 -03001134 ret = (cx24116_readreg(state, 0xFF) << 8) |
1135 cx24116_readreg(state, 0xFE);
Steven Toth0d467482008-09-04 01:14:43 -03001136 if (ret != 0x0501) {
Steven Toth98c94822008-10-16 20:24:42 -03001137 printk(KERN_INFO "Invalid probe, probably not a CX24116 device\n");
Darron Broad7396d3e2008-09-14 10:45:58 -03001138 goto error2;
Steven Toth0d467482008-09-04 01:14:43 -03001139 }
1140
1141 /* create dvb_frontend */
Steven Toth98c94822008-10-16 20:24:42 -03001142 memcpy(&state->frontend.ops, &cx24116_ops,
1143 sizeof(struct dvb_frontend_ops));
Steven Toth0d467482008-09-04 01:14:43 -03001144 state->frontend.demodulator_priv = state;
1145 return &state->frontend;
1146
Darron Broad7396d3e2008-09-14 10:45:58 -03001147error2: kfree(state);
Darron Broad490c8682008-09-13 19:42:16 -03001148error1: return NULL;
Steven Toth0d467482008-09-04 01:14:43 -03001149}
Steven Tothf11ec7d2008-10-16 20:22:01 -03001150EXPORT_SYMBOL(cx24116_attach);
1151
Darron Broad490c8682008-09-13 19:42:16 -03001152/*
1153 * Initialise or wake up device
1154 *
1155 * Power config will reset and load initial firmware if required
1156 */
Steven Tothf11ec7d2008-10-16 20:22:01 -03001157static int cx24116_initfe(struct dvb_frontend *fe)
Steven Toth0d467482008-09-04 01:14:43 -03001158{
Steven Tothf11ec7d2008-10-16 20:22:01 -03001159 struct cx24116_state *state = fe->demodulator_priv;
Darron Broad490c8682008-09-13 19:42:16 -03001160 struct cx24116_cmd cmd;
1161 int ret;
1162
Steven Tothf11ec7d2008-10-16 20:22:01 -03001163 dprintk("%s()\n", __func__);
Steven Toth0d467482008-09-04 01:14:43 -03001164
Darron Broad490c8682008-09-13 19:42:16 -03001165 /* Power on */
1166 cx24116_writereg(state, 0xe0, 0);
1167 cx24116_writereg(state, 0xe1, 0);
1168 cx24116_writereg(state, 0xea, 0);
1169
1170 /* Firmware CMD 36: Power config */
1171 cmd.args[0x00] = CMD_TUNERSLEEP;
1172 cmd.args[0x01] = 0;
Steven Tothf11ec7d2008-10-16 20:22:01 -03001173 cmd.len = 0x02;
Darron Broad490c8682008-09-13 19:42:16 -03001174 ret = cx24116_cmd_execute(fe, &cmd);
Steven Tothf11ec7d2008-10-16 20:22:01 -03001175 if (ret != 0)
Darron Broad490c8682008-09-13 19:42:16 -03001176 return ret;
1177
Igor M. Liplianin8afe6ad2009-01-29 21:57:07 -03001178 ret = cx24116_diseqc_init(fe);
1179 if (ret != 0)
1180 return ret;
1181
1182 /* HVR-4000 needs this */
1183 return cx24116_set_voltage(fe, SEC_VOLTAGE_13);
Steven Toth0d467482008-09-04 01:14:43 -03001184}
1185
Darron Broad490c8682008-09-13 19:42:16 -03001186/*
1187 * Put device to sleep
1188 */
Steven Tothf11ec7d2008-10-16 20:22:01 -03001189static int cx24116_sleep(struct dvb_frontend *fe)
Darron Broad490c8682008-09-13 19:42:16 -03001190{
Steven Tothf11ec7d2008-10-16 20:22:01 -03001191 struct cx24116_state *state = fe->demodulator_priv;
Darron Broad490c8682008-09-13 19:42:16 -03001192 struct cx24116_cmd cmd;
1193 int ret;
1194
Steven Tothf11ec7d2008-10-16 20:22:01 -03001195 dprintk("%s()\n", __func__);
Darron Broad490c8682008-09-13 19:42:16 -03001196
1197 /* Firmware CMD 36: Power config */
1198 cmd.args[0x00] = CMD_TUNERSLEEP;
1199 cmd.args[0x01] = 1;
Steven Tothf11ec7d2008-10-16 20:22:01 -03001200 cmd.len = 0x02;
Darron Broad490c8682008-09-13 19:42:16 -03001201 ret = cx24116_cmd_execute(fe, &cmd);
Steven Tothf11ec7d2008-10-16 20:22:01 -03001202 if (ret != 0)
Darron Broad490c8682008-09-13 19:42:16 -03001203 return ret;
1204
1205 /* Power off (Shutdown clocks) */
1206 cx24116_writereg(state, 0xea, 0xff);
1207 cx24116_writereg(state, 0xe1, 1);
1208 cx24116_writereg(state, 0xe0, 1);
1209
1210 return 0;
1211}
1212
Steven Tothf11ec7d2008-10-16 20:22:01 -03001213static int cx24116_set_property(struct dvb_frontend *fe,
1214 struct dtv_property *tvp)
Steven Toth0d467482008-09-04 01:14:43 -03001215{
1216 dprintk("%s(..)\n", __func__);
1217 return 0;
1218}
1219
Steven Tothf11ec7d2008-10-16 20:22:01 -03001220static int cx24116_get_property(struct dvb_frontend *fe,
1221 struct dtv_property *tvp)
Steven Toth0d467482008-09-04 01:14:43 -03001222{
Steven Tothbfbf2da2008-09-12 01:37:37 -03001223 dprintk("%s(..)\n", __func__);
Steven Toth0d467482008-09-04 01:14:43 -03001224 return 0;
1225}
1226
1227/* dvb-core told us to tune, the tv property cache will be complete,
1228 * it's safe for is to pull values and use them for tuning purposes.
1229 */
Steven Tothf11ec7d2008-10-16 20:22:01 -03001230static int cx24116_set_frontend(struct dvb_frontend *fe,
1231 struct dvb_frontend_parameters *p)
Steven Toth0d467482008-09-04 01:14:43 -03001232{
1233 struct cx24116_state *state = fe->demodulator_priv;
Steven Toth56f06802008-09-11 10:19:27 -03001234 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Steven Toth0d467482008-09-04 01:14:43 -03001235 struct cx24116_cmd cmd;
1236 fe_status_t tunerstat;
Darron Broad2fd93392008-12-18 06:27:23 -03001237 int i, status, ret, retune = 1;
Steven Toth0d467482008-09-04 01:14:43 -03001238
Steven Tothf11ec7d2008-10-16 20:22:01 -03001239 dprintk("%s()\n", __func__);
Steven Toth0d467482008-09-04 01:14:43 -03001240
Steven Tothf11ec7d2008-10-16 20:22:01 -03001241 switch (c->delivery_system) {
1242 case SYS_DVBS:
1243 dprintk("%s: DVB-S delivery system selected\n", __func__);
Darron Broad01a8f032008-10-03 11:47:46 -03001244
Steven Tothf11ec7d2008-10-16 20:22:01 -03001245 /* Only QPSK is supported for DVB-S */
1246 if (c->modulation != QPSK) {
1247 dprintk("%s: unsupported modulation selected (%d)\n",
1248 __func__, c->modulation);
1249 return -EOPNOTSUPP;
1250 }
Darron Broad01a8f032008-10-03 11:47:46 -03001251
Steven Tothf11ec7d2008-10-16 20:22:01 -03001252 /* Pilot doesn't exist in DVB-S, turn bit off */
1253 state->dnxt.pilot_val = CX24116_PILOT_OFF;
Steven Tothf11ec7d2008-10-16 20:22:01 -03001254
1255 /* DVB-S only supports 0.35 */
1256 if (c->rolloff != ROLLOFF_35) {
1257 dprintk("%s: unsupported rolloff selected (%d)\n",
1258 __func__, c->rolloff);
1259 return -EOPNOTSUPP;
1260 }
1261 state->dnxt.rolloff_val = CX24116_ROLLOFF_035;
1262 break;
1263
1264 case SYS_DVBS2:
1265 dprintk("%s: DVB-S2 delivery system selected\n", __func__);
1266
1267 /*
1268 * NBC 8PSK/QPSK with DVB-S is supported for DVB-S2,
1269 * but not hardware auto detection
1270 */
1271 if (c->modulation != PSK_8 && c->modulation != QPSK) {
1272 dprintk("%s: unsupported modulation selected (%d)\n",
1273 __func__, c->modulation);
1274 return -EOPNOTSUPP;
1275 }
1276
1277 switch (c->pilot) {
1278 case PILOT_AUTO: /* Not supported but emulated */
Christophe Thommeret74563212008-10-15 20:01:32 -03001279 state->dnxt.pilot_val = (c->modulation == QPSK)
1280 ? CX24116_PILOT_OFF : CX24116_PILOT_ON;
Darron Broad2fd93392008-12-18 06:27:23 -03001281 retune++;
Christophe Thommeret74563212008-10-15 20:01:32 -03001282 break;
Steven Tothf11ec7d2008-10-16 20:22:01 -03001283 case PILOT_OFF:
Darron Broad01a8f032008-10-03 11:47:46 -03001284 state->dnxt.pilot_val = CX24116_PILOT_OFF;
Steven Tothf11ec7d2008-10-16 20:22:01 -03001285 break;
1286 case PILOT_ON:
1287 state->dnxt.pilot_val = CX24116_PILOT_ON;
1288 break;
1289 default:
1290 dprintk("%s: unsupported pilot mode selected (%d)\n",
1291 __func__, c->pilot);
1292 return -EOPNOTSUPP;
1293 }
Darron Broad01a8f032008-10-03 11:47:46 -03001294
Steven Tothf11ec7d2008-10-16 20:22:01 -03001295 switch (c->rolloff) {
1296 case ROLLOFF_20:
1297 state->dnxt.rolloff_val = CX24116_ROLLOFF_020;
1298 break;
1299 case ROLLOFF_25:
1300 state->dnxt.rolloff_val = CX24116_ROLLOFF_025;
1301 break;
1302 case ROLLOFF_35:
Darron Broad7396d3e2008-09-14 10:45:58 -03001303 state->dnxt.rolloff_val = CX24116_ROLLOFF_035;
Darron Broad490c8682008-09-13 19:42:16 -03001304 break;
Steven Tothf11ec7d2008-10-16 20:22:01 -03001305 case ROLLOFF_AUTO: /* Rolloff must be explicit */
Darron Broad490c8682008-09-13 19:42:16 -03001306 default:
Steven Tothf11ec7d2008-10-16 20:22:01 -03001307 dprintk("%s: unsupported rolloff selected (%d)\n",
1308 __func__, c->rolloff);
Darron Broad490c8682008-09-13 19:42:16 -03001309 return -EOPNOTSUPP;
Steven Tothf11ec7d2008-10-16 20:22:01 -03001310 }
1311 break;
1312
1313 default:
1314 dprintk("%s: unsupported delivery system selected (%d)\n",
1315 __func__, c->delivery_system);
1316 return -EOPNOTSUPP;
Darron Broad490c8682008-09-13 19:42:16 -03001317 }
Darron Broad35694762008-12-18 06:21:51 -03001318 state->dnxt.delsys = c->delivery_system;
Darron Broad01a8f032008-10-03 11:47:46 -03001319 state->dnxt.modulation = c->modulation;
1320 state->dnxt.frequency = c->frequency;
1321 state->dnxt.pilot = c->pilot;
1322 state->dnxt.rolloff = c->rolloff;
Darron Broad490c8682008-09-13 19:42:16 -03001323
Steven Tothf11ec7d2008-10-16 20:22:01 -03001324 ret = cx24116_set_inversion(state, c->inversion);
1325 if (ret != 0)
Steven Toth0d467482008-09-04 01:14:43 -03001326 return ret;
1327
Darron Broad01a8f032008-10-03 11:47:46 -03001328 /* FEC_NONE/AUTO for DVB-S2 is not supported and detected here */
Darron Broad35694762008-12-18 06:21:51 -03001329 ret = cx24116_set_fec(state, c->delivery_system, c->modulation, c->fec_inner);
Steven Tothf11ec7d2008-10-16 20:22:01 -03001330 if (ret != 0)
Steven Toth0d467482008-09-04 01:14:43 -03001331 return ret;
1332
Steven Tothf11ec7d2008-10-16 20:22:01 -03001333 ret = cx24116_set_symbolrate(state, c->symbol_rate);
1334 if (ret != 0)
Steven Toth0d467482008-09-04 01:14:43 -03001335 return ret;
1336
1337 /* discard the 'current' tuning parameters and prepare to tune */
1338 cx24116_clone_params(fe);
1339
Darron Broad35694762008-12-18 06:21:51 -03001340 dprintk("%s: delsys = %d\n", __func__, state->dcur.delsys);
Darron Broad01a8f032008-10-03 11:47:46 -03001341 dprintk("%s: modulation = %d\n", __func__, state->dcur.modulation);
Steven Toth0d467482008-09-04 01:14:43 -03001342 dprintk("%s: frequency = %d\n", __func__, state->dcur.frequency);
Darron Broad01a8f032008-10-03 11:47:46 -03001343 dprintk("%s: pilot = %d (val = 0x%02x)\n", __func__,
1344 state->dcur.pilot, state->dcur.pilot_val);
1345 dprintk("%s: retune = %d\n", __func__, retune);
1346 dprintk("%s: rolloff = %d (val = 0x%02x)\n", __func__,
1347 state->dcur.rolloff, state->dcur.rolloff_val);
Steven Toth0d467482008-09-04 01:14:43 -03001348 dprintk("%s: symbol_rate = %d\n", __func__, state->dcur.symbol_rate);
1349 dprintk("%s: FEC = %d (mask/val = 0x%02x/0x%02x)\n", __func__,
1350 state->dcur.fec, state->dcur.fec_mask, state->dcur.fec_val);
1351 dprintk("%s: Inversion = %d (val = 0x%02x)\n", __func__,
1352 state->dcur.inversion, state->dcur.inversion_val);
1353
Darron Broad490c8682008-09-13 19:42:16 -03001354 /* This is also done in advise/acquire on HVR4000 but not on LITE */
Steven Toth0d467482008-09-04 01:14:43 -03001355 if (state->config->set_ts_params)
1356 state->config->set_ts_params(fe, 0);
1357
Darron Broad490c8682008-09-13 19:42:16 -03001358 /* Set/Reset B/W */
1359 cmd.args[0x00] = CMD_BANDWIDTH;
1360 cmd.args[0x01] = 0x01;
Steven Tothf11ec7d2008-10-16 20:22:01 -03001361 cmd.len = 0x02;
Darron Broad490c8682008-09-13 19:42:16 -03001362 ret = cx24116_cmd_execute(fe, &cmd);
1363 if (ret != 0)
1364 return ret;
Igor M. Liplianin3f8e51a2008-09-09 13:22:29 -03001365
Steven Toth0d467482008-09-04 01:14:43 -03001366 /* Prepare a tune request */
1367 cmd.args[0x00] = CMD_TUNEREQUEST;
1368
1369 /* Frequency */
1370 cmd.args[0x01] = (state->dcur.frequency & 0xff0000) >> 16;
1371 cmd.args[0x02] = (state->dcur.frequency & 0x00ff00) >> 8;
1372 cmd.args[0x03] = (state->dcur.frequency & 0x0000ff);
1373
1374 /* Symbol Rate */
1375 cmd.args[0x04] = ((state->dcur.symbol_rate / 1000) & 0xff00) >> 8;
1376 cmd.args[0x05] = ((state->dcur.symbol_rate / 1000) & 0x00ff);
1377
1378 /* Automatic Inversion */
1379 cmd.args[0x06] = state->dcur.inversion_val;
1380
Darron Broad01a8f032008-10-03 11:47:46 -03001381 /* Modulation / FEC / Pilot */
1382 cmd.args[0x07] = state->dcur.fec_val | state->dcur.pilot_val;
Steven Toth0d467482008-09-04 01:14:43 -03001383
1384 cmd.args[0x08] = CX24116_SEARCH_RANGE_KHZ >> 8;
1385 cmd.args[0x09] = CX24116_SEARCH_RANGE_KHZ & 0xff;
1386 cmd.args[0x0a] = 0x00;
1387 cmd.args[0x0b] = 0x00;
Darron Broad490c8682008-09-13 19:42:16 -03001388 cmd.args[0x0c] = state->dcur.rolloff_val;
Steven Toth0d467482008-09-04 01:14:43 -03001389 cmd.args[0x0d] = state->dcur.fec_mask;
Igor M. Liplianin3f8e51a2008-09-09 13:22:29 -03001390
Darron Broad490c8682008-09-13 19:42:16 -03001391 if (state->dcur.symbol_rate > 30000000) {
Igor M. Liplianin3f8e51a2008-09-09 13:22:29 -03001392 cmd.args[0x0e] = 0x04;
1393 cmd.args[0x0f] = 0x00;
1394 cmd.args[0x10] = 0x01;
1395 cmd.args[0x11] = 0x77;
1396 cmd.args[0x12] = 0x36;
Darron Broad490c8682008-09-13 19:42:16 -03001397 cx24116_writereg(state, CX24116_REG_CLKDIV, 0x44);
1398 cx24116_writereg(state, CX24116_REG_RATEDIV, 0x01);
Igor M. Liplianin3f8e51a2008-09-09 13:22:29 -03001399 } else {
1400 cmd.args[0x0e] = 0x06;
1401 cmd.args[0x0f] = 0x00;
1402 cmd.args[0x10] = 0x00;
1403 cmd.args[0x11] = 0xFA;
1404 cmd.args[0x12] = 0x24;
Darron Broad490c8682008-09-13 19:42:16 -03001405 cx24116_writereg(state, CX24116_REG_CLKDIV, 0x46);
1406 cx24116_writereg(state, CX24116_REG_RATEDIV, 0x00);
Igor M. Liplianin3f8e51a2008-09-09 13:22:29 -03001407 }
1408
Steven Tothf11ec7d2008-10-16 20:22:01 -03001409 cmd.len = 0x13;
Steven Toth0d467482008-09-04 01:14:43 -03001410
1411 /* We need to support pilot and non-pilot tuning in the
1412 * driver automatically. This is a workaround for because
1413 * the demod does not support autodetect.
1414 */
1415 do {
Darron Broad490c8682008-09-13 19:42:16 -03001416 /* Reset status register */
Steven Tothf11ec7d2008-10-16 20:22:01 -03001417 status = cx24116_readreg(state, CX24116_REG_SSTATUS)
1418 & CX24116_SIGNAL_MASK;
Darron Broad490c8682008-09-13 19:42:16 -03001419 cx24116_writereg(state, CX24116_REG_SSTATUS, status);
Steven Toth0d467482008-09-04 01:14:43 -03001420
1421 /* Tune */
1422 ret = cx24116_cmd_execute(fe, &cmd);
Steven Tothf11ec7d2008-10-16 20:22:01 -03001423 if (ret != 0)
Steven Toth0d467482008-09-04 01:14:43 -03001424 break;
1425
Darron Broad490c8682008-09-13 19:42:16 -03001426 /*
1427 * Wait for up to 500 ms before retrying
1428 *
1429 * If we are able to tune then generally it occurs within 100ms.
1430 * If it takes longer, try a different toneburst setting.
1431 */
Steven Tothf11ec7d2008-10-16 20:22:01 -03001432 for (i = 0; i < 50 ; i++) {
Darron Broad490c8682008-09-13 19:42:16 -03001433 cx24116_read_status(fe, &tunerstat);
1434 status = tunerstat & (FE_HAS_SIGNAL | FE_HAS_SYNC);
Steven Tothf11ec7d2008-10-16 20:22:01 -03001435 if (status == (FE_HAS_SIGNAL | FE_HAS_SYNC)) {
1436 dprintk("%s: Tuned\n", __func__);
Darron Broad490c8682008-09-13 19:42:16 -03001437 goto tuned;
1438 }
1439 msleep(10);
Steven Toth0d467482008-09-04 01:14:43 -03001440 }
Darron Broad490c8682008-09-13 19:42:16 -03001441
Steven Tothf11ec7d2008-10-16 20:22:01 -03001442 dprintk("%s: Not tuned\n", __func__);
Darron Broad490c8682008-09-13 19:42:16 -03001443
1444 /* Toggle pilot bit when in auto-pilot */
Steven Tothf11ec7d2008-10-16 20:22:01 -03001445 if (state->dcur.pilot == PILOT_AUTO)
Darron Broad01a8f032008-10-03 11:47:46 -03001446 cmd.args[0x07] ^= CX24116_PILOT_ON;
Steven Tothf11ec7d2008-10-16 20:22:01 -03001447 } while (--retune);
Steven Toth0d467482008-09-04 01:14:43 -03001448
Darron Broad490c8682008-09-13 19:42:16 -03001449tuned: /* Set/Reset B/W */
1450 cmd.args[0x00] = CMD_BANDWIDTH;
1451 cmd.args[0x01] = 0x00;
Steven Tothf11ec7d2008-10-16 20:22:01 -03001452 cmd.len = 0x02;
Darron Broad490c8682008-09-13 19:42:16 -03001453 ret = cx24116_cmd_execute(fe, &cmd);
1454 if (ret != 0)
1455 return ret;
1456
Steven Toth0d467482008-09-04 01:14:43 -03001457 return ret;
1458}
1459
Darron Broad6639f1e2008-12-18 06:28:18 -03001460static int cx24116_tune(struct dvb_frontend *fe, struct dvb_frontend_parameters *params,
1461 unsigned int mode_flags, unsigned int *delay, fe_status_t *status)
1462{
1463 *delay = HZ / 5;
1464 if (params) {
1465 int ret = cx24116_set_frontend(fe, params);
1466 if (ret)
1467 return ret;
1468 }
1469 return cx24116_read_status(fe, status);
1470}
1471
1472static int cx24116_get_algo(struct dvb_frontend *fe)
1473{
1474 return DVBFE_ALGO_HW;
1475}
1476
Steven Toth0d467482008-09-04 01:14:43 -03001477static struct dvb_frontend_ops cx24116_ops = {
1478
1479 .info = {
1480 .name = "Conexant CX24116/CX24118",
1481 .type = FE_QPSK,
1482 .frequency_min = 950000,
1483 .frequency_max = 2150000,
1484 .frequency_stepsize = 1011, /* kHz for QPSK frontends */
1485 .frequency_tolerance = 5000,
1486 .symbol_rate_min = 1000000,
1487 .symbol_rate_max = 45000000,
1488 .caps = FE_CAN_INVERSION_AUTO |
1489 FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
1490 FE_CAN_FEC_4_5 | FE_CAN_FEC_5_6 | FE_CAN_FEC_6_7 |
1491 FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
Klaus Schmidingerfaed4aa2008-12-31 14:13:56 -03001492 FE_CAN_2G_MODULATION |
Steven Toth0d467482008-09-04 01:14:43 -03001493 FE_CAN_QPSK | FE_CAN_RECOVER
1494 },
1495
1496 .release = cx24116_release,
1497
1498 .init = cx24116_initfe,
Darron Broad490c8682008-09-13 19:42:16 -03001499 .sleep = cx24116_sleep,
Steven Toth0d467482008-09-04 01:14:43 -03001500 .read_status = cx24116_read_status,
1501 .read_ber = cx24116_read_ber,
1502 .read_signal_strength = cx24116_read_signal_strength,
1503 .read_snr = cx24116_read_snr,
1504 .read_ucblocks = cx24116_read_ucblocks,
1505 .set_tone = cx24116_set_tone,
1506 .set_voltage = cx24116_set_voltage,
1507 .diseqc_send_master_cmd = cx24116_send_diseqc_msg,
1508 .diseqc_send_burst = cx24116_diseqc_send_burst,
Darron Broad6639f1e2008-12-18 06:28:18 -03001509 .get_frontend_algo = cx24116_get_algo,
1510 .tune = cx24116_tune,
Steven Toth0d467482008-09-04 01:14:43 -03001511
1512 .set_property = cx24116_set_property,
Steven Tothbfbf2da2008-09-12 01:37:37 -03001513 .get_property = cx24116_get_property,
Steven Toth0d467482008-09-04 01:14:43 -03001514 .set_frontend = cx24116_set_frontend,
1515};
1516
Steven Toth0d467482008-09-04 01:14:43 -03001517MODULE_DESCRIPTION("DVB Frontend module for Conexant cx24116/cx24118 hardware");
1518MODULE_AUTHOR("Steven Toth");
1519MODULE_LICENSE("GPL");
1520