2 * Support for OR51211 (pcHDTV HD-2000) - VSB
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.
21 #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
24 * This driver needs external firmware. Please use the command
25 * "<kerneldir>/Documentation/dvb/get_dvb_firmware or51211" to
26 * download/extract it, and then copy it to /usr/lib/hotplug/firmware
27 * or /lib/firmware (depending on configuration of firmware hotplug).
29 #define OR51211_DEFAULT_FIRMWARE "dvb-fe-or51211.fw"
31 #include <linux/kernel.h>
32 #include <linux/module.h>
33 #include <linux/device.h>
34 #include <linux/firmware.h>
35 #include <linux/string.h>
36 #include <linux/slab.h>
37 #include <asm/byteorder.h>
40 #include "dvb_frontend.h"
44 #define dprintk(args...) \
45 do { if (debug) pr_debug(args); } while (0)
47 static u8 run_buf[] = {0x7f,0x01};
48 static u8 cmd_buf[] = {0x04,0x01,0x50,0x80,0x06}; // ATSC
50 struct or51211_state {
52 struct i2c_adapter* i2c;
54 /* Configuration settings */
55 const struct or51211_config* config;
57 struct dvb_frontend frontend;
60 /* Demodulator private data */
62 u32 snr; /* Result of last SNR claculation */
64 /* Tuner private data */
65 u32 current_frequency;
68 static int i2c_writebytes (struct or51211_state* state, u8 reg, const u8 *buf,
78 if ((err = i2c_transfer (state->i2c, &msg, 1)) != 1) {
79 pr_warn("error (addr %02x, err == %i)\n", reg, err);
86 static int i2c_readbytes(struct or51211_state *state, u8 reg, u8 *buf, int len)
95 if ((err = i2c_transfer (state->i2c, &msg, 1)) != 1) {
96 pr_warn("error (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 %zu bytes\n", fw->size);
114 if (i2c_writebytes(state,0x50,tudata,1)) {
115 pr_warn("error eprom addr\n");
118 if (i2c_readbytes(state,0x50,&tudata[145],192)) {
119 pr_warn("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 pr_warn("error 1\n");
138 if (i2c_writebytes(state,state->config->demod_address,
139 &fw->data[393],8125)) {
140 pr_warn("error 2\n");
145 if (i2c_writebytes(state,state->config->demod_address,run_buf,2)) {
146 pr_warn("error 3\n");
150 /* Wait at least 5 msec */
152 if (i2c_writebytes(state,state->config->demod_address,run_buf,2)) {
153 pr_warn("error 4\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 pr_warn("error 1\n");
174 /* Wait at least 5 msec */
176 if (i2c_writebytes(state,state->config->demod_address,run_buf,2)) {
177 pr_warn("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 pr_warn("error 3\n");
202 if (i2c_writebytes(state,state->config->demod_address,rec_buf,3)) {
203 pr_warn("error 5\n");
206 if (i2c_readbytes(state,state->config->demod_address,&rec_buf[10],2)) {
207 pr_warn("error 6\n");
210 dprintk("rec status %02x %02x\n", rec_buf[10], rec_buf[11]);
215 static int or51211_set_parameters(struct dvb_frontend *fe)
217 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
218 struct or51211_state* state = fe->demodulator_priv;
220 /* Change only if we are actually changing the channel */
221 if (state->current_frequency != p->frequency) {
222 if (fe->ops.tuner_ops.set_params) {
223 fe->ops.tuner_ops.set_params(fe);
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 = p->frequency;
236 static int or51211_read_status(struct dvb_frontend *fe, enum fe_status *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 pr_warn("write error\n");
249 if (i2c_readbytes(state,state->config->demod_address,rec_buf,2)) {
250 pr_warn("read error\n");
253 dprintk("%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 pr_warn("error writing snr reg\n");
306 if (i2c_readbytes(state,state->config->demod_address,rec_buf,2)) {
307 pr_warn("read_status read error\n");
311 state->snr = calculate_snr(rec_buf[0], 89599047);
312 *snr = (state->snr) >> 16;
314 dprintk("noise = 0x%02x, snr = %d.%02d dB\n", rec_buf[0],
315 state->snr >> 24, (((state->snr>>8) & 0xffff) * 100) >> 16);
320 static int or51211_read_signal_strength(struct dvb_frontend* fe, u16* strength)
322 /* Calculate Strength from SNR up to 35dB */
323 /* Even though the SNR can go higher than 35dB, there is some comfort */
324 /* factor in having a range of strong signals that can show at 100% */
325 struct or51211_state* state = (struct or51211_state*)fe->demodulator_priv;
329 ret = fe->ops.read_snr(fe, &snr);
332 /* Rather than use the 8.8 value snr, use state->snr which is 8.24 */
333 /* scale the range 0 - 35*2^24 into 0 - 65535 */
334 if (state->snr >= 8960 * 0x10000)
337 *strength = state->snr / 8960;
342 static int or51211_read_ber(struct dvb_frontend* fe, u32* ber)
348 static int or51211_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
354 static int or51211_sleep(struct dvb_frontend* fe)
359 static int or51211_init(struct dvb_frontend* fe)
361 struct or51211_state* state = fe->demodulator_priv;
362 const struct or51211_config* config = state->config;
363 const struct firmware* fw;
364 unsigned char get_ver_buf[] = {0x04,0x00,0x30,0x00,0x00};
365 unsigned char rec_buf[14];
368 if (!state->initialized) {
369 /* Request the firmware, this will block until it uploads */
370 pr_info("Waiting for firmware upload (%s)...\n",
371 OR51211_DEFAULT_FIRMWARE);
372 ret = config->request_firmware(fe, &fw,
373 OR51211_DEFAULT_FIRMWARE);
374 pr_info("Got Hotplug firmware\n");
376 pr_warn("No firmware uploaded (timeout or file not found?)\n");
380 ret = or51211_load_firmware(fe, fw);
381 release_firmware(fw);
383 pr_warn("Writing firmware to device failed!\n");
386 pr_info("Firmware upload complete.\n");
388 /* Set operation mode in Receiver 1 register;
390 * data 0x50h Automatic sets receiver channel conditions
391 * Automatic NTSC rejection filter
392 * Enable MPEG serial data output
394 * High tuner phase noise
395 * normal +/-150kHz Carrier acquisition range
397 if (i2c_writebytes(state,state->config->demod_address,
399 pr_warn("Load DVR Error 5\n");
403 /* Read back ucode version to besure we loaded correctly */
404 /* and are really up and running */
410 if (i2c_writebytes(state,state->config->demod_address,
412 pr_warn("Load DVR Error A\n");
416 if (i2c_readbytes(state,state->config->demod_address,
418 pr_warn("Load DVR Error B\n");
427 if (i2c_writebytes(state,state->config->demod_address,
429 pr_warn("Load DVR Error C\n");
433 if (i2c_readbytes(state,state->config->demod_address,
435 pr_warn("Load DVR Error D\n");
439 for (i = 0; i < 8; i++)
442 for (i = 0; i < 5; i++) {
444 get_ver_buf[4] = i+1;
445 if (i2c_writebytes(state,state->config->demod_address,
447 pr_warn("Load DVR Error 6 - %d\n", i);
452 if (i2c_readbytes(state,state->config->demod_address,
454 pr_warn("Load DVR Error 7 - %d\n", i);
457 /* If we didn't receive the right index, try again */
458 if ((int)rec_buf[i*2+1]!=i+1){
462 dprintk("read_fwbits %10ph\n", rec_buf);
464 pr_info("ver TU%02x%02x%02x VSB mode %02x Status %02x\n",
465 rec_buf[2], rec_buf[4], rec_buf[6], rec_buf[12],
473 if (i2c_writebytes(state,state->config->demod_address,
475 pr_warn("Load DVR Error 8\n");
479 if (i2c_readbytes(state,state->config->demod_address,
481 pr_warn("Load DVR Error 9\n");
484 state->initialized = 1;
490 static int or51211_get_tune_settings(struct dvb_frontend* fe,
491 struct dvb_frontend_tune_settings* fesettings)
493 fesettings->min_delay_ms = 500;
494 fesettings->step_size = 0;
495 fesettings->max_drift = 0;
499 static void or51211_release(struct dvb_frontend* fe)
501 struct or51211_state* state = fe->demodulator_priv;
502 state->config->sleep(fe);
506 static const struct dvb_frontend_ops or51211_ops;
508 struct dvb_frontend* or51211_attach(const struct or51211_config* config,
509 struct i2c_adapter* i2c)
511 struct or51211_state* state = NULL;
513 /* Allocate memory for the internal state */
514 state = kzalloc(sizeof(struct or51211_state), GFP_KERNEL);
518 /* Setup the state */
519 state->config = config;
521 state->initialized = 0;
522 state->current_frequency = 0;
524 /* Create dvb_frontend */
525 memcpy(&state->frontend.ops, &or51211_ops, sizeof(struct dvb_frontend_ops));
526 state->frontend.demodulator_priv = state;
527 return &state->frontend;
530 static const struct dvb_frontend_ops or51211_ops = {
531 .delsys = { SYS_ATSC, SYS_DVBC_ANNEX_B },
533 .name = "Oren OR51211 VSB Frontend",
534 .frequency_min = 44000000,
535 .frequency_max = 958000000,
536 .frequency_stepsize = 166666,
537 .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
538 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
542 .release = or51211_release,
544 .init = or51211_init,
545 .sleep = or51211_sleep,
547 .set_frontend = or51211_set_parameters,
548 .get_tune_settings = or51211_get_tune_settings,
550 .read_status = or51211_read_status,
551 .read_ber = or51211_read_ber,
552 .read_signal_strength = or51211_read_signal_strength,
553 .read_snr = or51211_read_snr,
554 .read_ucblocks = or51211_read_ucblocks,
557 module_param(debug, int, 0644);
558 MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
560 MODULE_DESCRIPTION("Oren OR51211 VSB [pcHDTV HD-2000] Demodulator Driver");
561 MODULE_AUTHOR("Kirk Lapray");
562 MODULE_LICENSE("GPL");
564 EXPORT_SYMBOL(or51211_attach);