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.
21 #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/device.h>
28 #include <linux/firmware.h>
29 #include <linux/string.h>
30 #include <linux/slab.h>
31 #include <asm/byteorder.h>
34 #include "dvb_frontend.h"
38 #define dprintk(args...) \
39 do { if (debug) pr_debug(args); } while (0)
41 static u8 run_buf[] = {0x7f,0x01};
42 static u8 cmd_buf[] = {0x04,0x01,0x50,0x80,0x06}; // ATSC
44 struct or51211_state {
46 struct i2c_adapter* i2c;
48 /* Configuration settings */
49 const struct or51211_config* config;
51 struct dvb_frontend frontend;
54 /* Demodulator private data */
56 u32 snr; /* Result of last SNR claculation */
58 /* Tuner private data */
59 u32 current_frequency;
62 static int i2c_writebytes (struct or51211_state* state, u8 reg, const u8 *buf,
72 if ((err = i2c_transfer (state->i2c, &msg, 1)) != 1) {
73 pr_warn("error (addr %02x, err == %i)\n", reg, err);
80 static int i2c_readbytes(struct or51211_state *state, u8 reg, u8 *buf, int len)
89 if ((err = i2c_transfer (state->i2c, &msg, 1)) != 1) {
90 pr_warn("error (addr %02x, err == %i)\n", reg, err);
97 static int or51211_load_firmware (struct dvb_frontend* fe,
98 const struct firmware *fw)
100 struct or51211_state* state = fe->demodulator_priv;
104 dprintk("Firmware is %zu bytes\n", fw->size);
108 if (i2c_writebytes(state,0x50,tudata,1)) {
109 pr_warn("error eprom addr\n");
112 if (i2c_readbytes(state,0x50,&tudata[145],192)) {
113 pr_warn("error eprom\n");
117 /* Create firmware buffer */
118 for (i = 0; i < 145; i++)
119 tudata[i] = fw->data[i];
121 for (i = 0; i < 248; i++)
122 tudata[i+337] = fw->data[145+i];
124 state->config->reset(fe);
126 if (i2c_writebytes(state,state->config->demod_address,tudata,585)) {
127 pr_warn("error 1\n");
132 if (i2c_writebytes(state,state->config->demod_address,
133 &fw->data[393],8125)) {
134 pr_warn("error 2\n");
139 if (i2c_writebytes(state,state->config->demod_address,run_buf,2)) {
140 pr_warn("error 3\n");
144 /* Wait at least 5 msec */
146 if (i2c_writebytes(state,state->config->demod_address,run_buf,2)) {
147 pr_warn("error 4\n");
156 static int or51211_setmode(struct dvb_frontend* fe, int mode)
158 struct or51211_state* state = fe->demodulator_priv;
161 state->config->setmode(fe, mode);
163 if (i2c_writebytes(state,state->config->demod_address,run_buf,2)) {
164 pr_warn("error 1\n");
168 /* Wait at least 5 msec */
170 if (i2c_writebytes(state,state->config->demod_address,run_buf,2)) {
171 pr_warn("error 2\n");
177 /* Set operation mode in Receiver 1 register;
179 * data 0x50h Automatic sets receiver channel conditions
180 * Automatic NTSC rejection filter
181 * Enable MPEG serial data output
183 * High tuner phase noise
184 * normal +/-150kHz Carrier acquisition range
186 if (i2c_writebytes(state,state->config->demod_address,cmd_buf,3)) {
187 pr_warn("error 3\n");
196 if (i2c_writebytes(state,state->config->demod_address,rec_buf,3)) {
197 pr_warn("error 5\n");
200 if (i2c_readbytes(state,state->config->demod_address,&rec_buf[10],2)) {
201 pr_warn("error 6\n");
204 dprintk("rec status %02x %02x\n", rec_buf[10], rec_buf[11]);
209 static int or51211_set_parameters(struct dvb_frontend *fe)
211 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
212 struct or51211_state* state = fe->demodulator_priv;
214 /* Change only if we are actually changing the channel */
215 if (state->current_frequency != p->frequency) {
216 if (fe->ops.tuner_ops.set_params) {
217 fe->ops.tuner_ops.set_params(fe);
218 if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 0);
221 /* Set to ATSC mode */
222 or51211_setmode(fe,0);
224 /* Update current frequency */
225 state->current_frequency = p->frequency;
230 static int or51211_read_status(struct dvb_frontend *fe, enum fe_status *status)
232 struct or51211_state* state = fe->demodulator_priv;
233 unsigned char rec_buf[2];
234 unsigned char snd_buf[] = {0x04,0x00,0x03,0x00};
237 /* Receiver Status */
238 if (i2c_writebytes(state,state->config->demod_address,snd_buf,3)) {
239 pr_warn("write error\n");
243 if (i2c_readbytes(state,state->config->demod_address,rec_buf,2)) {
244 pr_warn("read error\n");
247 dprintk("%x %x\n", rec_buf[0], rec_buf[1]);
249 if (rec_buf[0] & 0x01) { /* Receiver Lock */
250 *status |= FE_HAS_SIGNAL;
251 *status |= FE_HAS_CARRIER;
252 *status |= FE_HAS_VITERBI;
253 *status |= FE_HAS_SYNC;
254 *status |= FE_HAS_LOCK;
259 /* Calculate SNR estimation (scaled by 2^24)
261 8-VSB SNR equation from Oren datasheets
264 SNR[dB] = 10 * log10(219037.9454 / MSE^2 )
266 We re-write the snr equation as:
267 SNR * 2^24 = 10*(c - 2*intlog10(MSE))
268 Where for 8-VSB, c = log10(219037.9454) * 2^24 */
270 static u32 calculate_snr(u32 mse, u32 c)
272 if (mse == 0) /* No signal */
275 mse = 2*intlog10(mse);
277 /* Negative SNR, which is possible, but realisticly the
278 demod will lose lock before the signal gets this bad. The
279 API only allows for unsigned values, so just return 0 */
285 static int or51211_read_snr(struct dvb_frontend* fe, u16* snr)
287 struct or51211_state* state = fe->demodulator_priv;
291 /* SNR after Equalizer */
296 if (i2c_writebytes(state,state->config->demod_address,snd_buf,3)) {
297 pr_warn("error writing snr reg\n");
300 if (i2c_readbytes(state,state->config->demod_address,rec_buf,2)) {
301 pr_warn("read_status read error\n");
305 state->snr = calculate_snr(rec_buf[0], 89599047);
306 *snr = (state->snr) >> 16;
308 dprintk("noise = 0x%02x, snr = %d.%02d dB\n", rec_buf[0],
309 state->snr >> 24, (((state->snr>>8) & 0xffff) * 100) >> 16);
314 static int or51211_read_signal_strength(struct dvb_frontend* fe, u16* strength)
316 /* Calculate Strength from SNR up to 35dB */
317 /* Even though the SNR can go higher than 35dB, there is some comfort */
318 /* factor in having a range of strong signals that can show at 100% */
319 struct or51211_state* state = (struct or51211_state*)fe->demodulator_priv;
323 ret = fe->ops.read_snr(fe, &snr);
326 /* Rather than use the 8.8 value snr, use state->snr which is 8.24 */
327 /* scale the range 0 - 35*2^24 into 0 - 65535 */
328 if (state->snr >= 8960 * 0x10000)
331 *strength = state->snr / 8960;
336 static int or51211_read_ber(struct dvb_frontend* fe, u32* ber)
342 static int or51211_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
348 static int or51211_sleep(struct dvb_frontend* fe)
353 static int or51211_init(struct dvb_frontend* fe)
355 struct or51211_state* state = fe->demodulator_priv;
356 const struct or51211_config* config = state->config;
357 const struct firmware* fw;
358 unsigned char get_ver_buf[] = {0x04,0x00,0x30,0x00,0x00};
359 unsigned char rec_buf[14];
362 if (!state->initialized) {
363 /* Request the firmware, this will block until it uploads */
364 pr_info("Waiting for firmware upload (%s)...\n",
366 ret = config->request_firmware(fe, &fw,
368 pr_info("Got Hotplug firmware\n");
370 pr_warn("No firmware uploaded (timeout or file not found?)\n");
374 ret = or51211_load_firmware(fe, fw);
375 release_firmware(fw);
377 pr_warn("Writing firmware to device failed!\n");
380 pr_info("Firmware upload complete.\n");
382 /* Set operation mode in Receiver 1 register;
384 * data 0x50h Automatic sets receiver channel conditions
385 * Automatic NTSC rejection filter
386 * Enable MPEG serial data output
388 * High tuner phase noise
389 * normal +/-150kHz Carrier acquisition range
391 if (i2c_writebytes(state,state->config->demod_address,
393 pr_warn("Load DVR Error 5\n");
397 /* Read back ucode version to besure we loaded correctly */
398 /* and are really up and running */
404 if (i2c_writebytes(state,state->config->demod_address,
406 pr_warn("Load DVR Error A\n");
410 if (i2c_readbytes(state,state->config->demod_address,
412 pr_warn("Load DVR Error B\n");
421 if (i2c_writebytes(state,state->config->demod_address,
423 pr_warn("Load DVR Error C\n");
427 if (i2c_readbytes(state,state->config->demod_address,
429 pr_warn("Load DVR Error D\n");
433 for (i = 0; i < 8; i++)
436 for (i = 0; i < 5; i++) {
438 get_ver_buf[4] = i+1;
439 if (i2c_writebytes(state,state->config->demod_address,
441 pr_warn("Load DVR Error 6 - %d\n", i);
446 if (i2c_readbytes(state,state->config->demod_address,
448 pr_warn("Load DVR Error 7 - %d\n", i);
451 /* If we didn't receive the right index, try again */
452 if ((int)rec_buf[i*2+1]!=i+1){
456 dprintk("read_fwbits %10ph\n", rec_buf);
458 pr_info("ver TU%02x%02x%02x VSB mode %02x Status %02x\n",
459 rec_buf[2], rec_buf[4], rec_buf[6], rec_buf[12],
467 if (i2c_writebytes(state,state->config->demod_address,
469 pr_warn("Load DVR Error 8\n");
473 if (i2c_readbytes(state,state->config->demod_address,
475 pr_warn("Load DVR Error 9\n");
478 state->initialized = 1;
484 static int or51211_get_tune_settings(struct dvb_frontend* fe,
485 struct dvb_frontend_tune_settings* fesettings)
487 fesettings->min_delay_ms = 500;
488 fesettings->step_size = 0;
489 fesettings->max_drift = 0;
493 static void or51211_release(struct dvb_frontend* fe)
495 struct or51211_state* state = fe->demodulator_priv;
496 state->config->sleep(fe);
500 static const struct dvb_frontend_ops or51211_ops;
502 struct dvb_frontend* or51211_attach(const struct or51211_config* config,
503 struct i2c_adapter* i2c)
505 struct or51211_state* state = NULL;
507 /* Allocate memory for the internal state */
508 state = kzalloc(sizeof(struct or51211_state), GFP_KERNEL);
512 /* Setup the state */
513 state->config = config;
515 state->initialized = 0;
516 state->current_frequency = 0;
518 /* Create dvb_frontend */
519 memcpy(&state->frontend.ops, &or51211_ops, sizeof(struct dvb_frontend_ops));
520 state->frontend.demodulator_priv = state;
521 return &state->frontend;
524 static const struct dvb_frontend_ops or51211_ops = {
525 .delsys = { SYS_ATSC, SYS_DVBC_ANNEX_B },
527 .name = "Oren OR51211 VSB Frontend",
528 .frequency_min = 44000000,
529 .frequency_max = 958000000,
530 .frequency_stepsize = 166666,
531 .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
532 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
536 .release = or51211_release,
538 .init = or51211_init,
539 .sleep = or51211_sleep,
541 .set_frontend = or51211_set_parameters,
542 .get_tune_settings = or51211_get_tune_settings,
544 .read_status = or51211_read_status,
545 .read_ber = or51211_read_ber,
546 .read_signal_strength = or51211_read_signal_strength,
547 .read_snr = or51211_read_snr,
548 .read_ucblocks = or51211_read_ucblocks,
551 module_param(debug, int, 0644);
552 MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
554 MODULE_DESCRIPTION("Oren OR51211 VSB [pcHDTV HD-2000] Demodulator Driver");
555 MODULE_AUTHOR("Kirk Lapray");
556 MODULE_LICENSE("GPL");
558 EXPORT_SYMBOL(or51211_attach);