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