2 Conexant cx24116/cx24118 - DVBS/S2 Satellite demod/tuner driver
4 Copyright (C) 2006-2008 Steven Toth <stoth@hauppauge.com>
5 Copyright (C) 2006-2007 Georg Acher
6 Copyright (C) 2007-2008 Darron Broad
10 Added corrected signal strength support.
12 Sync with legacy version.
14 Copyright (C) 2008 Igor Liplianin
16 Fixed locking on high symbol rates (>30000).
17 Implement MPEG initialization parameter.
19 This program is free software; you can redistribute it and/or modify
20 it under the terms of the GNU General Public License as published by
21 the Free Software Foundation; either version 2 of the License, or
22 (at your option) any later version.
24 This program is distributed in the hope that it will be useful,
25 but WITHOUT ANY WARRANTY; without even the implied warranty of
26 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 GNU General Public License for more details.
29 You should have received a copy of the GNU General Public License
30 along with this program; if not, write to the Free Software
31 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
34 #include <linux/slab.h>
35 #include <linux/kernel.h>
36 #include <linux/module.h>
37 #include <linux/moduleparam.h>
38 #include <linux/init.h>
39 #include <linux/firmware.h>
41 #include "dvb_frontend.h"
45 #define dprintk(args...) \
47 if (debug) printk ("cx24116: " args); \
50 #define CX24116_DEFAULT_FIRMWARE "dvb-fe-cx24116.fw"
51 #define CX24116_SEARCH_RANGE_KHZ 5000
54 #define CX24116_REG_COMMAND (0x00) /* command args 0x00..0x1e */
55 #define CX24116_REG_EXECUTE (0x1f) /* execute command */
56 #define CX24116_REG_MAILBOX (0x96) /* FW or multipurpose mailbox? */
57 #define CX24116_REG_RESET (0x20) /* reset status > 0 */
58 #define CX24116_REG_SIGNAL (0x9e) /* signal low */
59 #define CX24116_REG_SSTATUS (0x9d) /* signal high / status */
60 #define CX24116_REG_QUALITY8 (0xa3)
61 #define CX24116_REG_QSTATUS (0xbc)
62 #define CX24116_REG_QUALITY0 (0xd5)
63 #define CX24116_REG_BER0 (0xc9)
64 #define CX24116_REG_BER8 (0xc8)
65 #define CX24116_REG_BER16 (0xc7)
66 #define CX24116_REG_BER24 (0xc6)
67 #define CX24116_REG_UCB0 (0xcb)
68 #define CX24116_REG_UCB8 (0xca)
69 #define CX24116_REG_CLKDIV (0xf3)
70 #define CX24116_REG_RATEDIV (0xf9)
71 #define CX24116_REG_FECSTATUS (0x9c) /* configured fec (not tuned) or actual FEC (tuned) 1=1/2 2=2/3 etc */
74 #define CX24116_FEC_FECMASK (0x1f) /* mask to determine configured fec (not tuned) or actual fec (tuned) */
75 #define CX24116_FEC_DVBS (0x20) /* Select DVB-S demodulator, else DVB-S2 */
76 #define CX24116_FEC_UNKNOWN (0x40) /* Unknown/unused */
77 #define CX24116_FEC_PILOT (0x80) /* Pilot mode requested when tuning else always reset when tuned */
80 #define CX24116_ARGLEN (0x1e)
83 #define CX24116_ROLLOFF_020 (0x00)
84 #define CX24116_ROLLOFF_025 (0x01)
85 #define CX24116_ROLLOFF_035 (0x02)
88 #define CX24116_PILOT_OFF (0x00)
89 #define CX24116_PILOT_ON (0x40)
92 #define CX24116_HAS_SIGNAL (0x01)
93 #define CX24116_HAS_CARRIER (0x02)
94 #define CX24116_HAS_VITERBI (0x04)
95 #define CX24116_HAS_SYNCLOCK (0x08)
96 #define CX24116_HAS_UNKNOWN1 (0x10)
97 #define CX24116_HAS_UNKNOWN2 (0x20)
98 #define CX24116_STATUS_MASK (0x3f)
99 #define CX24116_SIGNAL_MASK (0xc0)
101 #define CX24116_DISEQC_TONEOFF (0) /* toneburst never sent */
102 #define CX24116_DISEQC_TONECACHE (1) /* toneburst cached */
103 #define CX24116_DISEQC_MESGCACHE (2) /* message cached */
105 /* arg offset for DiSEqC */
106 #define CX24116_DISEQC_BURST (1)
107 #define CX24116_DISEQC_ARG2_2 (2) /* unknown value=2 */
108 #define CX24116_DISEQC_ARG3_0 (3) /* unknown value=0 */
109 #define CX24116_DISEQC_ARG4_0 (4) /* unknown value=0 */
110 #define CX24116_DISEQC_MSGLEN (5)
111 #define CX24116_DISEQC_MSGOFS (6)
114 #define CX24116_DISEQC_MINI_A (0)
115 #define CX24116_DISEQC_MINI_B (1)
117 /* DiSEqC tone burst */
118 static int toneburst = 1;
120 /* SNR measurements */
121 static int esno_snr = 0;
126 CMD_TUNEREQUEST = 0x11,
127 CMD_MPEGCONFIG = 0x13,
128 CMD_TUNERINIT = 0x14,
129 CMD_BANDWIDTH = 0x15,
131 CMD_LNBCONFIG = 0x20,
132 CMD_LNBSEND = 0x21, /* Formerly CMD_SEND_DISEQC */
133 CMD_SET_TONEPRE = 0x22,
135 CMD_UPDFWVERS = 0x35,
136 CMD_TUNERSLEEP = 0x36,
137 CMD_AGCCONTROL = 0x3b, /* Unknown */
140 /* The Demod/Tuner can't easily provide these, we cache them */
141 struct cx24116_tuning
145 fe_spectral_inversion_t inversion;
148 fe_modulation_t modulation;
150 fe_rolloff_t rolloff;
160 /* Basic commands that are sent to the firmware */
164 u8 args[CX24116_ARGLEN];
169 struct i2c_adapter* i2c;
170 const struct cx24116_config* config;
172 struct dvb_frontend frontend;
174 struct cx24116_tuning dcur;
175 struct cx24116_tuning dnxt;
179 struct cx24116_cmd dsec_cmd;
182 static int cx24116_writereg(struct cx24116_state* state, int reg, int data)
184 u8 buf[] = { reg, data };
185 struct i2c_msg msg = { .addr = state->config->demod_address,
186 .flags = 0, .buf = buf, .len = 2 };
190 printk("cx24116: %s: write reg 0x%02x, value 0x%02x\n",
193 if ((err = i2c_transfer(state->i2c, &msg, 1)) != 1) {
194 printk("%s: writereg error(err == %i, reg == 0x%02x,"
195 " value == 0x%02x)\n", __func__, err, reg, data);
202 /* Bulk byte writes to a single I2C address, for 32k firmware load */
203 static int cx24116_writeregN(struct cx24116_state* state, int reg, u8 *data, u16 len)
205 int ret = -EREMOTEIO;
209 buf = kmalloc(len + 1, GFP_KERNEL);
211 printk("Unable to kmalloc\n");
217 memcpy(buf + 1, data, len);
219 msg.addr = state->config->demod_address;
225 printk("cx24116: %s: write regN 0x%02x, len = %d\n",
228 if ((ret = i2c_transfer(state->i2c, &msg, 1)) != 1) {
229 printk("%s: writereg error(err == %i, reg == 0x%02x\n",
240 static int cx24116_readreg(struct cx24116_state* state, u8 reg)
245 struct i2c_msg msg[] = {
246 { .addr = state->config->demod_address, .flags = 0, .buf = b0, .len = 1 },
247 { .addr = state->config->demod_address, .flags = I2C_M_RD, .buf = b1, .len = 1 }
250 ret = i2c_transfer(state->i2c, msg, 2);
253 printk("%s: reg=0x%x (error=%d)\n", __func__, reg, ret);
258 printk("cx24116: read reg 0x%02x, value 0x%02x\n",reg, b1[0]);
263 static int cx24116_set_inversion(struct cx24116_state* state, fe_spectral_inversion_t inversion)
265 dprintk("%s(%d)\n", __func__, inversion);
269 state->dnxt.inversion_val = 0x00;
272 state->dnxt.inversion_val = 0x04;
275 state->dnxt.inversion_val = 0x0C;
281 state->dnxt.inversion = inversion;
287 * modfec (modulation and FEC)
288 * ===========================
290 * MOD FEC mask/val standard
291 * ---- -------- ----------- --------
292 * QPSK FEC_1_2 0x02 0x02+X DVB-S
293 * QPSK FEC_2_3 0x04 0x02+X DVB-S
294 * QPSK FEC_3_4 0x08 0x02+X DVB-S
295 * QPSK FEC_4_5 0x10 0x02+X DVB-S (?)
296 * QPSK FEC_5_6 0x20 0x02+X DVB-S
297 * QPSK FEC_6_7 0x40 0x02+X DVB-S
298 * QPSK FEC_7_8 0x80 0x02+X DVB-S
299 * QPSK FEC_8_9 0x01 0x02+X DVB-S (?) (NOT SUPPORTED?)
300 * QPSK AUTO 0xff 0x02+X DVB-S
302 * For DVB-S high byte probably represents FEC
303 * and low byte selects the modulator. The high
304 * byte is search range mask. Bit 5 may turn
305 * on DVB-S and remaining bits represent some
306 * kind of calibration (how/what i do not know).
308 * Eg.(2/3) szap "Zone Horror"
310 * mask/val = 0x04, 0x20
311 * status 1f | signal c3c0 | snr a333 | ber 00000098 | unc 00000000 | FE_HAS_LOCK
313 * mask/val = 0x04, 0x30
314 * status 1f | signal c3c0 | snr a333 | ber 00000000 | unc 00000000 | FE_HAS_LOCK
316 * After tuning FECSTATUS contains actual FEC
317 * in use numbered 1 through to 8 for 1/2 .. 2/3 etc
319 * NBC=NOT/NON BACKWARD COMPATIBLE WITH DVB-S (DVB-S2 only)
321 * NBC-QPSK FEC_1_2 0x00, 0x04 DVB-S2
322 * NBC-QPSK FEC_3_5 0x00, 0x05 DVB-S2
323 * NBC-QPSK FEC_2_3 0x00, 0x06 DVB-S2
324 * NBC-QPSK FEC_3_4 0x00, 0x07 DVB-S2
325 * NBC-QPSK FEC_4_5 0x00, 0x08 DVB-S2
326 * NBC-QPSK FEC_5_6 0x00, 0x09 DVB-S2
327 * NBC-QPSK FEC_8_9 0x00, 0x0a DVB-S2
328 * NBC-QPSK FEC_9_10 0x00, 0x0b DVB-S2
330 * NBC-8PSK FEC_3_5 0x00, 0x0c DVB-S2
331 * NBC-8PSK FEC_2_3 0x00, 0x0d DVB-S2
332 * NBC-8PSK FEC_3_4 0x00, 0x0e DVB-S2
333 * NBC-8PSK FEC_5_6 0x00, 0x0f DVB-S2
334 * NBC-8PSK FEC_8_9 0x00, 0x10 DVB-S2
335 * NBC-8PSK FEC_9_10 0x00, 0x11 DVB-S2
337 * For DVB-S2 low bytes selects both modulator
338 * and FEC. High byte is meaningless here. To
339 * set pilot, bit 6 (0x40) is set. When inspecting
340 * FECSTATUS bit 7 (0x80) represents the pilot
341 * selection whilst not tuned. When tuned, actual FEC
342 * in use is found in FECSTATUS as per above. Pilot
346 /* A table of modulation, fec and configuration bytes for the demod.
347 * Not all S2 mmodulation schemes are support and not all rates with
348 * a scheme are support. Especially, no auto detect when in S2 mode.
350 struct cx24116_modfec {
351 fe_delivery_system_t delivery_system;
352 fe_modulation_t modulation;
354 u8 mask; /* In DVBS mode this is used to autodetect */
355 u8 val; /* Passed to the firmware to indicate mode selection */
356 } CX24116_MODFEC_MODES[] = {
357 /* QPSK. For unknown rates we set hardware to auto detect 0xfe 0x30 */
359 /*mod fec mask val */
360 { SYS_DVBS, QPSK, FEC_NONE, 0xfe, 0x30 },
361 { SYS_DVBS, QPSK, FEC_1_2, 0x02, 0x2e }, /* 00000010 00101110 */
362 { SYS_DVBS, QPSK, FEC_2_3, 0x04, 0x2f }, /* 00000100 00101111 */
363 { SYS_DVBS, QPSK, FEC_3_4, 0x08, 0x30 }, /* 00001000 00110000 */
364 { SYS_DVBS, QPSK, FEC_4_5, 0xfe, 0x30 }, /* 000?0000 ? */
365 { SYS_DVBS, QPSK, FEC_5_6, 0x20, 0x31 }, /* 00100000 00110001 */
366 { SYS_DVBS, QPSK, FEC_6_7, 0xfe, 0x30 }, /* 0?000000 ? */
367 { SYS_DVBS, QPSK, FEC_7_8, 0x80, 0x32 }, /* 10000000 00110010 */
368 { SYS_DVBS, QPSK, FEC_8_9, 0xfe, 0x30 }, /* 0000000? ? */
369 { SYS_DVBS, QPSK, FEC_AUTO, 0xfe, 0x30 },
371 { SYS_DVBS2, QPSK, FEC_1_2, 0x00, 0x04 },
372 { SYS_DVBS2, QPSK, FEC_3_5, 0x00, 0x05 },
373 { SYS_DVBS2, QPSK, FEC_2_3, 0x00, 0x06 },
374 { SYS_DVBS2, QPSK, FEC_3_4, 0x00, 0x07 },
375 { SYS_DVBS2, QPSK, FEC_4_5, 0x00, 0x08 },
376 { SYS_DVBS2, QPSK, FEC_5_6, 0x00, 0x09 },
377 { SYS_DVBS2, QPSK, FEC_8_9, 0x00, 0x0a },
378 { SYS_DVBS2, QPSK, FEC_9_10, 0x00, 0x0b },
380 { SYS_DVBS2, PSK_8, FEC_3_5, 0x00, 0x0c },
381 { SYS_DVBS2, PSK_8, FEC_2_3, 0x00, 0x0d },
382 { SYS_DVBS2, PSK_8, FEC_3_4, 0x00, 0x0e },
383 { SYS_DVBS2, PSK_8, FEC_5_6, 0x00, 0x0f },
384 { SYS_DVBS2, PSK_8, FEC_8_9, 0x00, 0x10 },
385 { SYS_DVBS2, PSK_8, FEC_9_10, 0x00, 0x11 },
387 * `val' can be found in the FECSTATUS register when tuning.
388 * FECSTATUS will give the actual FEC in use if tuning was successful.
392 static int cx24116_lookup_fecmod(struct cx24116_state* state,
393 fe_modulation_t m, fe_code_rate_t f)
395 int i, ret = -EOPNOTSUPP;
397 dprintk("%s(0x%02x,0x%02x)\n", __func__, m, f);
399 for(i=0 ; i < sizeof(CX24116_MODFEC_MODES) / sizeof(struct cx24116_modfec) ; i++)
401 if( (m == CX24116_MODFEC_MODES[i].modulation) &&
402 (f == CX24116_MODFEC_MODES[i].fec) )
412 static int cx24116_set_fec(struct cx24116_state* state, fe_modulation_t mod, fe_code_rate_t fec)
416 dprintk("%s(0x%02x,0x%02x)\n", __func__, mod, fec);
418 ret = cx24116_lookup_fecmod(state, mod, fec);
423 state->dnxt.fec = fec;
424 state->dnxt.fec_val = CX24116_MODFEC_MODES[ret].val;
425 state->dnxt.fec_mask = CX24116_MODFEC_MODES[ret].mask;
426 dprintk("%s() mask/val = 0x%02x/0x%02x\n", __func__,
427 state->dnxt.fec_mask, state->dnxt.fec_val);
432 static int cx24116_set_symbolrate(struct cx24116_state* state, u32 rate)
434 dprintk("%s(%d)\n", __func__, rate);
436 /* check if symbol rate is within limits */
437 if ((rate > state->frontend.ops.info.symbol_rate_max) ||
438 (rate < state->frontend.ops.info.symbol_rate_min)) {
439 dprintk("%s() unsupported symbol_rate = %d\n", __func__, rate);
443 state->dnxt.symbol_rate = rate;
444 dprintk("%s() symbol_rate = %d\n", __func__, rate);
449 static int cx24116_load_firmware (struct dvb_frontend* fe, const struct firmware *fw);
451 static int cx24116_firmware_ondemand(struct dvb_frontend* fe)
453 struct cx24116_state *state = fe->demodulator_priv;
454 const struct firmware *fw;
457 dprintk("%s()\n",__func__);
459 if (cx24116_readreg(state, 0x20) > 0)
462 if (state->skip_fw_load)
466 /* request the firmware, this will block until someone uploads it */
467 printk("%s: Waiting for firmware upload (%s)...\n", __func__, CX24116_DEFAULT_FIRMWARE);
468 ret = request_firmware(&fw, CX24116_DEFAULT_FIRMWARE, &state->i2c->dev);
469 printk("%s: Waiting for firmware upload(2)...\n", __func__);
471 printk("%s: No firmware uploaded (timeout or file not found?)\n", __func__);
475 /* Make sure we don't recurse back through here during loading */
476 state->skip_fw_load = 1;
478 ret = cx24116_load_firmware(fe, fw);
480 printk("%s: Writing firmware to device failed\n", __func__);
482 release_firmware(fw);
484 printk("%s: Firmware upload %s\n", __func__, ret == 0 ? "complete" : "failed");
486 /* Ensure firmware is always loaded if required */
487 state->skip_fw_load = 0;
493 /* Take a basic firmware command structure, format it and forward it for processing */
494 static int cx24116_cmd_execute(struct dvb_frontend* fe, struct cx24116_cmd *cmd)
496 struct cx24116_state *state = fe->demodulator_priv;
499 dprintk("%s()\n", __func__);
501 /* Load the firmware if required */
502 if ( (ret = cx24116_firmware_ondemand(fe)) != 0)
504 printk("%s(): Unable initialise the firmware\n", __func__);
508 /* Write the command */
509 for(i = 0; i < cmd->len ; i++)
511 dprintk("%s: 0x%02x == 0x%02x\n", __func__, i, cmd->args[i]);
512 cx24116_writereg(state, i, cmd->args[i]);
515 /* Start execution and wait for cmd to terminate */
516 cx24116_writereg(state, CX24116_REG_EXECUTE, 0x01);
517 while( cx24116_readreg(state, CX24116_REG_EXECUTE) )
522 /* Avoid looping forever if the firmware does no respond */
523 printk("%s() Firmware not responding\n", __func__);
530 static int cx24116_load_firmware (struct dvb_frontend* fe, const struct firmware *fw)
532 struct cx24116_state* state = fe->demodulator_priv;
533 struct cx24116_cmd cmd;
535 unsigned char vers[4];
537 dprintk("%s\n", __func__);
538 dprintk("Firmware is %zu bytes (%02x %02x .. %02x %02x)\n"
542 ,fw->data[ fw->size-2 ]
543 ,fw->data[ fw->size-1 ]
546 /* Toggle 88x SRST pin to reset demod */
547 if (state->config->reset_device)
548 state->config->reset_device(fe);
550 /* Begin the firmware load process */
551 /* Prepare the demod, load the firmware, cleanup after load */
554 cx24116_writereg(state, 0xE5, 0x00);
555 cx24116_writereg(state, 0xF1, 0x08);
556 cx24116_writereg(state, 0xF2, 0x13);
559 cx24116_writereg(state, 0xe0, 0x03);
560 cx24116_writereg(state, 0xe0, 0x00);
563 cx24116_writereg(state, CX24116_REG_CLKDIV, 0x46);
564 cx24116_writereg(state, CX24116_REG_RATEDIV, 0x00);
567 cx24116_writereg(state, 0xF0, 0x03);
568 cx24116_writereg(state, 0xF4, 0x81);
569 cx24116_writereg(state, 0xF5, 0x00);
570 cx24116_writereg(state, 0xF6, 0x00);
572 /* write the entire firmware as one transaction */
573 cx24116_writeregN(state, 0xF7, fw->data, fw->size);
575 cx24116_writereg(state, 0xF4, 0x10);
576 cx24116_writereg(state, 0xF0, 0x00);
577 cx24116_writereg(state, 0xF8, 0x06);
579 /* Firmware CMD 10: VCO config */
580 cmd.args[0x00] = CMD_SET_VCO;
581 cmd.args[0x01] = 0x05;
582 cmd.args[0x02] = 0xdc;
583 cmd.args[0x03] = 0xda;
584 cmd.args[0x04] = 0xae;
585 cmd.args[0x05] = 0xaa;
586 cmd.args[0x06] = 0x04;
587 cmd.args[0x07] = 0x9d;
588 cmd.args[0x08] = 0xfc;
589 cmd.args[0x09] = 0x06;
591 ret = cx24116_cmd_execute(fe, &cmd);
595 cx24116_writereg(state, CX24116_REG_SSTATUS, 0x00);
597 /* Firmware CMD 14: Tuner config */
598 cmd.args[0x00] = CMD_TUNERINIT;
599 cmd.args[0x01] = 0x00;
600 cmd.args[0x02] = 0x00;
602 ret = cx24116_cmd_execute(fe, &cmd);
606 cx24116_writereg(state, 0xe5, 0x00);
608 /* Firmware CMD 13: MPEG config */
609 cmd.args[0x00] = CMD_MPEGCONFIG;
610 cmd.args[0x01] = 0x01;
611 cmd.args[0x02] = 0x75;
612 cmd.args[0x03] = 0x00;
613 if (state->config->mpg_clk_pos_pol)
614 cmd.args[0x04] = state->config->mpg_clk_pos_pol;
616 cmd.args[0x04] = 0x02;
617 cmd.args[0x05] = 0x00;
619 ret = cx24116_cmd_execute(fe, &cmd);
623 /* Firmware CMD 35: Get firmware version */
624 cmd.args[0x00] = CMD_UPDFWVERS;
628 ret = cx24116_cmd_execute(fe, &cmd);
631 vers[i]= cx24116_readreg(state, CX24116_REG_MAILBOX);
633 printk("%s: FW version %i.%i.%i.%i\n", __func__,
634 vers[0], vers[1], vers[2], vers[3]);
639 static int cx24116_set_voltage(struct dvb_frontend* fe, fe_sec_voltage_t voltage)
641 /* The isl6421 module will override this function in the fops. */
642 dprintk("%s() This should never appear if the isl6421 module is loaded correctly\n",__func__);
647 static int cx24116_read_status(struct dvb_frontend* fe, fe_status_t* status)
649 struct cx24116_state *state = fe->demodulator_priv;
651 int lock = cx24116_readreg(state, CX24116_REG_SSTATUS);
653 dprintk("%s: status = 0x%02x\n", __func__, lock);
657 if (lock & CX24116_HAS_SIGNAL)
658 *status |= FE_HAS_SIGNAL;
659 if (lock & CX24116_HAS_CARRIER)
660 *status |= FE_HAS_CARRIER;
661 if (lock & CX24116_HAS_VITERBI)
662 *status |= FE_HAS_VITERBI;
663 if (lock & CX24116_HAS_SYNCLOCK)
664 *status |= FE_HAS_SYNC | FE_HAS_LOCK;
669 static int cx24116_read_ber(struct dvb_frontend* fe, u32* ber)
671 struct cx24116_state *state = fe->demodulator_priv;
673 dprintk("%s()\n", __func__);
675 *ber = ( cx24116_readreg(state, CX24116_REG_BER24) << 24 ) |
676 ( cx24116_readreg(state, CX24116_REG_BER16) << 16 ) |
677 ( cx24116_readreg(state, CX24116_REG_BER8 ) << 8 ) |
678 cx24116_readreg(state, CX24116_REG_BER0 );
683 /* TODO Determine function and scale appropriately */
684 static int cx24116_read_signal_strength(struct dvb_frontend* fe, u16* signal_strength)
686 struct cx24116_state *state = fe->demodulator_priv;
687 struct cx24116_cmd cmd;
691 dprintk("%s()\n", __func__);
693 /* Firmware CMD 19: Get AGC */
694 cmd.args[0x00] = CMD_GETAGC;
696 ret = cx24116_cmd_execute(fe, &cmd);
700 sig_reading = ( cx24116_readreg(state, CX24116_REG_SSTATUS) & CX24116_SIGNAL_MASK ) |
701 ( cx24116_readreg(state, CX24116_REG_SIGNAL) << 6 );
702 *signal_strength= 0 - sig_reading;
704 dprintk("%s: raw / cooked = 0x%04x / 0x%04x\n", __func__, sig_reading, *signal_strength);
709 /* SNR (0..100)% = (sig & 0xf0) * 10 + (sig & 0x0f) * 10 / 16 */
710 static int cx24116_read_snr_pct(struct dvb_frontend* fe, u16* snr)
712 struct cx24116_state *state = fe->demodulator_priv;
714 static const u32 snr_tab[] = { /* 10 x Table (rounded up) */
715 0x00000,0x0199A,0x03333,0x04ccD,0x06667,
716 0x08000,0x0999A,0x0b333,0x0cccD,0x0e667,
717 0x10000,0x1199A,0x13333,0x14ccD,0x16667,0x18000 };
719 dprintk("%s()\n", __func__);
721 snr_reading = cx24116_readreg(state, CX24116_REG_QUALITY0);
723 if(snr_reading >= 0xa0 /* 100% */)
726 *snr = snr_tab [ ( snr_reading & 0xf0 ) >> 4 ] +
727 ( snr_tab [ ( snr_reading & 0x0f ) ] >> 4 );
729 dprintk("%s: raw / cooked = 0x%02x / 0x%04x\n", __func__,
735 /* The reelbox patches show the value in the registers represents
736 * ESNO, from 0->30db (values 0->300). We provide this value by
739 static int cx24116_read_snr_esno(struct dvb_frontend* fe, u16* snr)
741 struct cx24116_state *state = fe->demodulator_priv;
743 dprintk("%s()\n", __func__);
745 *snr = cx24116_readreg(state, CX24116_REG_QUALITY8) << 8 |
746 cx24116_readreg(state, CX24116_REG_QUALITY0);
748 dprintk("%s: raw 0x%04x\n", __func__, *snr);
753 static int cx24116_read_snr(struct dvb_frontend* fe, u16* snr)
756 return cx24116_read_snr_esno(fe, snr);
758 return cx24116_read_snr_pct(fe, snr);
761 static int cx24116_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
763 struct cx24116_state *state = fe->demodulator_priv;
765 dprintk("%s()\n", __func__);
767 *ucblocks = ( cx24116_readreg(state, CX24116_REG_UCB8) << 8 ) |
768 cx24116_readreg(state, CX24116_REG_UCB0);
773 /* Overwrite the current tuning params, we are about to tune */
774 static void cx24116_clone_params(struct dvb_frontend* fe)
776 struct cx24116_state *state = fe->demodulator_priv;
777 memcpy(&state->dcur, &state->dnxt, sizeof(state->dcur));
781 static int cx24116_wait_for_lnb(struct dvb_frontend* fe)
783 struct cx24116_state *state = fe->demodulator_priv;
786 dprintk("%s() qstatus = 0x%02x\n", __func__,
787 cx24116_readreg(state, CX24116_REG_QSTATUS));
789 /* Wait for up to 300 ms */
790 for(i = 0; i < 30 ; i++) {
791 if (cx24116_readreg(state, CX24116_REG_QSTATUS) & 0x20)
796 dprintk("%s(): LNB not ready\n", __func__);
798 return -ETIMEDOUT; /* -EBUSY ? */
801 static int cx24116_set_tone(struct dvb_frontend* fe, fe_sec_tone_mode_t tone)
803 struct cx24116_cmd cmd;
806 dprintk("%s(%d)\n", __func__, tone);
807 if ( (tone != SEC_TONE_ON) && (tone != SEC_TONE_OFF) ) {
808 printk("%s: Invalid, tone=%d\n", __func__, tone);
812 /* Wait for LNB ready */
813 ret = cx24116_wait_for_lnb(fe);
817 /* Min delay time after DiSEqC send */
818 msleep(15); /* XXX determine is FW does this, see send_diseqc/burst */
820 /* This is always done before the tone is set */
821 cmd.args[0x00] = CMD_SET_TONEPRE;
822 cmd.args[0x01] = 0x00;
824 ret = cx24116_cmd_execute(fe, &cmd);
828 /* Now we set the tone */
829 cmd.args[0x00] = CMD_SET_TONE;
830 cmd.args[0x01] = 0x00;
831 cmd.args[0x02] = 0x00;
835 dprintk("%s: setting tone on\n", __func__);
836 cmd.args[0x03] = 0x01;
839 dprintk("%s: setting tone off\n",__func__);
840 cmd.args[0x03] = 0x00;
845 /* Min delay time before DiSEqC send */
846 msleep(15); /* XXX determine is FW does this, see send_diseqc/burst */
848 return cx24116_cmd_execute(fe, &cmd);
851 /* Initialise DiSEqC */
852 static int cx24116_diseqc_init(struct dvb_frontend* fe)
854 struct cx24116_state *state = fe->demodulator_priv;
855 struct cx24116_cmd cmd;
858 /* Firmware CMD 20: LNB/DiSEqC config */
859 cmd.args[0x00] = CMD_LNBCONFIG;
860 cmd.args[0x01] = 0x00;
861 cmd.args[0x02] = 0x10;
862 cmd.args[0x03] = 0x00;
863 cmd.args[0x04] = 0x8f;
864 cmd.args[0x05] = 0x28;
865 cmd.args[0x06] = (toneburst == CX24116_DISEQC_TONEOFF) ? 0x00 : 0x01;
866 cmd.args[0x07] = 0x01;
868 ret = cx24116_cmd_execute(fe, &cmd);
872 /* Prepare a DiSEqC command */
873 state->dsec_cmd.args[0x00] = CMD_LNBSEND;
876 state->dsec_cmd.args[CX24116_DISEQC_BURST] = CX24116_DISEQC_MINI_A;
879 state->dsec_cmd.args[CX24116_DISEQC_ARG2_2] = 0x02;
880 state->dsec_cmd.args[CX24116_DISEQC_ARG3_0] = 0x00;
881 state->dsec_cmd.args[CX24116_DISEQC_ARG4_0] = 0x00; /* Continuation flag? */
883 /* DiSEqC message length */
884 state->dsec_cmd.args[CX24116_DISEQC_MSGLEN] = 0x00;
887 state->dsec_cmd.len= CX24116_DISEQC_MSGOFS;
892 /* Send DiSEqC message with derived burst (hack) || previous burst */
893 static int cx24116_send_diseqc_msg(struct dvb_frontend* fe, struct dvb_diseqc_master_cmd *d)
895 struct cx24116_state *state = fe->demodulator_priv;
898 /* Dump DiSEqC message */
900 printk("cx24116: %s(", __func__);
901 for(i = 0 ; i < d->msg_len ;) {
902 printk("0x%02x", d->msg[i]);
906 printk(") toneburst=%d\n", toneburst);
909 /* Validate length */
910 if(d->msg_len > (CX24116_ARGLEN - CX24116_DISEQC_MSGOFS))
914 for (i = 0; i < d->msg_len; i++)
915 state->dsec_cmd.args[CX24116_DISEQC_MSGOFS + i] = d->msg[i];
917 /* DiSEqC message length */
918 state->dsec_cmd.args[CX24116_DISEQC_MSGLEN] = d->msg_len;
921 state->dsec_cmd.len= CX24116_DISEQC_MSGOFS + state->dsec_cmd.args[CX24116_DISEQC_MSGLEN];
923 /* DiSEqC toneburst */
924 if(toneburst == CX24116_DISEQC_MESGCACHE)
925 /* Message is cached */
928 else if(toneburst == CX24116_DISEQC_TONEOFF)
929 /* Message is sent without burst */
930 state->dsec_cmd.args[CX24116_DISEQC_BURST] = 0;
932 else if(toneburst == CX24116_DISEQC_TONECACHE) {
934 * Message is sent with derived else cached burst
936 * WRITE PORT GROUP COMMAND 38
938 * 0/A/A: E0 10 38 F0..F3
939 * 1/B/B: E0 10 38 F4..F7
940 * 2/C/A: E0 10 38 F8..FB
941 * 3/D/B: E0 10 38 FC..FF
943 * databyte[3]= 8421:8421
947 * WX= PORT SELECT 0..3 (X=TONEBURST)
948 * Y = VOLTAGE (0=13V, 1=18V)
949 * Z = BAND (0=LOW, 1=HIGH(22K))
951 if(d->msg_len >= 4 && d->msg[2] == 0x38)
952 state->dsec_cmd.args[CX24116_DISEQC_BURST] = ((d->msg[3] & 4) >> 2);
954 dprintk("%s burst=%d\n", __func__, state->dsec_cmd.args[CX24116_DISEQC_BURST]);
957 /* Wait for LNB ready */
958 ret = cx24116_wait_for_lnb(fe);
962 /* Wait for voltage/min repeat delay */
966 ret = cx24116_cmd_execute(fe, &state->dsec_cmd);
973 * >15ms delay + (XXX determine if FW does this, see set_tone)
977 * >15ms delay (XXX determine if FW does this, see set_tone)
979 msleep( (state->dsec_cmd.args[CX24116_DISEQC_MSGLEN] << 4) + ((toneburst == CX24116_DISEQC_TONEOFF) ? 30 : 60) );
984 /* Send DiSEqC burst */
985 static int cx24116_diseqc_send_burst(struct dvb_frontend* fe, fe_sec_mini_cmd_t burst)
987 struct cx24116_state *state = fe->demodulator_priv;
990 dprintk("%s(%d) toneburst=%d\n",__func__, burst, toneburst);
993 if (burst == SEC_MINI_A)
994 state->dsec_cmd.args[CX24116_DISEQC_BURST] = CX24116_DISEQC_MINI_A;
995 else if(burst == SEC_MINI_B)
996 state->dsec_cmd.args[CX24116_DISEQC_BURST] = CX24116_DISEQC_MINI_B;
1000 /* DiSEqC toneburst */
1001 if(toneburst != CX24116_DISEQC_MESGCACHE)
1002 /* Burst is cached */
1005 /* Burst is to be sent with cached message */
1007 /* Wait for LNB ready */
1008 ret = cx24116_wait_for_lnb(fe);
1012 /* Wait for voltage/min repeat delay */
1016 ret = cx24116_cmd_execute(fe, &state->dsec_cmd);
1024 * >15ms delay + (XXX determine if FW does this, see set_tone)
1028 * >15ms delay (XXX determine if FW does this, see set_tone)
1030 msleep( (state->dsec_cmd.args[CX24116_DISEQC_MSGLEN] << 4) + 60 );
1035 static void cx24116_release(struct dvb_frontend* fe)
1037 struct cx24116_state* state = fe->demodulator_priv;
1038 dprintk("%s\n",__func__);
1042 static struct dvb_frontend_ops cx24116_ops;
1044 struct dvb_frontend* cx24116_attach(const struct cx24116_config* config,
1045 struct i2c_adapter* i2c)
1047 struct cx24116_state* state = NULL;
1050 dprintk("%s\n",__func__);
1052 /* allocate memory for the internal state */
1053 state = kmalloc(sizeof(struct cx24116_state), GFP_KERNEL);
1054 if (state == NULL) {
1055 printk("Unable to kmalloc\n");
1059 /* setup the state */
1060 memset(state, 0, sizeof(struct cx24116_state));
1062 state->config = config;
1065 /* check if the demod is present */
1066 ret = (cx24116_readreg(state, 0xFF) << 8) | cx24116_readreg(state, 0xFE);
1067 if (ret != 0x0501) {
1068 printk("Invalid probe, probably not a CX24116 device\n");
1072 /* create dvb_frontend */
1073 memcpy(&state->frontend.ops, &cx24116_ops, sizeof(struct dvb_frontend_ops));
1074 state->frontend.demodulator_priv = state;
1075 return &state->frontend;
1077 error2: kfree(state);
1078 error1: return NULL;
1081 * Initialise or wake up device
1083 * Power config will reset and load initial firmware if required
1085 static int cx24116_initfe(struct dvb_frontend* fe)
1087 struct cx24116_state* state = fe->demodulator_priv;
1088 struct cx24116_cmd cmd;
1091 dprintk("%s()\n",__func__);
1094 cx24116_writereg(state, 0xe0, 0);
1095 cx24116_writereg(state, 0xe1, 0);
1096 cx24116_writereg(state, 0xea, 0);
1098 /* Firmware CMD 36: Power config */
1099 cmd.args[0x00] = CMD_TUNERSLEEP;
1102 ret = cx24116_cmd_execute(fe, &cmd);
1106 return cx24116_diseqc_init(fe);
1110 * Put device to sleep
1112 static int cx24116_sleep(struct dvb_frontend* fe)
1114 struct cx24116_state* state = fe->demodulator_priv;
1115 struct cx24116_cmd cmd;
1118 dprintk("%s()\n",__func__);
1120 /* Firmware CMD 36: Power config */
1121 cmd.args[0x00] = CMD_TUNERSLEEP;
1124 ret = cx24116_cmd_execute(fe, &cmd);
1128 /* Power off (Shutdown clocks) */
1129 cx24116_writereg(state, 0xea, 0xff);
1130 cx24116_writereg(state, 0xe1, 1);
1131 cx24116_writereg(state, 0xe0, 1);
1136 static int cx24116_set_property(struct dvb_frontend *fe, struct dtv_property* tvp)
1138 dprintk("%s(..)\n", __func__);
1142 static int cx24116_get_property(struct dvb_frontend *fe, struct dtv_property* tvp)
1144 dprintk("%s(..)\n", __func__);
1148 /* dvb-core told us to tune, the tv property cache will be complete,
1149 * it's safe for is to pull values and use them for tuning purposes.
1151 static int cx24116_set_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters *p)
1153 struct cx24116_state *state = fe->demodulator_priv;
1154 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1155 struct cx24116_cmd cmd;
1156 fe_status_t tunerstat;
1157 int i, status, ret, retune;
1159 dprintk("%s()\n",__func__);
1161 switch(c->delivery_system) {
1163 dprintk("%s: DVB-S delivery system selected\n",__func__);
1165 /* Only QPSK is supported for DVB-S */
1166 if(c->modulation != QPSK) {
1167 dprintk("%s: unsupported modulation selected (%d)\n",
1168 __func__, c->modulation);
1172 /* Pilot doesn't exist in DVB-S, turn bit off */
1173 state->dnxt.pilot_val = CX24116_PILOT_OFF;
1176 /* DVB-S only supports 0.35 */
1177 if(c->rolloff != ROLLOFF_35) {
1178 dprintk("%s: unsupported rolloff selected (%d)\n",
1179 __func__, c->rolloff);
1182 state->dnxt.rolloff_val = CX24116_ROLLOFF_035;
1186 dprintk("%s: DVB-S2 delivery system selected\n",__func__);
1189 * NBC 8PSK/QPSK with DVB-S is supported for DVB-S2,
1190 * but not hardware auto detection
1192 if(c->modulation != PSK_8 && c->modulation != QPSK) {
1193 dprintk("%s: unsupported modulation selected (%d)\n",
1194 __func__, c->modulation);
1199 case PILOT_AUTO: /* Not supported but emulated */
1200 retune = 2; /* Fall-through */
1202 state->dnxt.pilot_val = CX24116_PILOT_OFF;
1205 state->dnxt.pilot_val = CX24116_PILOT_ON;
1208 dprintk("%s: unsupported pilot mode selected (%d)\n",
1209 __func__, c->pilot);
1213 switch(c->rolloff) {
1215 state->dnxt.rolloff_val= CX24116_ROLLOFF_020;
1218 state->dnxt.rolloff_val= CX24116_ROLLOFF_025;
1221 state->dnxt.rolloff_val= CX24116_ROLLOFF_035;
1223 case ROLLOFF_AUTO: /* Rolloff must be explicit */
1225 dprintk("%s: unsupported rolloff selected (%d)\n",
1226 __func__, c->rolloff);
1232 dprintk("%s: unsupported delivery system selected (%d)\n",
1233 __func__, c->delivery_system);
1236 state->dnxt.modulation = c->modulation;
1237 state->dnxt.frequency = c->frequency;
1238 state->dnxt.pilot = c->pilot;
1239 state->dnxt.rolloff = c->rolloff;
1241 if ((ret = cx24116_set_inversion(state, c->inversion)) != 0)
1244 /* FEC_NONE/AUTO for DVB-S2 is not supported and detected here */
1245 if ((ret = cx24116_set_fec(state, c->modulation, c->fec_inner)) != 0)
1248 if ((ret = cx24116_set_symbolrate(state, c->symbol_rate)) != 0)
1251 /* discard the 'current' tuning parameters and prepare to tune */
1252 cx24116_clone_params(fe);
1254 dprintk("%s: modulation = %d\n", __func__, state->dcur.modulation);
1255 dprintk("%s: frequency = %d\n", __func__, state->dcur.frequency);
1256 dprintk("%s: pilot = %d (val = 0x%02x)\n", __func__,
1257 state->dcur.pilot, state->dcur.pilot_val);
1258 dprintk("%s: retune = %d\n", __func__, retune);
1259 dprintk("%s: rolloff = %d (val = 0x%02x)\n", __func__,
1260 state->dcur.rolloff, state->dcur.rolloff_val);
1261 dprintk("%s: symbol_rate = %d\n", __func__, state->dcur.symbol_rate);
1262 dprintk("%s: FEC = %d (mask/val = 0x%02x/0x%02x)\n", __func__,
1263 state->dcur.fec, state->dcur.fec_mask, state->dcur.fec_val);
1264 dprintk("%s: Inversion = %d (val = 0x%02x)\n", __func__,
1265 state->dcur.inversion, state->dcur.inversion_val);
1267 /* This is also done in advise/acquire on HVR4000 but not on LITE */
1268 if (state->config->set_ts_params)
1269 state->config->set_ts_params(fe, 0);
1272 cmd.args[0x00] = CMD_BANDWIDTH;
1273 cmd.args[0x01] = 0x01;
1275 ret = cx24116_cmd_execute(fe, &cmd);
1279 /* Prepare a tune request */
1280 cmd.args[0x00] = CMD_TUNEREQUEST;
1283 cmd.args[0x01] = (state->dcur.frequency & 0xff0000) >> 16;
1284 cmd.args[0x02] = (state->dcur.frequency & 0x00ff00) >> 8;
1285 cmd.args[0x03] = (state->dcur.frequency & 0x0000ff);
1288 cmd.args[0x04] = ((state->dcur.symbol_rate / 1000) & 0xff00) >> 8;
1289 cmd.args[0x05] = ((state->dcur.symbol_rate / 1000) & 0x00ff);
1291 /* Automatic Inversion */
1292 cmd.args[0x06] = state->dcur.inversion_val;
1294 /* Modulation / FEC / Pilot */
1295 cmd.args[0x07] = state->dcur.fec_val | state->dcur.pilot_val;
1297 cmd.args[0x08] = CX24116_SEARCH_RANGE_KHZ >> 8;
1298 cmd.args[0x09] = CX24116_SEARCH_RANGE_KHZ & 0xff;
1299 cmd.args[0x0a] = 0x00;
1300 cmd.args[0x0b] = 0x00;
1301 cmd.args[0x0c] = state->dcur.rolloff_val;
1302 cmd.args[0x0d] = state->dcur.fec_mask;
1304 if (state->dcur.symbol_rate > 30000000) {
1305 cmd.args[0x0e] = 0x04;
1306 cmd.args[0x0f] = 0x00;
1307 cmd.args[0x10] = 0x01;
1308 cmd.args[0x11] = 0x77;
1309 cmd.args[0x12] = 0x36;
1310 cx24116_writereg(state, CX24116_REG_CLKDIV, 0x44);
1311 cx24116_writereg(state, CX24116_REG_RATEDIV, 0x01);
1313 cmd.args[0x0e] = 0x06;
1314 cmd.args[0x0f] = 0x00;
1315 cmd.args[0x10] = 0x00;
1316 cmd.args[0x11] = 0xFA;
1317 cmd.args[0x12] = 0x24;
1318 cx24116_writereg(state, CX24116_REG_CLKDIV, 0x46);
1319 cx24116_writereg(state, CX24116_REG_RATEDIV, 0x00);
1324 /* We need to support pilot and non-pilot tuning in the
1325 * driver automatically. This is a workaround for because
1326 * the demod does not support autodetect.
1329 /* Reset status register */
1330 status = cx24116_readreg(state, CX24116_REG_SSTATUS) & CX24116_SIGNAL_MASK;
1331 cx24116_writereg(state, CX24116_REG_SSTATUS, status);
1334 ret = cx24116_cmd_execute(fe, &cmd);
1339 * Wait for up to 500 ms before retrying
1341 * If we are able to tune then generally it occurs within 100ms.
1342 * If it takes longer, try a different toneburst setting.
1344 for(i = 0; i < 50 ; i++) {
1345 cx24116_read_status(fe, &tunerstat);
1346 status = tunerstat & (FE_HAS_SIGNAL | FE_HAS_SYNC);
1347 if(status == (FE_HAS_SIGNAL | FE_HAS_SYNC)) {
1348 dprintk("%s: Tuned\n",__func__);
1354 dprintk("%s: Not tuned\n",__func__);
1356 /* Toggle pilot bit when in auto-pilot */
1357 if(state->dcur.pilot == PILOT_AUTO)
1358 cmd.args[0x07] ^= CX24116_PILOT_ON;
1362 tuned: /* Set/Reset B/W */
1363 cmd.args[0x00] = CMD_BANDWIDTH;
1364 cmd.args[0x01] = 0x00;
1366 ret = cx24116_cmd_execute(fe, &cmd);
1373 static struct dvb_frontend_ops cx24116_ops = {
1376 .name = "Conexant CX24116/CX24118",
1378 .frequency_min = 950000,
1379 .frequency_max = 2150000,
1380 .frequency_stepsize = 1011, /* kHz for QPSK frontends */
1381 .frequency_tolerance = 5000,
1382 .symbol_rate_min = 1000000,
1383 .symbol_rate_max = 45000000,
1384 .caps = FE_CAN_INVERSION_AUTO |
1385 FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
1386 FE_CAN_FEC_4_5 | FE_CAN_FEC_5_6 | FE_CAN_FEC_6_7 |
1387 FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
1388 FE_CAN_QPSK | FE_CAN_RECOVER
1391 .release = cx24116_release,
1393 .init = cx24116_initfe,
1394 .sleep = cx24116_sleep,
1395 .read_status = cx24116_read_status,
1396 .read_ber = cx24116_read_ber,
1397 .read_signal_strength = cx24116_read_signal_strength,
1398 .read_snr = cx24116_read_snr,
1399 .read_ucblocks = cx24116_read_ucblocks,
1400 .set_tone = cx24116_set_tone,
1401 .set_voltage = cx24116_set_voltage,
1402 .diseqc_send_master_cmd = cx24116_send_diseqc_msg,
1403 .diseqc_send_burst = cx24116_diseqc_send_burst,
1405 .set_property = cx24116_set_property,
1406 .get_property = cx24116_get_property,
1407 .set_frontend = cx24116_set_frontend,
1410 module_param(debug, int, 0644);
1411 MODULE_PARM_DESC(debug, "Activates frontend debugging (default:0)");
1413 module_param(toneburst, int, 0644);
1414 MODULE_PARM_DESC(toneburst, "DiSEqC toneburst 0=OFF, 1=TONE CACHE, 2=MESSAGE CACHE (default:1)");
1416 module_param(esno_snr, int, 0644);
1417 MODULE_PARM_DESC(debug, "SNR return units, 0=PERCENTAGE 0-100, 1=ESNO(db * 10) (default:0)");
1419 MODULE_DESCRIPTION("DVB Frontend module for Conexant cx24116/cx24118 hardware");
1420 MODULE_AUTHOR("Steven Toth");
1421 MODULE_LICENSE("GPL");
1423 EXPORT_SYMBOL(cx24116_attach);