Linux 2.6.21
[linux/fpc-iii.git] / drivers / media / dvb / frontends / or51132.c
blob5a3a6e53cda25877b6e97bc28f310a05688cab3c
1 /*
2 * Support for OR51132 (pcHDTV HD-3000) - VSB/QAM
4 * Copyright (C) 2005 Kirk Lapray <kirk_lapray@bigfoot.com>
6 * Based on code from Jack Kelliher (kelliher@xmission.com)
7 * Copyright (C) 2002 & pcHDTV, inc.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 * This driver needs two external firmware files. Please copy
27 * "dvb-fe-or51132-vsb.fw" and "dvb-fe-or51132-qam.fw" to
28 * /usr/lib/hotplug/firmware/ or /lib/firmware/
29 * (depending on configuration of firmware hotplug).
31 #define OR51132_VSB_FIRMWARE "dvb-fe-or51132-vsb.fw"
32 #define OR51132_QAM_FIRMWARE "dvb-fe-or51132-qam.fw"
34 #include <linux/kernel.h>
35 #include <linux/module.h>
36 #include <linux/moduleparam.h>
37 #include <linux/init.h>
38 #include <linux/delay.h>
39 #include <linux/string.h>
40 #include <linux/slab.h>
41 #include <asm/byteorder.h>
43 #include "dvb_math.h"
44 #include "dvb_frontend.h"
45 #include "dvb-pll.h"
46 #include "or51132.h"
48 static int debug;
49 #define dprintk(args...) \
50 do { \
51 if (debug) printk(KERN_DEBUG "or51132: " args); \
52 } while (0)
55 struct or51132_state
57 struct i2c_adapter* i2c;
59 /* Configuration settings */
60 const struct or51132_config* config;
62 struct dvb_frontend frontend;
64 /* Demodulator private data */
65 fe_modulation_t current_modulation;
66 u32 snr; /* Result of last SNR calculation */
68 /* Tuner private data */
69 u32 current_frequency;
72 static int i2c_writebytes (struct or51132_state* state, u8 reg, u8 *buf, int len)
74 int err;
75 struct i2c_msg msg;
76 msg.addr = reg;
77 msg.flags = 0;
78 msg.len = len;
79 msg.buf = buf;
81 if ((err = i2c_transfer(state->i2c, &msg, 1)) != 1) {
82 printk(KERN_WARNING "or51132: i2c_writebytes error (addr %02x, err == %i)\n", reg, err);
83 return -EREMOTEIO;
86 return 0;
89 static u8 i2c_readbytes (struct or51132_state* state, u8 reg, u8* buf, int len)
91 int err;
92 struct i2c_msg msg;
93 msg.addr = reg;
94 msg.flags = I2C_M_RD;
95 msg.len = len;
96 msg.buf = buf;
98 if ((err = i2c_transfer(state->i2c, &msg, 1)) != 1) {
99 printk(KERN_WARNING "or51132: i2c_readbytes error (addr %02x, err == %i)\n", reg, err);
100 return -EREMOTEIO;
103 return 0;
106 static int or51132_load_firmware (struct dvb_frontend* fe, const struct firmware *fw)
108 struct or51132_state* state = fe->demodulator_priv;
109 static u8 run_buf[] = {0x7F,0x01};
110 u8 rec_buf[8];
111 u8 cmd_buf[3];
112 u32 firmwareAsize, firmwareBsize;
113 int i,ret;
115 dprintk("Firmware is %Zd bytes\n",fw->size);
117 /* Get size of firmware A and B */
118 firmwareAsize = le32_to_cpu(*((u32*)fw->data));
119 dprintk("FirmwareA is %i bytes\n",firmwareAsize);
120 firmwareBsize = le32_to_cpu(*((u32*)(fw->data+4)));
121 dprintk("FirmwareB is %i bytes\n",firmwareBsize);
123 /* Upload firmware */
124 if ((ret = i2c_writebytes(state,state->config->demod_address,
125 &fw->data[8],firmwareAsize))) {
126 printk(KERN_WARNING "or51132: load_firmware error 1\n");
127 return ret;
129 msleep(1); /* 1ms */
130 if ((ret = i2c_writebytes(state,state->config->demod_address,
131 &fw->data[8+firmwareAsize],firmwareBsize))) {
132 printk(KERN_WARNING "or51132: load_firmware error 2\n");
133 return ret;
135 msleep(1); /* 1ms */
137 if ((ret = i2c_writebytes(state,state->config->demod_address,
138 run_buf,2))) {
139 printk(KERN_WARNING "or51132: load_firmware error 3\n");
140 return ret;
143 /* Wait at least 5 msec */
144 msleep(20); /* 10ms */
146 if ((ret = i2c_writebytes(state,state->config->demod_address,
147 run_buf,2))) {
148 printk(KERN_WARNING "or51132: load_firmware error 4\n");
149 return ret;
152 /* 50ms for operation to begin */
153 msleep(50);
155 /* Read back ucode version to besure we loaded correctly and are really up and running */
156 /* Get uCode version */
157 cmd_buf[0] = 0x10;
158 cmd_buf[1] = 0x10;
159 cmd_buf[2] = 0x00;
160 msleep(20); /* 20ms */
161 if ((ret = i2c_writebytes(state,state->config->demod_address,
162 cmd_buf,3))) {
163 printk(KERN_WARNING "or51132: load_firmware error a\n");
164 return ret;
167 cmd_buf[0] = 0x04;
168 cmd_buf[1] = 0x17;
169 msleep(20); /* 20ms */
170 if ((ret = i2c_writebytes(state,state->config->demod_address,
171 cmd_buf,2))) {
172 printk(KERN_WARNING "or51132: load_firmware error b\n");
173 return ret;
176 cmd_buf[0] = 0x00;
177 cmd_buf[1] = 0x00;
178 msleep(20); /* 20ms */
179 if ((ret = i2c_writebytes(state,state->config->demod_address,
180 cmd_buf,2))) {
181 printk(KERN_WARNING "or51132: load_firmware error c\n");
182 return ret;
185 for(i=0;i<4;i++) {
186 msleep(20); /* 20ms */
187 /* Once upon a time, this command might have had something
188 to do with getting the firmware version, but it's
189 not used anymore:
190 {0x04,0x00,0x30,0x00,i+1} */
191 /* Read 8 bytes, two bytes at a time */
192 if ((ret = i2c_readbytes(state,state->config->demod_address,
193 &rec_buf[i*2],2))) {
194 printk(KERN_WARNING
195 "or51132: load_firmware error d - %d\n",i);
196 return ret;
200 printk(KERN_WARNING
201 "or51132: Version: %02X%02X%02X%02X-%02X%02X%02X%02X (%02X%01X-%01X-%02X%01X-%01X)\n",
202 rec_buf[1],rec_buf[0],rec_buf[3],rec_buf[2],
203 rec_buf[5],rec_buf[4],rec_buf[7],rec_buf[6],
204 rec_buf[3],rec_buf[2]>>4,rec_buf[2]&0x0f,
205 rec_buf[5],rec_buf[4]>>4,rec_buf[4]&0x0f);
207 cmd_buf[0] = 0x10;
208 cmd_buf[1] = 0x00;
209 cmd_buf[2] = 0x00;
210 msleep(20); /* 20ms */
211 if ((ret = i2c_writebytes(state,state->config->demod_address,
212 cmd_buf,3))) {
213 printk(KERN_WARNING "or51132: load_firmware error e\n");
214 return ret;
216 return 0;
219 static int or51132_init(struct dvb_frontend* fe)
221 return 0;
224 static int or51132_read_ber(struct dvb_frontend* fe, u32* ber)
226 *ber = 0;
227 return 0;
230 static int or51132_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
232 *ucblocks = 0;
233 return 0;
236 static int or51132_sleep(struct dvb_frontend* fe)
238 return 0;
241 static int or51132_setmode(struct dvb_frontend* fe)
243 struct or51132_state* state = fe->demodulator_priv;
244 unsigned char cmd_buf[3];
246 dprintk("setmode %d\n",(int)state->current_modulation);
247 /* set operation mode in Receiver 1 register; */
248 cmd_buf[0] = 0x04;
249 cmd_buf[1] = 0x01;
250 switch (state->current_modulation) {
251 case QAM_256:
252 case QAM_64:
253 case QAM_AUTO:
254 /* Auto-deinterleave; MPEG ser, MPEG2tr, phase noise-high*/
255 cmd_buf[2] = 0x5F;
256 break;
257 case VSB_8:
258 /* Auto CH, Auto NTSC rej, MPEGser, MPEG2tr, phase noise-high*/
259 cmd_buf[2] = 0x50;
260 break;
261 default:
262 printk("setmode:Modulation set to unsupported value\n");
264 if (i2c_writebytes(state,state->config->demod_address,
265 cmd_buf,3)) {
266 printk(KERN_WARNING "or51132: set_mode error 1\n");
267 return -1;
269 dprintk("or51132: set #1 to %02x\n", cmd_buf[2]);
271 /* Set operation mode in Receiver 6 register */
272 cmd_buf[0] = 0x1C;
273 switch (state->current_modulation) {
274 case QAM_AUTO:
275 /* REC MODE Normal Carrier Lock */
276 cmd_buf[1] = 0x00;
277 /* Channel MODE Auto QAM64/256 */
278 cmd_buf[2] = 0x4f;
279 break;
280 case QAM_256:
281 /* REC MODE Normal Carrier Lock */
282 cmd_buf[1] = 0x00;
283 /* Channel MODE QAM256 */
284 cmd_buf[2] = 0x45;
285 break;
286 case QAM_64:
287 /* REC MODE Normal Carrier Lock */
288 cmd_buf[1] = 0x00;
289 /* Channel MODE QAM64 */
290 cmd_buf[2] = 0x43;
291 break;
292 case VSB_8:
293 /* REC MODE inv IF spectrum, Normal */
294 cmd_buf[1] = 0x03;
295 /* Channel MODE ATSC/VSB8 */
296 cmd_buf[2] = 0x06;
297 break;
298 default:
299 printk("setmode: Modulation set to unsupported value\n");
301 msleep(20); /* 20ms */
302 if (i2c_writebytes(state,state->config->demod_address,
303 cmd_buf,3)) {
304 printk(KERN_WARNING "or51132: set_mode error 2\n");
305 return -1;
307 dprintk("or51132: set #6 to 0x%02x%02x\n", cmd_buf[1], cmd_buf[2]);
309 return 0;
312 /* Some modulations use the same firmware. This classifies modulations
313 by the firmware they use. */
314 #define MOD_FWCLASS_UNKNOWN 0
315 #define MOD_FWCLASS_VSB 1
316 #define MOD_FWCLASS_QAM 2
317 static int modulation_fw_class(fe_modulation_t modulation)
319 switch(modulation) {
320 case VSB_8:
321 return MOD_FWCLASS_VSB;
322 case QAM_AUTO:
323 case QAM_64:
324 case QAM_256:
325 return MOD_FWCLASS_QAM;
326 default:
327 return MOD_FWCLASS_UNKNOWN;
331 static int or51132_set_parameters(struct dvb_frontend* fe,
332 struct dvb_frontend_parameters *param)
334 int ret;
335 struct or51132_state* state = fe->demodulator_priv;
336 const struct firmware *fw;
337 const char *fwname;
338 int clock_mode;
340 /* Upload new firmware only if we need a different one */
341 if (modulation_fw_class(state->current_modulation) !=
342 modulation_fw_class(param->u.vsb.modulation)) {
343 switch(modulation_fw_class(param->u.vsb.modulation)) {
344 case MOD_FWCLASS_VSB:
345 dprintk("set_parameters VSB MODE\n");
346 fwname = OR51132_VSB_FIRMWARE;
348 /* Set non-punctured clock for VSB */
349 clock_mode = 0;
350 break;
351 case MOD_FWCLASS_QAM:
352 dprintk("set_parameters QAM MODE\n");
353 fwname = OR51132_QAM_FIRMWARE;
355 /* Set punctured clock for QAM */
356 clock_mode = 1;
357 break;
358 default:
359 printk("or51132: Modulation type(%d) UNSUPPORTED\n",
360 param->u.vsb.modulation);
361 return -1;
363 printk("or51132: Waiting for firmware upload(%s)...\n",
364 fwname);
365 ret = request_firmware(&fw, fwname, &state->i2c->dev);
366 if (ret) {
367 printk(KERN_WARNING "or51132: No firmware up"
368 "loaded(timeout or file not found?)\n");
369 return ret;
371 ret = or51132_load_firmware(fe, fw);
372 release_firmware(fw);
373 if (ret) {
374 printk(KERN_WARNING "or51132: Writing firmware to "
375 "device failed!\n");
376 return ret;
378 printk("or51132: Firmware upload complete.\n");
379 state->config->set_ts_params(fe, clock_mode);
381 /* Change only if we are actually changing the modulation */
382 if (state->current_modulation != param->u.vsb.modulation) {
383 state->current_modulation = param->u.vsb.modulation;
384 or51132_setmode(fe);
387 if (fe->ops.tuner_ops.set_params) {
388 fe->ops.tuner_ops.set_params(fe, param);
389 if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 0);
392 /* Set to current mode */
393 or51132_setmode(fe);
395 /* Update current frequency */
396 state->current_frequency = param->frequency;
397 return 0;
400 static int or51132_get_parameters(struct dvb_frontend* fe,
401 struct dvb_frontend_parameters *param)
403 struct or51132_state* state = fe->demodulator_priv;
404 u8 buf[2];
406 /* Receiver Status */
407 buf[0]=0x04;
408 buf[1]=0x00;
409 msleep(30); /* 30ms */
410 if (i2c_writebytes(state,state->config->demod_address,buf,2)) {
411 printk(KERN_WARNING "or51132: get_parameters write error\n");
412 return -EREMOTEIO;
414 msleep(30); /* 30ms */
415 if (i2c_readbytes(state,state->config->demod_address,buf,2)) {
416 printk(KERN_WARNING "or51132: get_parameters read error\n");
417 return -EREMOTEIO;
419 switch(buf[0]) {
420 case 0x06: param->u.vsb.modulation = VSB_8; break;
421 case 0x43: param->u.vsb.modulation = QAM_64; break;
422 case 0x45: param->u.vsb.modulation = QAM_256; break;
423 default:
424 printk(KERN_WARNING "or51132: unknown status 0x%02x\n",
425 buf[0]);
426 return -EREMOTEIO;
429 /* FIXME: Read frequency from frontend, take AFC into account */
430 param->frequency = state->current_frequency;
432 /* FIXME: How to read inversion setting? Receiver 6 register? */
433 param->inversion = INVERSION_AUTO;
435 return 0;
438 static int or51132_read_status(struct dvb_frontend* fe, fe_status_t* status)
440 struct or51132_state* state = fe->demodulator_priv;
441 unsigned char rec_buf[2];
442 unsigned char snd_buf[2];
443 *status = 0;
445 /* Receiver Status */
446 snd_buf[0]=0x04;
447 snd_buf[1]=0x00;
448 msleep(30); /* 30ms */
449 if (i2c_writebytes(state,state->config->demod_address,snd_buf,2)) {
450 printk(KERN_WARNING "or51132: read_status write error\n");
451 return -1;
453 msleep(30); /* 30ms */
454 if (i2c_readbytes(state,state->config->demod_address,rec_buf,2)) {
455 printk(KERN_WARNING "or51132: read_status read error\n");
456 return -1;
458 dprintk("read_status %x %x\n",rec_buf[0],rec_buf[1]);
460 if (rec_buf[1] & 0x01) { /* Receiver Lock */
461 *status |= FE_HAS_SIGNAL;
462 *status |= FE_HAS_CARRIER;
463 *status |= FE_HAS_VITERBI;
464 *status |= FE_HAS_SYNC;
465 *status |= FE_HAS_LOCK;
467 return 0;
470 /* Calculate SNR estimation (scaled by 2^24)
472 8-VSB SNR and QAM equations from Oren datasheets
474 For 8-VSB:
475 SNR[dB] = 10 * log10(897152044.8282 / MSE^2 ) - K
477 Where K = 0 if NTSC rejection filter is OFF; and
478 K = 3 if NTSC rejection filter is ON
480 For QAM64:
481 SNR[dB] = 10 * log10(897152044.8282 / MSE^2 )
483 For QAM256:
484 SNR[dB] = 10 * log10(907832426.314266 / MSE^2 )
486 We re-write the snr equation as:
487 SNR * 2^24 = 10*(c - 2*intlog10(MSE))
488 Where for QAM256, c = log10(907832426.314266) * 2^24
489 and for 8-VSB and QAM64, c = log10(897152044.8282) * 2^24 */
491 static u32 calculate_snr(u32 mse, u32 c)
493 if (mse == 0) /* No signal */
494 return 0;
496 mse = 2*intlog10(mse);
497 if (mse > c) {
498 /* Negative SNR, which is possible, but realisticly the
499 demod will lose lock before the signal gets this bad. The
500 API only allows for unsigned values, so just return 0 */
501 return 0;
503 return 10*(c - mse);
506 static int or51132_read_snr(struct dvb_frontend* fe, u16* snr)
508 struct or51132_state* state = fe->demodulator_priv;
509 u8 rec_buf[2];
510 u8 snd_buf[2];
511 u32 noise;
512 u32 c;
513 u32 usK;
515 /* Register is same for VSB or QAM firmware */
516 snd_buf[0]=0x04;
517 snd_buf[1]=0x02; /* SNR after Equalizer */
518 msleep(30); /* 30ms */
519 if (i2c_writebytes(state,state->config->demod_address,snd_buf,2)) {
520 printk(KERN_WARNING "or51132: snr write error\n");
521 return -EREMOTEIO;
523 msleep(30); /* 30ms */
524 if (i2c_readbytes(state,state->config->demod_address,rec_buf,2)) {
525 printk(KERN_WARNING "or51132: snr read error\n");
526 return -EREMOTEIO;
528 noise = rec_buf[0] | (rec_buf[1] << 8);
529 dprintk("read_snr noise %x %x (%i)\n",rec_buf[0],rec_buf[1],noise);
531 /* Read status, contains modulation type for QAM_AUTO and
532 NTSC filter for VSB */
533 snd_buf[0]=0x04;
534 snd_buf[1]=0x00; /* Status register */
535 msleep(30); /* 30ms */
536 if (i2c_writebytes(state,state->config->demod_address,snd_buf,2)) {
537 printk(KERN_WARNING "or51132: status write error\n");
538 return -EREMOTEIO;
540 msleep(30); /* 30ms */
541 if (i2c_readbytes(state,state->config->demod_address,rec_buf,2)) {
542 printk(KERN_WARNING "or51132: status read error\n");
543 return -EREMOTEIO;
546 usK = 0;
547 switch (rec_buf[0]) {
548 case 0x06:
549 usK = (rec_buf[1] & 0x10) ? 0x03000000 : 0;
550 /* Fall through to QAM64 case */
551 case 0x43:
552 c = 150204167;
553 break;
554 case 0x45:
555 c = 150290396;
556 break;
557 default:
558 printk(KERN_ERR "or51132: unknown status 0x%02x\n", rec_buf[0]);
559 return -EREMOTEIO;
561 dprintk("%s: modulation %02x, NTSC rej O%s\n", __FUNCTION__,
562 rec_buf[0], rec_buf[1]&0x10?"n":"ff");
564 /* Calculate SNR using noise, c, and NTSC rejection correction */
565 state->snr = calculate_snr(noise, c) - usK;
566 *snr = (state->snr) >> 16;
568 dprintk("%s: noise = 0x%08x, snr = %d.%02d dB\n", __FUNCTION__, noise,
569 state->snr >> 24, (((state->snr>>8) & 0xffff) * 100) >> 16);
571 return 0;
574 static int or51132_read_signal_strength(struct dvb_frontend* fe, u16* strength)
576 /* Calculate Strength from SNR up to 35dB */
577 /* Even though the SNR can go higher than 35dB, there is some comfort */
578 /* factor in having a range of strong signals that can show at 100% */
579 struct or51132_state* state = (struct or51132_state*) fe->demodulator_priv;
580 u16 snr;
581 int ret;
583 ret = fe->ops.read_snr(fe, &snr);
584 if (ret != 0)
585 return ret;
586 /* Rather than use the 8.8 value snr, use state->snr which is 8.24 */
587 /* scale the range 0 - 35*2^24 into 0 - 65535 */
588 if (state->snr >= 8960 * 0x10000)
589 *strength = 0xffff;
590 else
591 *strength = state->snr / 8960;
593 return 0;
596 static int or51132_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings* fe_tune_settings)
598 fe_tune_settings->min_delay_ms = 500;
599 fe_tune_settings->step_size = 0;
600 fe_tune_settings->max_drift = 0;
602 return 0;
605 static void or51132_release(struct dvb_frontend* fe)
607 struct or51132_state* state = fe->demodulator_priv;
608 kfree(state);
611 static struct dvb_frontend_ops or51132_ops;
613 struct dvb_frontend* or51132_attach(const struct or51132_config* config,
614 struct i2c_adapter* i2c)
616 struct or51132_state* state = NULL;
618 /* Allocate memory for the internal state */
619 state = kmalloc(sizeof(struct or51132_state), GFP_KERNEL);
620 if (state == NULL)
621 goto error;
623 /* Setup the state */
624 state->config = config;
625 state->i2c = i2c;
626 state->current_frequency = -1;
627 state->current_modulation = -1;
629 /* Create dvb_frontend */
630 memcpy(&state->frontend.ops, &or51132_ops, sizeof(struct dvb_frontend_ops));
631 state->frontend.demodulator_priv = state;
632 return &state->frontend;
634 error:
635 kfree(state);
636 return NULL;
639 static struct dvb_frontend_ops or51132_ops = {
641 .info = {
642 .name = "Oren OR51132 VSB/QAM Frontend",
643 .type = FE_ATSC,
644 .frequency_min = 44000000,
645 .frequency_max = 958000000,
646 .frequency_stepsize = 166666,
647 .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
648 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
649 FE_CAN_QAM_64 | FE_CAN_QAM_256 | FE_CAN_QAM_AUTO |
650 FE_CAN_8VSB
653 .release = or51132_release,
655 .init = or51132_init,
656 .sleep = or51132_sleep,
658 .set_frontend = or51132_set_parameters,
659 .get_frontend = or51132_get_parameters,
660 .get_tune_settings = or51132_get_tune_settings,
662 .read_status = or51132_read_status,
663 .read_ber = or51132_read_ber,
664 .read_signal_strength = or51132_read_signal_strength,
665 .read_snr = or51132_read_snr,
666 .read_ucblocks = or51132_read_ucblocks,
669 module_param(debug, int, 0644);
670 MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
672 MODULE_DESCRIPTION("OR51132 ATSC [pcHDTV HD-3000] (8VSB & ITU J83 AnnexB FEC QAM64/256) Demodulator Driver");
673 MODULE_AUTHOR("Kirk Lapray");
674 MODULE_LICENSE("GPL");
676 EXPORT_SYMBOL(or51132_attach);
679 * Local variables:
680 * c-basic-offset: 8
681 * End: