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