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.
25 #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
29 #include <linux/kernel.h>
30 #include <linux/module.h>
31 #include <linux/device.h>
32 #include <linux/firmware.h>
33 #include <linux/string.h>
34 #include <linux/slab.h>
35 #include <asm/byteorder.h>
38 #include "dvb_frontend.h"
42 #define dprintk(args...) \
43 do { if (debug) pr_debug(args); } while (0)
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 pr_warn("error (addr %02x, err == %i)\n", reg, err);
84 static int i2c_readbytes(struct or51211_state *state, u8 reg, u8 *buf, int len)
93 if ((err = i2c_transfer (state->i2c, &msg, 1)) != 1) {
94 pr_warn("error (addr %02x, err == %i)\n", reg, err);
101 static int or51211_load_firmware (struct dvb_frontend* fe,
102 const struct firmware *fw)
104 struct or51211_state* state = fe->demodulator_priv;
108 dprintk("Firmware is %zu bytes\n", fw->size);
112 if (i2c_writebytes(state,0x50,tudata,1)) {
113 pr_warn("error eprom addr\n");
116 if (i2c_readbytes(state,0x50,&tudata[145],192)) {
117 pr_warn("error eprom\n");
121 /* Create firmware buffer */
122 for (i = 0; i < 145; i++)
123 tudata[i] = fw->data[i];
125 for (i = 0; i < 248; i++)
126 tudata[i+337] = fw->data[145+i];
128 state->config->reset(fe);
130 if (i2c_writebytes(state,state->config->demod_address,tudata,585)) {
131 pr_warn("error 1\n");
136 if (i2c_writebytes(state,state->config->demod_address,
137 &fw->data[393],8125)) {
138 pr_warn("error 2\n");
143 if (i2c_writebytes(state,state->config->demod_address,run_buf,2)) {
144 pr_warn("error 3\n");
148 /* Wait at least 5 msec */
150 if (i2c_writebytes(state,state->config->demod_address,run_buf,2)) {
151 pr_warn("error 4\n");
160 static int or51211_setmode(struct dvb_frontend* fe, int mode)
162 struct or51211_state* state = fe->demodulator_priv;
165 state->config->setmode(fe, mode);
167 if (i2c_writebytes(state,state->config->demod_address,run_buf,2)) {
168 pr_warn("error 1\n");
172 /* Wait at least 5 msec */
174 if (i2c_writebytes(state,state->config->demod_address,run_buf,2)) {
175 pr_warn("error 2\n");
181 /* Set operation mode in Receiver 1 register;
183 * data 0x50h Automatic sets receiver channel conditions
184 * Automatic NTSC rejection filter
185 * Enable MPEG serial data output
187 * High tuner phase noise
188 * normal +/-150kHz Carrier acquisition range
190 if (i2c_writebytes(state,state->config->demod_address,cmd_buf,3)) {
191 pr_warn("error 3\n");
200 if (i2c_writebytes(state,state->config->demod_address,rec_buf,3)) {
201 pr_warn("error 5\n");
204 if (i2c_readbytes(state,state->config->demod_address,&rec_buf[10],2)) {
205 pr_warn("error 6\n");
208 dprintk("rec status %02x %02x\n", rec_buf[10], rec_buf[11]);
213 static int or51211_set_parameters(struct dvb_frontend *fe)
215 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
216 struct or51211_state* state = fe->demodulator_priv;
218 /* Change only if we are actually changing the channel */
219 if (state->current_frequency != p->frequency) {
220 if (fe->ops.tuner_ops.set_params) {
221 fe->ops.tuner_ops.set_params(fe);
222 if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 0);
225 /* Set to ATSC mode */
226 or51211_setmode(fe,0);
228 /* Update current frequency */
229 state->current_frequency = p->frequency;
234 static int or51211_read_status(struct dvb_frontend *fe, enum fe_status *status)
236 struct or51211_state* state = fe->demodulator_priv;
237 unsigned char rec_buf[2];
238 unsigned char snd_buf[] = {0x04,0x00,0x03,0x00};
241 /* Receiver Status */
242 if (i2c_writebytes(state,state->config->demod_address,snd_buf,3)) {
243 pr_warn("write error\n");
247 if (i2c_readbytes(state,state->config->demod_address,rec_buf,2)) {
248 pr_warn("read error\n");
251 dprintk("%x %x\n", rec_buf[0], rec_buf[1]);
253 if (rec_buf[0] & 0x01) { /* Receiver Lock */
254 *status |= FE_HAS_SIGNAL;
255 *status |= FE_HAS_CARRIER;
256 *status |= FE_HAS_VITERBI;
257 *status |= FE_HAS_SYNC;
258 *status |= FE_HAS_LOCK;
263 /* Calculate SNR estimation (scaled by 2^24)
265 8-VSB SNR equation from Oren datasheets
268 SNR[dB] = 10 * log10(219037.9454 / MSE^2 )
270 We re-write the snr equation as:
271 SNR * 2^24 = 10*(c - 2*intlog10(MSE))
272 Where for 8-VSB, c = log10(219037.9454) * 2^24 */
274 static u32 calculate_snr(u32 mse, u32 c)
276 if (mse == 0) /* No signal */
279 mse = 2*intlog10(mse);
281 /* Negative SNR, which is possible, but realisticly the
282 demod will lose lock before the signal gets this bad. The
283 API only allows for unsigned values, so just return 0 */
289 static int or51211_read_snr(struct dvb_frontend* fe, u16* snr)
291 struct or51211_state* state = fe->demodulator_priv;
295 /* SNR after Equalizer */
300 if (i2c_writebytes(state,state->config->demod_address,snd_buf,3)) {
301 pr_warn("error writing snr reg\n");
304 if (i2c_readbytes(state,state->config->demod_address,rec_buf,2)) {
305 pr_warn("read_status read error\n");
309 state->snr = calculate_snr(rec_buf[0], 89599047);
310 *snr = (state->snr) >> 16;
312 dprintk("noise = 0x%02x, snr = %d.%02d dB\n", rec_buf[0],
313 state->snr >> 24, (((state->snr>>8) & 0xffff) * 100) >> 16);
318 static int or51211_read_signal_strength(struct dvb_frontend* fe, u16* strength)
320 /* Calculate Strength from SNR up to 35dB */
321 /* Even though the SNR can go higher than 35dB, there is some comfort */
322 /* factor in having a range of strong signals that can show at 100% */
323 struct or51211_state* state = (struct or51211_state*)fe->demodulator_priv;
327 ret = fe->ops.read_snr(fe, &snr);
330 /* Rather than use the 8.8 value snr, use state->snr which is 8.24 */
331 /* scale the range 0 - 35*2^24 into 0 - 65535 */
332 if (state->snr >= 8960 * 0x10000)
335 *strength = state->snr / 8960;
340 static int or51211_read_ber(struct dvb_frontend* fe, u32* ber)
346 static int or51211_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
352 static int or51211_sleep(struct dvb_frontend* fe)
357 static int or51211_init(struct dvb_frontend* fe)
359 struct or51211_state* state = fe->demodulator_priv;
360 const struct or51211_config* config = state->config;
361 const struct firmware* fw;
362 unsigned char get_ver_buf[] = {0x04,0x00,0x30,0x00,0x00};
363 unsigned char rec_buf[14];
366 if (!state->initialized) {
367 /* Request the firmware, this will block until it uploads */
368 pr_info("Waiting for firmware upload (%s)...\n",
370 ret = config->request_firmware(fe, &fw,
372 pr_info("Got Hotplug firmware\n");
374 pr_warn("No firmware uploaded "
375 "(timeout or file not found?)\n");
379 ret = or51211_load_firmware(fe, fw);
380 release_firmware(fw);
382 pr_warn("Writing firmware to device failed!\n");
385 pr_info("Firmware upload complete.\n");
387 /* Set operation mode in Receiver 1 register;
389 * data 0x50h Automatic sets receiver channel conditions
390 * Automatic NTSC rejection filter
391 * Enable MPEG serial data output
393 * High tuner phase noise
394 * normal +/-150kHz Carrier acquisition range
396 if (i2c_writebytes(state,state->config->demod_address,
398 pr_warn("Load DVR Error 5\n");
402 /* Read back ucode version to besure we loaded correctly */
403 /* and are really up and running */
409 if (i2c_writebytes(state,state->config->demod_address,
411 pr_warn("Load DVR Error A\n");
415 if (i2c_readbytes(state,state->config->demod_address,
417 pr_warn("Load DVR Error B\n");
426 if (i2c_writebytes(state,state->config->demod_address,
428 pr_warn("Load DVR Error C\n");
432 if (i2c_readbytes(state,state->config->demod_address,
434 pr_warn("Load DVR Error D\n");
438 for (i = 0; i < 8; i++)
441 for (i = 0; i < 5; i++) {
443 get_ver_buf[4] = i+1;
444 if (i2c_writebytes(state,state->config->demod_address,
446 pr_warn("Load DVR Error 6 - %d\n", i);
451 if (i2c_readbytes(state,state->config->demod_address,
453 pr_warn("Load DVR Error 7 - %d\n", i);
456 /* If we didn't receive the right index, try again */
457 if ((int)rec_buf[i*2+1]!=i+1){
461 dprintk("read_fwbits %10ph\n", rec_buf);
463 pr_info("ver TU%02x%02x%02x VSB mode %02x Status %02x\n",
464 rec_buf[2], rec_buf[4], rec_buf[6], rec_buf[12],
472 if (i2c_writebytes(state,state->config->demod_address,
474 pr_warn("Load DVR Error 8\n");
478 if (i2c_readbytes(state,state->config->demod_address,
480 pr_warn("Load DVR Error 9\n");
483 state->initialized = 1;
489 static int or51211_get_tune_settings(struct dvb_frontend* fe,
490 struct dvb_frontend_tune_settings* fesettings)
492 fesettings->min_delay_ms = 500;
493 fesettings->step_size = 0;
494 fesettings->max_drift = 0;
498 static void or51211_release(struct dvb_frontend* fe)
500 struct or51211_state* state = fe->demodulator_priv;
501 state->config->sleep(fe);
505 static struct dvb_frontend_ops or51211_ops;
507 struct dvb_frontend* or51211_attach(const struct or51211_config* config,
508 struct i2c_adapter* i2c)
510 struct or51211_state* state = NULL;
512 /* Allocate memory for the internal state */
513 state = kzalloc(sizeof(struct or51211_state), GFP_KERNEL);
517 /* Setup the state */
518 state->config = config;
520 state->initialized = 0;
521 state->current_frequency = 0;
523 /* Create dvb_frontend */
524 memcpy(&state->frontend.ops, &or51211_ops, sizeof(struct dvb_frontend_ops));
525 state->frontend.demodulator_priv = state;
526 return &state->frontend;
529 static struct dvb_frontend_ops or51211_ops = {
530 .delsys = { SYS_ATSC, SYS_DVBC_ANNEX_B },
532 .name = "Oren OR51211 VSB Frontend",
533 .frequency_min = 44000000,
534 .frequency_max = 958000000,
535 .frequency_stepsize = 166666,
536 .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
537 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
541 .release = or51211_release,
543 .init = or51211_init,
544 .sleep = or51211_sleep,
546 .set_frontend = or51211_set_parameters,
547 .get_tune_settings = or51211_get_tune_settings,
549 .read_status = or51211_read_status,
550 .read_ber = or51211_read_ber,
551 .read_signal_strength = or51211_read_signal_strength,
552 .read_snr = or51211_read_snr,
553 .read_ucblocks = or51211_read_ucblocks,
556 module_param(debug, int, 0644);
557 MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
559 MODULE_DESCRIPTION("Oren OR51211 VSB [pcHDTV HD-2000] Demodulator Driver");
560 MODULE_AUTHOR("Kirk Lapray");
561 MODULE_LICENSE("GPL");
563 EXPORT_SYMBOL(or51211_attach);