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.
26 * This driver needs external firmware. Please use the command
27 * "<kerneldir>/Documentation/dvb/get_dvb_firmware or51211" to
28 * download/extract it, and then copy it to /usr/lib/hotplug/firmware
29 * or /lib/firmware (depending on configuration of firmware hotplug).
31 #define OR51211_DEFAULT_FIRMWARE "dvb-fe-or51211.fw"
33 #include <linux/kernel.h>
34 #include <linux/module.h>
35 #include <linux/moduleparam.h>
36 #include <linux/device.h>
37 #include <linux/firmware.h>
38 #include <linux/string.h>
39 #include <linux/slab.h>
40 #include <asm/byteorder.h>
43 #include "dvb_frontend.h"
47 #define dprintk(args...) \
49 if (debug) printk(KERN_DEBUG "or51211: " args); \
52 static u8 run_buf[] = {0x7f,0x01};
53 static u8 cmd_buf[] = {0x04,0x01,0x50,0x80,0x06}; // ATSC
55 struct or51211_state {
57 struct i2c_adapter* i2c;
59 /* Configuration settings */
60 const struct or51211_config* config;
62 struct dvb_frontend frontend;
65 /* Demodulator private data */
67 u32 snr; /* Result of last SNR claculation */
69 /* Tuner private data */
70 u32 current_frequency;
73 static int i2c_writebytes (struct or51211_state* state, u8 reg, u8 *buf,
83 if ((err = i2c_transfer (state->i2c, &msg, 1)) != 1) {
84 printk(KERN_WARNING "or51211: i2c_writebytes error "
85 "(addr %02x, err == %i)\n", reg, err);
92 static u8 i2c_readbytes (struct or51211_state* state, u8 reg, u8* buf, int len)
101 if ((err = i2c_transfer (state->i2c, &msg, 1)) != 1) {
102 printk(KERN_WARNING "or51211: i2c_readbytes error "
103 "(addr %02x, err == %i)\n", reg, err);
110 static int or51211_load_firmware (struct dvb_frontend* fe,
111 const struct firmware *fw)
113 struct or51211_state* state = fe->demodulator_priv;
117 dprintk("Firmware is %zd bytes\n",fw->size);
121 if (i2c_writebytes(state,0x50,tudata,1)) {
122 printk(KERN_WARNING "or51211:load_firmware error eprom addr\n");
125 if (i2c_readbytes(state,0x50,&tudata[145],192)) {
126 printk(KERN_WARNING "or51211: load_firmware error eprom\n");
130 /* Create firmware buffer */
131 for (i = 0; i < 145; i++)
132 tudata[i] = fw->data[i];
134 for (i = 0; i < 248; i++)
135 tudata[i+337] = fw->data[145+i];
137 state->config->reset(fe);
139 if (i2c_writebytes(state,state->config->demod_address,tudata,585)) {
140 printk(KERN_WARNING "or51211: load_firmware error 1\n");
145 if (i2c_writebytes(state,state->config->demod_address,
146 &fw->data[393],8125)) {
147 printk(KERN_WARNING "or51211: load_firmware error 2\n");
152 if (i2c_writebytes(state,state->config->demod_address,run_buf,2)) {
153 printk(KERN_WARNING "or51211: load_firmware error 3\n");
157 /* Wait at least 5 msec */
159 if (i2c_writebytes(state,state->config->demod_address,run_buf,2)) {
160 printk(KERN_WARNING "or51211: load_firmware error 4\n");
165 printk("or51211: Done.\n");
169 static int or51211_setmode(struct dvb_frontend* fe, int mode)
171 struct or51211_state* state = fe->demodulator_priv;
174 state->config->setmode(fe, mode);
176 if (i2c_writebytes(state,state->config->demod_address,run_buf,2)) {
177 printk(KERN_WARNING "or51211: setmode error 1\n");
181 /* Wait at least 5 msec */
183 if (i2c_writebytes(state,state->config->demod_address,run_buf,2)) {
184 printk(KERN_WARNING "or51211: setmode error 2\n");
190 /* Set operation mode in Receiver 1 register;
192 * data 0x50h Automatic sets receiver channel conditions
193 * Automatic NTSC rejection filter
194 * Enable MPEG serial data output
196 * High tuner phase noise
197 * normal +/-150kHz Carrier acquisition range
199 if (i2c_writebytes(state,state->config->demod_address,cmd_buf,3)) {
200 printk(KERN_WARNING "or51211: setmode error 3\n");
209 if (i2c_writebytes(state,state->config->demod_address,rec_buf,3)) {
210 printk(KERN_WARNING "or51211: setmode error 5\n");
213 if (i2c_readbytes(state,state->config->demod_address,&rec_buf[10],2)) {
214 printk(KERN_WARNING "or51211: setmode error 6");
217 dprintk("setmode rec status %02x %02x\n",rec_buf[10],rec_buf[11]);
222 static int or51211_set_parameters(struct dvb_frontend* fe,
223 struct dvb_frontend_parameters *param)
225 struct or51211_state* state = fe->demodulator_priv;
230 /* Change only if we are actually changing the channel */
231 if (state->current_frequency != param->frequency) {
232 freq = 44000 + (param->frequency/1000);
233 tunerfreq = freq * 16/1000;
235 dprintk("set_parameters frequency = %d (tunerfreq = %d)\n",
236 param->frequency,tunerfreq);
238 buf[0] = (tunerfreq >> 8) & 0x7F;
239 buf[1] = (tunerfreq & 0xFF);
242 if (param->frequency < 157250000) {
244 dprintk("set_parameters VHF low range\n");
245 } else if (param->frequency < 454000000) {
247 dprintk("set_parameters VHF high range\n");
250 dprintk("set_parameters UHF range\n");
252 dprintk("set_parameters tuner bytes: 0x%02x 0x%02x "
253 "0x%02x 0x%02x\n",buf[0],buf[1],buf[2],buf[3]);
255 if (i2c_writebytes(state,0xC2>>1,buf,4))
256 printk(KERN_WARNING "or51211:set_parameters error "
257 "writing to tuner\n");
259 /* Set to ATSC mode */
260 or51211_setmode(fe,0);
262 /* Update current frequency */
263 state->current_frequency = param->frequency;
268 static int or51211_read_status(struct dvb_frontend* fe, fe_status_t* status)
270 struct or51211_state* state = fe->demodulator_priv;
271 unsigned char rec_buf[2];
272 unsigned char snd_buf[] = {0x04,0x00,0x03,0x00};
275 /* Receiver Status */
276 if (i2c_writebytes(state,state->config->demod_address,snd_buf,3)) {
277 printk(KERN_WARNING "or51132: read_status write error\n");
281 if (i2c_readbytes(state,state->config->demod_address,rec_buf,2)) {
282 printk(KERN_WARNING "or51132: read_status read error\n");
285 dprintk("read_status %x %x\n",rec_buf[0],rec_buf[1]);
287 if (rec_buf[0] & 0x01) { /* Receiver Lock */
288 *status |= FE_HAS_SIGNAL;
289 *status |= FE_HAS_CARRIER;
290 *status |= FE_HAS_VITERBI;
291 *status |= FE_HAS_SYNC;
292 *status |= FE_HAS_LOCK;
297 /* Calculate SNR estimation (scaled by 2^24)
299 8-VSB SNR equation from Oren datasheets
302 SNR[dB] = 10 * log10(219037.9454 / MSE^2 )
304 We re-write the snr equation as:
305 SNR * 2^24 = 10*(c - 2*intlog10(MSE))
306 Where for 8-VSB, c = log10(219037.9454) * 2^24 */
308 static u32 calculate_snr(u32 mse, u32 c)
310 if (mse == 0) /* No signal */
313 mse = 2*intlog10(mse);
315 /* Negative SNR, which is possible, but realisticly the
316 demod will lose lock before the signal gets this bad. The
317 API only allows for unsigned values, so just return 0 */
323 static int or51211_read_snr(struct dvb_frontend* fe, u16* snr)
325 struct or51211_state* state = fe->demodulator_priv;
329 /* SNR after Equalizer */
334 if (i2c_writebytes(state,state->config->demod_address,snd_buf,3)) {
335 printk(KERN_WARNING "%s: error writing snr reg\n",
339 if (i2c_readbytes(state,state->config->demod_address,rec_buf,2)) {
340 printk(KERN_WARNING "%s: read_status read error\n",
345 state->snr = calculate_snr(rec_buf[0], 89599047);
346 *snr = (state->snr) >> 16;
348 dprintk("%s: noise = 0x%02x, snr = %d.%02d dB\n", __FUNCTION__, rec_buf[0],
349 state->snr >> 24, (((state->snr>>8) & 0xffff) * 100) >> 16);
354 static int or51211_read_signal_strength(struct dvb_frontend* fe, u16* strength)
356 /* Calculate Strength from SNR up to 35dB */
357 /* Even though the SNR can go higher than 35dB, there is some comfort */
358 /* factor in having a range of strong signals that can show at 100% */
359 struct or51211_state* state = (struct or51211_state*)fe->demodulator_priv;
363 ret = fe->ops.read_snr(fe, &snr);
366 /* Rather than use the 8.8 value snr, use state->snr which is 8.24 */
367 /* scale the range 0 - 35*2^24 into 0 - 65535 */
368 if (state->snr >= 8960 * 0x10000)
371 *strength = state->snr / 8960;
376 static int or51211_read_ber(struct dvb_frontend* fe, u32* ber)
382 static int or51211_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
388 static int or51211_sleep(struct dvb_frontend* fe)
393 static int or51211_init(struct dvb_frontend* fe)
395 struct or51211_state* state = fe->demodulator_priv;
396 const struct or51211_config* config = state->config;
397 const struct firmware* fw;
398 unsigned char get_ver_buf[] = {0x04,0x00,0x30,0x00,0x00};
399 unsigned char rec_buf[14];
402 if (!state->initialized) {
403 /* Request the firmware, this will block until it uploads */
404 printk(KERN_INFO "or51211: Waiting for firmware upload "
405 "(%s)...\n", OR51211_DEFAULT_FIRMWARE);
406 ret = config->request_firmware(fe, &fw,
407 OR51211_DEFAULT_FIRMWARE);
408 printk(KERN_INFO "or51211:Got Hotplug firmware\n");
410 printk(KERN_WARNING "or51211: No firmware uploaded "
411 "(timeout or file not found?)\n");
415 ret = or51211_load_firmware(fe, fw);
416 release_firmware(fw);
418 printk(KERN_WARNING "or51211: Writing firmware to "
422 printk(KERN_INFO "or51211: Firmware upload complete.\n");
424 /* Set operation mode in Receiver 1 register;
426 * data 0x50h Automatic sets receiver channel conditions
427 * Automatic NTSC rejection filter
428 * Enable MPEG serial data output
430 * High tuner phase noise
431 * normal +/-150kHz Carrier acquisition range
433 if (i2c_writebytes(state,state->config->demod_address,
435 printk(KERN_WARNING "or51211: Load DVR Error 5\n");
439 /* Read back ucode version to besure we loaded correctly */
440 /* and are really up and running */
446 if (i2c_writebytes(state,state->config->demod_address,
448 printk(KERN_WARNING "or51211: Load DVR Error A\n");
452 if (i2c_readbytes(state,state->config->demod_address,
454 printk(KERN_WARNING "or51211: Load DVR Error B\n");
463 if (i2c_writebytes(state,state->config->demod_address,
465 printk(KERN_WARNING "or51211: Load DVR Error C\n");
469 if (i2c_readbytes(state,state->config->demod_address,
471 printk(KERN_WARNING "or51211: Load DVR Error D\n");
475 for (i = 0; i < 8; i++)
478 for (i = 0; i < 5; i++) {
480 get_ver_buf[4] = i+1;
481 if (i2c_writebytes(state,state->config->demod_address,
483 printk(KERN_WARNING "or51211:Load DVR Error 6"
489 if (i2c_readbytes(state,state->config->demod_address,
491 printk(KERN_WARNING "or51211:Load DVR Error 7"
495 /* If we didn't receive the right index, try again */
496 if ((int)rec_buf[i*2+1]!=i+1){
500 dprintk("read_fwbits %x %x %x %x %x %x %x %x %x %x\n",
501 rec_buf[0], rec_buf[1], rec_buf[2], rec_buf[3],
502 rec_buf[4], rec_buf[5], rec_buf[6], rec_buf[7],
503 rec_buf[8], rec_buf[9]);
505 printk(KERN_INFO "or51211: ver TU%02x%02x%02x VSB mode %02x"
507 rec_buf[2], rec_buf[4],rec_buf[6],
508 rec_buf[12],rec_buf[10]);
515 if (i2c_writebytes(state,state->config->demod_address,
517 printk(KERN_WARNING "or51211: Load DVR Error 8\n");
521 if (i2c_readbytes(state,state->config->demod_address,
523 printk(KERN_WARNING "or51211: Load DVR Error 9\n");
526 state->initialized = 1;
532 static int or51211_get_tune_settings(struct dvb_frontend* fe,
533 struct dvb_frontend_tune_settings* fesettings)
535 fesettings->min_delay_ms = 500;
536 fesettings->step_size = 0;
537 fesettings->max_drift = 0;
541 static void or51211_release(struct dvb_frontend* fe)
543 struct or51211_state* state = fe->demodulator_priv;
544 state->config->sleep(fe);
548 static struct dvb_frontend_ops or51211_ops;
550 struct dvb_frontend* or51211_attach(const struct or51211_config* config,
551 struct i2c_adapter* i2c)
553 struct or51211_state* state = NULL;
555 /* Allocate memory for the internal state */
556 state = kmalloc(sizeof(struct or51211_state), GFP_KERNEL);
560 /* Setup the state */
561 state->config = config;
563 state->initialized = 0;
564 state->current_frequency = 0;
566 /* Create dvb_frontend */
567 memcpy(&state->frontend.ops, &or51211_ops, sizeof(struct dvb_frontend_ops));
568 state->frontend.demodulator_priv = state;
569 return &state->frontend;
576 static struct dvb_frontend_ops or51211_ops = {
579 .name = "Oren OR51211 VSB Frontend",
581 .frequency_min = 44000000,
582 .frequency_max = 958000000,
583 .frequency_stepsize = 166666,
584 .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
585 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
589 .release = or51211_release,
591 .init = or51211_init,
592 .sleep = or51211_sleep,
594 .set_frontend = or51211_set_parameters,
595 .get_tune_settings = or51211_get_tune_settings,
597 .read_status = or51211_read_status,
598 .read_ber = or51211_read_ber,
599 .read_signal_strength = or51211_read_signal_strength,
600 .read_snr = or51211_read_snr,
601 .read_ucblocks = or51211_read_ucblocks,
604 module_param(debug, int, 0644);
605 MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
607 MODULE_DESCRIPTION("Oren OR51211 VSB [pcHDTV HD-2000] Demodulator Driver");
608 MODULE_AUTHOR("Kirk Lapray");
609 MODULE_LICENSE("GPL");
611 EXPORT_SYMBOL(or51211_attach);