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>
44 #include "dvb_frontend.h"
49 #define dprintk(args...) \
51 if (debug) printk(KERN_DEBUG "or51132: " args); \
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)
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);
89 static u8 i2c_readbytes (struct or51132_state* state, u8 reg, u8* buf, int len)
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);
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};
112 u32 firmwareAsize, firmwareBsize;
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");
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");
137 if ((ret = i2c_writebytes(state,state->config->demod_address,
139 printk(KERN_WARNING "or51132: load_firmware error 3\n");
143 /* Wait at least 5 msec */
144 msleep(20); /* 10ms */
146 if ((ret = i2c_writebytes(state,state->config->demod_address,
148 printk(KERN_WARNING "or51132: load_firmware error 4\n");
152 /* 50ms for operation to begin */
155 /* Read back ucode version to besure we loaded correctly and are really up and running */
156 /* Get uCode version */
160 msleep(20); /* 20ms */
161 if ((ret = i2c_writebytes(state,state->config->demod_address,
163 printk(KERN_WARNING "or51132: load_firmware error a\n");
169 msleep(20); /* 20ms */
170 if ((ret = i2c_writebytes(state,state->config->demod_address,
172 printk(KERN_WARNING "or51132: load_firmware error b\n");
178 msleep(20); /* 20ms */
179 if ((ret = i2c_writebytes(state,state->config->demod_address,
181 printk(KERN_WARNING "or51132: load_firmware error c\n");
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
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,
195 "or51132: load_firmware error d - %d\n",i);
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);
210 msleep(20); /* 20ms */
211 if ((ret = i2c_writebytes(state,state->config->demod_address,
213 printk(KERN_WARNING "or51132: load_firmware error e\n");
219 static int or51132_init(struct dvb_frontend* fe)
224 static int or51132_read_ber(struct dvb_frontend* fe, u32* ber)
230 static int or51132_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
236 static int or51132_sleep(struct dvb_frontend* fe)
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; */
250 switch (state->current_modulation) {
254 /* Auto-deinterleave; MPEG ser, MPEG2tr, phase noise-high*/
258 /* Auto CH, Auto NTSC rej, MPEGser, MPEG2tr, phase noise-high*/
262 printk("setmode:Modulation set to unsupported value\n");
264 if (i2c_writebytes(state,state->config->demod_address,
266 printk(KERN_WARNING "or51132: set_mode error 1\n");
269 dprintk("or51132: set #1 to %02x\n", cmd_buf[2]);
271 /* Set operation mode in Receiver 6 register */
273 switch (state->current_modulation) {
275 /* REC MODE Normal Carrier Lock */
277 /* Channel MODE Auto QAM64/256 */
281 /* REC MODE Normal Carrier Lock */
283 /* Channel MODE QAM256 */
287 /* REC MODE Normal Carrier Lock */
289 /* Channel MODE QAM64 */
293 /* REC MODE inv IF spectrum, Normal */
295 /* Channel MODE ATSC/VSB8 */
299 printk("setmode: Modulation set to unsupported value\n");
301 msleep(20); /* 20ms */
302 if (i2c_writebytes(state,state->config->demod_address,
304 printk(KERN_WARNING "or51132: set_mode error 2\n");
307 dprintk("or51132: set #6 to 0x%02x%02x\n", cmd_buf[1], cmd_buf[2]);
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)
321 return MOD_FWCLASS_VSB;
325 return MOD_FWCLASS_QAM;
327 return MOD_FWCLASS_UNKNOWN;
331 static int or51132_set_parameters(struct dvb_frontend* fe,
332 struct dvb_frontend_parameters *param)
335 struct or51132_state* state = fe->demodulator_priv;
336 const struct firmware *fw;
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 */
351 case MOD_FWCLASS_QAM:
352 dprintk("set_parameters QAM MODE\n");
353 fwname = OR51132_QAM_FIRMWARE;
355 /* Set punctured clock for QAM */
359 printk("or51132: Modulation type(%d) UNSUPPORTED\n",
360 param->u.vsb.modulation);
363 printk("or51132: Waiting for firmware upload(%s)...\n",
365 ret = request_firmware(&fw, fwname, &state->i2c->dev);
367 printk(KERN_WARNING "or51132: No firmware up"
368 "loaded(timeout or file not found?)\n");
371 ret = or51132_load_firmware(fe, fw);
372 release_firmware(fw);
374 printk(KERN_WARNING "or51132: Writing firmware to "
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;
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 */
395 /* Update current frequency */
396 state->current_frequency = param->frequency;
400 static int or51132_get_parameters(struct dvb_frontend* fe,
401 struct dvb_frontend_parameters *param)
403 struct or51132_state* state = fe->demodulator_priv;
406 /* Receiver Status */
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");
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");
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;
424 printk(KERN_WARNING "or51132: unknown status 0x%02x\n",
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;
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];
445 /* Receiver Status */
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");
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");
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;
470 /* Calculate SNR estimation (scaled by 2^24)
472 8-VSB SNR and QAM equations from Oren datasheets
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
481 SNR[dB] = 10 * log10(897152044.8282 / MSE^2 )
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 */
496 mse = 2*intlog10(mse);
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 */
506 static int or51132_read_snr(struct dvb_frontend* fe, u16* snr)
508 struct or51132_state* state = fe->demodulator_priv;
515 /* Register is same for VSB or QAM firmware */
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");
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");
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 */
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");
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");
547 switch (rec_buf[0]) {
549 usK = (rec_buf[1] & 0x10) ? 0x03000000 : 0;
550 /* Fall through to QAM64 case */
558 printk(KERN_ERR "or51132: unknown status 0x%02x\n", rec_buf[0]);
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);
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;
583 ret = fe->ops.read_snr(fe, &snr);
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)
591 *strength = state->snr / 8960;
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;
605 static void or51132_release(struct dvb_frontend* fe)
607 struct or51132_state* state = fe->demodulator_priv;
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);
623 /* Setup the state */
624 state->config = config;
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;
639 static struct dvb_frontend_ops or51132_ops = {
642 .name = "Oren OR51132 VSB/QAM Frontend",
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 |
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);