2 * Support for OR51211 (pcHDTV HD-2000) - VSB
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.
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/device.h>
30 #include <linux/firmware.h>
31 #include <linux/string.h>
32 #include <linux/slab.h>
33 #include <asm/byteorder.h>
36 #include "dvb_frontend.h"
40 #define dprintk(args...) \
42 if (debug) printk(KERN_DEBUG "or51211: " args); \
45 static u8 run_buf[] = {0x7f,0x01};
46 static u8 cmd_buf[] = {0x04,0x01,0x50,0x80,0x06}; // ATSC
48 struct or51211_state {
50 struct i2c_adapter* i2c;
52 /* Configuration settings */
53 const struct or51211_config* config;
55 struct dvb_frontend frontend;
58 /* Demodulator private data */
60 u32 snr; /* Result of last SNR claculation */
62 /* Tuner private data */
63 u32 current_frequency;
66 static int i2c_writebytes (struct or51211_state* state, u8 reg, const u8 *buf,
76 if ((err = i2c_transfer (state->i2c, &msg, 1)) != 1) {
77 printk(KERN_WARNING "or51211: i2c_writebytes error "
78 "(addr %02x, err == %i)\n", reg, err);
85 static int i2c_readbytes(struct or51211_state *state, u8 reg, u8 *buf, int len)
94 if ((err = i2c_transfer (state->i2c, &msg, 1)) != 1) {
95 printk(KERN_WARNING "or51211: i2c_readbytes error "
96 "(addr %02x, err == %i)\n", reg, err);
103 static int or51211_load_firmware (struct dvb_frontend* fe,
104 const struct firmware *fw)
106 struct or51211_state* state = fe->demodulator_priv;
110 dprintk("Firmware is %zd bytes\n",fw->size);
114 if (i2c_writebytes(state,0x50,tudata,1)) {
115 printk(KERN_WARNING "or51211:load_firmware error eprom addr\n");
118 if (i2c_readbytes(state,0x50,&tudata[145],192)) {
119 printk(KERN_WARNING "or51211: load_firmware error eprom\n");
123 /* Create firmware buffer */
124 for (i = 0; i < 145; i++)
125 tudata[i] = fw->data[i];
127 for (i = 0; i < 248; i++)
128 tudata[i+337] = fw->data[145+i];
130 state->config->reset(fe);
132 if (i2c_writebytes(state,state->config->demod_address,tudata,585)) {
133 printk(KERN_WARNING "or51211: load_firmware error 1\n");
138 if (i2c_writebytes(state,state->config->demod_address,
139 &fw->data[393],8125)) {
140 printk(KERN_WARNING "or51211: load_firmware error 2\n");
145 if (i2c_writebytes(state,state->config->demod_address,run_buf,2)) {
146 printk(KERN_WARNING "or51211: load_firmware error 3\n");
150 /* Wait at least 5 msec */
152 if (i2c_writebytes(state,state->config->demod_address,run_buf,2)) {
153 printk(KERN_WARNING "or51211: load_firmware error 4\n");
158 printk("or51211: Done.\n");
162 static int or51211_setmode(struct dvb_frontend* fe, int mode)
164 struct or51211_state* state = fe->demodulator_priv;
167 state->config->setmode(fe, mode);
169 if (i2c_writebytes(state,state->config->demod_address,run_buf,2)) {
170 printk(KERN_WARNING "or51211: setmode error 1\n");
174 /* Wait at least 5 msec */
176 if (i2c_writebytes(state,state->config->demod_address,run_buf,2)) {
177 printk(KERN_WARNING "or51211: setmode error 2\n");
183 /* Set operation mode in Receiver 1 register;
185 * data 0x50h Automatic sets receiver channel conditions
186 * Automatic NTSC rejection filter
187 * Enable MPEG serial data output
189 * High tuner phase noise
190 * normal +/-150kHz Carrier acquisition range
192 if (i2c_writebytes(state,state->config->demod_address,cmd_buf,3)) {
193 printk(KERN_WARNING "or51211: setmode error 3\n");
202 if (i2c_writebytes(state,state->config->demod_address,rec_buf,3)) {
203 printk(KERN_WARNING "or51211: setmode error 5\n");
206 if (i2c_readbytes(state,state->config->demod_address,&rec_buf[10],2)) {
207 printk(KERN_WARNING "or51211: setmode error 6");
210 dprintk("setmode rec status %02x %02x\n",rec_buf[10],rec_buf[11]);
215 static int or51211_set_parameters(struct dvb_frontend* fe,
216 struct dvb_frontend_parameters *param)
218 struct or51211_state* state = fe->demodulator_priv;
220 /* Change only if we are actually changing the channel */
221 if (state->current_frequency != param->frequency) {
222 if (fe->ops.tuner_ops.set_params) {
223 fe->ops.tuner_ops.set_params(fe, param);
224 if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 0);
227 /* Set to ATSC mode */
228 or51211_setmode(fe,0);
230 /* Update current frequency */
231 state->current_frequency = param->frequency;
236 static int or51211_read_status(struct dvb_frontend* fe, fe_status_t* status)
238 struct or51211_state* state = fe->demodulator_priv;
239 unsigned char rec_buf[2];
240 unsigned char snd_buf[] = {0x04,0x00,0x03,0x00};
243 /* Receiver Status */
244 if (i2c_writebytes(state,state->config->demod_address,snd_buf,3)) {
245 printk(KERN_WARNING "or51132: read_status write error\n");
249 if (i2c_readbytes(state,state->config->demod_address,rec_buf,2)) {
250 printk(KERN_WARNING "or51132: read_status read error\n");
253 dprintk("read_status %x %x\n",rec_buf[0],rec_buf[1]);
255 if (rec_buf[0] & 0x01) { /* Receiver Lock */
256 *status |= FE_HAS_SIGNAL;
257 *status |= FE_HAS_CARRIER;
258 *status |= FE_HAS_VITERBI;
259 *status |= FE_HAS_SYNC;
260 *status |= FE_HAS_LOCK;
265 /* Calculate SNR estimation (scaled by 2^24)
267 8-VSB SNR equation from Oren datasheets
270 SNR[dB] = 10 * log10(219037.9454 / MSE^2 )
272 We re-write the snr equation as:
273 SNR * 2^24 = 10*(c - 2*intlog10(MSE))
274 Where for 8-VSB, c = log10(219037.9454) * 2^24 */
276 static u32 calculate_snr(u32 mse, u32 c)
278 if (mse == 0) /* No signal */
281 mse = 2*intlog10(mse);
283 /* Negative SNR, which is possible, but realisticly the
284 demod will lose lock before the signal gets this bad. The
285 API only allows for unsigned values, so just return 0 */
291 static int or51211_read_snr(struct dvb_frontend* fe, u16* snr)
293 struct or51211_state* state = fe->demodulator_priv;
297 /* SNR after Equalizer */
302 if (i2c_writebytes(state,state->config->demod_address,snd_buf,3)) {
303 printk(KERN_WARNING "%s: error writing snr reg\n",
307 if (i2c_readbytes(state,state->config->demod_address,rec_buf,2)) {
308 printk(KERN_WARNING "%s: read_status read error\n",
313 state->snr = calculate_snr(rec_buf[0], 89599047);
314 *snr = (state->snr) >> 16;
316 dprintk("%s: noise = 0x%02x, snr = %d.%02d dB\n", __func__, rec_buf[0],
317 state->snr >> 24, (((state->snr>>8) & 0xffff) * 100) >> 16);
322 static int or51211_read_signal_strength(struct dvb_frontend* fe, u16* strength)
324 /* Calculate Strength from SNR up to 35dB */
325 /* Even though the SNR can go higher than 35dB, there is some comfort */
326 /* factor in having a range of strong signals that can show at 100% */
327 struct or51211_state* state = (struct or51211_state*)fe->demodulator_priv;
331 ret = fe->ops.read_snr(fe, &snr);
334 /* Rather than use the 8.8 value snr, use state->snr which is 8.24 */
335 /* scale the range 0 - 35*2^24 into 0 - 65535 */
336 if (state->snr >= 8960 * 0x10000)
339 *strength = state->snr / 8960;
344 static int or51211_read_ber(struct dvb_frontend* fe, u32* ber)
350 static int or51211_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
356 static int or51211_sleep(struct dvb_frontend* fe)
361 static int or51211_init(struct dvb_frontend* fe)
363 struct or51211_state* state = fe->demodulator_priv;
364 const struct or51211_config* config = state->config;
365 const struct firmware* fw;
366 unsigned char get_ver_buf[] = {0x04,0x00,0x30,0x00,0x00};
367 unsigned char rec_buf[14];
370 if (!state->initialized) {
371 /* Request the firmware, this will block until it uploads */
372 printk(KERN_INFO "or51211: Waiting for firmware upload "
373 "(%s)...\n", "/*(DEBLOBBED)*/");
374 ret = config->reject_firmware(fe, &fw,
376 printk(KERN_INFO "or51211:Got Hotplug firmware\n");
378 printk(KERN_WARNING "or51211: No firmware uploaded "
379 "(timeout or file not found?)\n");
383 ret = or51211_load_firmware(fe, fw);
384 release_firmware(fw);
386 printk(KERN_WARNING "or51211: Writing firmware to "
390 printk(KERN_INFO "or51211: Firmware upload complete.\n");
392 /* Set operation mode in Receiver 1 register;
394 * data 0x50h Automatic sets receiver channel conditions
395 * Automatic NTSC rejection filter
396 * Enable MPEG serial data output
398 * High tuner phase noise
399 * normal +/-150kHz Carrier acquisition range
401 if (i2c_writebytes(state,state->config->demod_address,
403 printk(KERN_WARNING "or51211: Load DVR Error 5\n");
407 /* Read back ucode version to besure we loaded correctly */
408 /* and are really up and running */
414 if (i2c_writebytes(state,state->config->demod_address,
416 printk(KERN_WARNING "or51211: Load DVR Error A\n");
420 if (i2c_readbytes(state,state->config->demod_address,
422 printk(KERN_WARNING "or51211: Load DVR Error B\n");
431 if (i2c_writebytes(state,state->config->demod_address,
433 printk(KERN_WARNING "or51211: Load DVR Error C\n");
437 if (i2c_readbytes(state,state->config->demod_address,
439 printk(KERN_WARNING "or51211: Load DVR Error D\n");
443 for (i = 0; i < 8; i++)
446 for (i = 0; i < 5; i++) {
448 get_ver_buf[4] = i+1;
449 if (i2c_writebytes(state,state->config->demod_address,
451 printk(KERN_WARNING "or51211:Load DVR Error 6"
457 if (i2c_readbytes(state,state->config->demod_address,
459 printk(KERN_WARNING "or51211:Load DVR Error 7"
463 /* If we didn't receive the right index, try again */
464 if ((int)rec_buf[i*2+1]!=i+1){
468 dprintk("read_fwbits %x %x %x %x %x %x %x %x %x %x\n",
469 rec_buf[0], rec_buf[1], rec_buf[2], rec_buf[3],
470 rec_buf[4], rec_buf[5], rec_buf[6], rec_buf[7],
471 rec_buf[8], rec_buf[9]);
473 printk(KERN_INFO "or51211: ver TU%02x%02x%02x VSB mode %02x"
475 rec_buf[2], rec_buf[4],rec_buf[6],
476 rec_buf[12],rec_buf[10]);
483 if (i2c_writebytes(state,state->config->demod_address,
485 printk(KERN_WARNING "or51211: Load DVR Error 8\n");
489 if (i2c_readbytes(state,state->config->demod_address,
491 printk(KERN_WARNING "or51211: Load DVR Error 9\n");
494 state->initialized = 1;
500 static int or51211_get_tune_settings(struct dvb_frontend* fe,
501 struct dvb_frontend_tune_settings* fesettings)
503 fesettings->min_delay_ms = 500;
504 fesettings->step_size = 0;
505 fesettings->max_drift = 0;
509 static void or51211_release(struct dvb_frontend* fe)
511 struct or51211_state* state = fe->demodulator_priv;
512 state->config->sleep(fe);
516 static struct dvb_frontend_ops or51211_ops;
518 struct dvb_frontend* or51211_attach(const struct or51211_config* config,
519 struct i2c_adapter* i2c)
521 struct or51211_state* state = NULL;
523 /* Allocate memory for the internal state */
524 state = kzalloc(sizeof(struct or51211_state), GFP_KERNEL);
528 /* Setup the state */
529 state->config = config;
531 state->initialized = 0;
532 state->current_frequency = 0;
534 /* Create dvb_frontend */
535 memcpy(&state->frontend.ops, &or51211_ops, sizeof(struct dvb_frontend_ops));
536 state->frontend.demodulator_priv = state;
537 return &state->frontend;
540 static struct dvb_frontend_ops or51211_ops = {
543 .name = "Oren OR51211 VSB Frontend",
545 .frequency_min = 44000000,
546 .frequency_max = 958000000,
547 .frequency_stepsize = 166666,
548 .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
549 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
553 .release = or51211_release,
555 .init = or51211_init,
556 .sleep = or51211_sleep,
558 .set_frontend = or51211_set_parameters,
559 .get_tune_settings = or51211_get_tune_settings,
561 .read_status = or51211_read_status,
562 .read_ber = or51211_read_ber,
563 .read_signal_strength = or51211_read_signal_strength,
564 .read_snr = or51211_read_snr,
565 .read_ucblocks = or51211_read_ucblocks,
568 module_param(debug, int, 0644);
569 MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
571 MODULE_DESCRIPTION("Oren OR51211 VSB [pcHDTV HD-2000] Demodulator Driver");
572 MODULE_AUTHOR("Kirk Lapray");
573 MODULE_LICENSE("GPL");
575 EXPORT_SYMBOL(or51211_attach);