V4L/DVB (8994): Adjust MPEG initialization in cx24116
[linux-2.6] / 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
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 /*
22  * Updates by Darron Broad 2007.
23  *
24  * March
25  *      Fixed some bugs.
26  *      Added diseqc support.
27  *      Added corrected signal strength support.
28  *
29  * August
30  *      Sync with legacy version.
31  *      Some clean ups.
32  */
33 /* Updates by Igor Liplianin
34  *
35  * September, 9th 2008
36  *      Fixed locking on high symbol rates (>30000).
37  */
38
39 #include <linux/slab.h>
40 #include <linux/kernel.h>
41 #include <linux/module.h>
42 #include <linux/moduleparam.h>
43 #include <linux/init.h>
44 #include <linux/firmware.h>
45
46 #include "dvb_frontend.h"
47 #include "cx24116.h"
48
49 /*
50  * Fetch firmware in the following manner.
51  *
52  * #!/bin/sh
53  * wget ftp://167.206.143.11/outgoing/Oxford/88x_2_117_24275_1_INF.zip
54  * unzip 88x_2_117_24275_1_INF.zip
55  * dd if=Driver88/hcw88bda.sys of=dvb-fe-cx24116.fw skip=81768 bs=1 count=32522
56  */
57 #define CX24116_DEFAULT_FIRMWARE "dvb-fe-cx24116.fw"
58 #define CX24116_SEARCH_RANGE_KHZ 5000
59
60 /* registers (TO BE COMPLETED) */
61 #define CX24116_REG_SIGNAL (0xd5)
62
63 /* arg buffer size */
64 #define CX24116_ARGLEN (0x1e)
65
66 /* arg offset for DiSEqC */
67 #define CX24116_DISEQC_BURST  (1)
68 #define CX24116_DISEQC_ARG2_2 (2)   /* unknown value=2 */
69 #define CX24116_DISEQC_ARG3_0 (3)   /* unknown value=0 */
70 #define CX24116_DISEQC_ARG4_0 (4)   /* unknown value=0 */
71 #define CX24116_DISEQC_MSGLEN (5)
72 #define CX24116_DISEQC_MSGOFS (6)
73
74 /* DiSEqC burst */
75 #define CX24116_DISEQC_MINI_A (0)
76 #define CX24116_DISEQC_MINI_B (1)
77
78 static int debug = 0;
79 #define dprintk(args...) \
80         do { \
81                 if (debug) printk ("cx24116: " args); \
82         } while (0)
83
84 enum cmds
85 {
86         CMD_INIT_CMD10  = 0x10,
87         CMD_TUNEREQUEST = 0x11,
88         CMD_INIT_CMD13  = 0x13,
89         CMD_INIT_CMD14  = 0x14,
90         CMD_SEND_DISEQC = 0x21,
91         CMD_SET_TONEPRE = 0x22,
92         CMD_SET_TONE    = 0x23,
93 };
94
95 /* The Demod/Tuner can't easily provide these, we cache them */
96 struct cx24116_tuning
97 {
98         u32 frequency;
99         u32 symbol_rate;
100         fe_spectral_inversion_t inversion;
101         fe_code_rate_t fec;
102
103         fe_modulation_t modulation;
104
105         /* Demod values */
106         u8 fec_val;
107         u8 fec_mask;
108         u8 inversion_val;
109 };
110
111 /* Basic commands that are sent to the firmware */
112 struct cx24116_cmd
113 {
114         u8 len;
115         u8 args[CX24116_ARGLEN];
116 };
117
118 struct cx24116_state
119 {
120         struct i2c_adapter* i2c;
121         const struct cx24116_config* config;
122
123         struct dvb_frontend frontend;
124
125         struct cx24116_tuning dcur;
126         struct cx24116_tuning dnxt;
127
128         u8 skip_fw_load;
129         u8 burst;
130 };
131
132 static int cx24116_writereg(struct cx24116_state* state, int reg, int data)
133 {
134         u8 buf[] = { reg, data };
135         struct i2c_msg msg = { .addr = state->config->demod_address,
136                 .flags = 0, .buf = buf, .len = 2 };
137         int err;
138
139         if (debug>1)
140                 printk("cx24116: %s: write reg 0x%02x, value 0x%02x\n",
141                                                 __func__,reg, data);
142
143         if ((err = i2c_transfer(state->i2c, &msg, 1)) != 1) {
144                 printk("%s: writereg error(err == %i, reg == 0x%02x,"
145                          " value == 0x%02x)\n", __func__, err, reg, data);
146                 return -EREMOTEIO;
147         }
148
149         return 0;
150 }
151
152 /* Bulk byte writes to a single I2C address, for 32k firmware load */
153 static int cx24116_writeregN(struct cx24116_state* state, int reg, u8 *data, u16 len)
154 {
155         int ret = -EREMOTEIO;
156         struct i2c_msg msg;
157         u8 *buf;
158
159         buf = kmalloc(len + 1, GFP_KERNEL);
160         if (buf == NULL) {
161                 printk("Unable to kmalloc\n");
162                 ret = -ENOMEM;
163                 goto error;
164         }
165
166         *(buf) = reg;
167         memcpy(buf + 1, data, len);
168
169         msg.addr = state->config->demod_address;
170         msg.flags = 0;
171         msg.buf = buf;
172         msg.len = len + 1;
173
174         if (debug>1)
175                 printk("cx24116: %s:  write regN 0x%02x, len = %d\n",
176                                                 __func__,reg, len);
177
178         if ((ret = i2c_transfer(state->i2c, &msg, 1)) != 1) {
179                 printk("%s: writereg error(err == %i, reg == 0x%02x\n",
180                          __func__, ret, reg);
181                 ret = -EREMOTEIO;
182         }
183
184 error:
185         kfree(buf);
186
187         return ret;
188 }
189
190 static int cx24116_readreg(struct cx24116_state* state, u8 reg)
191 {
192         int ret;
193         u8 b0[] = { reg };
194         u8 b1[] = { 0 };
195         struct i2c_msg msg[] = {
196                 { .addr = state->config->demod_address, .flags = 0, .buf = b0, .len = 1 },
197                 { .addr = state->config->demod_address, .flags = I2C_M_RD, .buf = b1, .len = 1 }
198         };
199
200         ret = i2c_transfer(state->i2c, msg, 2);
201
202         if (ret != 2) {
203                 printk("%s: reg=0x%x (error=%d)\n", __func__, reg, ret);
204                 return ret;
205         }
206
207         if (debug>1)
208                 printk("cx24116: read reg 0x%02x, value 0x%02x\n",reg, b1[0]);
209
210         return b1[0];
211 }
212
213 static int cx24116_set_inversion(struct cx24116_state* state, fe_spectral_inversion_t inversion)
214 {
215         dprintk("%s(%d)\n", __func__, inversion);
216
217         switch (inversion) {
218         case INVERSION_OFF:
219                 state->dnxt.inversion_val = 0x00;
220                 break;
221         case INVERSION_ON:
222                 state->dnxt.inversion_val = 0x04;
223                 break;
224         case INVERSION_AUTO:
225                 state->dnxt.inversion_val = 0x0C;
226                 break;
227         default:
228                 return -EINVAL;
229         }
230
231         state->dnxt.inversion = inversion;
232
233         return 0;
234 }
235
236 /* A table of modulation, fec and configuration bytes for the demod.
237  * Not all S2 mmodulation schemes are support and not all rates with
238  * a scheme are support. Especially, no auto detect when in S2 mode.
239  */
240 struct cx24116_modfec {
241         fe_modulation_t modulation;
242         fe_code_rate_t fec;
243         u8 mask;        /* In DVBS mode this is used to autodetect */
244         u8 val;         /* Passed to the firmware to indicate mode selection */
245 } CX24116_MODFEC_MODES[] = {
246  /* QPSK. For unknown rates we set hardware to auto detect 0xfe 0x30 */
247  { QPSK, FEC_NONE, 0xfe, 0x30 },
248  { QPSK, FEC_1_2,  0x02, 0x2e },
249  { QPSK, FEC_2_3,  0x04, 0x2f },
250  { QPSK, FEC_3_4,  0x08, 0x30 },
251  { QPSK, FEC_4_5,  0xfe, 0x30 },
252  { QPSK, FEC_5_6,  0x20, 0x31 },
253  { QPSK, FEC_6_7,  0xfe, 0x30 },
254  { QPSK, FEC_7_8,  0x80, 0x32 },
255  { QPSK, FEC_8_9,  0xfe, 0x30 },
256  { QPSK, FEC_AUTO, 0xfe, 0x30 },
257  /* NBC-QPSK */
258  { NBC_QPSK, FEC_1_2,  0x00, 0x04 },
259  { NBC_QPSK, FEC_3_5,  0x00, 0x05 },
260  { NBC_QPSK, FEC_2_3,  0x00, 0x06 },
261  { NBC_QPSK, FEC_3_4,  0x00, 0x07 },
262  { NBC_QPSK, FEC_4_5,  0x00, 0x08 },
263  { NBC_QPSK, FEC_5_6,  0x00, 0x09 },
264  { NBC_QPSK, FEC_8_9,  0x00, 0x0a },
265  { NBC_QPSK, FEC_9_10, 0x00, 0x0b },
266  /* 8PSK */
267  { _8PSK, FEC_3_5,  0x00, 0x0c },
268  { _8PSK, FEC_2_3,  0x00, 0x0d },
269  { _8PSK, FEC_3_4,  0x00, 0x0e },
270  { _8PSK, FEC_5_6,  0x00, 0x0f },
271  { _8PSK, FEC_9_10, 0x00, 0x11 },
272 };
273
274 static int cx24116_lookup_fecmod(struct cx24116_state* state,
275         fe_modulation_t m, fe_code_rate_t f)
276 {
277         int i, ret = -EOPNOTSUPP;
278
279         for(i=0 ; i < sizeof(CX24116_MODFEC_MODES) / sizeof(struct cx24116_modfec) ; i++)
280         {
281                 if( (m == CX24116_MODFEC_MODES[i].modulation) &&
282                         (f == CX24116_MODFEC_MODES[i].fec) )
283                         {
284                                 ret = i;
285                                 break;
286                         }
287         }
288
289         return ret;
290 }
291
292 static int cx24116_set_fec(struct cx24116_state* state, fe_modulation_t mod, fe_code_rate_t fec)
293 {
294         int ret = 0;
295         dprintk("%s()\n", __func__);
296
297         ret = cx24116_lookup_fecmod(state, mod, fec);
298
299         if(ret < 0)
300                 return ret;
301
302         state->dnxt.fec_val = CX24116_MODFEC_MODES[ret].val;
303         state->dnxt.fec_mask = CX24116_MODFEC_MODES[ret].mask;
304         dprintk("%s() fec_val/mask = 0x%02x/0x%02x\n", __func__,
305                 state->dnxt.fec_val, state->dnxt.fec_mask);
306
307         return 0;
308 }
309
310 static int cx24116_set_symbolrate(struct cx24116_state* state, u32 rate)
311 {
312         int ret = 0;
313
314         dprintk("%s()\n", __func__);
315
316         state->dnxt.symbol_rate = rate;
317
318         dprintk("%s() symbol_rate = %d\n", __func__, state->dnxt.symbol_rate);
319
320         /*  check if symbol rate is within limits */
321         if ((state->dnxt.symbol_rate > state->frontend.ops.info.symbol_rate_max) ||
322             (state->dnxt.symbol_rate < state->frontend.ops.info.symbol_rate_min))
323                 ret = -EOPNOTSUPP;
324
325         return ret;
326 }
327
328 static int cx24116_load_firmware (struct dvb_frontend* fe, const struct firmware *fw);
329
330 static int cx24116_firmware_ondemand(struct dvb_frontend* fe)
331 {
332         struct cx24116_state *state = fe->demodulator_priv;
333         const struct firmware *fw;
334         int ret = 0;
335
336         dprintk("%s()\n",__func__);
337
338         if (cx24116_readreg(state, 0x20) > 0)
339         {
340
341                 if (state->skip_fw_load)
342                         return 0;
343
344                 /* Load firmware */
345                 /* request the firmware, this will block until someone uploads it */
346                 printk("%s: Waiting for firmware upload (%s)...\n", __func__, CX24116_DEFAULT_FIRMWARE);
347                 ret = request_firmware(&fw, CX24116_DEFAULT_FIRMWARE, &state->i2c->dev);
348                 printk("%s: Waiting for firmware upload(2)...\n", __func__);
349                 if (ret) {
350                         printk("%s: No firmware uploaded (timeout or file not found?)\n", __func__);
351                         return ret;
352                 }
353
354                 /* Make sure we don't recurse back through here during loading */
355                 state->skip_fw_load = 1;
356
357                 ret = cx24116_load_firmware(fe, fw);
358                 if (ret)
359                         printk("%s: Writing firmware to device failed\n", __func__);
360
361                 release_firmware(fw);
362
363                 printk("%s: Firmware upload %s\n", __func__, ret == 0 ? "complete" : "failed");
364
365                 /* Ensure firmware is always loaded if required */
366                 state->skip_fw_load = 0;
367         }
368
369         return ret;
370 }
371
372 /* Take a basic firmware command structure, format it and forward it for processing */
373 static int cx24116_cmd_execute(struct dvb_frontend* fe, struct cx24116_cmd *cmd)
374 {
375         struct cx24116_state *state = fe->demodulator_priv;
376         int i, ret;
377
378         dprintk("%s()\n", __func__);
379
380         /* Load the firmware if required */
381         if ( (ret = cx24116_firmware_ondemand(fe)) != 0)
382         {
383                 printk("%s(): Unable initialise the firmware\n", __func__);
384                 return ret;
385         }
386
387         /* Write the command */
388         for(i = 0; i < cmd->len ; i++)
389         {
390                 dprintk("%s: 0x%02x == 0x%02x\n", __func__, i, cmd->args[i]);
391                 cx24116_writereg(state, i, cmd->args[i]);
392         }
393
394         /* Start execution and wait for cmd to terminate */
395         cx24116_writereg(state, 0x1f, 0x01);
396         while( cx24116_readreg(state, 0x1f) )
397         {
398                 msleep(10);
399                 if(i++ > 64)
400                 {
401                         /* Avoid looping forever if the firmware does no respond */
402                         printk("%s() Firmware not responding\n", __func__);
403                         return -EREMOTEIO;
404                 }
405         }
406         return 0;
407 }
408
409 static int cx24116_load_firmware (struct dvb_frontend* fe, const struct firmware *fw)
410 {
411         struct cx24116_state* state = fe->demodulator_priv;
412         struct cx24116_cmd cmd;
413         int ret;
414
415         dprintk("%s\n", __func__);
416         dprintk("Firmware is %zu bytes (%02x %02x .. %02x %02x)\n"
417                         ,fw->size
418                         ,fw->data[0]
419                         ,fw->data[1]
420                         ,fw->data[ fw->size-2 ]
421                         ,fw->data[ fw->size-1 ]
422                         );
423
424         /* Toggle 88x SRST pin to reset demod */
425         if (state->config->reset_device)
426                 state->config->reset_device(fe);
427
428         /* Begin the firmware load process */
429         /* Prepare the demod, load the firmware, cleanup after load */
430         cx24116_writereg(state, 0xF1, 0x08);
431         cx24116_writereg(state, 0xF2, cx24116_readreg(state, 0xF2) | 0x03);
432         cx24116_writereg(state, 0xF3, 0x46);
433         cx24116_writereg(state, 0xF9, 0x00);
434
435         cx24116_writereg(state, 0xF0, 0x03);
436         cx24116_writereg(state, 0xF4, 0x81);
437         cx24116_writereg(state, 0xF5, 0x00);
438         cx24116_writereg(state, 0xF6, 0x00);
439
440         /* write the entire firmware as one transaction */
441         cx24116_writeregN(state, 0xF7, fw->data, fw->size);
442
443         cx24116_writereg(state, 0xF4, 0x10);
444         cx24116_writereg(state, 0xF0, 0x00);
445         cx24116_writereg(state, 0xF8, 0x06);
446
447         /* Firmware CMD 10: Chip config? */
448         cmd.args[0x00] = CMD_INIT_CMD10;
449         cmd.args[0x01] = 0x05;
450         cmd.args[0x02] = 0xdc;
451         cmd.args[0x03] = 0xda;
452         cmd.args[0x04] = 0xae;
453         cmd.args[0x05] = 0xaa;
454         cmd.args[0x06] = 0x04;
455         cmd.args[0x07] = 0x9d;
456         cmd.args[0x08] = 0xfc;
457         cmd.args[0x09] = 0x06;
458         cmd.len= 0x0a;
459         ret = cx24116_cmd_execute(fe, &cmd);
460         if (ret != 0)
461                 return ret;
462
463         cx24116_writereg(state, 0x9d, 0x00);
464
465         /* Firmware CMD 14: Unknown */
466         cmd.args[0x00] = CMD_INIT_CMD14;
467         cmd.args[0x01] = 0x00;
468         cmd.args[0x02] = 0x00;
469         cmd.len= 0x03;
470         ret = cx24116_cmd_execute(fe, &cmd);
471         if (ret != 0)
472                 return ret;
473
474         cx24116_writereg(state, 0xe5, 0x00);
475
476         /* Firmware CMD 13: Unknown - Firmware config? */
477         cmd.args[0x00] = CMD_INIT_CMD13;
478         cmd.args[0x01] = 0x01;
479         cmd.args[0x02] = 0x75;
480         cmd.args[0x03] = 0x00;
481         if (state->config->mpg_clk_pos_pol)
482                 cmd.args[0x04] = state->config->mpg_clk_pos_pol;
483         else
484                 cmd.args[0x04] = 0x02;
485         cmd.args[0x05] = 0x00;
486         cmd.len= 0x06;
487         ret = cx24116_cmd_execute(fe, &cmd);
488         if (ret != 0)
489                 return ret;
490
491         return 0;
492 }
493
494 static int cx24116_set_voltage(struct dvb_frontend* fe, fe_sec_voltage_t voltage)
495 {
496         /* The isl6421 module will override this function in the fops. */
497         dprintk("%s() This should never appear if the isl6421 module is loaded correctly\n",__func__);
498
499         return -EOPNOTSUPP;
500 }
501
502 static int cx24116_read_status(struct dvb_frontend* fe, fe_status_t* status)
503 {
504         struct cx24116_state *state = fe->demodulator_priv;
505
506         int lock = cx24116_readreg(state, 0x9d);
507
508         dprintk("%s: status = 0x%02x\n", __func__, lock);
509
510         *status = 0;
511
512         if (lock & 0x01)
513                 *status |= FE_HAS_SIGNAL;
514         if (lock & 0x02)
515                 *status |= FE_HAS_CARRIER;
516         if (lock & 0x04)
517                 *status |= FE_HAS_VITERBI;
518         if (lock & 0x08)
519                 *status |= FE_HAS_SYNC | FE_HAS_LOCK;
520
521         return 0;
522 }
523
524 /* TODO: Not clear how we do this */
525 static int cx24116_read_ber(struct dvb_frontend* fe, u32* ber)
526 {
527         //struct cx24116_state *state = fe->demodulator_priv;
528         dprintk("%s()\n", __func__);
529         *ber = 0;
530
531         return 0;
532 }
533
534 /* Signal strength (0..100)% = (sig & 0xf0) * 10 + (sig & 0x0f) * 10 / 16 */
535 static int cx24116_read_signal_strength(struct dvb_frontend* fe, u16* signal_strength)
536 {
537         struct cx24116_state *state = fe->demodulator_priv;
538         u8 strength_reg;
539         static const u32 strength_tab[] = { /* 10 x Table (rounded up) */
540                 0x00000,0x0199A,0x03333,0x04ccD,0x06667,0x08000,0x0999A,0x0b333,0x0cccD,0x0e667,
541                 0x10000,0x1199A,0x13333,0x14ccD,0x16667,0x18000 };
542
543         dprintk("%s()\n", __func__);
544
545         strength_reg = cx24116_readreg(state, CX24116_REG_SIGNAL);
546
547         if(strength_reg < 0xa0)
548                 *signal_strength = strength_tab [ ( strength_reg & 0xf0 )   >> 4 ] +
549                                 ( strength_tab [ ( strength_reg & 0x0f ) ] >> 4 );
550         else
551                 *signal_strength = 0xffff;
552
553         dprintk("%s: Signal strength (raw / cooked) = (0x%02x / 0x%04x)\n",
554                 __func__,strength_reg,*signal_strength);
555
556         return 0;
557 }
558
559 /* TODO: Not clear how we do this */
560 static int cx24116_read_snr(struct dvb_frontend* fe, u16* snr)
561 {
562         //struct cx24116_state *state = fe->demodulator_priv;
563         dprintk("%s()\n", __func__);
564         *snr = 0;
565
566         return 0;
567 }
568
569 /* TODO: Not clear how we do this */
570 static int cx24116_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
571 {
572         //struct cx24116_state *state = fe->demodulator_priv;
573         dprintk("%s()\n", __func__);
574         *ucblocks = 0;
575
576         return 0;
577 }
578
579 /* Overwrite the current tuning params, we are about to tune */
580 static void cx24116_clone_params(struct dvb_frontend* fe)
581 {
582         struct cx24116_state *state = fe->demodulator_priv;
583         memcpy(&state->dcur, &state->dnxt, sizeof(state->dcur));
584 }
585
586 static int cx24116_set_tone(struct dvb_frontend* fe, fe_sec_tone_mode_t tone)
587 {
588         struct cx24116_cmd cmd;
589         int ret;
590
591         dprintk("%s(%d)\n", __func__, tone);
592         if ( (tone != SEC_TONE_ON) && (tone != SEC_TONE_OFF) ) {
593                 printk("%s: Invalid, tone=%d\n", __func__, tone);
594                 return -EINVAL;
595         }
596
597         /* This is always done before the tone is set */
598         cmd.args[0x00] = CMD_SET_TONEPRE;
599         cmd.args[0x01] = 0x00;
600         cmd.len= 0x02;
601         ret = cx24116_cmd_execute(fe, &cmd);
602         if (ret != 0)
603                 return ret;
604
605         /* Now we set the tone */
606         cmd.args[0x00] = CMD_SET_TONE;
607         cmd.args[0x01] = 0x00;
608         cmd.args[0x02] = 0x00;
609
610         switch (tone) {
611         case SEC_TONE_ON:
612                 dprintk("%s: setting tone on\n", __func__);
613                 cmd.args[0x03] = 0x01;
614                 break;
615         case SEC_TONE_OFF:
616                 dprintk("%s: setting tone off\n",__func__);
617                 cmd.args[0x03] = 0x00;
618                 break;
619         }
620         cmd.len= 0x04;
621
622         return cx24116_cmd_execute(fe, &cmd);
623 }
624
625 /* Initialise DiSEqC */
626 static int cx24116_diseqc_init(struct dvb_frontend* fe)
627 {
628         struct cx24116_state *state = fe->demodulator_priv;
629
630         /* Default DiSEqC burst state */
631         state->burst = CX24116_DISEQC_MINI_A;
632
633         return 0;
634 }
635
636 /* Send DiSEqC message with derived burst (hack) || previous burst */
637 static int cx24116_send_diseqc_msg(struct dvb_frontend* fe, struct dvb_diseqc_master_cmd *d)
638 {
639         struct cx24116_state *state = fe->demodulator_priv;
640         struct cx24116_cmd cmd;
641         int i, ret;
642
643         /* Dump DiSEqC message */
644         if (debug) {
645                 printk("cx24116: %s(", __func__);
646                 for(i = 0 ; i < d->msg_len ;) {
647                         printk("0x%02x", d->msg[i]);
648                         if(++i < d->msg_len)
649                                 printk(", ");
650                 }
651                 printk(")\n");
652         }
653
654         if(d->msg_len > (CX24116_ARGLEN - CX24116_DISEQC_MSGOFS))
655                 return -EINVAL;
656
657         cmd.args[0x00] = CMD_SEND_DISEQC;
658         cmd.args[CX24116_DISEQC_ARG2_2] = 0x02;
659         cmd.args[CX24116_DISEQC_ARG3_0] = 0x00;
660         cmd.args[CX24116_DISEQC_ARG4_0] = 0x00;
661
662         /* DiSEqC message */
663         for (i = 0; i < d->msg_len; i++)
664                 cmd.args[CX24116_DISEQC_MSGOFS + i] = d->msg[i];
665
666         /* Hack: Derive burst from command else use previous burst */
667         if(d->msg_len >= 4 && d->msg[2] == 0x38)
668                 cmd.args[CX24116_DISEQC_BURST] = (d->msg[3] >> 2) & 1;
669         else
670                 cmd.args[CX24116_DISEQC_BURST] = state->burst;
671
672         cmd.args[CX24116_DISEQC_MSGLEN] = d->msg_len;
673         cmd.len = CX24116_DISEQC_MSGOFS + d->msg_len;
674
675         ret = cx24116_cmd_execute(fe, &cmd);
676
677         /* Firmware command duration is unknown, so guess...
678          *
679          * Eutelsat spec:
680          * >15ms delay          +
681          *  13.5ms per byte     +
682          * >15ms delay          +
683          *  12.5ms burst        +
684          * >15ms delay
685          */
686         if(ret == 0)
687                 msleep( (cmd.args[CX24116_DISEQC_MSGLEN] << 4) + 60 );
688
689         return ret;
690 }
691
692 /* Send DiSEqC burst */
693 static int cx24116_diseqc_send_burst(struct dvb_frontend* fe, fe_sec_mini_cmd_t burst)
694 {
695         struct cx24116_state *state = fe->demodulator_priv;
696         struct cx24116_cmd cmd;
697         int ret;
698
699         dprintk("%s(%d)\n",__func__,(int)burst);
700
701         cmd.args[0x00] = CMD_SEND_DISEQC;
702         cmd.args[CX24116_DISEQC_ARG2_2] = 0x02;
703         cmd.args[CX24116_DISEQC_ARG3_0] = 0x00;
704         cmd.args[CX24116_DISEQC_ARG4_0] = 0x00;
705
706         if (burst == SEC_MINI_A)
707                 cmd.args[CX24116_DISEQC_BURST] = CX24116_DISEQC_MINI_A;
708         else if(burst == SEC_MINI_B)
709                 cmd.args[CX24116_DISEQC_BURST] = CX24116_DISEQC_MINI_B;
710         else
711                 return -EINVAL;
712
713         /* Cache as previous burst state */
714         state->burst= cmd.args[CX24116_DISEQC_BURST];
715
716         cmd.args[CX24116_DISEQC_MSGLEN] = 0x00;
717         cmd.len= CX24116_DISEQC_MSGOFS;
718
719         ret= cx24116_cmd_execute(fe, &cmd);
720
721         /* Firmware command duration is unknown, so guess... */
722         if(ret == 0)
723                 msleep(60);
724
725         return ret;
726 }
727
728 static void cx24116_release(struct dvb_frontend* fe)
729 {
730         struct cx24116_state* state = fe->demodulator_priv;
731         dprintk("%s\n",__func__);
732         kfree(state);
733 }
734
735 static struct dvb_frontend_ops cx24116_ops;
736
737 struct dvb_frontend* cx24116_attach(const struct cx24116_config* config,
738                                     struct i2c_adapter* i2c)
739 {
740         struct cx24116_state* state = NULL;
741         int ret;
742
743         dprintk("%s\n",__func__);
744
745         /* allocate memory for the internal state */
746         state = kmalloc(sizeof(struct cx24116_state), GFP_KERNEL);
747         if (state == NULL) {
748                 printk("Unable to kmalloc\n");
749                 goto error;
750         }
751
752         /* setup the state */
753         memset(state, 0, sizeof(struct cx24116_state));
754
755         state->config = config;
756         state->i2c = i2c;
757
758         /* check if the demod is present */
759         ret = (cx24116_readreg(state, 0xFF) << 8) | cx24116_readreg(state, 0xFE);
760         if (ret != 0x0501) {
761                 printk("Invalid probe, probably not a CX24116 device\n");
762                 goto error;
763         }
764
765         /* create dvb_frontend */
766         memcpy(&state->frontend.ops, &cx24116_ops, sizeof(struct dvb_frontend_ops));
767         state->frontend.demodulator_priv = state;
768         return &state->frontend;
769
770 error:
771         kfree(state);
772
773         return NULL;
774 }
775
776 static int cx24116_get_params(struct dvb_frontend* fe)
777 {
778         struct cx24116_state *state = fe->demodulator_priv;
779         struct tv_frontend_properties *cache = &fe->tv_property_cache;
780
781         dprintk("%s()\n",__func__);
782
783         cache->frequency = state->dcur.frequency;
784         cache->inversion = state->dcur.inversion;
785         cache->modulation = state->dcur.modulation;
786         cache->fec_inner = state->dcur.fec;
787         cache->symbol_rate = state->dcur.symbol_rate;
788
789         return 0;
790 }
791
792 static int cx24116_initfe(struct dvb_frontend* fe)
793 {
794         dprintk("%s()\n",__func__);
795
796         return cx24116_diseqc_init(fe);
797 }
798
799 static int cx24116_set_property(struct dvb_frontend *fe, tv_property_t* tvp)
800 {
801         dprintk("%s(..)\n", __func__);
802         return 0;
803 }
804
805 static int cx24116_set_params(struct dvb_frontend *fe)
806 {
807         dprintk("%s(..) We were notified that a tune request may occur\n", __func__);
808         return 0;
809 }
810
811 /* dvb-core told us to tune, the tv property cache will be complete,
812  * it's safe for is to pull values and use them for tuning purposes.
813  */
814 static int cx24116_set_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters *p)
815 {
816         struct cx24116_state *state = fe->demodulator_priv;
817         struct tv_frontend_properties *c = &fe->tv_property_cache;
818         struct cx24116_cmd cmd;
819         fe_status_t tunerstat;
820         int ret, above30msps;
821         u8 retune=4;
822
823         dprintk("%s()\n",__func__);
824
825         state->dnxt.modulation = c->modulation;
826         state->dnxt.frequency = c->frequency;
827
828         if ((ret = cx24116_set_inversion(state, c->inversion)) !=  0)
829                 return ret;
830
831         if ((ret = cx24116_set_fec(state, c->modulation, c->fec_inner)) !=  0)
832                 return ret;
833
834         if ((ret = cx24116_set_symbolrate(state, c->symbol_rate)) !=  0)
835                 return ret;
836
837         /* discard the 'current' tuning parameters and prepare to tune */
838         cx24116_clone_params(fe);
839
840         dprintk("%s:   frequency   = %d\n", __func__, state->dcur.frequency);
841         dprintk("%s:   symbol_rate = %d\n", __func__, state->dcur.symbol_rate);
842         dprintk("%s:   FEC         = %d (mask/val = 0x%02x/0x%02x)\n", __func__,
843                 state->dcur.fec, state->dcur.fec_mask, state->dcur.fec_val);
844         dprintk("%s:   Inversion   = %d (val = 0x%02x)\n", __func__,
845                 state->dcur.inversion, state->dcur.inversion_val);
846
847         if (state->config->set_ts_params)
848                 state->config->set_ts_params(fe, 0);
849
850         above30msps = (state->dcur.symbol_rate > 30000000);
851
852         if (above30msps){
853                 cx24116_writereg(state, 0xF9, 0x01);
854                 cx24116_writereg(state, 0xF3, 0x44);
855         } else {
856                 cx24116_writereg(state, 0xF9, 0x00);
857                 cx24116_writereg(state, 0xF3, 0x46);
858         }
859
860         /* Prepare a tune request */
861         cmd.args[0x00] = CMD_TUNEREQUEST;
862
863         /* Frequency */
864         cmd.args[0x01] = (state->dcur.frequency & 0xff0000) >> 16;
865         cmd.args[0x02] = (state->dcur.frequency & 0x00ff00) >> 8;
866         cmd.args[0x03] = (state->dcur.frequency & 0x0000ff);
867
868         /* Symbol Rate */
869         cmd.args[0x04] = ((state->dcur.symbol_rate / 1000) & 0xff00) >> 8;
870         cmd.args[0x05] = ((state->dcur.symbol_rate / 1000) & 0x00ff);
871
872         /* Automatic Inversion */
873         cmd.args[0x06] = state->dcur.inversion_val;
874
875         /* Modulation / FEC & Pilot Off */
876         cmd.args[0x07] = state->dcur.fec_val;
877
878         if (c->pilot == PILOT_ON)
879                 cmd.args[0x07] |= 0x40;
880
881         cmd.args[0x08] = CX24116_SEARCH_RANGE_KHZ >> 8;
882         cmd.args[0x09] = CX24116_SEARCH_RANGE_KHZ & 0xff;
883         cmd.args[0x0a] = 0x00;
884         cmd.args[0x0b] = 0x00;
885         cmd.args[0x0c] = 0x02;
886         cmd.args[0x0d] = state->dcur.fec_mask;
887
888         if (above30msps){
889                 cmd.args[0x0e] = 0x04;
890                 cmd.args[0x0f] = 0x00;
891                 cmd.args[0x10] = 0x01;
892                 cmd.args[0x11] = 0x77;
893                 cmd.args[0x12] = 0x36;
894         } else {
895                 cmd.args[0x0e] = 0x06;
896                 cmd.args[0x0f] = 0x00;
897                 cmd.args[0x10] = 0x00;
898                 cmd.args[0x11] = 0xFA;
899                 cmd.args[0x12] = 0x24;
900         }
901
902         cmd.len= 0x13;
903
904         /* We need to support pilot and non-pilot tuning in the
905          * driver automatically. This is a workaround for because
906          * the demod does not support autodetect.
907          */
908         do {
909                 /* Reset status register? */
910                 cx24116_writereg(state, 0x9d, 0xc1);
911
912                 /* Tune */
913                 ret = cx24116_cmd_execute(fe, &cmd);
914                 if( ret != 0 )
915                         break;
916
917                 /* The hardware can take time to lock, wait a while */
918                 msleep(500);
919
920                 cx24116_read_status(fe, &tunerstat);
921                 if(tunerstat & FE_HAS_SIGNAL) {
922                         if(tunerstat & FE_HAS_SYNC)
923                                 /* Tuned */
924                                 break;
925                         else if(c->pilot == PILOT_AUTO)
926                                 /* Toggle pilot bit */
927                                 cmd.args[0x07] ^= 0x40;
928                 }
929         }
930         while(--retune);
931
932         return ret;
933 }
934
935 static struct dvb_frontend_ops cx24116_ops = {
936
937         .info = {
938                 .name = "Conexant CX24116/CX24118",
939                 .type = FE_QPSK,
940                 .frequency_min = 950000,
941                 .frequency_max = 2150000,
942                 .frequency_stepsize = 1011, /* kHz for QPSK frontends */
943                 .frequency_tolerance = 5000,
944                 .symbol_rate_min = 1000000,
945                 .symbol_rate_max = 45000000,
946                 .caps = FE_CAN_INVERSION_AUTO |
947                         FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
948                         FE_CAN_FEC_4_5 | FE_CAN_FEC_5_6 | FE_CAN_FEC_6_7 |
949                         FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
950                         FE_CAN_QPSK | FE_CAN_RECOVER
951         },
952
953         .release = cx24116_release,
954
955         .init = cx24116_initfe,
956         .read_status = cx24116_read_status,
957         .read_ber = cx24116_read_ber,
958         .read_signal_strength = cx24116_read_signal_strength,
959         .read_snr = cx24116_read_snr,
960         .read_ucblocks = cx24116_read_ucblocks,
961         .set_tone = cx24116_set_tone,
962         .set_voltage = cx24116_set_voltage,
963         .diseqc_send_master_cmd = cx24116_send_diseqc_msg,
964         .diseqc_send_burst = cx24116_diseqc_send_burst,
965
966         .set_property = cx24116_set_property,
967         .set_params = cx24116_set_params,
968         .set_frontend = cx24116_set_frontend,
969 };
970
971 module_param(debug, int, 0644);
972 MODULE_PARM_DESC(debug, "Activates frontend debugging (default:0)");
973
974 MODULE_DESCRIPTION("DVB Frontend module for Conexant cx24116/cx24118 hardware");
975 MODULE_AUTHOR("Steven Toth");
976 MODULE_LICENSE("GPL");
977
978 EXPORT_SYMBOL(cx24116_attach);