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