]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/media/dvb/frontends/cx24116.c
Merge git://git.linux-nfs.org/projects/trondmy/nfs-2.6
[linux-2.6-omap-h63xx.git] / drivers / media / dvb / frontends / cx24116.c
1 /*
2     Conexant cx24116/cx24118 - DVBS/S2 Satellite demod/tuner driver
3
4     Copyright (C) 2006-2008 Steven Toth <stoth@hauppauge.com>
5     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
16             Fixed locking on high symbol rates (>30000).
17             Implement MPEG initialization parameter.
18
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.
23
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.
28
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.
32 */
33
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>
40
41 #include "dvb_frontend.h"
42 #include "cx24116.h"
43
44 static int debug;
45 module_param(debug, int, 0644);
46 MODULE_PARM_DESC(debug, "Activates frontend debugging (default:0)");
47
48 #define dprintk(args...) \
49         do { \
50                 if (debug) \
51                         printk(KERN_INFO "cx24116: " args); \
52         } while (0)
53
54 #define CX24116_DEFAULT_FIRMWARE "dvb-fe-cx24116.fw"
55 #define CX24116_SEARCH_RANGE_KHZ 5000
56
57 /* known registers */
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)
75
76 /* configured fec (not tuned) or actual FEC (tuned) 1=1/2 2=2/3 etc */
77 #define CX24116_REG_FECSTATUS (0x9c)
78
79 /* FECSTATUS bits */
80 /* mask to determine configured fec (not tuned) or actual fec (tuned) */
81 #define CX24116_FEC_FECMASK   (0x1f)
82
83 /* Select DVB-S demodulator, else DVB-S2 */
84 #define CX24116_FEC_DVBS      (0x20)
85 #define CX24116_FEC_UNKNOWN   (0x40)    /* Unknown/unused */
86
87 /* Pilot mode requested when tuning else always reset when tuned */
88 #define CX24116_FEC_PILOT     (0x80)
89
90 /* arg buffer size */
91 #define CX24116_ARGLEN (0x1e)
92
93 /* rolloff */
94 #define CX24116_ROLLOFF_020 (0x00)
95 #define CX24116_ROLLOFF_025 (0x01)
96 #define CX24116_ROLLOFF_035 (0x02)
97
98 /* pilot bit */
99 #define CX24116_PILOT_OFF (0x00)
100 #define CX24116_PILOT_ON (0x40)
101
102 /* signal status */
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)
111
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       */
115
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)
123
124 /* DiSEqC burst */
125 #define CX24116_DISEQC_MINI_A (0)
126 #define CX24116_DISEQC_MINI_B (1)
127
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)");
133
134 /* SNR measurements */
135 static int esno_snr;
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)");
139
140 enum cmds {
141         CMD_SET_VCO     = 0x10,
142         CMD_TUNEREQUEST = 0x11,
143         CMD_MPEGCONFIG  = 0x13,
144         CMD_TUNERINIT   = 0x14,
145         CMD_BANDWIDTH   = 0x15,
146         CMD_GETAGC      = 0x19,
147         CMD_LNBCONFIG   = 0x20,
148         CMD_LNBSEND     = 0x21, /* Formerly CMD_SEND_DISEQC */
149         CMD_SET_TONEPRE = 0x22,
150         CMD_SET_TONE    = 0x23,
151         CMD_UPDFWVERS   = 0x35,
152         CMD_TUNERSLEEP  = 0x36,
153         CMD_AGCCONTROL  = 0x3b, /* Unknown */
154 };
155
156 /* The Demod/Tuner can't easily provide these, we cache them */
157 struct cx24116_tuning {
158         u32 frequency;
159         u32 symbol_rate;
160         fe_spectral_inversion_t inversion;
161         fe_code_rate_t fec;
162
163         fe_modulation_t modulation;
164         fe_pilot_t pilot;
165         fe_rolloff_t rolloff;
166
167         /* Demod values */
168         u8 fec_val;
169         u8 fec_mask;
170         u8 inversion_val;
171         u8 pilot_val;
172         u8 rolloff_val;
173 };
174
175 /* Basic commands that are sent to the firmware */
176 struct cx24116_cmd {
177         u8 len;
178         u8 args[CX24116_ARGLEN];
179 };
180
181 struct cx24116_state {
182         struct i2c_adapter *i2c;
183         const struct cx24116_config *config;
184
185         struct dvb_frontend frontend;
186
187         struct cx24116_tuning dcur;
188         struct cx24116_tuning dnxt;
189
190         u8 skip_fw_load;
191         u8 burst;
192         struct cx24116_cmd dsec_cmd;
193 };
194
195 static int cx24116_writereg(struct cx24116_state *state, int reg, int data)
196 {
197         u8 buf[] = { reg, data };
198         struct i2c_msg msg = { .addr = state->config->demod_address,
199                 .flags = 0, .buf = buf, .len = 2 };
200         int err;
201
202         if (debug > 1)
203                 printk("cx24116: %s: write reg 0x%02x, value 0x%02x\n",
204                         __func__, reg, data);
205
206         err = i2c_transfer(state->i2c, &msg, 1);
207         if (err != 1) {
208                 printk(KERN_ERR "%s: writereg error(err == %i, reg == 0x%02x,"
209                          " value == 0x%02x)\n", __func__, err, reg, data);
210                 return -EREMOTEIO;
211         }
212
213         return 0;
214 }
215
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)
219 {
220         int ret = -EREMOTEIO;
221         struct i2c_msg msg;
222         u8 *buf;
223
224         buf = kmalloc(len + 1, GFP_KERNEL);
225         if (buf == NULL) {
226                 printk("Unable to kmalloc\n");
227                 ret = -ENOMEM;
228                 goto error;
229         }
230
231         *(buf) = reg;
232         memcpy(buf + 1, data, len);
233
234         msg.addr = state->config->demod_address;
235         msg.flags = 0;
236         msg.buf = buf;
237         msg.len = len + 1;
238
239         if (debug > 1)
240                 printk(KERN_INFO "cx24116: %s:  write regN 0x%02x, len = %d\n",
241                         __func__, reg, len);
242
243         ret = i2c_transfer(state->i2c, &msg, 1);
244         if (ret != 1) {
245                 printk(KERN_ERR "%s: writereg error(err == %i, reg == 0x%02x\n",
246                          __func__, ret, reg);
247                 ret = -EREMOTEIO;
248         }
249
250 error:
251         kfree(buf);
252
253         return ret;
254 }
255
256 static int cx24116_readreg(struct cx24116_state *state, u8 reg)
257 {
258         int ret;
259         u8 b0[] = { reg };
260         u8 b1[] = { 0 };
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 }
266         };
267
268         ret = i2c_transfer(state->i2c, msg, 2);
269
270         if (ret != 2) {
271                 printk(KERN_ERR "%s: reg=0x%x (error=%d)\n",
272                         __func__, reg, ret);
273                 return ret;
274         }
275
276         if (debug > 1)
277                 printk(KERN_INFO "cx24116: read reg 0x%02x, value 0x%02x\n",
278                         reg, b1[0]);
279
280         return b1[0];
281 }
282
283 static int cx24116_set_inversion(struct cx24116_state *state,
284         fe_spectral_inversion_t inversion)
285 {
286         dprintk("%s(%d)\n", __func__, inversion);
287
288         switch (inversion) {
289         case INVERSION_OFF:
290                 state->dnxt.inversion_val = 0x00;
291                 break;
292         case INVERSION_ON:
293                 state->dnxt.inversion_val = 0x04;
294                 break;
295         case INVERSION_AUTO:
296                 state->dnxt.inversion_val = 0x0C;
297                 break;
298         default:
299                 return -EINVAL;
300         }
301
302         state->dnxt.inversion = inversion;
303
304         return 0;
305 }
306
307 /*
308  * modfec (modulation and FEC)
309  * ===========================
310  *
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
322  *
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).
328  *
329  * Eg.(2/3) szap "Zone Horror"
330  *
331  * mask/val = 0x04, 0x20
332  * status 1f | signal c3c0 | snr a333 | ber 00000098 | unc 0 | FE_HAS_LOCK
333  *
334  * mask/val = 0x04, 0x30
335  * status 1f | signal c3c0 | snr a333 | ber 00000000 | unc 0 | FE_HAS_LOCK
336  *
337  * After tuning FECSTATUS contains actual FEC
338  * in use numbered 1 through to 8 for 1/2 .. 2/3 etc
339  *
340  * NBC=NOT/NON BACKWARD COMPATIBLE WITH DVB-S (DVB-S2 only)
341  *
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
350  *
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
357  *
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
364  * value is reset.
365  */
366
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.
370  */
371 struct cx24116_modfec {
372         fe_delivery_system_t delivery_system;
373         fe_modulation_t modulation;
374         fe_code_rate_t fec;
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 */
379
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 },
391  /* NBC-QPSK */
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 },
400  /* 8PSK */
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 },
407  /*
408   * `val' can be found in the FECSTATUS register when tuning.
409   * FECSTATUS will give the actual FEC in use if tuning was successful.
410   */
411 };
412
413 static int cx24116_lookup_fecmod(struct cx24116_state *state,
414         fe_modulation_t m, fe_code_rate_t f)
415 {
416         int i, ret = -EOPNOTSUPP;
417
418         dprintk("%s(0x%02x,0x%02x)\n", __func__, m, f);
419
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)) {
423                                 ret = i;
424                                 break;
425                         }
426         }
427
428         return ret;
429 }
430
431 static int cx24116_set_fec(struct cx24116_state *state,
432         fe_modulation_t mod, fe_code_rate_t fec)
433 {
434         int ret = 0;
435
436         dprintk("%s(0x%02x,0x%02x)\n", __func__, mod, fec);
437
438         ret = cx24116_lookup_fecmod(state, mod, fec);
439
440         if (ret < 0)
441                 return ret;
442
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);
448
449         return 0;
450 }
451
452 static int cx24116_set_symbolrate(struct cx24116_state *state, u32 rate)
453 {
454         dprintk("%s(%d)\n", __func__, rate);
455
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);
460                 return -EOPNOTSUPP;
461         }
462
463         state->dnxt.symbol_rate = rate;
464         dprintk("%s() symbol_rate = %d\n", __func__, rate);
465
466         return 0;
467 }
468
469 static int cx24116_load_firmware(struct dvb_frontend *fe,
470         const struct firmware *fw);
471
472 static int cx24116_firmware_ondemand(struct dvb_frontend *fe)
473 {
474         struct cx24116_state *state = fe->demodulator_priv;
475         const struct firmware *fw;
476         int ret = 0;
477
478         dprintk("%s()\n", __func__);
479
480         if (cx24116_readreg(state, 0x20) > 0) {
481
482                 if (state->skip_fw_load)
483                         return 0;
484
485                 /* Load firmware */
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,
490                         &state->i2c->dev);
491                 printk(KERN_INFO "%s: Waiting for firmware upload(2)...\n",
492                         __func__);
493                 if (ret) {
494                         printk(KERN_ERR "%s: No firmware uploaded "
495                                 "(timeout or file not found?)\n", __func__);
496                         return ret;
497                 }
498
499                 /* Make sure we don't recurse back through here
500                  * during loading */
501                 state->skip_fw_load = 1;
502
503                 ret = cx24116_load_firmware(fe, fw);
504                 if (ret)
505                         printk(KERN_ERR "%s: Writing firmware to device failed\n",
506                                 __func__);
507
508                 release_firmware(fw);
509
510                 printk(KERN_INFO "%s: Firmware upload %s\n", __func__,
511                         ret == 0 ? "complete" : "failed");
512
513                 /* Ensure firmware is always loaded if required */
514                 state->skip_fw_load = 0;
515         }
516
517         return ret;
518 }
519
520 /* Take a basic firmware command structure, format it
521  * and forward it for processing
522  */
523 static int cx24116_cmd_execute(struct dvb_frontend *fe, struct cx24116_cmd *cmd)
524 {
525         struct cx24116_state *state = fe->demodulator_priv;
526         int i, ret;
527
528         dprintk("%s()\n", __func__);
529
530         /* Load the firmware if required */
531         ret = cx24116_firmware_ondemand(fe);
532         if (ret != 0) {
533                 printk(KERN_ERR "%s(): Unable initialise the firmware\n",
534                         __func__);
535                 return ret;
536         }
537
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]);
542         }
543
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)) {
547                 msleep(10);
548                 if (i++ > 64) {
549                         /* Avoid looping forever if the firmware does
550                                 not respond */
551                         printk(KERN_WARNING "%s() Firmware not responding\n",
552                                 __func__);
553                         return -EREMOTEIO;
554                 }
555         }
556         return 0;
557 }
558
559 static int cx24116_load_firmware(struct dvb_frontend *fe,
560         const struct firmware *fw)
561 {
562         struct cx24116_state *state = fe->demodulator_priv;
563         struct cx24116_cmd cmd;
564         int i, ret;
565         unsigned char vers[4];
566
567         dprintk("%s\n", __func__);
568         dprintk("Firmware is %zu bytes (%02x %02x .. %02x %02x)\n",
569                         fw->size,
570                         fw->data[0],
571                         fw->data[1],
572                         fw->data[fw->size-2],
573                         fw->data[fw->size-1]);
574
575         /* Toggle 88x SRST pin to reset demod */
576         if (state->config->reset_device)
577                 state->config->reset_device(fe);
578
579         /* Begin the firmware load process */
580         /* Prepare the demod, load the firmware, cleanup after load */
581
582         /* Init PLL */
583         cx24116_writereg(state, 0xE5, 0x00);
584         cx24116_writereg(state, 0xF1, 0x08);
585         cx24116_writereg(state, 0xF2, 0x13);
586
587         /* Start PLL */
588         cx24116_writereg(state, 0xe0, 0x03);
589         cx24116_writereg(state, 0xe0, 0x00);
590
591         /* Unknown */
592         cx24116_writereg(state, CX24116_REG_CLKDIV, 0x46);
593         cx24116_writereg(state, CX24116_REG_RATEDIV, 0x00);
594
595         /* Unknown */
596         cx24116_writereg(state, 0xF0, 0x03);
597         cx24116_writereg(state, 0xF4, 0x81);
598         cx24116_writereg(state, 0xF5, 0x00);
599         cx24116_writereg(state, 0xF6, 0x00);
600
601         /* write the entire firmware as one transaction */
602         cx24116_writeregN(state, 0xF7, fw->data, fw->size);
603
604         cx24116_writereg(state, 0xF4, 0x10);
605         cx24116_writereg(state, 0xF0, 0x00);
606         cx24116_writereg(state, 0xF8, 0x06);
607
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;
619         cmd.len = 0x0a;
620         ret = cx24116_cmd_execute(fe, &cmd);
621         if (ret != 0)
622                 return ret;
623
624         cx24116_writereg(state, CX24116_REG_SSTATUS, 0x00);
625
626         /* Firmware CMD 14: Tuner config */
627         cmd.args[0x00] = CMD_TUNERINIT;
628         cmd.args[0x01] = 0x00;
629         cmd.args[0x02] = 0x00;
630         cmd.len = 0x03;
631         ret = cx24116_cmd_execute(fe, &cmd);
632         if (ret != 0)
633                 return ret;
634
635         cx24116_writereg(state, 0xe5, 0x00);
636
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;
644         else
645                 cmd.args[0x04] = 0x02;
646         cmd.args[0x05] = 0x00;
647         cmd.len = 0x06;
648         ret = cx24116_cmd_execute(fe, &cmd);
649         if (ret != 0)
650                 return ret;
651
652         /* Firmware CMD 35: Get firmware version */
653         cmd.args[0x00] = CMD_UPDFWVERS;
654         cmd.len = 0x02;
655         for (i = 0; i < 4; i++) {
656                 cmd.args[0x01] = i;
657                 ret = cx24116_cmd_execute(fe, &cmd);
658                 if (ret != 0)
659                         return ret;
660                 vers[i] = cx24116_readreg(state, CX24116_REG_MAILBOX);
661         }
662         printk(KERN_INFO "%s: FW version %i.%i.%i.%i\n", __func__,
663                 vers[0], vers[1], vers[2], vers[3]);
664
665         return 0;
666 }
667
668 static int cx24116_set_voltage(struct dvb_frontend *fe,
669         fe_sec_voltage_t voltage)
670 {
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__);
674
675         return -EOPNOTSUPP;
676 }
677
678 static int cx24116_read_status(struct dvb_frontend *fe, fe_status_t *status)
679 {
680         struct cx24116_state *state = fe->demodulator_priv;
681
682         int lock = cx24116_readreg(state, CX24116_REG_SSTATUS);
683
684         dprintk("%s: status = 0x%02x\n", __func__, lock);
685
686         *status = 0;
687
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;
696
697         return 0;
698 }
699
700 static int cx24116_read_ber(struct dvb_frontend *fe, u32 *ber)
701 {
702         struct cx24116_state *state = fe->demodulator_priv;
703
704         dprintk("%s()\n", __func__);
705
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);
710
711         return 0;
712 }
713
714 /* TODO Determine function and scale appropriately */
715 static int cx24116_read_signal_strength(struct dvb_frontend *fe,
716         u16 *signal_strength)
717 {
718         struct cx24116_state *state = fe->demodulator_priv;
719         struct cx24116_cmd cmd;
720         int ret;
721         u16 sig_reading;
722
723         dprintk("%s()\n", __func__);
724
725         /* Firmware CMD 19: Get AGC */
726         cmd.args[0x00] = CMD_GETAGC;
727         cmd.len = 0x01;
728         ret = cx24116_cmd_execute(fe, &cmd);
729         if (ret != 0)
730                 return ret;
731
732         sig_reading =
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;
737
738         dprintk("%s: raw / cooked = 0x%04x / 0x%04x\n",
739                 __func__, sig_reading, *signal_strength);
740
741         return 0;
742 }
743
744 /* SNR (0..100)% = (sig & 0xf0) * 10 + (sig & 0x0f) * 10 / 16 */
745 static int cx24116_read_snr_pct(struct dvb_frontend *fe, u16 *snr)
746 {
747         struct cx24116_state *state = fe->demodulator_priv;
748         u8 snr_reading;
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,
753                 0x18000 };
754
755         dprintk("%s()\n", __func__);
756
757         snr_reading = cx24116_readreg(state, CX24116_REG_QUALITY0);
758
759         if (snr_reading >= 0xa0 /* 100% */)
760                 *snr = 0xffff;
761         else
762                 *snr = snr_tab[(snr_reading & 0xf0) >> 4] +
763                         (snr_tab[(snr_reading & 0x0f)] >> 4);
764
765         dprintk("%s: raw / cooked = 0x%02x / 0x%04x\n", __func__,
766                 snr_reading, *snr);
767
768         return 0;
769 }
770
771 /* The reelbox patches show the value in the registers represents
772  * ESNO, from 0->30db (values 0->300). We provide this value by
773  * default.
774  */
775 static int cx24116_read_snr_esno(struct dvb_frontend *fe, u16 *snr)
776 {
777         struct cx24116_state *state = fe->demodulator_priv;
778
779         dprintk("%s()\n", __func__);
780
781         *snr = cx24116_readreg(state, CX24116_REG_QUALITY8) << 8 |
782                 cx24116_readreg(state, CX24116_REG_QUALITY0);
783
784         dprintk("%s: raw 0x%04x\n", __func__, *snr);
785
786         return 0;
787 }
788
789 static int cx24116_read_snr(struct dvb_frontend *fe, u16 *snr)
790 {
791         if (esno_snr == 1)
792                 return cx24116_read_snr_esno(fe, snr);
793         else
794                 return cx24116_read_snr_pct(fe, snr);
795 }
796
797 static int cx24116_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
798 {
799         struct cx24116_state *state = fe->demodulator_priv;
800
801         dprintk("%s()\n", __func__);
802
803         *ucblocks = (cx24116_readreg(state, CX24116_REG_UCB8) << 8) |
804                 cx24116_readreg(state, CX24116_REG_UCB0);
805
806         return 0;
807 }
808
809 /* Overwrite the current tuning params, we are about to tune */
810 static void cx24116_clone_params(struct dvb_frontend *fe)
811 {
812         struct cx24116_state *state = fe->demodulator_priv;
813         memcpy(&state->dcur, &state->dnxt, sizeof(state->dcur));
814 }
815
816 /* Wait for LNB */
817 static int cx24116_wait_for_lnb(struct dvb_frontend *fe)
818 {
819         struct cx24116_state *state = fe->demodulator_priv;
820         int i;
821
822         dprintk("%s() qstatus = 0x%02x\n", __func__,
823                 cx24116_readreg(state, CX24116_REG_QSTATUS));
824
825         /* Wait for up to 300 ms */
826         for (i = 0; i < 30 ; i++) {
827                 if (cx24116_readreg(state, CX24116_REG_QSTATUS) & 0x20)
828                         return 0;
829                 msleep(10);
830         }
831
832         dprintk("%s(): LNB not ready\n", __func__);
833
834         return -ETIMEDOUT; /* -EBUSY ? */
835 }
836
837 static int cx24116_set_tone(struct dvb_frontend *fe,
838         fe_sec_tone_mode_t tone)
839 {
840         struct cx24116_cmd cmd;
841         int ret;
842
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);
846                 return -EINVAL;
847         }
848
849         /* Wait for LNB ready */
850         ret = cx24116_wait_for_lnb(fe);
851         if (ret != 0)
852                 return ret;
853
854         /* Min delay time after DiSEqC send */
855         msleep(15); /* XXX determine is FW does this, see send_diseqc/burst */
856
857         /* This is always done before the tone is set */
858         cmd.args[0x00] = CMD_SET_TONEPRE;
859         cmd.args[0x01] = 0x00;
860         cmd.len = 0x02;
861         ret = cx24116_cmd_execute(fe, &cmd);
862         if (ret != 0)
863                 return ret;
864
865         /* Now we set the tone */
866         cmd.args[0x00] = CMD_SET_TONE;
867         cmd.args[0x01] = 0x00;
868         cmd.args[0x02] = 0x00;
869
870         switch (tone) {
871         case SEC_TONE_ON:
872                 dprintk("%s: setting tone on\n", __func__);
873                 cmd.args[0x03] = 0x01;
874                 break;
875         case SEC_TONE_OFF:
876                 dprintk("%s: setting tone off\n", __func__);
877                 cmd.args[0x03] = 0x00;
878                 break;
879         }
880         cmd.len = 0x04;
881
882         /* Min delay time before DiSEqC send */
883         msleep(15); /* XXX determine is FW does this, see send_diseqc/burst */
884
885         return cx24116_cmd_execute(fe, &cmd);
886 }
887
888 /* Initialise DiSEqC */
889 static int cx24116_diseqc_init(struct dvb_frontend *fe)
890 {
891         struct cx24116_state *state = fe->demodulator_priv;
892         struct cx24116_cmd cmd;
893         int ret;
894
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;
904         cmd.len = 0x08;
905         ret = cx24116_cmd_execute(fe, &cmd);
906         if (ret != 0)
907                 return ret;
908
909         /* Prepare a DiSEqC command */
910         state->dsec_cmd.args[0x00] = CMD_LNBSEND;
911
912         /* DiSEqC burst */
913         state->dsec_cmd.args[CX24116_DISEQC_BURST]  = CX24116_DISEQC_MINI_A;
914
915         /* Unknown */
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;
920
921         /* DiSEqC message length */
922         state->dsec_cmd.args[CX24116_DISEQC_MSGLEN] = 0x00;
923
924         /* Command length */
925         state->dsec_cmd.len = CX24116_DISEQC_MSGOFS;
926
927         return 0;
928 }
929
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)
933 {
934         struct cx24116_state *state = fe->demodulator_priv;
935         int i, ret;
936
937         /* Dump DiSEqC message */
938         if (debug) {
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 ", ");
944                 }
945                 printk(") toneburst=%d\n", toneburst);
946         }
947
948         /* Validate length */
949         if (d->msg_len > (CX24116_ARGLEN - CX24116_DISEQC_MSGOFS))
950                 return -EINVAL;
951
952         /* DiSEqC message */
953         for (i = 0; i < d->msg_len; i++)
954                 state->dsec_cmd.args[CX24116_DISEQC_MSGOFS + i] = d->msg[i];
955
956         /* DiSEqC message length */
957         state->dsec_cmd.args[CX24116_DISEQC_MSGLEN] = d->msg_len;
958
959         /* Command length */
960         state->dsec_cmd.len = CX24116_DISEQC_MSGOFS +
961                 state->dsec_cmd.args[CX24116_DISEQC_MSGLEN];
962
963         /* DiSEqC toneburst */
964         if (toneburst == CX24116_DISEQC_MESGCACHE)
965                 /* Message is cached */
966                 return 0;
967
968         else if (toneburst == CX24116_DISEQC_TONEOFF)
969                 /* Message is sent without burst */
970                 state->dsec_cmd.args[CX24116_DISEQC_BURST] = 0;
971
972         else if (toneburst == CX24116_DISEQC_TONECACHE) {
973                 /*
974                  * Message is sent with derived else cached burst
975                  *
976                  * WRITE PORT GROUP COMMAND 38
977                  *
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
982                  *
983                  * databyte[3]= 8421:8421
984                  *              ABCD:WXYZ
985                  *              CLR :SET
986                  *
987                  *              WX= PORT SELECT 0..3    (X=TONEBURST)
988                  *              Y = VOLTAGE             (0=13V, 1=18V)
989                  *              Z = BAND                (0=LOW, 1=HIGH(22K))
990                  */
991                 if (d->msg_len >= 4 && d->msg[2] == 0x38)
992                         state->dsec_cmd.args[CX24116_DISEQC_BURST] =
993                                 ((d->msg[3] & 4) >> 2);
994                 if (debug)
995                         dprintk("%s burst=%d\n", __func__,
996                                 state->dsec_cmd.args[CX24116_DISEQC_BURST]);
997         }
998
999         /* Wait for LNB ready */
1000         ret = cx24116_wait_for_lnb(fe);
1001         if (ret != 0)
1002                 return ret;
1003
1004         /* Wait for voltage/min repeat delay */
1005         msleep(100);
1006
1007         /* Command */
1008         ret = cx24116_cmd_execute(fe, &state->dsec_cmd);
1009         if (ret != 0)
1010                 return ret;
1011         /*
1012          * Wait for send
1013          *
1014          * Eutelsat spec:
1015          * >15ms delay          + (XXX determine if FW does this, see set_tone)
1016          *  13.5ms per byte     +
1017          * >15ms delay          +
1018          *  12.5ms burst        +
1019          * >15ms delay            (XXX determine if FW does this, see set_tone)
1020          */
1021         msleep((state->dsec_cmd.args[CX24116_DISEQC_MSGLEN] << 4) +
1022                 ((toneburst == CX24116_DISEQC_TONEOFF) ? 30 : 60));
1023
1024         return 0;
1025 }
1026
1027 /* Send DiSEqC burst */
1028 static int cx24116_diseqc_send_burst(struct dvb_frontend *fe,
1029         fe_sec_mini_cmd_t burst)
1030 {
1031         struct cx24116_state *state = fe->demodulator_priv;
1032         int ret;
1033
1034         dprintk("%s(%d) toneburst=%d\n", __func__, burst, toneburst);
1035
1036         /* DiSEqC burst */
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;
1043         else
1044                 return -EINVAL;
1045
1046         /* DiSEqC toneburst */
1047         if (toneburst != CX24116_DISEQC_MESGCACHE)
1048                 /* Burst is cached */
1049                 return 0;
1050
1051         /* Burst is to be sent with cached message */
1052
1053         /* Wait for LNB ready */
1054         ret = cx24116_wait_for_lnb(fe);
1055         if (ret != 0)
1056                 return ret;
1057
1058         /* Wait for voltage/min repeat delay */
1059         msleep(100);
1060
1061         /* Command */
1062         ret = cx24116_cmd_execute(fe, &state->dsec_cmd);
1063         if (ret != 0)
1064                 return ret;
1065
1066         /*
1067          * Wait for send
1068          *
1069          * Eutelsat spec:
1070          * >15ms delay          + (XXX determine if FW does this, see set_tone)
1071          *  13.5ms per byte     +
1072          * >15ms delay          +
1073          *  12.5ms burst        +
1074          * >15ms delay            (XXX determine if FW does this, see set_tone)
1075          */
1076         msleep((state->dsec_cmd.args[CX24116_DISEQC_MSGLEN] << 4) + 60);
1077
1078         return 0;
1079 }
1080
1081 static void cx24116_release(struct dvb_frontend *fe)
1082 {
1083         struct cx24116_state *state = fe->demodulator_priv;
1084         dprintk("%s\n", __func__);
1085         kfree(state);
1086 }
1087
1088 static struct dvb_frontend_ops cx24116_ops;
1089
1090 struct dvb_frontend *cx24116_attach(const struct cx24116_config *config,
1091         struct i2c_adapter *i2c)
1092 {
1093         struct cx24116_state *state = NULL;
1094         int ret;
1095
1096         dprintk("%s\n", __func__);
1097
1098         /* allocate memory for the internal state */
1099         state = kmalloc(sizeof(struct cx24116_state), GFP_KERNEL);
1100         if (state == NULL)
1101                 goto error1;
1102
1103         /* setup the state */
1104         memset(state, 0, sizeof(struct cx24116_state));
1105
1106         state->config = config;
1107         state->i2c = i2c;
1108
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");
1114                 goto error2;
1115         }
1116
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;
1122
1123 error2: kfree(state);
1124 error1: return NULL;
1125 }
1126 EXPORT_SYMBOL(cx24116_attach);
1127
1128 /*
1129  * Initialise or wake up device
1130  *
1131  * Power config will reset and load initial firmware if required
1132  */
1133 static int cx24116_initfe(struct dvb_frontend *fe)
1134 {
1135         struct cx24116_state *state = fe->demodulator_priv;
1136         struct cx24116_cmd cmd;
1137         int ret;
1138
1139         dprintk("%s()\n", __func__);
1140
1141         /* Power on */
1142         cx24116_writereg(state, 0xe0, 0);
1143         cx24116_writereg(state, 0xe1, 0);
1144         cx24116_writereg(state, 0xea, 0);
1145
1146         /* Firmware CMD 36: Power config */
1147         cmd.args[0x00] = CMD_TUNERSLEEP;
1148         cmd.args[0x01] = 0;
1149         cmd.len = 0x02;
1150         ret = cx24116_cmd_execute(fe, &cmd);
1151         if (ret != 0)
1152                 return ret;
1153
1154         return cx24116_diseqc_init(fe);
1155 }
1156
1157 /*
1158  * Put device to sleep
1159  */
1160 static int cx24116_sleep(struct dvb_frontend *fe)
1161 {
1162         struct cx24116_state *state = fe->demodulator_priv;
1163         struct cx24116_cmd cmd;
1164         int ret;
1165
1166         dprintk("%s()\n", __func__);
1167
1168         /* Firmware CMD 36: Power config */
1169         cmd.args[0x00] = CMD_TUNERSLEEP;
1170         cmd.args[0x01] = 1;
1171         cmd.len = 0x02;
1172         ret = cx24116_cmd_execute(fe, &cmd);
1173         if (ret != 0)
1174                 return ret;
1175
1176         /* Power off (Shutdown clocks) */
1177         cx24116_writereg(state, 0xea, 0xff);
1178         cx24116_writereg(state, 0xe1, 1);
1179         cx24116_writereg(state, 0xe0, 1);
1180
1181         return 0;
1182 }
1183
1184 static int cx24116_set_property(struct dvb_frontend *fe,
1185         struct dtv_property *tvp)
1186 {
1187         dprintk("%s(..)\n", __func__);
1188         return 0;
1189 }
1190
1191 static int cx24116_get_property(struct dvb_frontend *fe,
1192         struct dtv_property *tvp)
1193 {
1194         dprintk("%s(..)\n", __func__);
1195         return 0;
1196 }
1197
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.
1200  */
1201 static int cx24116_set_frontend(struct dvb_frontend *fe,
1202         struct dvb_frontend_parameters *p)
1203 {
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;
1209
1210         dprintk("%s()\n", __func__);
1211
1212         switch (c->delivery_system) {
1213         case SYS_DVBS:
1214                 dprintk("%s: DVB-S delivery system selected\n", __func__);
1215
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);
1220                         return -EOPNOTSUPP;
1221                 }
1222
1223                 /* Pilot doesn't exist in DVB-S, turn bit off */
1224                 state->dnxt.pilot_val = CX24116_PILOT_OFF;
1225                 retune = 1;
1226
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);
1231                         return -EOPNOTSUPP;
1232                 }
1233                 state->dnxt.rolloff_val = CX24116_ROLLOFF_035;
1234                 break;
1235
1236         case SYS_DVBS2:
1237                 dprintk("%s: DVB-S2 delivery system selected\n", __func__);
1238
1239                 /*
1240                  * NBC 8PSK/QPSK with DVB-S is supported for DVB-S2,
1241                  * but not hardware auto detection
1242                  */
1243                 if (c->modulation != PSK_8 && c->modulation != QPSK) {
1244                         dprintk("%s: unsupported modulation selected (%d)\n",
1245                                 __func__, c->modulation);
1246                         return -EOPNOTSUPP;
1247                 }
1248
1249                 switch (c->pilot) {
1250                 case PILOT_AUTO:        /* Not supported but emulated */
1251                         state->dnxt.pilot_val = (c->modulation == QPSK)
1252                                 ? CX24116_PILOT_OFF : CX24116_PILOT_ON;
1253                         retune = 2;
1254                         break;
1255                 case PILOT_OFF:
1256                         state->dnxt.pilot_val = CX24116_PILOT_OFF;
1257                         break;
1258                 case PILOT_ON:
1259                         state->dnxt.pilot_val = CX24116_PILOT_ON;
1260                         break;
1261                 default:
1262                         dprintk("%s: unsupported pilot mode selected (%d)\n",
1263                                 __func__, c->pilot);
1264                         return -EOPNOTSUPP;
1265                 }
1266
1267                 switch (c->rolloff) {
1268                 case ROLLOFF_20:
1269                         state->dnxt.rolloff_val = CX24116_ROLLOFF_020;
1270                         break;
1271                 case ROLLOFF_25:
1272                         state->dnxt.rolloff_val = CX24116_ROLLOFF_025;
1273                         break;
1274                 case ROLLOFF_35:
1275                         state->dnxt.rolloff_val = CX24116_ROLLOFF_035;
1276                         break;
1277                 case ROLLOFF_AUTO:      /* Rolloff must be explicit */
1278                 default:
1279                         dprintk("%s: unsupported rolloff selected (%d)\n",
1280                                 __func__, c->rolloff);
1281                         return -EOPNOTSUPP;
1282                 }
1283                 break;
1284
1285         default:
1286                 dprintk("%s: unsupported delivery system selected (%d)\n",
1287                         __func__, c->delivery_system);
1288                 return -EOPNOTSUPP;
1289         }
1290         state->dnxt.modulation = c->modulation;
1291         state->dnxt.frequency = c->frequency;
1292         state->dnxt.pilot = c->pilot;
1293         state->dnxt.rolloff = c->rolloff;
1294
1295         ret = cx24116_set_inversion(state, c->inversion);
1296         if (ret !=  0)
1297                 return ret;
1298
1299         /* FEC_NONE/AUTO for DVB-S2 is not supported and detected here */
1300         ret = cx24116_set_fec(state, c->modulation, c->fec_inner);
1301         if (ret !=  0)
1302                 return ret;
1303
1304         ret = cx24116_set_symbolrate(state, c->symbol_rate);
1305         if (ret !=  0)
1306                 return ret;
1307
1308         /* discard the 'current' tuning parameters and prepare to tune */
1309         cx24116_clone_params(fe);
1310
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);
1323
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);
1327
1328         /* Set/Reset B/W */
1329         cmd.args[0x00] = CMD_BANDWIDTH;
1330         cmd.args[0x01] = 0x01;
1331         cmd.len = 0x02;
1332         ret = cx24116_cmd_execute(fe, &cmd);
1333         if (ret != 0)
1334                 return ret;
1335
1336         /* Prepare a tune request */
1337         cmd.args[0x00] = CMD_TUNEREQUEST;
1338
1339         /* Frequency */
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);
1343
1344         /* Symbol Rate */
1345         cmd.args[0x04] = ((state->dcur.symbol_rate / 1000) & 0xff00) >> 8;
1346         cmd.args[0x05] = ((state->dcur.symbol_rate / 1000) & 0x00ff);
1347
1348         /* Automatic Inversion */
1349         cmd.args[0x06] = state->dcur.inversion_val;
1350
1351         /* Modulation / FEC / Pilot */
1352         cmd.args[0x07] = state->dcur.fec_val | state->dcur.pilot_val;
1353
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;
1360
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);
1369         } else {
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);
1377         }
1378
1379         cmd.len = 0x13;
1380
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.
1384          */
1385         do {
1386                 /* Reset status register */
1387                 status = cx24116_readreg(state, CX24116_REG_SSTATUS)
1388                         & CX24116_SIGNAL_MASK;
1389                 cx24116_writereg(state, CX24116_REG_SSTATUS, status);
1390
1391                 /* Tune */
1392                 ret = cx24116_cmd_execute(fe, &cmd);
1393                 if (ret != 0)
1394                         break;
1395
1396                 /*
1397                  * Wait for up to 500 ms before retrying
1398                  *
1399                  * If we are able to tune then generally it occurs within 100ms.
1400                  * If it takes longer, try a different toneburst setting.
1401                  */
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__);
1407                                 goto tuned;
1408                         }
1409                         msleep(10);
1410                 }
1411
1412                 dprintk("%s: Not tuned\n", __func__);
1413
1414                 /* Toggle pilot bit when in auto-pilot */
1415                 if (state->dcur.pilot == PILOT_AUTO)
1416                         cmd.args[0x07] ^= CX24116_PILOT_ON;
1417         } while (--retune);
1418
1419 tuned:  /* Set/Reset B/W */
1420         cmd.args[0x00] = CMD_BANDWIDTH;
1421         cmd.args[0x01] = 0x00;
1422         cmd.len = 0x02;
1423         ret = cx24116_cmd_execute(fe, &cmd);
1424         if (ret != 0)
1425                 return ret;
1426
1427         return ret;
1428 }
1429
1430 static struct dvb_frontend_ops cx24116_ops = {
1431
1432         .info = {
1433                 .name = "Conexant CX24116/CX24118",
1434                 .type = FE_QPSK,
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
1446         },
1447
1448         .release = cx24116_release,
1449
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,
1461
1462         .set_property = cx24116_set_property,
1463         .get_property = cx24116_get_property,
1464         .set_frontend = cx24116_set_frontend,
1465 };
1466
1467 MODULE_DESCRIPTION("DVB Frontend module for Conexant cx24116/cx24118 hardware");
1468 MODULE_AUTHOR("Steven Toth");
1469 MODULE_LICENSE("GPL");
1470