[media] cx24120: minor checkpatch fixes
[firefly-linux-kernel-4.4.55.git] / drivers / media / dvb-frontends / cx24120.c
1 /*
2     Conexant cx24120/cx24118 - DVBS/S2 Satellite demod/tuner driver
3
4     Copyright (C) 2008 Patrick Boettcher <pb@linuxtv.org>
5     Copyright (C) 2009 Sergey Tyurin <forum.free-x.de>
6     Updated 2012 by Jannis Achstetter <jannis_achstetter@web.de>
7     Copyright (C) 2015 Jemma Denson <jdenson@gmail.com>
8         April 2015
9             Refactored & simplified driver
10             Updated to work with delivery system supplied by DVBv5
11             Add frequency, fec & pilot to get_frontend
12
13         Cards supported: Technisat Skystar S2
14
15     This program is free software; you can redistribute it and/or modify
16     it under the terms of the GNU General Public License as published by
17     the Free Software Foundation; either version 2 of the License, or
18     (at your option) any later version.
19
20     This program is distributed in the hope that it will be useful,
21     but WITHOUT ANY WARRANTY; without even the implied warranty of
22     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23     GNU General Public License for more details.
24 */
25
26 #include <linux/slab.h>
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/moduleparam.h>
30 #include <linux/init.h>
31 #include <linux/firmware.h>
32 #include "dvb_frontend.h"
33 #include "cx24120.h"
34
35 #define CX24120_SEARCH_RANGE_KHZ 5000
36 #define CX24120_FIRMWARE "dvb-fe-cx24120-1.20.58.2.fw"
37
38 /* cx24120 i2c registers  */
39 #define CX24120_REG_CMD_START   (0x00)          /* write cmd_id */
40 #define CX24120_REG_CMD_ARGS    (0x01)          /* write command arguments */
41 #define CX24120_REG_CMD_END     (0x1f)          /* write 0x01 for end */
42
43 #define CX24120_REG_MAILBOX     (0x33)
44 #define CX24120_REG_FREQ3       (0x34)          /* frequency */
45 #define CX24120_REG_FREQ2       (0x35)
46 #define CX24120_REG_FREQ1       (0x36)
47
48 #define CX24120_REG_FECMODE     (0x39)          /* FEC status */
49 #define CX24120_REG_STATUS      (0x3a)          /* Tuner status */
50 #define CX24120_REG_SIGSTR_H    (0x3a)          /* Signal strength high */
51 #define CX24120_REG_SIGSTR_L    (0x3b)          /* Signal strength low byte */
52 #define CX24120_REG_QUALITY_H   (0x40)          /* SNR high byte */
53 #define CX24120_REG_QUALITY_L   (0x41)          /* SNR low byte */
54
55 #define CX24120_REG_BER_HH      (0x47)          /* BER high byte of high word */
56 #define CX24120_REG_BER_HL      (0x48)          /* BER low byte of high word */
57 #define CX24120_REG_BER_LH      (0x49)          /* BER high byte of low word */
58 #define CX24120_REG_BER_LL      (0x4a)          /* BER low byte of low word */
59
60 #define CX24120_REG_UCB_H       (0x50)          /* UCB high byte */
61 #define CX24120_REG_UCB_L       (0x51)          /* UCB low byte  */
62
63 #define CX24120_REG_CLKDIV      (0xe6)
64 #define CX24120_REG_RATEDIV     (0xf0)
65
66 #define CX24120_REG_REVISION    (0xff)          /* Chip revision (ro) */
67
68
69 /* Command messages */
70 enum command_message_id {
71         CMD_VCO_SET             = 0x10,         /* cmd.len = 12; */
72         CMD_TUNEREQUEST         = 0x11,         /* cmd.len = 15; */
73
74         CMD_MPEG_ONOFF          = 0x13,         /* cmd.len = 4; */
75         CMD_MPEG_INIT           = 0x14,         /* cmd.len = 7; */
76         CMD_BANDWIDTH           = 0x15,         /* cmd.len = 12; */
77         CMD_CLOCK_READ          = 0x16,         /* read clock */
78         CMD_CLOCK_SET           = 0x17,         /* cmd.len = 10; */
79
80         CMD_DISEQC_MSG1         = 0x20,         /* cmd.len = 11; */
81         CMD_DISEQC_MSG2         = 0x21,         /* cmd.len = d->msg_len + 6; */
82         CMD_SETVOLTAGE          = 0x22,         /* cmd.len = 2; */
83         CMD_SETTONE             = 0x23,         /* cmd.len = 4; */
84         CMD_DISEQC_BURST        = 0x24,         /* cmd.len not used !!! */
85
86         CMD_READ_SNR            = 0x1a,         /* Read signal strength */
87         CMD_START_TUNER         = 0x1b,         /* ??? */
88
89         CMD_FWVERSION           = 0x35,
90
91         CMD_TUNER_INIT          = 0x3c,         /* cmd.len = 0x03; */
92 };
93
94 #define CX24120_MAX_CMD_LEN     30
95
96 /* pilot mask */
97 #define CX24120_PILOT_OFF       (0x00)
98 #define CX24120_PILOT_ON        (0x40)
99 #define CX24120_PILOT_AUTO      (0x80)
100
101 /* signal status */
102 #define CX24120_HAS_SIGNAL      (0x01)
103 #define CX24120_HAS_CARRIER     (0x02)
104 #define CX24120_HAS_VITERBI     (0x04)
105 #define CX24120_HAS_LOCK        (0x08)
106 #define CX24120_HAS_UNK1        (0x10)
107 #define CX24120_HAS_UNK2        (0x20)
108 #define CX24120_STATUS_MASK     (0x0f)
109 #define CX24120_SIGNAL_MASK     (0xc0)
110
111 #define info(args...) pr_info("cx24120: " args)
112 #define err(args...)  pr_err("cx24120: ### ERROR: " args)
113
114 /* The Demod/Tuner can't easily provide these, we cache them */
115 struct cx24120_tuning {
116         u32 frequency;
117         u32 symbol_rate;
118         fe_spectral_inversion_t inversion;
119         fe_code_rate_t fec;
120
121         fe_delivery_system_t delsys;
122         fe_modulation_t modulation;
123         fe_pilot_t pilot;
124
125         /* Demod values */
126         u8 fec_val;
127         u8 fec_mask;
128         u8 clkdiv;
129         u8 ratediv;
130         u8 inversion_val;
131         u8 pilot_val;
132 };
133
134
135 /* Private state */
136 struct cx24120_state {
137         struct i2c_adapter *i2c;
138         const struct cx24120_config *config;
139         struct dvb_frontend frontend;
140
141         u8 cold_init;
142         u8 mpeg_enabled;
143
144         /* current and next tuning parameters */
145         struct cx24120_tuning dcur;
146         struct cx24120_tuning dnxt;
147 };
148
149
150 /* Command message to firmware */
151 struct cx24120_cmd {
152         u8 id;
153         u8 len;
154         u8 arg[CX24120_MAX_CMD_LEN];
155 };
156
157
158 /* Read single register */
159 static int cx24120_readreg(struct cx24120_state *state, u8 reg)
160 {
161         int ret;
162         u8 buf = 0;
163         struct i2c_msg msg[] = {
164                 {       .addr = state->config->i2c_addr,
165                         .flags = 0,
166                         .len = 1,
167                         .buf = &reg     },
168
169                 {       .addr = state->config->i2c_addr,
170                         .flags = I2C_M_RD,
171                         .len = 1,
172                         .buf = &buf     }
173         };
174         ret = i2c_transfer(state->i2c, msg, 2);
175         if (ret != 2) {
176                 err("Read error: reg=0x%02x, ret=0x%02x)\n", reg, ret);
177                 return ret;
178         }
179
180         dev_dbg(&state->i2c->dev, "%s: reg=0x%02x; data=0x%02x\n",
181                 __func__, reg, buf);
182
183         return buf;
184 }
185
186
187 /* Write single register */
188 static int cx24120_writereg(struct cx24120_state *state, u8 reg, u8 data)
189 {
190         u8 buf[] = { reg, data };
191         struct i2c_msg msg = {
192                 .addr = state->config->i2c_addr,
193                 .flags = 0,
194                 .buf = buf,
195                 .len = 2 };
196         int ret;
197
198         ret = i2c_transfer(state->i2c, &msg, 1);
199         if (ret != 1) {
200                 err("Write error: i2c_write error(err == %i, 0x%02x: 0x%02x)\n",
201                                  ret, reg, data);
202                 return ret;
203         }
204
205         dev_dbg(&state->i2c->dev, "%s: reg=0x%02x; data=0x%02x\n",
206                 __func__, reg, data);
207
208         return 0;
209 }
210
211
212 /* Write multiple registers */
213 static int cx24120_writeregN(struct cx24120_state *state,
214                         u8 reg, const u8 *values, u16 len, u8 incr)
215 {
216         int ret;
217         u8 buf[5]; /* maximum 4 data bytes at once - flexcop limitation
218                         (very limited i2c-interface this one) */
219
220         struct i2c_msg msg = {
221                 .addr = state->config->i2c_addr,
222                 .flags = 0,
223                 .buf = buf,
224                 .len = len };
225
226         while (len) {
227                 buf[0] = reg;
228                 msg.len = len > 4 ? 4 : len;
229                 memcpy(&buf[1], values, msg.len);
230
231                 len  -= msg.len;                /* data length revers counter */
232                 values += msg.len;              /* incr data pointer */
233
234                 if (incr)
235                         reg += msg.len;
236                 msg.len++;                      /* don't forget the addr byte */
237
238                 ret = i2c_transfer(state->i2c, &msg, 1);
239                 if (ret != 1) {
240                         err("i2c_write error(err == %i, 0x%02x)\n", ret, reg);
241                         return ret;
242                 }
243
244                 dev_dbg(&state->i2c->dev,
245                         "%s: reg=0x%02x; data=0x%02x,0x%02x,0x%02x,0x%02x\n",
246                         __func__, reg,
247                         buf[1], buf[2], buf[3], buf[4]);
248
249         }
250
251         return 0;
252 }
253
254
255 static struct dvb_frontend_ops cx24120_ops;
256
257 struct dvb_frontend *cx24120_attach(const struct cx24120_config *config,
258                         struct i2c_adapter *i2c)
259 {
260         struct cx24120_state *state = NULL;
261         int demod_rev;
262
263         info("Conexant cx24120/cx24118 - DVBS/S2 Satellite demod/tuner\n");
264         state = kzalloc(sizeof(struct cx24120_state),
265                                                 GFP_KERNEL);
266         if (state == NULL) {
267                 err("Unable to allocate memory for cx24120_state\n");
268                 goto error;
269         }
270
271         /* setup the state */
272         state->config = config;
273         state->i2c = i2c;
274
275         /* check if the demod is present and has proper type */
276         demod_rev = cx24120_readreg(state, CX24120_REG_REVISION);
277         switch (demod_rev) {
278         case 0x07:
279                 info("Demod cx24120 rev. 0x07 detected.\n");
280                 break;
281         case 0x05:
282                 info("Demod cx24120 rev. 0x05 detected.\n");
283                 break;
284         default:
285                 err("Unsupported demod revision: 0x%x detected.\n",
286                         demod_rev);
287                 goto error;
288         }
289
290         /* create dvb_frontend */
291         state->cold_init = 0;
292         memcpy(&state->frontend.ops, &cx24120_ops,
293                         sizeof(struct dvb_frontend_ops));
294         state->frontend.demodulator_priv = state;
295
296         info("Conexant cx24120/cx24118 attached.\n");
297         return &state->frontend;
298
299 error:
300         kfree(state);
301         return NULL;
302 }
303 EXPORT_SYMBOL(cx24120_attach);
304
305 static int cx24120_test_rom(struct cx24120_state *state)
306 {
307         int err, ret;
308
309         err = cx24120_readreg(state, 0xfd);
310         if (err & 4) {
311                 ret = cx24120_readreg(state, 0xdf) & 0xfe;
312                 err = cx24120_writereg(state, 0xdf, ret);
313         }
314         return err;
315 }
316
317
318 static int cx24120_read_snr(struct dvb_frontend *fe, u16 *snr)
319 {
320         struct cx24120_state *state = fe->demodulator_priv;
321
322         *snr =  (cx24120_readreg(state, CX24120_REG_QUALITY_H)<<8) |
323                 (cx24120_readreg(state, CX24120_REG_QUALITY_L));
324         dev_dbg(&state->i2c->dev, "%s: read SNR index = %d\n",
325                         __func__, *snr);
326
327         return 0;
328 }
329
330
331 static int cx24120_read_ber(struct dvb_frontend *fe, u32 *ber)
332 {
333         struct cx24120_state *state = fe->demodulator_priv;
334
335         *ber =  (cx24120_readreg(state, CX24120_REG_BER_HH) << 24)      |
336                 (cx24120_readreg(state, CX24120_REG_BER_HL) << 16)      |
337                 (cx24120_readreg(state, CX24120_REG_BER_LH)  << 8)      |
338                  cx24120_readreg(state, CX24120_REG_BER_LL);
339         dev_dbg(&state->i2c->dev, "%s: read BER index = %d\n",
340                         __func__, *ber);
341
342         return 0;
343 }
344
345 static int cx24120_msg_mpeg_output_global_config(struct cx24120_state *state,
346                         u8 flag);
347
348 /* Check if we're running a command that needs to disable mpeg out */
349 static void cx24120_check_cmd(struct cx24120_state *state, u8 id)
350 {
351         switch (id) {
352         case CMD_TUNEREQUEST:
353         case CMD_CLOCK_READ:
354         case CMD_DISEQC_MSG1:
355         case CMD_DISEQC_MSG2:
356         case CMD_SETVOLTAGE:
357         case CMD_SETTONE:
358                 cx24120_msg_mpeg_output_global_config(state, 0);
359                 /* Old driver would do a msleep(100) here */
360         default:
361                 return;
362         }
363 }
364
365
366 /* Send a message to the firmware */
367 static int cx24120_message_send(struct cx24120_state *state,
368                         struct cx24120_cmd *cmd)
369 {
370         int ret, ficus;
371
372         if (state->mpeg_enabled) {
373                 /* Disable mpeg out on certain commands */
374                 cx24120_check_cmd(state, cmd->id);
375         }
376
377         ret = cx24120_writereg(state, CX24120_REG_CMD_START, cmd->id);
378         ret = cx24120_writeregN(state, CX24120_REG_CMD_ARGS, &cmd->arg[0],
379                                 cmd->len, 1);
380         ret = cx24120_writereg(state, CX24120_REG_CMD_END, 0x01);
381
382         ficus = 1000;
383         while (cx24120_readreg(state, CX24120_REG_CMD_END)) {
384                 msleep(20);
385                 ficus -= 20;
386                 if (ficus == 0) {
387                         err("Error sending message to firmware\n");
388                         return -EREMOTEIO;
389                 }
390         }
391         dev_dbg(&state->i2c->dev, "%s: Successfully send message 0x%02x\n",
392                 __func__, cmd->id);
393
394         return 0;
395 }
396
397 /* Send a message and fill arg[] with the results */
398 static int cx24120_message_sendrcv(struct cx24120_state *state,
399                         struct cx24120_cmd *cmd, u8 numreg)
400 {
401         int ret, i;
402
403         if (numreg > CX24120_MAX_CMD_LEN) {
404                 err("Too many registers to read. cmd->reg = %d", numreg);
405                 return -EREMOTEIO;
406         }
407
408         ret = cx24120_message_send(state, cmd);
409         if (ret != 0)
410                 return ret;
411
412         if (!numreg)
413                 return 0;
414
415         /* Read numreg registers starting from register cmd->len */
416         for (i = 0; i < numreg; i++)
417                 cmd->arg[i] = cx24120_readreg(state, (cmd->len+i+1));
418
419         return 0;
420 }
421
422
423
424 static int cx24120_read_signal_strength(struct dvb_frontend *fe,
425                         u16 *signal_strength)
426 {
427         struct cx24120_state *state = fe->demodulator_priv;
428         struct cx24120_cmd cmd;
429         int ret, sigstr_h, sigstr_l;
430
431         cmd.id = CMD_READ_SNR;
432         cmd.len = 1;
433         cmd.arg[0] = 0x00;
434
435         ret = cx24120_message_send(state, &cmd);
436         if (ret != 0) {
437                 err("error reading signal strength\n");
438                 return -EREMOTEIO;
439         }
440
441         /* raw */
442         sigstr_h = (cx24120_readreg(state, CX24120_REG_SIGSTR_H) >> 6) << 8;
443         sigstr_l = cx24120_readreg(state, CX24120_REG_SIGSTR_L);
444         dev_dbg(&state->i2c->dev, "%s: Signal strength from firmware= 0x%x\n",
445                         __func__, (sigstr_h | sigstr_l));
446
447         /* cooked */
448         *signal_strength = ((sigstr_h | sigstr_l)  << 5) & 0x0000ffff;
449         dev_dbg(&state->i2c->dev, "%s: Signal strength= 0x%x\n",
450                         __func__, *signal_strength);
451
452         return 0;
453 }
454
455
456 static int cx24120_msg_mpeg_output_global_config(struct cx24120_state *state,
457                         u8 enable)
458 {
459         struct cx24120_cmd cmd;
460         int ret;
461
462         cmd.id = CMD_MPEG_ONOFF;
463         cmd.len = 4;
464         cmd.arg[0] = 0x01;
465         cmd.arg[1] = 0x00;
466         cmd.arg[2] = enable ? 0 : (u8)(-1);
467         cmd.arg[3] = 0x01;
468
469         ret = cx24120_message_send(state, &cmd);
470         if (ret != 0) {
471                 dev_dbg(&state->i2c->dev,
472                         "%s: Failed to set MPEG output to %s\n",
473                         __func__,
474                         (enable)?"enabled":"disabled");
475                 return ret;
476         }
477
478         state->mpeg_enabled = enable;
479         dev_dbg(&state->i2c->dev, "%s: MPEG output %s\n",
480                 __func__,
481                 (enable)?"enabled":"disabled");
482
483         return 0;
484 }
485
486
487 static int cx24120_msg_mpeg_output_config(struct cx24120_state *state, u8 seq)
488 {
489         struct cx24120_cmd cmd;
490         struct cx24120_initial_mpeg_config i =
491                         state->config->initial_mpeg_config;
492
493         cmd.id = CMD_MPEG_INIT;
494         cmd.len = 7;
495         cmd.arg[0] = seq;               /* sequental number - can be 0,1,2 */
496         cmd.arg[1] = ((i.x1 & 0x01) << 1) | ((i.x1 >> 1) & 0x01);
497         cmd.arg[2] = 0x05;
498         cmd.arg[3] = 0x02;
499         cmd.arg[4] = ((i.x2 >> 1) & 0x01);
500         cmd.arg[5] = (i.x2 & 0xf0) | (i.x3 & 0x0f);
501         cmd.arg[6] = 0x10;
502
503         return cx24120_message_send(state, &cmd);
504 }
505
506
507 static int cx24120_diseqc_send_burst(struct dvb_frontend *fe,
508                         fe_sec_mini_cmd_t burst)
509 {
510         struct cx24120_state *state = fe->demodulator_priv;
511         struct cx24120_cmd cmd;
512
513         /* Yes, cmd.len is set to zero. The old driver
514          * didn't specify any len, but also had a
515          * memset 0 before every use of the cmd struct
516          * which would have set it to zero.
517          * This quite probably needs looking into.
518          */
519         cmd.id = CMD_DISEQC_BURST;
520         cmd.len = 0;
521         cmd.arg[0] = 0x00;
522         if (burst)
523                 cmd.arg[1] = 0x01;
524         dev_dbg(&state->i2c->dev, "%s: burst sent.\n", __func__);
525
526         return cx24120_message_send(state, &cmd);
527 }
528
529
530 static int cx24120_set_tone(struct dvb_frontend *fe, fe_sec_tone_mode_t tone)
531 {
532         struct cx24120_state *state = fe->demodulator_priv;
533         struct cx24120_cmd cmd;
534
535         dev_dbg(&state->i2c->dev, "%s(%d)\n",
536                         __func__, tone);
537
538         if ((tone != SEC_TONE_ON) && (tone != SEC_TONE_OFF)) {
539                 err("Invalid tone=%d\n", tone);
540                 return -EINVAL;
541         }
542
543         cmd.id = CMD_SETTONE;
544         cmd.len = 4;
545         cmd.arg[0] = 0x00;
546         cmd.arg[1] = 0x00;
547         cmd.arg[2] = 0x00;
548         cmd.arg[3] = (tone == SEC_TONE_ON)?0x01:0x00;
549
550         return cx24120_message_send(state, &cmd);
551 }
552
553
554 static int cx24120_set_voltage(struct dvb_frontend *fe,
555                         fe_sec_voltage_t voltage)
556 {
557         struct cx24120_state *state = fe->demodulator_priv;
558         struct cx24120_cmd cmd;
559
560         dev_dbg(&state->i2c->dev, "%s(%d)\n",
561                         __func__, voltage);
562
563         cmd.id = CMD_SETVOLTAGE;
564         cmd.len = 2;
565         cmd.arg[0] = 0x00;
566         cmd.arg[1] = (voltage == SEC_VOLTAGE_18)?0x01:0x00;
567
568         return cx24120_message_send(state, &cmd);
569 }
570
571
572 static int cx24120_send_diseqc_msg(struct dvb_frontend *fe,
573                         struct dvb_diseqc_master_cmd *d)
574 {
575         struct cx24120_state *state = fe->demodulator_priv;
576         struct cx24120_cmd cmd;
577         int back_count;
578
579         dev_dbg(&state->i2c->dev, "%s()\n", __func__);
580
581         cmd.id = CMD_DISEQC_MSG1;
582         cmd.len = 11;
583         cmd.arg[0] = 0x00;
584         cmd.arg[1] = 0x00;
585         cmd.arg[2] = 0x03;
586         cmd.arg[3] = 0x16;
587         cmd.arg[4] = 0x28;
588         cmd.arg[5] = 0x01;
589         cmd.arg[6] = 0x01;
590         cmd.arg[7] = 0x14;
591         cmd.arg[8] = 0x19;
592         cmd.arg[9] = 0x14;
593         cmd.arg[10] = 0x1e;
594
595         if (cx24120_message_send(state, &cmd)) {
596                 err("send 1st message(0x%x) failed\n", cmd.id);
597                 return -EREMOTEIO;
598         }
599
600         cmd.id = CMD_DISEQC_MSG2;
601         cmd.len = d->msg_len + 6;
602         cmd.arg[0] = 0x00;
603         cmd.arg[1] = 0x01;
604         cmd.arg[2] = 0x02;
605         cmd.arg[3] = 0x00;
606         cmd.arg[4] = 0x00;
607         cmd.arg[5] = d->msg_len;
608
609         memcpy(&cmd.arg[6], &d->msg, d->msg_len);
610
611         if (cx24120_message_send(state, &cmd)) {
612                 err("send 2nd message(0x%x) failed\n", cmd.id);
613                 return -EREMOTEIO;
614         }
615
616         back_count = 500;
617         do {
618                 if (!(cx24120_readreg(state, 0x93) & 0x01)) {
619                         dev_dbg(&state->i2c->dev,
620                                 "%s: diseqc sequence sent success\n",
621                                 __func__);
622                         return 0;
623                 }
624                 msleep(20);
625                 back_count -= 20;
626         } while (back_count);
627
628         err("Too long waiting for diseqc.\n");
629         return -ETIMEDOUT;
630 }
631
632
633 /* Read current tuning status */
634 static int cx24120_read_status(struct dvb_frontend *fe, fe_status_t *status)
635 {
636         struct cx24120_state *state = fe->demodulator_priv;
637         int lock;
638
639         lock = cx24120_readreg(state, CX24120_REG_STATUS);
640
641         dev_dbg(&state->i2c->dev, "%s() status = 0x%02x\n",
642                 __func__, lock);
643
644         *status = 0;
645
646         if (lock & CX24120_HAS_SIGNAL)
647                 *status = FE_HAS_SIGNAL;
648         if (lock & CX24120_HAS_CARRIER)
649                 *status |= FE_HAS_CARRIER;
650         if (lock & CX24120_HAS_VITERBI)
651                 *status |= FE_HAS_VITERBI | FE_HAS_SYNC;
652         if (lock & CX24120_HAS_LOCK)
653                 *status |= FE_HAS_LOCK;
654
655         /* TODO: is FE_HAS_SYNC in the right place?
656          * Other cx241xx drivers have this slightly
657          * different */
658
659         return 0;
660 }
661
662
663 /* FEC & modulation lookup table
664  * Used for decoding the REG_FECMODE register
665  * once tuned in.
666  */
667 static struct cx24120_modfec {
668         fe_delivery_system_t delsys;
669         fe_modulation_t mod;
670         fe_code_rate_t fec;
671         u8 val;
672 } modfec_lookup_table[] = {
673 /*delsys        mod     fec             val */
674 { SYS_DVBS,     QPSK,   FEC_1_2,        0x01 },
675 { SYS_DVBS,     QPSK,   FEC_2_3,        0x02 },
676 { SYS_DVBS,     QPSK,   FEC_3_4,        0x03 },
677 { SYS_DVBS,     QPSK,   FEC_4_5,        0x04 },
678 { SYS_DVBS,     QPSK,   FEC_5_6,        0x05 },
679 { SYS_DVBS,     QPSK,   FEC_6_7,        0x06 },
680 { SYS_DVBS,     QPSK,   FEC_7_8,        0x07 },
681
682 { SYS_DVBS2,    QPSK,   FEC_1_2,        0x04 },
683 { SYS_DVBS2,    QPSK,   FEC_3_5,        0x05 },
684 { SYS_DVBS2,    QPSK,   FEC_2_3,        0x06 },
685 { SYS_DVBS2,    QPSK,   FEC_3_4,        0x07 },
686 { SYS_DVBS2,    QPSK,   FEC_4_5,        0x08 },
687 { SYS_DVBS2,    QPSK,   FEC_5_6,        0x09 },
688 { SYS_DVBS2,    QPSK,   FEC_8_9,        0x0a },
689 { SYS_DVBS2,    QPSK,   FEC_9_10,       0x0b },
690
691 { SYS_DVBS2,    PSK_8,  FEC_3_5,        0x0c },
692 { SYS_DVBS2,    PSK_8,  FEC_2_3,        0x0d },
693 { SYS_DVBS2,    PSK_8,  FEC_3_4,        0x0e },
694 { SYS_DVBS2,    PSK_8,  FEC_5_6,        0x0f },
695 { SYS_DVBS2,    PSK_8,  FEC_8_9,        0x10 },
696 { SYS_DVBS2,    PSK_8,  FEC_9_10,       0x11 },
697 };
698
699
700 /* Retrieve current fec, modulation & pilot values */
701 static int cx24120_get_fec(struct dvb_frontend *fe)
702 {
703         struct dtv_frontend_properties *c = &fe->dtv_property_cache;
704         struct cx24120_state *state = fe->demodulator_priv;
705         int idx;
706         int ret;
707         int GettedFEC;
708
709         dev_dbg(&state->i2c->dev, "%s()\n", __func__);
710
711         ret = cx24120_readreg(state, CX24120_REG_FECMODE);
712         GettedFEC = ret & 0x3f;         /* Lower 6 bits */
713
714         dev_dbg(&state->i2c->dev, "%s: Get FEC: %d\n", __func__, GettedFEC);
715
716         for (idx = 0; idx < ARRAY_SIZE(modfec_lookup_table); idx++) {
717                 if (modfec_lookup_table[idx].delsys != state->dcur.delsys)
718                         continue;
719                 if (modfec_lookup_table[idx].val != GettedFEC)
720                         continue;
721
722                 break;  /* found */
723         }
724
725         if (idx >= ARRAY_SIZE(modfec_lookup_table)) {
726                 dev_dbg(&state->i2c->dev, "%s: Couldn't find fec!\n",
727                         __func__);
728                 return -EINVAL;
729         }
730
731         /* save values back to cache */
732         c->modulation = modfec_lookup_table[idx].mod;
733         c->fec_inner = modfec_lookup_table[idx].fec;
734         c->pilot = (ret & 0x80) ? PILOT_ON : PILOT_OFF;
735
736         dev_dbg(&state->i2c->dev,
737                 "%s: mod(%d), fec(%d), pilot(%d)\n",
738                 __func__,
739                 c->modulation, c->fec_inner, c->pilot);
740
741         return 0;
742 }
743
744
745 /* Clock ratios lookup table
746  *
747  * Values obtained from much larger table in old driver
748  * which had numerous entries which would never match.
749  *
750  * There's probably some way of calculating these but I
751  * can't determine the pattern
752 */
753 static struct cx24120_clock_ratios_table {
754         fe_delivery_system_t delsys;
755         fe_pilot_t pilot;
756         fe_modulation_t mod;
757         fe_code_rate_t fec;
758         u32 m_rat;
759         u32 n_rat;
760         u32 rate;
761 } clock_ratios_table[] = {
762 /*delsys        pilot           mod     fec             m_rat   n_rat   rate */
763 { SYS_DVBS2,    PILOT_OFF,      QPSK,   FEC_1_2,        273088, 254505, 274 },
764 { SYS_DVBS2,    PILOT_OFF,      QPSK,   FEC_3_5,        17272,  13395,  330 },
765 { SYS_DVBS2,    PILOT_OFF,      QPSK,   FEC_2_3,        24344,  16967,  367 },
766 { SYS_DVBS2,    PILOT_OFF,      QPSK,   FEC_3_4,        410788, 254505, 413 },
767 { SYS_DVBS2,    PILOT_OFF,      QPSK,   FEC_4_5,        438328, 254505, 440 },
768 { SYS_DVBS2,    PILOT_OFF,      QPSK,   FEC_5_6,        30464,  16967,  459 },
769 { SYS_DVBS2,    PILOT_OFF,      QPSK,   FEC_8_9,        487832, 254505, 490 },
770 { SYS_DVBS2,    PILOT_OFF,      QPSK,   FEC_9_10,       493952, 254505, 496 },
771 { SYS_DVBS2,    PILOT_OFF,      PSK_8,  FEC_3_5,        328168, 169905, 494 },
772 { SYS_DVBS2,    PILOT_OFF,      PSK_8,  FEC_2_3,        24344,  11327,  550 },
773 { SYS_DVBS2,    PILOT_OFF,      PSK_8,  FEC_3_4,        410788, 169905, 618 },
774 { SYS_DVBS2,    PILOT_OFF,      PSK_8,  FEC_5_6,        30464,  11327,  688 },
775 { SYS_DVBS2,    PILOT_OFF,      PSK_8,  FEC_8_9,        487832, 169905, 735 },
776 { SYS_DVBS2,    PILOT_OFF,      PSK_8,  FEC_9_10,       493952, 169905, 744 },
777 { SYS_DVBS2,    PILOT_ON,       QPSK,   FEC_1_2,        273088, 260709, 268 },
778 { SYS_DVBS2,    PILOT_ON,       QPSK,   FEC_3_5,        328168, 260709, 322 },
779 { SYS_DVBS2,    PILOT_ON,       QPSK,   FEC_2_3,        121720, 86903,  358 },
780 { SYS_DVBS2,    PILOT_ON,       QPSK,   FEC_3_4,        410788, 260709, 403 },
781 { SYS_DVBS2,    PILOT_ON,       QPSK,   FEC_4_5,        438328, 260709, 430 },
782 { SYS_DVBS2,    PILOT_ON,       QPSK,   FEC_5_6,        152320, 86903,  448 },
783 { SYS_DVBS2,    PILOT_ON,       QPSK,   FEC_8_9,        487832, 260709, 479 },
784 { SYS_DVBS2,    PILOT_ON,       QPSK,   FEC_9_10,       493952, 260709, 485 },
785 { SYS_DVBS2,    PILOT_ON,       PSK_8,  FEC_3_5,        328168, 173853, 483 },
786 { SYS_DVBS2,    PILOT_ON,       PSK_8,  FEC_2_3,        121720, 57951,  537 },
787 { SYS_DVBS2,    PILOT_ON,       PSK_8,  FEC_3_4,        410788, 173853, 604 },
788 { SYS_DVBS2,    PILOT_ON,       PSK_8,  FEC_5_6,        152320, 57951,  672 },
789 { SYS_DVBS2,    PILOT_ON,       PSK_8,  FEC_8_9,        487832, 173853, 718 },
790 { SYS_DVBS2,    PILOT_ON,       PSK_8,  FEC_9_10,       493952, 173853, 727 },
791 { SYS_DVBS,     PILOT_OFF,      QPSK,   FEC_1_2,        152592, 152592, 256 },
792 { SYS_DVBS,     PILOT_OFF,      QPSK,   FEC_2_3,        305184, 228888, 341 },
793 { SYS_DVBS,     PILOT_OFF,      QPSK,   FEC_3_4,        457776, 305184, 384 },
794 { SYS_DVBS,     PILOT_OFF,      QPSK,   FEC_5_6,        762960, 457776, 427 },
795 { SYS_DVBS,     PILOT_OFF,      QPSK,   FEC_7_8,        1068144, 610368, 448 },
796 };
797
798
799 /* Set clock ratio from lookup table */
800 static void cx24120_set_clock_ratios(struct dvb_frontend *fe)
801 {
802         struct dtv_frontend_properties *c = &fe->dtv_property_cache;
803         struct cx24120_state *state = fe->demodulator_priv;
804         struct cx24120_cmd cmd;
805         int ret, idx;
806
807         /* Find fec, modulation, pilot */
808         ret = cx24120_get_fec(fe);
809         if (ret != 0)
810                 return;
811
812         /* Find the clock ratios in the lookup table */
813         for (idx = 0; idx < ARRAY_SIZE(clock_ratios_table); idx++) {
814                 if (clock_ratios_table[idx].delsys != state->dcur.delsys)
815                         continue;
816                 if (clock_ratios_table[idx].mod != c->modulation)
817                         continue;
818                 if (clock_ratios_table[idx].fec != c->fec_inner)
819                         continue;
820                 if (clock_ratios_table[idx].pilot != c->pilot)
821                         continue;
822
823                 break;          /* found */
824         }
825
826         if (idx >= ARRAY_SIZE(clock_ratios_table)) {
827                 info("Clock ratio not found - data reception in danger\n");
828                 return;
829         }
830
831
832         /* Read current values? */
833         cmd.id = CMD_CLOCK_READ;
834         cmd.len = 1;
835         cmd.arg[0] = 0x00;
836         ret = cx24120_message_sendrcv(state, &cmd, 6);
837         if (ret != 0)
838                 return;
839         /* in cmd[0]-[5] - result */
840
841         dev_dbg(&state->i2c->dev,
842                 "%s: m=%d, n=%d; idx: %d m=%d, n=%d, rate=%d\n",
843                 __func__,
844                 cmd.arg[2] | (cmd.arg[1] << 8) | (cmd.arg[0] << 16),
845                 cmd.arg[5] | (cmd.arg[4] << 8) | (cmd.arg[3] << 16),
846                 idx,
847                 clock_ratios_table[idx].m_rat,
848                 clock_ratios_table[idx].n_rat,
849                 clock_ratios_table[idx].rate);
850
851
852
853         /* Set the clock */
854         cmd.id = CMD_CLOCK_SET;
855         cmd.len = 10;
856         cmd.arg[0] = 0;
857         cmd.arg[1] = 0x10;
858         cmd.arg[2] = (clock_ratios_table[idx].m_rat >> 16) & 0xff;
859         cmd.arg[3] = (clock_ratios_table[idx].m_rat >>  8) & 0xff;
860         cmd.arg[4] = (clock_ratios_table[idx].m_rat >>  0) & 0xff;
861         cmd.arg[5] = (clock_ratios_table[idx].n_rat >> 16) & 0xff;
862         cmd.arg[6] = (clock_ratios_table[idx].n_rat >>  8) & 0xff;
863         cmd.arg[7] = (clock_ratios_table[idx].n_rat >>  0) & 0xff;
864         cmd.arg[8] = (clock_ratios_table[idx].rate >> 8) & 0xff;
865         cmd.arg[9] = (clock_ratios_table[idx].rate >> 0) & 0xff;
866
867         cx24120_message_send(state, &cmd);
868
869 }
870
871
872 /* Set inversion value */
873 static int cx24120_set_inversion(struct cx24120_state *state,
874         fe_spectral_inversion_t inversion)
875 {
876         dev_dbg(&state->i2c->dev, "%s(%d)\n",
877                 __func__, inversion);
878
879         switch (inversion) {
880         case INVERSION_OFF:
881                 state->dnxt.inversion_val = 0x00;
882                 break;
883         case INVERSION_ON:
884                 state->dnxt.inversion_val = 0x04;
885                 break;
886         case INVERSION_AUTO:
887                 state->dnxt.inversion_val = 0x0c;
888                 break;
889         default:
890                 return -EINVAL;
891         }
892
893         state->dnxt.inversion = inversion;
894
895         return 0;
896 }
897
898 /* FEC lookup table for tuning
899  * Some DVB-S2 val's have been found by trial
900  * and error. Sofar it seems to match up with
901  * the contents of the REG_FECMODE after tuning
902  * The rest will probably be the same but would
903  * need testing.
904  * Anything not in the table will run with
905  * FEC_AUTO and take a while longer to tune in
906  * ( c.500ms instead of 30ms )
907  */
908 static struct cx24120_modfec_table {
909         fe_delivery_system_t delsys;
910         fe_modulation_t mod;
911         fe_code_rate_t fec;
912         u8 val;
913 } modfec_table[] = {
914 /*delsys        mod     fec      val */
915 { SYS_DVBS,     QPSK,   FEC_1_2, 0x2e },
916 { SYS_DVBS,     QPSK,   FEC_2_3, 0x2f },
917 { SYS_DVBS,     QPSK,   FEC_3_4, 0x30 },
918 { SYS_DVBS,     QPSK,   FEC_5_6, 0x31 },
919 { SYS_DVBS,     QPSK,   FEC_6_7, 0x32 },
920 { SYS_DVBS,     QPSK,   FEC_7_8, 0x33 },
921
922 { SYS_DVBS2,    QPSK,   FEC_3_4, 0x07 },
923
924 { SYS_DVBS2,    PSK_8,  FEC_2_3, 0x0d },
925 { SYS_DVBS2,    PSK_8,  FEC_3_4, 0x0e },
926 };
927
928 /* Set fec_val & fec_mask values from delsys, modulation & fec */
929 static int cx24120_set_fec(struct cx24120_state *state,
930         fe_modulation_t mod, fe_code_rate_t fec)
931 {
932         int idx;
933
934         dev_dbg(&state->i2c->dev,
935                 "%s(0x%02x,0x%02x)\n", __func__, mod, fec);
936
937         state->dnxt.fec = fec;
938
939         /* Lookup fec_val from modfec table */
940         for (idx = 0; idx < ARRAY_SIZE(modfec_table); idx++) {
941                 if (modfec_table[idx].delsys != state->dnxt.delsys)
942                         continue;
943                 if (modfec_table[idx].mod != mod)
944                         continue;
945                 if (modfec_table[idx].fec != fec)
946                         continue;
947
948                 /* found */
949                 state->dnxt.fec_mask = 0x00;
950                 state->dnxt.fec_val = modfec_table[idx].val;
951                 return 0;
952         }
953
954
955         if (state->dnxt.delsys == SYS_DVBS2) {
956                 /* DVBS2 auto is 0x00/0x00 */
957                 state->dnxt.fec_mask = 0x00;
958                 state->dnxt.fec_val  = 0x00;
959         } else {
960                 /* Set DVB-S to auto */
961                 state->dnxt.fec_val  = 0x2e;
962                 state->dnxt.fec_mask = 0xac;
963         }
964
965         return 0;
966 }
967
968
969 /* Set pilot */
970 static int cx24120_set_pilot(struct cx24120_state *state,
971                 fe_pilot_t pilot) {
972
973         dev_dbg(&state->i2c->dev,
974                 "%s(%d)\n", __func__, pilot);
975
976         /* Pilot only valid in DVBS2 */
977         if (state->dnxt.delsys != SYS_DVBS2) {
978                 state->dnxt.pilot_val = CX24120_PILOT_OFF;
979                 return 0;
980         }
981
982
983         switch (pilot) {
984         case PILOT_OFF:
985                 state->dnxt.pilot_val = CX24120_PILOT_OFF;
986                 break;
987         case PILOT_ON:
988                 state->dnxt.pilot_val = CX24120_PILOT_ON;
989                 break;
990         case PILOT_AUTO:
991         default:
992                 state->dnxt.pilot_val = CX24120_PILOT_AUTO;
993         }
994
995         return 0;
996 }
997
998 /* Set symbol rate */
999 static int cx24120_set_symbolrate(struct cx24120_state *state, u32 rate)
1000 {
1001         dev_dbg(&state->i2c->dev, "%s(%d)\n",
1002                 __func__, rate);
1003
1004         state->dnxt.symbol_rate = rate;
1005
1006         /* Check symbol rate */
1007         if (rate  > 31000000) {
1008                 state->dnxt.clkdiv  = (-(rate < 31000001) & 3) + 2;
1009                 state->dnxt.ratediv = (-(rate < 31000001) & 6) + 4;
1010         } else {
1011                 state->dnxt.clkdiv  = 3;
1012                 state->dnxt.ratediv = 6;
1013         }
1014
1015         return 0;
1016 }
1017
1018
1019 /* Overwrite the current tuning params, we are about to tune */
1020 static void cx24120_clone_params(struct dvb_frontend *fe)
1021 {
1022         struct cx24120_state *state = fe->demodulator_priv;
1023
1024         state->dcur = state->dnxt;
1025 }
1026
1027
1028 /* Table of time to tune for different symrates */
1029 static struct cx24120_symrate_delay {
1030         fe_delivery_system_t delsys;
1031         u32 symrate;            /* Check for >= this symrate */
1032         u32 delay;              /* Timeout in ms */
1033 } symrates_delay_table[] = {
1034 { SYS_DVBS,     10000000,       400   },
1035 { SYS_DVBS,     8000000,        2000  },
1036 { SYS_DVBS,     6000000,        5000  },
1037 { SYS_DVBS,     3000000,        10000 },
1038 { SYS_DVBS,     0,              15000 },
1039 { SYS_DVBS2,    10000000,       600   }, /* DVBS2 needs a little longer */
1040 { SYS_DVBS2,    8000000,        2000  }, /* (so these might need bumping too) */
1041 { SYS_DVBS2,    6000000,        5000  },
1042 { SYS_DVBS2,    3000000,        10000 },
1043 { SYS_DVBS2,    0,              15000 },
1044 };
1045
1046
1047 static int cx24120_set_frontend(struct dvb_frontend *fe)
1048 {
1049         struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1050         struct cx24120_state *state = fe->demodulator_priv;
1051         struct cx24120_cmd cmd;
1052         int ret;
1053         int delay_cnt, sd_idx = 0;
1054         fe_status_t status;
1055
1056         switch (c->delivery_system) {
1057         case SYS_DVBS2:
1058                 dev_dbg(&state->i2c->dev, "%s() DVB-S2\n",
1059                         __func__);
1060                 break;
1061         case SYS_DVBS:
1062                 dev_dbg(&state->i2c->dev, "%s() DVB-S\n",
1063                         __func__);
1064                 break;
1065         default:
1066                 dev_dbg(&state->i2c->dev,
1067                         "%s() Delivery system(%d) not supported\n",
1068                         __func__, c->delivery_system);
1069                 ret = -EINVAL;
1070                 break;
1071         }
1072
1073
1074         state->dnxt.delsys = c->delivery_system;
1075         state->dnxt.modulation = c->modulation;
1076         state->dnxt.frequency = c->frequency;
1077         state->dnxt.pilot = c->pilot;
1078
1079         ret = cx24120_set_inversion(state, c->inversion);
1080         if (ret !=  0)
1081                 return ret;
1082
1083         ret = cx24120_set_fec(state, c->modulation, c->fec_inner);
1084         if (ret !=  0)
1085                 return ret;
1086
1087         ret = cx24120_set_pilot(state, c->pilot);
1088         if (ret != 0)
1089                 return ret;
1090
1091         ret = cx24120_set_symbolrate(state, c->symbol_rate);
1092         if (ret !=  0)
1093                 return ret;
1094
1095
1096         /* discard the 'current' tuning parameters and prepare to tune */
1097         cx24120_clone_params(fe);
1098
1099         dev_dbg(&state->i2c->dev,
1100                 "%s: delsys      = %d\n", __func__, state->dcur.delsys);
1101         dev_dbg(&state->i2c->dev,
1102                 "%s: modulation  = %d\n", __func__, state->dcur.modulation);
1103         dev_dbg(&state->i2c->dev,
1104                 "%s: frequency   = %d\n", __func__, state->dcur.frequency);
1105         dev_dbg(&state->i2c->dev,
1106                 "%s: pilot       = %d (val = 0x%02x)\n", __func__,
1107                 state->dcur.pilot, state->dcur.pilot_val);
1108         dev_dbg(&state->i2c->dev,
1109                 "%s: symbol_rate = %d (clkdiv/ratediv = 0x%02x/0x%02x)\n",
1110                  __func__, state->dcur.symbol_rate,
1111                  state->dcur.clkdiv, state->dcur.ratediv);
1112         dev_dbg(&state->i2c->dev,
1113                 "%s: FEC         = %d (mask/val = 0x%02x/0x%02x)\n", __func__,
1114                 state->dcur.fec, state->dcur.fec_mask, state->dcur.fec_val);
1115         dev_dbg(&state->i2c->dev,
1116                 "%s: Inversion   = %d (val = 0x%02x)\n", __func__,
1117                 state->dcur.inversion, state->dcur.inversion_val);
1118
1119
1120
1121         /* Tune in */
1122         cmd.id = CMD_TUNEREQUEST;
1123         cmd.len = 15;
1124         cmd.arg[0] = 0;
1125         cmd.arg[1]  = (state->dcur.frequency & 0xff0000) >> 16;
1126         cmd.arg[2]  = (state->dcur.frequency & 0x00ff00) >> 8;
1127         cmd.arg[3]  = (state->dcur.frequency & 0x0000ff);
1128         cmd.arg[4]  = ((state->dcur.symbol_rate/1000) & 0xff00) >> 8;
1129         cmd.arg[5]  = ((state->dcur.symbol_rate/1000) & 0x00ff);
1130         cmd.arg[6]  = state->dcur.inversion;
1131         cmd.arg[7]  = state->dcur.fec_val | state->dcur.pilot_val;
1132         cmd.arg[8]  = CX24120_SEARCH_RANGE_KHZ >> 8;
1133         cmd.arg[9]  = CX24120_SEARCH_RANGE_KHZ & 0xff;
1134         cmd.arg[10] = 0;                /* maybe rolloff? */
1135         cmd.arg[11] = state->dcur.fec_mask;
1136         cmd.arg[12] = state->dcur.ratediv;
1137         cmd.arg[13] = state->dcur.clkdiv;
1138         cmd.arg[14] = 0;
1139
1140
1141         /* Send tune command */
1142         ret = cx24120_message_send(state, &cmd);
1143         if (ret != 0)
1144                 return ret;
1145
1146         /* Write symbol rate values */
1147         ret = cx24120_writereg(state, CX24120_REG_CLKDIV, state->dcur.clkdiv);
1148         ret = cx24120_readreg(state, CX24120_REG_RATEDIV);
1149         ret &= 0xfffffff0;
1150         ret |= state->dcur.ratediv;
1151         ret = cx24120_writereg(state, CX24120_REG_RATEDIV, ret);
1152
1153         /* Default time to tune */
1154         delay_cnt = 500;
1155
1156         /* Establish time to tune from symrates_delay_table */
1157         for (sd_idx = 0; sd_idx < ARRAY_SIZE(symrates_delay_table); sd_idx++) {
1158                 if (state->dcur.delsys != symrates_delay_table[sd_idx].delsys)
1159                         continue;
1160                 if (c->symbol_rate < symrates_delay_table[sd_idx].symrate)
1161                         continue;
1162
1163                 /* found */
1164                 delay_cnt = symrates_delay_table[sd_idx].delay;
1165                 dev_dbg(&state->i2c->dev, "%s: Found symrate delay = %d\n",
1166                         __func__, delay_cnt);
1167                 break;
1168         }
1169
1170         /* Wait for tuning */
1171         while (delay_cnt >= 0) {
1172                 cx24120_read_status(fe, &status);
1173                 if (status & FE_HAS_LOCK)
1174                         goto tuned;
1175                 msleep(20);
1176                 delay_cnt -= 20;
1177         }
1178
1179
1180         /* Fail to tune */
1181         dev_dbg(&state->i2c->dev, "%s: Tuning failed\n",
1182                 __func__);
1183
1184         return -EINVAL;
1185
1186
1187 tuned:
1188         dev_dbg(&state->i2c->dev, "%s: Tuning successful\n",
1189                 __func__);
1190
1191         /* Set clock ratios */
1192         cx24120_set_clock_ratios(fe);
1193
1194         /* Old driver would do a msleep(200) here */
1195
1196         /* Renable mpeg output */
1197         if (!state->mpeg_enabled)
1198                 cx24120_msg_mpeg_output_global_config(state, 1);
1199
1200         return 0;
1201 }
1202
1203
1204 /* Calculate vco from config */
1205 static u64 cx24120_calculate_vco(struct cx24120_state *state)
1206 {
1207         u32 vco;
1208         u64 inv_vco, res, xxyyzz;
1209         u32 xtal_khz = state->config->xtal_khz;
1210
1211         xxyyzz = 0x400000000ULL;
1212         vco = xtal_khz * 10 * 4;
1213         inv_vco = xxyyzz / vco;
1214         res = xxyyzz % vco;
1215
1216         if (inv_vco > xtal_khz * 10 * 2)
1217                 ++inv_vco;
1218
1219         dev_dbg(&state->i2c->dev,
1220                 "%s: xtal=%d, vco=%d, inv_vco=%lld, res=%lld\n",
1221                 __func__, xtal_khz, vco, inv_vco, res);
1222
1223         return inv_vco;
1224 }
1225
1226
1227 int cx24120_init(struct dvb_frontend *fe)
1228 {
1229         const struct firmware *fw;
1230         struct cx24120_state *state = fe->demodulator_priv;
1231         struct cx24120_cmd cmd;
1232         u8 ret, ret_EA, reg1;
1233         u64 inv_vco;
1234         int reset_result;
1235
1236         int i;
1237         unsigned char vers[4];
1238
1239         if (state->cold_init)
1240                 return 0;
1241
1242         /* ???? */
1243         ret = cx24120_writereg(state, 0xea, 0x00);
1244         ret = cx24120_test_rom(state);
1245         ret = cx24120_readreg(state, 0xfb) & 0xfe;
1246         ret = cx24120_writereg(state, 0xfb, ret);
1247         ret = cx24120_readreg(state, 0xfc) & 0xfe;
1248         ret = cx24120_writereg(state, 0xfc, ret);
1249         ret = cx24120_writereg(state, 0xc3, 0x04);
1250         ret = cx24120_writereg(state, 0xc4, 0x04);
1251         ret = cx24120_writereg(state, 0xce, 0x00);
1252         ret = cx24120_writereg(state, 0xcf, 0x00);
1253         ret_EA = cx24120_readreg(state, 0xea) & 0xfe;
1254         ret = cx24120_writereg(state, 0xea, ret_EA);
1255         ret = cx24120_writereg(state, 0xeb, 0x0c);
1256         ret = cx24120_writereg(state, 0xec, 0x06);
1257         ret = cx24120_writereg(state, 0xed, 0x05);
1258         ret = cx24120_writereg(state, 0xee, 0x03);
1259         ret = cx24120_writereg(state, 0xef, 0x05);
1260         ret = cx24120_writereg(state, 0xf3, 0x03);
1261         ret = cx24120_writereg(state, 0xf4, 0x44);
1262
1263         for (reg1 = 0xf0; reg1 < 0xf3; reg1++) {
1264                 cx24120_writereg(state, reg1, 0x04);
1265                 cx24120_writereg(state, reg1 - 10, 0x02);
1266         }
1267
1268         ret = cx24120_writereg(state, 0xea, (ret_EA | 0x01));
1269         for (reg1 = 0xc5; reg1 < 0xcb; reg1 += 2) {
1270                 ret = cx24120_writereg(state, reg1, 0x00);
1271                 ret = cx24120_writereg(state, reg1 + 1, 0x00);
1272         }
1273
1274         ret = cx24120_writereg(state, 0xe4, 0x03);
1275         ret = cx24120_writereg(state, 0xeb, 0x0a);
1276
1277         dev_dbg(&state->i2c->dev,
1278                 "%s: Requesting firmware (%s) to download...\n",
1279                 __func__, CX24120_FIRMWARE);
1280
1281         ret = state->config->request_firmware(fe, &fw, CX24120_FIRMWARE);
1282         if (ret) {
1283                 err("Could not load firmware (%s): %d\n",
1284                         CX24120_FIRMWARE, ret);
1285                 return ret;
1286         }
1287
1288         dev_dbg(&state->i2c->dev,
1289                 "%s: Firmware found, size %d bytes (%02x %02x .. %02x %02x)\n",
1290                 __func__,
1291                 (int)fw->size,                  /* firmware_size in bytes */
1292                 fw->data[0],                    /* fw 1st byte */
1293                 fw->data[1],                    /* fw 2d byte */
1294                 fw->data[fw->size - 2],         /* fw before last byte */
1295                 fw->data[fw->size - 1]);        /* fw last byte */
1296
1297         ret = cx24120_test_rom(state);
1298         ret = cx24120_readreg(state, 0xfb) & 0xfe;
1299         ret = cx24120_writereg(state, 0xfb, ret);
1300         ret = cx24120_writereg(state, 0xe0, 0x76);
1301         ret = cx24120_writereg(state, 0xf7, 0x81);
1302         ret = cx24120_writereg(state, 0xf8, 0x00);
1303         ret = cx24120_writereg(state, 0xf9, 0x00);
1304         ret = cx24120_writeregN(state, 0xfa, fw->data, (fw->size - 1), 0x00);
1305         ret = cx24120_writereg(state, 0xf7, 0xc0);
1306         ret = cx24120_writereg(state, 0xe0, 0x00);
1307         ret = (fw->size - 2) & 0x00ff;
1308         ret = cx24120_writereg(state, 0xf8, ret);
1309         ret = ((fw->size - 2) >> 8) & 0x00ff;
1310         ret = cx24120_writereg(state, 0xf9, ret);
1311         ret = cx24120_writereg(state, 0xf7, 0x00);
1312         ret = cx24120_writereg(state, 0xdc, 0x00);
1313         ret = cx24120_writereg(state, 0xdc, 0x07);
1314         msleep(500);
1315
1316         /* Check final byte matches final byte of firmware */
1317         ret = cx24120_readreg(state, 0xe1);
1318         if (ret == fw->data[fw->size - 1]) {
1319                 dev_dbg(&state->i2c->dev,
1320                         "%s: Firmware uploaded successfully\n",
1321                         __func__);
1322                 reset_result = 0;
1323         } else {
1324                 err("Firmware upload failed. Last byte returned=0x%x\n", ret);
1325                 reset_result = -EREMOTEIO;
1326         }
1327         ret = cx24120_writereg(state, 0xdc, 0x00);
1328         release_firmware(fw);
1329         if (reset_result != 0)
1330                 return reset_result;
1331
1332
1333         /* Start tuner */
1334         cmd.id = CMD_START_TUNER;
1335         cmd.len = 3;
1336         cmd.arg[0] = 0x00;
1337         cmd.arg[1] = 0x00;
1338         cmd.arg[2] = 0x00;
1339
1340         if (cx24120_message_send(state, &cmd) != 0) {
1341                 err("Error tuner start! :(\n");
1342                 return -EREMOTEIO;
1343         }
1344
1345         /* Set VCO */
1346         inv_vco = cx24120_calculate_vco(state);
1347
1348         cmd.id = CMD_VCO_SET;
1349         cmd.len = 12;
1350         cmd.arg[0] = 0x06;
1351         cmd.arg[1] = 0x2b;
1352         cmd.arg[2] = 0xd8;
1353         cmd.arg[3] = (inv_vco >> 8) & 0xff;
1354         cmd.arg[4] = (inv_vco) & 0xff;
1355         cmd.arg[5] = 0x03;
1356         cmd.arg[6] = 0x9d;
1357         cmd.arg[7] = 0xfc;
1358         cmd.arg[8] = 0x06;
1359         cmd.arg[9] = 0x03;
1360         cmd.arg[10] = 0x27;
1361         cmd.arg[11] = 0x7f;
1362
1363         if (cx24120_message_send(state, &cmd)) {
1364                 err("Error set VCO! :(\n");
1365                 return -EREMOTEIO;
1366         }
1367
1368
1369         /* set bandwidth */
1370         cmd.id = CMD_BANDWIDTH;
1371         cmd.len = 12;
1372         cmd.arg[0] = 0x00;
1373         cmd.arg[1] = 0x00;
1374         cmd.arg[2] = 0x00;
1375         cmd.arg[3] = 0x00;
1376         cmd.arg[4] = 0x05;
1377         cmd.arg[5] = 0x02;
1378         cmd.arg[6] = 0x02;
1379         cmd.arg[7] = 0x00;
1380         cmd.arg[8] = 0x05;
1381         cmd.arg[9] = 0x02;
1382         cmd.arg[10] = 0x02;
1383         cmd.arg[11] = 0x00;
1384
1385         if (cx24120_message_send(state, &cmd)) {
1386                 err("Error set bandwidth!\n");
1387                 return -EREMOTEIO;
1388         }
1389
1390         ret = cx24120_readreg(state, 0xba);
1391         if (ret > 3) {
1392                 dev_dbg(&state->i2c->dev, "%s: Reset-readreg 0xba: %x\n",
1393                         __func__, ret);
1394                 err("Error initialising tuner!\n");
1395                 return -EREMOTEIO;
1396         }
1397
1398         dev_dbg(&state->i2c->dev, "%s: Tuner initialised correctly.\n",
1399                         __func__);
1400
1401
1402         /* Initialise mpeg outputs */
1403         ret = cx24120_writereg(state, 0xeb, 0x0a);
1404         if (cx24120_msg_mpeg_output_global_config(state, 0) ||
1405             cx24120_msg_mpeg_output_config(state, 0) ||
1406             cx24120_msg_mpeg_output_config(state, 1) ||
1407             cx24120_msg_mpeg_output_config(state, 2)) {
1408                 err("Error initialising mpeg output. :(\n");
1409                 return -EREMOTEIO;
1410         }
1411
1412
1413         /* ???? */
1414         cmd.id = CMD_TUNER_INIT;
1415         cmd.len = 3;
1416         cmd.arg[0] = 0x00;
1417         cmd.arg[1] = 0x10;
1418         cmd.arg[2] = 0x10;
1419         if (cx24120_message_send(state, &cmd)) {
1420                 err("Error sending final init message. :(\n");
1421                 return -EREMOTEIO;
1422         }
1423
1424
1425         /* Firmware CMD 35: Get firmware version */
1426         cmd.id = CMD_FWVERSION;
1427         cmd.len = 1;
1428         for (i = 0; i < 4; i++) {
1429                 cmd.arg[0] = i;
1430                 ret = cx24120_message_send(state, &cmd);
1431                 if (ret != 0)
1432                         return ret;
1433                 vers[i] = cx24120_readreg(state, CX24120_REG_MAILBOX);
1434         }
1435         info("FW version %i.%i.%i.%i\n", vers[0], vers[1], vers[2], vers[3]);
1436
1437
1438         state->cold_init = 1;
1439         return 0;
1440 }
1441
1442
1443 static int cx24120_tune(struct dvb_frontend *fe, bool re_tune,
1444         unsigned int mode_flags, unsigned int *delay, fe_status_t *status)
1445 {
1446         struct cx24120_state *state = fe->demodulator_priv;
1447         int ret;
1448
1449         dev_dbg(&state->i2c->dev, "%s(%d)\n", __func__, re_tune);
1450
1451         /* TODO: Do we need to set delay? */
1452
1453         if (re_tune) {
1454                 ret = cx24120_set_frontend(fe);
1455                 if (ret)
1456                         return ret;
1457         }
1458
1459         return cx24120_read_status(fe, status);
1460 }
1461
1462
1463
1464 static int cx24120_get_algo(struct dvb_frontend *fe)
1465 {
1466         return DVBFE_ALGO_HW;
1467 }
1468
1469
1470 static int cx24120_sleep(struct dvb_frontend *fe)
1471 {
1472         return 0;
1473 }
1474
1475
1476 /*static int cx24120_wakeup(struct dvb_frontend *fe)
1477  * {
1478  *   return 0;
1479  * }
1480 */
1481
1482
1483 static int cx24120_get_frontend(struct dvb_frontend *fe)
1484 {
1485         struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1486         struct cx24120_state *state = fe->demodulator_priv;
1487         u8 freq1, freq2, freq3;
1488
1489         dev_dbg(&state->i2c->dev, "%s()", __func__);
1490
1491         /* don't return empty data if we're not tuned in */
1492         if (state->mpeg_enabled)
1493                 return 0;
1494
1495         /* Get frequency */
1496         freq1 = cx24120_readreg(state, CX24120_REG_FREQ1);
1497         freq2 = cx24120_readreg(state, CX24120_REG_FREQ2);
1498         freq3 = cx24120_readreg(state, CX24120_REG_FREQ3);
1499         c->frequency = (freq3 << 16) | (freq2 << 8) | freq1;
1500         dev_dbg(&state->i2c->dev, "%s frequency = %d\n", __func__,
1501                 c->frequency);
1502
1503         /* Get modulation, fec, pilot */
1504         cx24120_get_fec(fe);
1505
1506         return 0;
1507 }
1508
1509
1510 static void cx24120_release(struct dvb_frontend *fe)
1511 {
1512         struct cx24120_state *state = fe->demodulator_priv;
1513
1514         dev_dbg(&state->i2c->dev, "%s: Clear state structure\n", __func__);
1515         kfree(state);
1516 }
1517
1518
1519 static int cx24120_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
1520 {
1521         struct cx24120_state *state = fe->demodulator_priv;
1522
1523         *ucblocks = (cx24120_readreg(state, CX24120_REG_UCB_H) << 8) |
1524                      cx24120_readreg(state, CX24120_REG_UCB_L);
1525
1526         dev_dbg(&state->i2c->dev, "%s: Blocks = %d\n",
1527                         __func__, *ucblocks);
1528         return 0;
1529 }
1530
1531
1532 static struct dvb_frontend_ops cx24120_ops = {
1533         .delsys = { SYS_DVBS, SYS_DVBS2 },
1534         .info = {
1535                 .name = "Conexant CX24120/CX24118",
1536                 .frequency_min = 950000,
1537                 .frequency_max = 2150000,
1538                 .frequency_stepsize = 1011, /* kHz for QPSK frontends */
1539                 .frequency_tolerance = 5000,
1540                 .symbol_rate_min = 1000000,
1541                 .symbol_rate_max = 45000000,
1542                 .caps = FE_CAN_INVERSION_AUTO |
1543                         FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
1544                         FE_CAN_FEC_4_5 | FE_CAN_FEC_5_6 | FE_CAN_FEC_6_7 |
1545                         FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
1546                         FE_CAN_2G_MODULATION |
1547                         FE_CAN_QPSK | FE_CAN_RECOVER
1548         },
1549         .release =                      cx24120_release,
1550
1551         .init =                         cx24120_init,
1552         .sleep =                        cx24120_sleep,
1553
1554         .tune =                         cx24120_tune,
1555         .get_frontend_algo =            cx24120_get_algo,
1556         .set_frontend =                 cx24120_set_frontend,
1557
1558         .get_frontend =                 cx24120_get_frontend,
1559         .read_status =                  cx24120_read_status,
1560         .read_ber =                     cx24120_read_ber,
1561         .read_signal_strength =         cx24120_read_signal_strength,
1562         .read_snr =                     cx24120_read_snr,
1563         .read_ucblocks =                cx24120_read_ucblocks,
1564
1565         .diseqc_send_master_cmd =       cx24120_send_diseqc_msg,
1566
1567         .diseqc_send_burst =            cx24120_diseqc_send_burst,
1568         .set_tone =                     cx24120_set_tone,
1569         .set_voltage =                  cx24120_set_voltage,
1570 };
1571
1572 MODULE_DESCRIPTION("DVB Frontend module for Conexant CX24120/CX24118 hardware");
1573 MODULE_AUTHOR("Jemma Denson");
1574 MODULE_LICENSE("GPL");