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 module_param(debug, int, 0644);
46 MODULE_PARM_DESC(debug, "Activates frontend debugging (default:0)");
48 #define dprintk(args...) \
51 printk(KERN_INFO "cx24116: " args); \
54 #define CX24116_DEFAULT_FIRMWARE "dvb-fe-cx24116.fw"
55 #define CX24116_SEARCH_RANGE_KHZ 5000
58 #define CX24116_REG_COMMAND (0x00) /* command args 0x00..0x1e */
59 #define CX24116_REG_EXECUTE (0x1f) /* execute command */
60 #define CX24116_REG_MAILBOX (0x96) /* FW or multipurpose mailbox? */
61 #define CX24116_REG_RESET (0x20) /* reset status > 0 */
62 #define CX24116_REG_SIGNAL (0x9e) /* signal low */
63 #define CX24116_REG_SSTATUS (0x9d) /* signal high / status */
64 #define CX24116_REG_QUALITY8 (0xa3)
65 #define CX24116_REG_QSTATUS (0xbc)
66 #define CX24116_REG_QUALITY0 (0xd5)
67 #define CX24116_REG_BER0 (0xc9)
68 #define CX24116_REG_BER8 (0xc8)
69 #define CX24116_REG_BER16 (0xc7)
70 #define CX24116_REG_BER24 (0xc6)
71 #define CX24116_REG_UCB0 (0xcb)
72 #define CX24116_REG_UCB8 (0xca)
73 #define CX24116_REG_CLKDIV (0xf3)
74 #define CX24116_REG_RATEDIV (0xf9)
76 /* configured fec (not tuned) or actual FEC (tuned) 1=1/2 2=2/3 etc */
77 #define CX24116_REG_FECSTATUS (0x9c)
80 /* mask to determine configured fec (not tuned) or actual fec (tuned) */
81 #define CX24116_FEC_FECMASK (0x1f)
83 /* Select DVB-S demodulator, else DVB-S2 */
84 #define CX24116_FEC_DVBS (0x20)
85 #define CX24116_FEC_UNKNOWN (0x40) /* Unknown/unused */
87 /* Pilot mode requested when tuning else always reset when tuned */
88 #define CX24116_FEC_PILOT (0x80)
91 #define CX24116_ARGLEN (0x1e)
94 #define CX24116_ROLLOFF_020 (0x00)
95 #define CX24116_ROLLOFF_025 (0x01)
96 #define CX24116_ROLLOFF_035 (0x02)
99 #define CX24116_PILOT_OFF (0x00)
100 #define CX24116_PILOT_ON (0x40)
103 #define CX24116_HAS_SIGNAL (0x01)
104 #define CX24116_HAS_CARRIER (0x02)
105 #define CX24116_HAS_VITERBI (0x04)
106 #define CX24116_HAS_SYNCLOCK (0x08)
107 #define CX24116_HAS_UNKNOWN1 (0x10)
108 #define CX24116_HAS_UNKNOWN2 (0x20)
109 #define CX24116_STATUS_MASK (0x3f)
110 #define CX24116_SIGNAL_MASK (0xc0)
112 #define CX24116_DISEQC_TONEOFF (0) /* toneburst never sent */
113 #define CX24116_DISEQC_TONECACHE (1) /* toneburst cached */
114 #define CX24116_DISEQC_MESGCACHE (2) /* message cached */
116 /* arg offset for DiSEqC */
117 #define CX24116_DISEQC_BURST (1)
118 #define CX24116_DISEQC_ARG2_2 (2) /* unknown value=2 */
119 #define CX24116_DISEQC_ARG3_0 (3) /* unknown value=0 */
120 #define CX24116_DISEQC_ARG4_0 (4) /* unknown value=0 */
121 #define CX24116_DISEQC_MSGLEN (5)
122 #define CX24116_DISEQC_MSGOFS (6)
125 #define CX24116_DISEQC_MINI_A (0)
126 #define CX24116_DISEQC_MINI_B (1)
128 /* DiSEqC tone burst */
129 static int toneburst = 1;
130 module_param(toneburst, int, 0644);
131 MODULE_PARM_DESC(toneburst, "DiSEqC toneburst 0=OFF, 1=TONE CACHE, "\
132 "2=MESSAGE CACHE (default:1)");
134 /* SNR measurements */
136 module_param(esno_snr, int, 0644);
137 MODULE_PARM_DESC(debug, "SNR return units, 0=PERCENTAGE 0-100, "\
138 "1=ESNO(db * 10) (default:0)");
142 CMD_TUNEREQUEST = 0x11,
143 CMD_MPEGCONFIG = 0x13,
144 CMD_TUNERINIT = 0x14,
145 CMD_BANDWIDTH = 0x15,
147 CMD_LNBCONFIG = 0x20,
148 CMD_LNBSEND = 0x21, /* Formerly CMD_SEND_DISEQC */
149 CMD_SET_TONEPRE = 0x22,
151 CMD_UPDFWVERS = 0x35,
152 CMD_TUNERSLEEP = 0x36,
153 CMD_AGCCONTROL = 0x3b, /* Unknown */
156 /* The Demod/Tuner can't easily provide these, we cache them */
157 struct cx24116_tuning {
160 fe_spectral_inversion_t inversion;
163 fe_modulation_t modulation;
165 fe_rolloff_t rolloff;
175 /* Basic commands that are sent to the firmware */
178 u8 args[CX24116_ARGLEN];
181 struct cx24116_state {
182 struct i2c_adapter *i2c;
183 const struct cx24116_config *config;
185 struct dvb_frontend frontend;
187 struct cx24116_tuning dcur;
188 struct cx24116_tuning dnxt;
192 struct cx24116_cmd dsec_cmd;
195 static int cx24116_writereg(struct cx24116_state *state, int reg, int data)
197 u8 buf[] = { reg, data };
198 struct i2c_msg msg = { .addr = state->config->demod_address,
199 .flags = 0, .buf = buf, .len = 2 };
203 printk("cx24116: %s: write reg 0x%02x, value 0x%02x\n",
204 __func__, reg, data);
206 err = i2c_transfer(state->i2c, &msg, 1);
208 printk(KERN_ERR "%s: writereg error(err == %i, reg == 0x%02x,"
209 " value == 0x%02x)\n", __func__, err, reg, data);
216 /* Bulk byte writes to a single I2C address, for 32k firmware load */
217 static int cx24116_writeregN(struct cx24116_state *state, int reg,
218 const u8 *data, u16 len)
220 int ret = -EREMOTEIO;
224 buf = kmalloc(len + 1, GFP_KERNEL);
226 printk("Unable to kmalloc\n");
232 memcpy(buf + 1, data, len);
234 msg.addr = state->config->demod_address;
240 printk(KERN_INFO "cx24116: %s: write regN 0x%02x, len = %d\n",
243 ret = i2c_transfer(state->i2c, &msg, 1);
245 printk(KERN_ERR "%s: writereg error(err == %i, reg == 0x%02x\n",
256 static int cx24116_readreg(struct cx24116_state *state, u8 reg)
261 struct i2c_msg msg[] = {
262 { .addr = state->config->demod_address, .flags = 0,
263 .buf = b0, .len = 1 },
264 { .addr = state->config->demod_address, .flags = I2C_M_RD,
265 .buf = b1, .len = 1 }
268 ret = i2c_transfer(state->i2c, msg, 2);
271 printk(KERN_ERR "%s: reg=0x%x (error=%d)\n",
277 printk(KERN_INFO "cx24116: read reg 0x%02x, value 0x%02x\n",
283 static int cx24116_set_inversion(struct cx24116_state *state,
284 fe_spectral_inversion_t inversion)
286 dprintk("%s(%d)\n", __func__, inversion);
290 state->dnxt.inversion_val = 0x00;
293 state->dnxt.inversion_val = 0x04;
296 state->dnxt.inversion_val = 0x0C;
302 state->dnxt.inversion = inversion;
308 * modfec (modulation and FEC)
309 * ===========================
311 * MOD FEC mask/val standard
312 * ---- -------- ----------- --------
313 * QPSK FEC_1_2 0x02 0x02+X DVB-S
314 * QPSK FEC_2_3 0x04 0x02+X DVB-S
315 * QPSK FEC_3_4 0x08 0x02+X DVB-S
316 * QPSK FEC_4_5 0x10 0x02+X DVB-S (?)
317 * QPSK FEC_5_6 0x20 0x02+X DVB-S
318 * QPSK FEC_6_7 0x40 0x02+X DVB-S
319 * QPSK FEC_7_8 0x80 0x02+X DVB-S
320 * QPSK FEC_8_9 0x01 0x02+X DVB-S (?) (NOT SUPPORTED?)
321 * QPSK AUTO 0xff 0x02+X DVB-S
323 * For DVB-S high byte probably represents FEC
324 * and low byte selects the modulator. The high
325 * byte is search range mask. Bit 5 may turn
326 * on DVB-S and remaining bits represent some
327 * kind of calibration (how/what i do not know).
329 * Eg.(2/3) szap "Zone Horror"
331 * mask/val = 0x04, 0x20
332 * status 1f | signal c3c0 | snr a333 | ber 00000098 | unc 0 | FE_HAS_LOCK
334 * mask/val = 0x04, 0x30
335 * status 1f | signal c3c0 | snr a333 | ber 00000000 | unc 0 | FE_HAS_LOCK
337 * After tuning FECSTATUS contains actual FEC
338 * in use numbered 1 through to 8 for 1/2 .. 2/3 etc
340 * NBC=NOT/NON BACKWARD COMPATIBLE WITH DVB-S (DVB-S2 only)
342 * NBC-QPSK FEC_1_2 0x00, 0x04 DVB-S2
343 * NBC-QPSK FEC_3_5 0x00, 0x05 DVB-S2
344 * NBC-QPSK FEC_2_3 0x00, 0x06 DVB-S2
345 * NBC-QPSK FEC_3_4 0x00, 0x07 DVB-S2
346 * NBC-QPSK FEC_4_5 0x00, 0x08 DVB-S2
347 * NBC-QPSK FEC_5_6 0x00, 0x09 DVB-S2
348 * NBC-QPSK FEC_8_9 0x00, 0x0a DVB-S2
349 * NBC-QPSK FEC_9_10 0x00, 0x0b DVB-S2
351 * NBC-8PSK FEC_3_5 0x00, 0x0c DVB-S2
352 * NBC-8PSK FEC_2_3 0x00, 0x0d DVB-S2
353 * NBC-8PSK FEC_3_4 0x00, 0x0e DVB-S2
354 * NBC-8PSK FEC_5_6 0x00, 0x0f DVB-S2
355 * NBC-8PSK FEC_8_9 0x00, 0x10 DVB-S2
356 * NBC-8PSK FEC_9_10 0x00, 0x11 DVB-S2
358 * For DVB-S2 low bytes selects both modulator
359 * and FEC. High byte is meaningless here. To
360 * set pilot, bit 6 (0x40) is set. When inspecting
361 * FECSTATUS bit 7 (0x80) represents the pilot
362 * selection whilst not tuned. When tuned, actual FEC
363 * in use is found in FECSTATUS as per above. Pilot
367 /* A table of modulation, fec and configuration bytes for the demod.
368 * Not all S2 mmodulation schemes are support and not all rates with
369 * a scheme are support. Especially, no auto detect when in S2 mode.
371 struct cx24116_modfec {
372 fe_delivery_system_t delivery_system;
373 fe_modulation_t modulation;
375 u8 mask; /* In DVBS mode this is used to autodetect */
376 u8 val; /* Passed to the firmware to indicate mode selection */
377 } CX24116_MODFEC_MODES[] = {
378 /* QPSK. For unknown rates we set hardware to auto detect 0xfe 0x30 */
380 /*mod fec mask val */
381 { SYS_DVBS, QPSK, FEC_NONE, 0xfe, 0x30 },
382 { SYS_DVBS, QPSK, FEC_1_2, 0x02, 0x2e }, /* 00000010 00101110 */
383 { SYS_DVBS, QPSK, FEC_2_3, 0x04, 0x2f }, /* 00000100 00101111 */
384 { SYS_DVBS, QPSK, FEC_3_4, 0x08, 0x30 }, /* 00001000 00110000 */
385 { SYS_DVBS, QPSK, FEC_4_5, 0xfe, 0x30 }, /* 000?0000 ? */
386 { SYS_DVBS, QPSK, FEC_5_6, 0x20, 0x31 }, /* 00100000 00110001 */
387 { SYS_DVBS, QPSK, FEC_6_7, 0xfe, 0x30 }, /* 0?000000 ? */
388 { SYS_DVBS, QPSK, FEC_7_8, 0x80, 0x32 }, /* 10000000 00110010 */
389 { SYS_DVBS, QPSK, FEC_8_9, 0xfe, 0x30 }, /* 0000000? ? */
390 { SYS_DVBS, QPSK, FEC_AUTO, 0xfe, 0x30 },
392 { SYS_DVBS2, QPSK, FEC_1_2, 0x00, 0x04 },
393 { SYS_DVBS2, QPSK, FEC_3_5, 0x00, 0x05 },
394 { SYS_DVBS2, QPSK, FEC_2_3, 0x00, 0x06 },
395 { SYS_DVBS2, QPSK, FEC_3_4, 0x00, 0x07 },
396 { SYS_DVBS2, QPSK, FEC_4_5, 0x00, 0x08 },
397 { SYS_DVBS2, QPSK, FEC_5_6, 0x00, 0x09 },
398 { SYS_DVBS2, QPSK, FEC_8_9, 0x00, 0x0a },
399 { SYS_DVBS2, QPSK, FEC_9_10, 0x00, 0x0b },
401 { SYS_DVBS2, PSK_8, FEC_3_5, 0x00, 0x0c },
402 { SYS_DVBS2, PSK_8, FEC_2_3, 0x00, 0x0d },
403 { SYS_DVBS2, PSK_8, FEC_3_4, 0x00, 0x0e },
404 { SYS_DVBS2, PSK_8, FEC_5_6, 0x00, 0x0f },
405 { SYS_DVBS2, PSK_8, FEC_8_9, 0x00, 0x10 },
406 { SYS_DVBS2, PSK_8, FEC_9_10, 0x00, 0x11 },
408 * `val' can be found in the FECSTATUS register when tuning.
409 * FECSTATUS will give the actual FEC in use if tuning was successful.
413 static int cx24116_lookup_fecmod(struct cx24116_state *state,
414 fe_modulation_t m, fe_code_rate_t f)
416 int i, ret = -EOPNOTSUPP;
418 dprintk("%s(0x%02x,0x%02x)\n", __func__, m, f);
420 for (i = 0; i < ARRAY_SIZE(CX24116_MODFEC_MODES); i++) {
421 if ((m == CX24116_MODFEC_MODES[i].modulation) &&
422 (f == CX24116_MODFEC_MODES[i].fec)) {
431 static int cx24116_set_fec(struct cx24116_state *state,
432 fe_modulation_t mod, fe_code_rate_t fec)
436 dprintk("%s(0x%02x,0x%02x)\n", __func__, mod, fec);
438 ret = cx24116_lookup_fecmod(state, mod, fec);
443 state->dnxt.fec = fec;
444 state->dnxt.fec_val = CX24116_MODFEC_MODES[ret].val;
445 state->dnxt.fec_mask = CX24116_MODFEC_MODES[ret].mask;
446 dprintk("%s() mask/val = 0x%02x/0x%02x\n", __func__,
447 state->dnxt.fec_mask, state->dnxt.fec_val);
452 static int cx24116_set_symbolrate(struct cx24116_state *state, u32 rate)
454 dprintk("%s(%d)\n", __func__, rate);
456 /* check if symbol rate is within limits */
457 if ((rate > state->frontend.ops.info.symbol_rate_max) ||
458 (rate < state->frontend.ops.info.symbol_rate_min)) {
459 dprintk("%s() unsupported symbol_rate = %d\n", __func__, rate);
463 state->dnxt.symbol_rate = rate;
464 dprintk("%s() symbol_rate = %d\n", __func__, rate);
469 static int cx24116_load_firmware(struct dvb_frontend *fe,
470 const struct firmware *fw);
472 static int cx24116_firmware_ondemand(struct dvb_frontend *fe)
474 struct cx24116_state *state = fe->demodulator_priv;
475 const struct firmware *fw;
478 dprintk("%s()\n", __func__);
480 if (cx24116_readreg(state, 0x20) > 0) {
482 if (state->skip_fw_load)
486 /* request the firmware, this will block until loaded */
487 printk(KERN_INFO "%s: Waiting for firmware upload (%s)...\n",
488 __func__, CX24116_DEFAULT_FIRMWARE);
489 ret = request_firmware(&fw, CX24116_DEFAULT_FIRMWARE,
491 printk(KERN_INFO "%s: Waiting for firmware upload(2)...\n",
494 printk(KERN_ERR "%s: No firmware uploaded "
495 "(timeout or file not found?)\n", __func__);
499 /* Make sure we don't recurse back through here
501 state->skip_fw_load = 1;
503 ret = cx24116_load_firmware(fe, fw);
505 printk(KERN_ERR "%s: Writing firmware to device failed\n",
508 release_firmware(fw);
510 printk(KERN_INFO "%s: Firmware upload %s\n", __func__,
511 ret == 0 ? "complete" : "failed");
513 /* Ensure firmware is always loaded if required */
514 state->skip_fw_load = 0;
520 /* Take a basic firmware command structure, format it
521 * and forward it for processing
523 static int cx24116_cmd_execute(struct dvb_frontend *fe, struct cx24116_cmd *cmd)
525 struct cx24116_state *state = fe->demodulator_priv;
528 dprintk("%s()\n", __func__);
530 /* Load the firmware if required */
531 ret = cx24116_firmware_ondemand(fe);
533 printk(KERN_ERR "%s(): Unable initialise the firmware\n",
538 /* Write the command */
539 for (i = 0; i < cmd->len ; i++) {
540 dprintk("%s: 0x%02x == 0x%02x\n", __func__, i, cmd->args[i]);
541 cx24116_writereg(state, i, cmd->args[i]);
544 /* Start execution and wait for cmd to terminate */
545 cx24116_writereg(state, CX24116_REG_EXECUTE, 0x01);
546 while (cx24116_readreg(state, CX24116_REG_EXECUTE)) {
549 /* Avoid looping forever if the firmware does
551 printk(KERN_WARNING "%s() Firmware not responding\n",
559 static int cx24116_load_firmware(struct dvb_frontend *fe,
560 const struct firmware *fw)
562 struct cx24116_state *state = fe->demodulator_priv;
563 struct cx24116_cmd cmd;
565 unsigned char vers[4];
567 dprintk("%s\n", __func__);
568 dprintk("Firmware is %zu bytes (%02x %02x .. %02x %02x)\n",
572 fw->data[fw->size-2],
573 fw->data[fw->size-1]);
575 /* Toggle 88x SRST pin to reset demod */
576 if (state->config->reset_device)
577 state->config->reset_device(fe);
579 /* Begin the firmware load process */
580 /* Prepare the demod, load the firmware, cleanup after load */
583 cx24116_writereg(state, 0xE5, 0x00);
584 cx24116_writereg(state, 0xF1, 0x08);
585 cx24116_writereg(state, 0xF2, 0x13);
588 cx24116_writereg(state, 0xe0, 0x03);
589 cx24116_writereg(state, 0xe0, 0x00);
592 cx24116_writereg(state, CX24116_REG_CLKDIV, 0x46);
593 cx24116_writereg(state, CX24116_REG_RATEDIV, 0x00);
596 cx24116_writereg(state, 0xF0, 0x03);
597 cx24116_writereg(state, 0xF4, 0x81);
598 cx24116_writereg(state, 0xF5, 0x00);
599 cx24116_writereg(state, 0xF6, 0x00);
601 /* write the entire firmware as one transaction */
602 cx24116_writeregN(state, 0xF7, fw->data, fw->size);
604 cx24116_writereg(state, 0xF4, 0x10);
605 cx24116_writereg(state, 0xF0, 0x00);
606 cx24116_writereg(state, 0xF8, 0x06);
608 /* Firmware CMD 10: VCO config */
609 cmd.args[0x00] = CMD_SET_VCO;
610 cmd.args[0x01] = 0x05;
611 cmd.args[0x02] = 0xdc;
612 cmd.args[0x03] = 0xda;
613 cmd.args[0x04] = 0xae;
614 cmd.args[0x05] = 0xaa;
615 cmd.args[0x06] = 0x04;
616 cmd.args[0x07] = 0x9d;
617 cmd.args[0x08] = 0xfc;
618 cmd.args[0x09] = 0x06;
620 ret = cx24116_cmd_execute(fe, &cmd);
624 cx24116_writereg(state, CX24116_REG_SSTATUS, 0x00);
626 /* Firmware CMD 14: Tuner config */
627 cmd.args[0x00] = CMD_TUNERINIT;
628 cmd.args[0x01] = 0x00;
629 cmd.args[0x02] = 0x00;
631 ret = cx24116_cmd_execute(fe, &cmd);
635 cx24116_writereg(state, 0xe5, 0x00);
637 /* Firmware CMD 13: MPEG config */
638 cmd.args[0x00] = CMD_MPEGCONFIG;
639 cmd.args[0x01] = 0x01;
640 cmd.args[0x02] = 0x75;
641 cmd.args[0x03] = 0x00;
642 if (state->config->mpg_clk_pos_pol)
643 cmd.args[0x04] = state->config->mpg_clk_pos_pol;
645 cmd.args[0x04] = 0x02;
646 cmd.args[0x05] = 0x00;
648 ret = cx24116_cmd_execute(fe, &cmd);
652 /* Firmware CMD 35: Get firmware version */
653 cmd.args[0x00] = CMD_UPDFWVERS;
655 for (i = 0; i < 4; i++) {
657 ret = cx24116_cmd_execute(fe, &cmd);
660 vers[i] = cx24116_readreg(state, CX24116_REG_MAILBOX);
662 printk(KERN_INFO "%s: FW version %i.%i.%i.%i\n", __func__,
663 vers[0], vers[1], vers[2], vers[3]);
668 static int cx24116_set_voltage(struct dvb_frontend *fe,
669 fe_sec_voltage_t voltage)
671 /* The isl6421 module will override this function in the fops. */
672 dprintk("%s() This should never appear if the isl6421 module "
673 "is loaded correctly\n", __func__);
678 static int cx24116_read_status(struct dvb_frontend *fe, fe_status_t *status)
680 struct cx24116_state *state = fe->demodulator_priv;
682 int lock = cx24116_readreg(state, CX24116_REG_SSTATUS);
684 dprintk("%s: status = 0x%02x\n", __func__, lock);
688 if (lock & CX24116_HAS_SIGNAL)
689 *status |= FE_HAS_SIGNAL;
690 if (lock & CX24116_HAS_CARRIER)
691 *status |= FE_HAS_CARRIER;
692 if (lock & CX24116_HAS_VITERBI)
693 *status |= FE_HAS_VITERBI;
694 if (lock & CX24116_HAS_SYNCLOCK)
695 *status |= FE_HAS_SYNC | FE_HAS_LOCK;
700 static int cx24116_read_ber(struct dvb_frontend *fe, u32 *ber)
702 struct cx24116_state *state = fe->demodulator_priv;
704 dprintk("%s()\n", __func__);
706 *ber = (cx24116_readreg(state, CX24116_REG_BER24) << 24) |
707 (cx24116_readreg(state, CX24116_REG_BER16) << 16) |
708 (cx24116_readreg(state, CX24116_REG_BER8) << 8) |
709 cx24116_readreg(state, CX24116_REG_BER0);
714 /* TODO Determine function and scale appropriately */
715 static int cx24116_read_signal_strength(struct dvb_frontend *fe,
716 u16 *signal_strength)
718 struct cx24116_state *state = fe->demodulator_priv;
719 struct cx24116_cmd cmd;
723 dprintk("%s()\n", __func__);
725 /* Firmware CMD 19: Get AGC */
726 cmd.args[0x00] = CMD_GETAGC;
728 ret = cx24116_cmd_execute(fe, &cmd);
733 (cx24116_readreg(state,
734 CX24116_REG_SSTATUS) & CX24116_SIGNAL_MASK) |
735 (cx24116_readreg(state, CX24116_REG_SIGNAL) << 6);
736 *signal_strength = 0 - sig_reading;
738 dprintk("%s: raw / cooked = 0x%04x / 0x%04x\n",
739 __func__, sig_reading, *signal_strength);
744 /* SNR (0..100)% = (sig & 0xf0) * 10 + (sig & 0x0f) * 10 / 16 */
745 static int cx24116_read_snr_pct(struct dvb_frontend *fe, u16 *snr)
747 struct cx24116_state *state = fe->demodulator_priv;
749 static const u32 snr_tab[] = { /* 10 x Table (rounded up) */
750 0x00000, 0x0199A, 0x03333, 0x04ccD, 0x06667,
751 0x08000, 0x0999A, 0x0b333, 0x0cccD, 0x0e667,
752 0x10000, 0x1199A, 0x13333, 0x14ccD, 0x16667,
755 dprintk("%s()\n", __func__);
757 snr_reading = cx24116_readreg(state, CX24116_REG_QUALITY0);
759 if (snr_reading >= 0xa0 /* 100% */)
762 *snr = snr_tab[(snr_reading & 0xf0) >> 4] +
763 (snr_tab[(snr_reading & 0x0f)] >> 4);
765 dprintk("%s: raw / cooked = 0x%02x / 0x%04x\n", __func__,
771 /* The reelbox patches show the value in the registers represents
772 * ESNO, from 0->30db (values 0->300). We provide this value by
775 static int cx24116_read_snr_esno(struct dvb_frontend *fe, u16 *snr)
777 struct cx24116_state *state = fe->demodulator_priv;
779 dprintk("%s()\n", __func__);
781 *snr = cx24116_readreg(state, CX24116_REG_QUALITY8) << 8 |
782 cx24116_readreg(state, CX24116_REG_QUALITY0);
784 dprintk("%s: raw 0x%04x\n", __func__, *snr);
789 static int cx24116_read_snr(struct dvb_frontend *fe, u16 *snr)
792 return cx24116_read_snr_esno(fe, snr);
794 return cx24116_read_snr_pct(fe, snr);
797 static int cx24116_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
799 struct cx24116_state *state = fe->demodulator_priv;
801 dprintk("%s()\n", __func__);
803 *ucblocks = (cx24116_readreg(state, CX24116_REG_UCB8) << 8) |
804 cx24116_readreg(state, CX24116_REG_UCB0);
809 /* Overwrite the current tuning params, we are about to tune */
810 static void cx24116_clone_params(struct dvb_frontend *fe)
812 struct cx24116_state *state = fe->demodulator_priv;
813 memcpy(&state->dcur, &state->dnxt, sizeof(state->dcur));
817 static int cx24116_wait_for_lnb(struct dvb_frontend *fe)
819 struct cx24116_state *state = fe->demodulator_priv;
822 dprintk("%s() qstatus = 0x%02x\n", __func__,
823 cx24116_readreg(state, CX24116_REG_QSTATUS));
825 /* Wait for up to 300 ms */
826 for (i = 0; i < 30 ; i++) {
827 if (cx24116_readreg(state, CX24116_REG_QSTATUS) & 0x20)
832 dprintk("%s(): LNB not ready\n", __func__);
834 return -ETIMEDOUT; /* -EBUSY ? */
837 static int cx24116_set_tone(struct dvb_frontend *fe,
838 fe_sec_tone_mode_t tone)
840 struct cx24116_cmd cmd;
843 dprintk("%s(%d)\n", __func__, tone);
844 if ((tone != SEC_TONE_ON) && (tone != SEC_TONE_OFF)) {
845 printk(KERN_ERR "%s: Invalid, tone=%d\n", __func__, tone);
849 /* Wait for LNB ready */
850 ret = cx24116_wait_for_lnb(fe);
854 /* Min delay time after DiSEqC send */
855 msleep(15); /* XXX determine is FW does this, see send_diseqc/burst */
857 /* This is always done before the tone is set */
858 cmd.args[0x00] = CMD_SET_TONEPRE;
859 cmd.args[0x01] = 0x00;
861 ret = cx24116_cmd_execute(fe, &cmd);
865 /* Now we set the tone */
866 cmd.args[0x00] = CMD_SET_TONE;
867 cmd.args[0x01] = 0x00;
868 cmd.args[0x02] = 0x00;
872 dprintk("%s: setting tone on\n", __func__);
873 cmd.args[0x03] = 0x01;
876 dprintk("%s: setting tone off\n", __func__);
877 cmd.args[0x03] = 0x00;
882 /* Min delay time before DiSEqC send */
883 msleep(15); /* XXX determine is FW does this, see send_diseqc/burst */
885 return cx24116_cmd_execute(fe, &cmd);
888 /* Initialise DiSEqC */
889 static int cx24116_diseqc_init(struct dvb_frontend *fe)
891 struct cx24116_state *state = fe->demodulator_priv;
892 struct cx24116_cmd cmd;
895 /* Firmware CMD 20: LNB/DiSEqC config */
896 cmd.args[0x00] = CMD_LNBCONFIG;
897 cmd.args[0x01] = 0x00;
898 cmd.args[0x02] = 0x10;
899 cmd.args[0x03] = 0x00;
900 cmd.args[0x04] = 0x8f;
901 cmd.args[0x05] = 0x28;
902 cmd.args[0x06] = (toneburst == CX24116_DISEQC_TONEOFF) ? 0x00 : 0x01;
903 cmd.args[0x07] = 0x01;
905 ret = cx24116_cmd_execute(fe, &cmd);
909 /* Prepare a DiSEqC command */
910 state->dsec_cmd.args[0x00] = CMD_LNBSEND;
913 state->dsec_cmd.args[CX24116_DISEQC_BURST] = CX24116_DISEQC_MINI_A;
916 state->dsec_cmd.args[CX24116_DISEQC_ARG2_2] = 0x02;
917 state->dsec_cmd.args[CX24116_DISEQC_ARG3_0] = 0x00;
918 /* Continuation flag? */
919 state->dsec_cmd.args[CX24116_DISEQC_ARG4_0] = 0x00;
921 /* DiSEqC message length */
922 state->dsec_cmd.args[CX24116_DISEQC_MSGLEN] = 0x00;
925 state->dsec_cmd.len = CX24116_DISEQC_MSGOFS;
930 /* Send DiSEqC message with derived burst (hack) || previous burst */
931 static int cx24116_send_diseqc_msg(struct dvb_frontend *fe,
932 struct dvb_diseqc_master_cmd *d)
934 struct cx24116_state *state = fe->demodulator_priv;
937 /* Dump DiSEqC message */
939 printk(KERN_INFO "cx24116: %s(", __func__);
940 for (i = 0 ; i < d->msg_len ;) {
941 printk(KERN_INFO "0x%02x", d->msg[i]);
942 if (++i < d->msg_len)
943 printk(KERN_INFO ", ");
945 printk(") toneburst=%d\n", toneburst);
948 /* Validate length */
949 if (d->msg_len > (CX24116_ARGLEN - CX24116_DISEQC_MSGOFS))
953 for (i = 0; i < d->msg_len; i++)
954 state->dsec_cmd.args[CX24116_DISEQC_MSGOFS + i] = d->msg[i];
956 /* DiSEqC message length */
957 state->dsec_cmd.args[CX24116_DISEQC_MSGLEN] = d->msg_len;
960 state->dsec_cmd.len = CX24116_DISEQC_MSGOFS +
961 state->dsec_cmd.args[CX24116_DISEQC_MSGLEN];
963 /* DiSEqC toneburst */
964 if (toneburst == CX24116_DISEQC_MESGCACHE)
965 /* Message is cached */
968 else if (toneburst == CX24116_DISEQC_TONEOFF)
969 /* Message is sent without burst */
970 state->dsec_cmd.args[CX24116_DISEQC_BURST] = 0;
972 else if (toneburst == CX24116_DISEQC_TONECACHE) {
974 * Message is sent with derived else cached burst
976 * WRITE PORT GROUP COMMAND 38
978 * 0/A/A: E0 10 38 F0..F3
979 * 1/B/B: E0 10 38 F4..F7
980 * 2/C/A: E0 10 38 F8..FB
981 * 3/D/B: E0 10 38 FC..FF
983 * databyte[3]= 8421:8421
987 * WX= PORT SELECT 0..3 (X=TONEBURST)
988 * Y = VOLTAGE (0=13V, 1=18V)
989 * Z = BAND (0=LOW, 1=HIGH(22K))
991 if (d->msg_len >= 4 && d->msg[2] == 0x38)
992 state->dsec_cmd.args[CX24116_DISEQC_BURST] =
993 ((d->msg[3] & 4) >> 2);
995 dprintk("%s burst=%d\n", __func__,
996 state->dsec_cmd.args[CX24116_DISEQC_BURST]);
999 /* Wait for LNB ready */
1000 ret = cx24116_wait_for_lnb(fe);
1004 /* Wait for voltage/min repeat delay */
1008 ret = cx24116_cmd_execute(fe, &state->dsec_cmd);
1015 * >15ms delay + (XXX determine if FW does this, see set_tone)
1019 * >15ms delay (XXX determine if FW does this, see set_tone)
1021 msleep((state->dsec_cmd.args[CX24116_DISEQC_MSGLEN] << 4) +
1022 ((toneburst == CX24116_DISEQC_TONEOFF) ? 30 : 60));
1027 /* Send DiSEqC burst */
1028 static int cx24116_diseqc_send_burst(struct dvb_frontend *fe,
1029 fe_sec_mini_cmd_t burst)
1031 struct cx24116_state *state = fe->demodulator_priv;
1034 dprintk("%s(%d) toneburst=%d\n", __func__, burst, toneburst);
1037 if (burst == SEC_MINI_A)
1038 state->dsec_cmd.args[CX24116_DISEQC_BURST] =
1039 CX24116_DISEQC_MINI_A;
1040 else if (burst == SEC_MINI_B)
1041 state->dsec_cmd.args[CX24116_DISEQC_BURST] =
1042 CX24116_DISEQC_MINI_B;
1046 /* DiSEqC toneburst */
1047 if (toneburst != CX24116_DISEQC_MESGCACHE)
1048 /* Burst is cached */
1051 /* Burst is to be sent with cached message */
1053 /* Wait for LNB ready */
1054 ret = cx24116_wait_for_lnb(fe);
1058 /* Wait for voltage/min repeat delay */
1062 ret = cx24116_cmd_execute(fe, &state->dsec_cmd);
1070 * >15ms delay + (XXX determine if FW does this, see set_tone)
1074 * >15ms delay (XXX determine if FW does this, see set_tone)
1076 msleep((state->dsec_cmd.args[CX24116_DISEQC_MSGLEN] << 4) + 60);
1081 static void cx24116_release(struct dvb_frontend *fe)
1083 struct cx24116_state *state = fe->demodulator_priv;
1084 dprintk("%s\n", __func__);
1088 static struct dvb_frontend_ops cx24116_ops;
1090 struct dvb_frontend *cx24116_attach(const struct cx24116_config *config,
1091 struct i2c_adapter *i2c)
1093 struct cx24116_state *state = NULL;
1096 dprintk("%s\n", __func__);
1098 /* allocate memory for the internal state */
1099 state = kmalloc(sizeof(struct cx24116_state), GFP_KERNEL);
1103 /* setup the state */
1104 memset(state, 0, sizeof(struct cx24116_state));
1106 state->config = config;
1109 /* check if the demod is present */
1110 ret = (cx24116_readreg(state, 0xFF) << 8) |
1111 cx24116_readreg(state, 0xFE);
1112 if (ret != 0x0501) {
1113 printk(KERN_INFO "Invalid probe, probably not a CX24116 device\n");
1117 /* create dvb_frontend */
1118 memcpy(&state->frontend.ops, &cx24116_ops,
1119 sizeof(struct dvb_frontend_ops));
1120 state->frontend.demodulator_priv = state;
1121 return &state->frontend;
1123 error2: kfree(state);
1124 error1: return NULL;
1126 EXPORT_SYMBOL(cx24116_attach);
1129 * Initialise or wake up device
1131 * Power config will reset and load initial firmware if required
1133 static int cx24116_initfe(struct dvb_frontend *fe)
1135 struct cx24116_state *state = fe->demodulator_priv;
1136 struct cx24116_cmd cmd;
1139 dprintk("%s()\n", __func__);
1142 cx24116_writereg(state, 0xe0, 0);
1143 cx24116_writereg(state, 0xe1, 0);
1144 cx24116_writereg(state, 0xea, 0);
1146 /* Firmware CMD 36: Power config */
1147 cmd.args[0x00] = CMD_TUNERSLEEP;
1150 ret = cx24116_cmd_execute(fe, &cmd);
1154 return cx24116_diseqc_init(fe);
1158 * Put device to sleep
1160 static int cx24116_sleep(struct dvb_frontend *fe)
1162 struct cx24116_state *state = fe->demodulator_priv;
1163 struct cx24116_cmd cmd;
1166 dprintk("%s()\n", __func__);
1168 /* Firmware CMD 36: Power config */
1169 cmd.args[0x00] = CMD_TUNERSLEEP;
1172 ret = cx24116_cmd_execute(fe, &cmd);
1176 /* Power off (Shutdown clocks) */
1177 cx24116_writereg(state, 0xea, 0xff);
1178 cx24116_writereg(state, 0xe1, 1);
1179 cx24116_writereg(state, 0xe0, 1);
1184 static int cx24116_set_property(struct dvb_frontend *fe,
1185 struct dtv_property *tvp)
1187 dprintk("%s(..)\n", __func__);
1191 static int cx24116_get_property(struct dvb_frontend *fe,
1192 struct dtv_property *tvp)
1194 dprintk("%s(..)\n", __func__);
1198 /* dvb-core told us to tune, the tv property cache will be complete,
1199 * it's safe for is to pull values and use them for tuning purposes.
1201 static int cx24116_set_frontend(struct dvb_frontend *fe,
1202 struct dvb_frontend_parameters *p)
1204 struct cx24116_state *state = fe->demodulator_priv;
1205 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1206 struct cx24116_cmd cmd;
1207 fe_status_t tunerstat;
1208 int i, status, ret, retune;
1210 dprintk("%s()\n", __func__);
1212 switch (c->delivery_system) {
1214 dprintk("%s: DVB-S delivery system selected\n", __func__);
1216 /* Only QPSK is supported for DVB-S */
1217 if (c->modulation != QPSK) {
1218 dprintk("%s: unsupported modulation selected (%d)\n",
1219 __func__, c->modulation);
1223 /* Pilot doesn't exist in DVB-S, turn bit off */
1224 state->dnxt.pilot_val = CX24116_PILOT_OFF;
1227 /* DVB-S only supports 0.35 */
1228 if (c->rolloff != ROLLOFF_35) {
1229 dprintk("%s: unsupported rolloff selected (%d)\n",
1230 __func__, c->rolloff);
1233 state->dnxt.rolloff_val = CX24116_ROLLOFF_035;
1237 dprintk("%s: DVB-S2 delivery system selected\n", __func__);
1240 * NBC 8PSK/QPSK with DVB-S is supported for DVB-S2,
1241 * but not hardware auto detection
1243 if (c->modulation != PSK_8 && c->modulation != QPSK) {
1244 dprintk("%s: unsupported modulation selected (%d)\n",
1245 __func__, c->modulation);
1250 case PILOT_AUTO: /* Not supported but emulated */
1251 state->dnxt.pilot_val = (c->modulation == QPSK)
1252 ? CX24116_PILOT_OFF : CX24116_PILOT_ON;
1256 state->dnxt.pilot_val = CX24116_PILOT_OFF;
1259 state->dnxt.pilot_val = CX24116_PILOT_ON;
1262 dprintk("%s: unsupported pilot mode selected (%d)\n",
1263 __func__, c->pilot);
1267 switch (c->rolloff) {
1269 state->dnxt.rolloff_val = CX24116_ROLLOFF_020;
1272 state->dnxt.rolloff_val = CX24116_ROLLOFF_025;
1275 state->dnxt.rolloff_val = CX24116_ROLLOFF_035;
1277 case ROLLOFF_AUTO: /* Rolloff must be explicit */
1279 dprintk("%s: unsupported rolloff selected (%d)\n",
1280 __func__, c->rolloff);
1286 dprintk("%s: unsupported delivery system selected (%d)\n",
1287 __func__, c->delivery_system);
1290 state->dnxt.modulation = c->modulation;
1291 state->dnxt.frequency = c->frequency;
1292 state->dnxt.pilot = c->pilot;
1293 state->dnxt.rolloff = c->rolloff;
1295 ret = cx24116_set_inversion(state, c->inversion);
1299 /* FEC_NONE/AUTO for DVB-S2 is not supported and detected here */
1300 ret = cx24116_set_fec(state, c->modulation, c->fec_inner);
1304 ret = cx24116_set_symbolrate(state, c->symbol_rate);
1308 /* discard the 'current' tuning parameters and prepare to tune */
1309 cx24116_clone_params(fe);
1311 dprintk("%s: modulation = %d\n", __func__, state->dcur.modulation);
1312 dprintk("%s: frequency = %d\n", __func__, state->dcur.frequency);
1313 dprintk("%s: pilot = %d (val = 0x%02x)\n", __func__,
1314 state->dcur.pilot, state->dcur.pilot_val);
1315 dprintk("%s: retune = %d\n", __func__, retune);
1316 dprintk("%s: rolloff = %d (val = 0x%02x)\n", __func__,
1317 state->dcur.rolloff, state->dcur.rolloff_val);
1318 dprintk("%s: symbol_rate = %d\n", __func__, state->dcur.symbol_rate);
1319 dprintk("%s: FEC = %d (mask/val = 0x%02x/0x%02x)\n", __func__,
1320 state->dcur.fec, state->dcur.fec_mask, state->dcur.fec_val);
1321 dprintk("%s: Inversion = %d (val = 0x%02x)\n", __func__,
1322 state->dcur.inversion, state->dcur.inversion_val);
1324 /* This is also done in advise/acquire on HVR4000 but not on LITE */
1325 if (state->config->set_ts_params)
1326 state->config->set_ts_params(fe, 0);
1329 cmd.args[0x00] = CMD_BANDWIDTH;
1330 cmd.args[0x01] = 0x01;
1332 ret = cx24116_cmd_execute(fe, &cmd);
1336 /* Prepare a tune request */
1337 cmd.args[0x00] = CMD_TUNEREQUEST;
1340 cmd.args[0x01] = (state->dcur.frequency & 0xff0000) >> 16;
1341 cmd.args[0x02] = (state->dcur.frequency & 0x00ff00) >> 8;
1342 cmd.args[0x03] = (state->dcur.frequency & 0x0000ff);
1345 cmd.args[0x04] = ((state->dcur.symbol_rate / 1000) & 0xff00) >> 8;
1346 cmd.args[0x05] = ((state->dcur.symbol_rate / 1000) & 0x00ff);
1348 /* Automatic Inversion */
1349 cmd.args[0x06] = state->dcur.inversion_val;
1351 /* Modulation / FEC / Pilot */
1352 cmd.args[0x07] = state->dcur.fec_val | state->dcur.pilot_val;
1354 cmd.args[0x08] = CX24116_SEARCH_RANGE_KHZ >> 8;
1355 cmd.args[0x09] = CX24116_SEARCH_RANGE_KHZ & 0xff;
1356 cmd.args[0x0a] = 0x00;
1357 cmd.args[0x0b] = 0x00;
1358 cmd.args[0x0c] = state->dcur.rolloff_val;
1359 cmd.args[0x0d] = state->dcur.fec_mask;
1361 if (state->dcur.symbol_rate > 30000000) {
1362 cmd.args[0x0e] = 0x04;
1363 cmd.args[0x0f] = 0x00;
1364 cmd.args[0x10] = 0x01;
1365 cmd.args[0x11] = 0x77;
1366 cmd.args[0x12] = 0x36;
1367 cx24116_writereg(state, CX24116_REG_CLKDIV, 0x44);
1368 cx24116_writereg(state, CX24116_REG_RATEDIV, 0x01);
1370 cmd.args[0x0e] = 0x06;
1371 cmd.args[0x0f] = 0x00;
1372 cmd.args[0x10] = 0x00;
1373 cmd.args[0x11] = 0xFA;
1374 cmd.args[0x12] = 0x24;
1375 cx24116_writereg(state, CX24116_REG_CLKDIV, 0x46);
1376 cx24116_writereg(state, CX24116_REG_RATEDIV, 0x00);
1381 /* We need to support pilot and non-pilot tuning in the
1382 * driver automatically. This is a workaround for because
1383 * the demod does not support autodetect.
1386 /* Reset status register */
1387 status = cx24116_readreg(state, CX24116_REG_SSTATUS)
1388 & CX24116_SIGNAL_MASK;
1389 cx24116_writereg(state, CX24116_REG_SSTATUS, status);
1392 ret = cx24116_cmd_execute(fe, &cmd);
1397 * Wait for up to 500 ms before retrying
1399 * If we are able to tune then generally it occurs within 100ms.
1400 * If it takes longer, try a different toneburst setting.
1402 for (i = 0; i < 50 ; i++) {
1403 cx24116_read_status(fe, &tunerstat);
1404 status = tunerstat & (FE_HAS_SIGNAL | FE_HAS_SYNC);
1405 if (status == (FE_HAS_SIGNAL | FE_HAS_SYNC)) {
1406 dprintk("%s: Tuned\n", __func__);
1412 dprintk("%s: Not tuned\n", __func__);
1414 /* Toggle pilot bit when in auto-pilot */
1415 if (state->dcur.pilot == PILOT_AUTO)
1416 cmd.args[0x07] ^= CX24116_PILOT_ON;
1419 tuned: /* Set/Reset B/W */
1420 cmd.args[0x00] = CMD_BANDWIDTH;
1421 cmd.args[0x01] = 0x00;
1423 ret = cx24116_cmd_execute(fe, &cmd);
1430 static struct dvb_frontend_ops cx24116_ops = {
1433 .name = "Conexant CX24116/CX24118",
1435 .frequency_min = 950000,
1436 .frequency_max = 2150000,
1437 .frequency_stepsize = 1011, /* kHz for QPSK frontends */
1438 .frequency_tolerance = 5000,
1439 .symbol_rate_min = 1000000,
1440 .symbol_rate_max = 45000000,
1441 .caps = FE_CAN_INVERSION_AUTO |
1442 FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
1443 FE_CAN_FEC_4_5 | FE_CAN_FEC_5_6 | FE_CAN_FEC_6_7 |
1444 FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
1445 FE_CAN_QPSK | FE_CAN_RECOVER
1448 .release = cx24116_release,
1450 .init = cx24116_initfe,
1451 .sleep = cx24116_sleep,
1452 .read_status = cx24116_read_status,
1453 .read_ber = cx24116_read_ber,
1454 .read_signal_strength = cx24116_read_signal_strength,
1455 .read_snr = cx24116_read_snr,
1456 .read_ucblocks = cx24116_read_ucblocks,
1457 .set_tone = cx24116_set_tone,
1458 .set_voltage = cx24116_set_voltage,
1459 .diseqc_send_master_cmd = cx24116_send_diseqc_msg,
1460 .diseqc_send_burst = cx24116_diseqc_send_burst,
1462 .set_property = cx24116_set_property,
1463 .get_property = cx24116_get_property,
1464 .set_frontend = cx24116_set_frontend,
1467 MODULE_DESCRIPTION("DVB Frontend module for Conexant cx24116/cx24118 hardware");
1468 MODULE_AUTHOR("Steven Toth");
1469 MODULE_LICENSE("GPL");