1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Support for OR51211 (pcHDTV HD-2000) - VSB
5 * Copyright (C) 2005 Kirk Lapray <kirk_lapray@bigfoot.com>
7 * Based on code from Jack Kelliher (kelliher@xmission.com)
8 * Copyright (C) 2002 & pcHDTV, inc.
11 #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/device.h>
18 #include <linux/firmware.h>
19 #include <linux/string.h>
20 #include <linux/slab.h>
21 #include <asm/byteorder.h>
23 #include <media/dvb_math.h>
24 #include <media/dvb_frontend.h>
28 #define dprintk(args...) \
29 do { if (debug) pr_debug(args); } while (0)
31 static u8 run_buf[] = {0x7f,0x01};
32 static u8 cmd_buf[] = {0x04,0x01,0x50,0x80,0x06}; // ATSC
34 struct or51211_state {
36 struct i2c_adapter* i2c;
38 /* Configuration settings */
39 const struct or51211_config* config;
41 struct dvb_frontend frontend;
44 /* Demodulator private data */
46 u32 snr; /* Result of last SNR calculation */
48 /* Tuner private data */
49 u32 current_frequency;
52 static int i2c_writebytes (struct or51211_state* state, u8 reg, const u8 *buf,
62 if ((err = i2c_transfer (state->i2c, &msg, 1)) != 1) {
63 pr_warn("error (addr %02x, err == %i)\n", reg, err);
70 static int i2c_readbytes(struct or51211_state *state, u8 reg, u8 *buf, int len)
79 if ((err = i2c_transfer (state->i2c, &msg, 1)) != 1) {
80 pr_warn("error (addr %02x, err == %i)\n", reg, err);
87 static int or51211_load_firmware (struct dvb_frontend* fe,
88 const struct firmware *fw)
90 struct or51211_state* state = fe->demodulator_priv;
94 dprintk("Firmware is %zu bytes\n", fw->size);
98 if (i2c_writebytes(state,0x50,tudata,1)) {
99 pr_warn("error eprom addr\n");
102 if (i2c_readbytes(state,0x50,&tudata[145],192)) {
103 pr_warn("error eprom\n");
107 /* Create firmware buffer */
108 for (i = 0; i < 145; i++)
109 tudata[i] = fw->data[i];
111 for (i = 0; i < 248; i++)
112 tudata[i+337] = fw->data[145+i];
114 state->config->reset(fe);
116 if (i2c_writebytes(state,state->config->demod_address,tudata,585)) {
117 pr_warn("error 1\n");
122 if (i2c_writebytes(state,state->config->demod_address,
123 &fw->data[393],8125)) {
124 pr_warn("error 2\n");
129 if (i2c_writebytes(state,state->config->demod_address,run_buf,2)) {
130 pr_warn("error 3\n");
134 /* Wait at least 5 msec */
136 if (i2c_writebytes(state,state->config->demod_address,run_buf,2)) {
137 pr_warn("error 4\n");
146 static int or51211_setmode(struct dvb_frontend* fe, int mode)
148 struct or51211_state* state = fe->demodulator_priv;
151 state->config->setmode(fe, mode);
153 if (i2c_writebytes(state,state->config->demod_address,run_buf,2)) {
154 pr_warn("error 1\n");
158 /* Wait at least 5 msec */
160 if (i2c_writebytes(state,state->config->demod_address,run_buf,2)) {
161 pr_warn("error 2\n");
167 /* Set operation mode in Receiver 1 register;
169 * data 0x50h Automatic sets receiver channel conditions
170 * Automatic NTSC rejection filter
171 * Enable MPEG serial data output
173 * High tuner phase noise
174 * normal +/-150kHz Carrier acquisition range
176 if (i2c_writebytes(state,state->config->demod_address,cmd_buf,3)) {
177 pr_warn("error 3\n");
186 if (i2c_writebytes(state,state->config->demod_address,rec_buf,3)) {
187 pr_warn("error 5\n");
190 if (i2c_readbytes(state,state->config->demod_address,&rec_buf[10],2)) {
191 pr_warn("error 6\n");
194 dprintk("rec status %02x %02x\n", rec_buf[10], rec_buf[11]);
199 static int or51211_set_parameters(struct dvb_frontend *fe)
201 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
202 struct or51211_state* state = fe->demodulator_priv;
204 /* Change only if we are actually changing the channel */
205 if (state->current_frequency != p->frequency) {
206 if (fe->ops.tuner_ops.set_params) {
207 fe->ops.tuner_ops.set_params(fe);
208 if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 0);
211 /* Set to ATSC mode */
212 or51211_setmode(fe,0);
214 /* Update current frequency */
215 state->current_frequency = p->frequency;
220 static int or51211_read_status(struct dvb_frontend *fe, enum fe_status *status)
222 struct or51211_state* state = fe->demodulator_priv;
223 unsigned char rec_buf[2];
224 unsigned char snd_buf[] = {0x04,0x00,0x03,0x00};
227 /* Receiver Status */
228 if (i2c_writebytes(state,state->config->demod_address,snd_buf,3)) {
229 pr_warn("write error\n");
233 if (i2c_readbytes(state,state->config->demod_address,rec_buf,2)) {
234 pr_warn("read error\n");
237 dprintk("%x %x\n", rec_buf[0], rec_buf[1]);
239 if (rec_buf[0] & 0x01) { /* Receiver Lock */
240 *status |= FE_HAS_SIGNAL;
241 *status |= FE_HAS_CARRIER;
242 *status |= FE_HAS_VITERBI;
243 *status |= FE_HAS_SYNC;
244 *status |= FE_HAS_LOCK;
249 /* Calculate SNR estimation (scaled by 2^24)
251 8-VSB SNR equation from Oren datasheets
254 SNR[dB] = 10 * log10(219037.9454 / MSE^2 )
256 We re-write the snr equation as:
257 SNR * 2^24 = 10*(c - 2*intlog10(MSE))
258 Where for 8-VSB, c = log10(219037.9454) * 2^24 */
260 static u32 calculate_snr(u32 mse, u32 c)
262 if (mse == 0) /* No signal */
265 mse = 2*intlog10(mse);
267 /* Negative SNR, which is possible, but realisticly the
268 demod will lose lock before the signal gets this bad. The
269 API only allows for unsigned values, so just return 0 */
275 static int or51211_read_snr(struct dvb_frontend* fe, u16* snr)
277 struct or51211_state* state = fe->demodulator_priv;
281 /* SNR after Equalizer */
286 if (i2c_writebytes(state,state->config->demod_address,snd_buf,3)) {
287 pr_warn("error writing snr reg\n");
290 if (i2c_readbytes(state,state->config->demod_address,rec_buf,2)) {
291 pr_warn("read_status read error\n");
295 state->snr = calculate_snr(rec_buf[0], 89599047);
296 *snr = (state->snr) >> 16;
298 dprintk("noise = 0x%02x, snr = %d.%02d dB\n", rec_buf[0],
299 state->snr >> 24, (((state->snr>>8) & 0xffff) * 100) >> 16);
304 static int or51211_read_signal_strength(struct dvb_frontend* fe, u16* strength)
306 /* Calculate Strength from SNR up to 35dB */
307 /* Even though the SNR can go higher than 35dB, there is some comfort */
308 /* factor in having a range of strong signals that can show at 100% */
309 struct or51211_state* state = (struct or51211_state*)fe->demodulator_priv;
313 ret = fe->ops.read_snr(fe, &snr);
316 /* Rather than use the 8.8 value snr, use state->snr which is 8.24 */
317 /* scale the range 0 - 35*2^24 into 0 - 65535 */
318 if (state->snr >= 8960 * 0x10000)
321 *strength = state->snr / 8960;
326 static int or51211_read_ber(struct dvb_frontend* fe, u32* ber)
332 static int or51211_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
338 static int or51211_sleep(struct dvb_frontend* fe)
343 static int or51211_init(struct dvb_frontend* fe)
345 struct or51211_state* state = fe->demodulator_priv;
346 const struct or51211_config* config = state->config;
347 const struct firmware* fw;
348 unsigned char get_ver_buf[] = {0x04,0x00,0x30,0x00,0x00};
349 unsigned char rec_buf[14];
352 if (!state->initialized) {
353 /* Request the firmware, this will block until it uploads */
354 pr_info("Waiting for firmware upload (%s)...\n",
356 ret = config->request_firmware(fe, &fw,
358 pr_info("Got Hotplug firmware\n");
360 pr_warn("No firmware uploaded (timeout or file not found?)\n");
364 ret = or51211_load_firmware(fe, fw);
365 release_firmware(fw);
367 pr_warn("Writing firmware to device failed!\n");
370 pr_info("Firmware upload complete.\n");
372 /* Set operation mode in Receiver 1 register;
374 * data 0x50h Automatic sets receiver channel conditions
375 * Automatic NTSC rejection filter
376 * Enable MPEG serial data output
378 * High tuner phase noise
379 * normal +/-150kHz Carrier acquisition range
381 if (i2c_writebytes(state,state->config->demod_address,
383 pr_warn("Load DVR Error 5\n");
387 /* Read back ucode version to besure we loaded correctly */
388 /* and are really up and running */
394 if (i2c_writebytes(state,state->config->demod_address,
396 pr_warn("Load DVR Error A\n");
400 if (i2c_readbytes(state,state->config->demod_address,
402 pr_warn("Load DVR Error B\n");
411 if (i2c_writebytes(state,state->config->demod_address,
413 pr_warn("Load DVR Error C\n");
417 if (i2c_readbytes(state,state->config->demod_address,
419 pr_warn("Load DVR Error D\n");
423 for (i = 0; i < 8; i++)
426 for (i = 0; i < 5; i++) {
428 get_ver_buf[4] = i+1;
429 if (i2c_writebytes(state,state->config->demod_address,
431 pr_warn("Load DVR Error 6 - %d\n", i);
436 if (i2c_readbytes(state,state->config->demod_address,
438 pr_warn("Load DVR Error 7 - %d\n", i);
441 /* If we didn't receive the right index, try again */
442 if ((int)rec_buf[i*2+1]!=i+1){
446 dprintk("read_fwbits %10ph\n", rec_buf);
448 pr_info("ver TU%02x%02x%02x VSB mode %02x Status %02x\n",
449 rec_buf[2], rec_buf[4], rec_buf[6], rec_buf[12],
457 if (i2c_writebytes(state,state->config->demod_address,
459 pr_warn("Load DVR Error 8\n");
463 if (i2c_readbytes(state,state->config->demod_address,
465 pr_warn("Load DVR Error 9\n");
468 state->initialized = 1;
474 static int or51211_get_tune_settings(struct dvb_frontend* fe,
475 struct dvb_frontend_tune_settings* fesettings)
477 fesettings->min_delay_ms = 500;
478 fesettings->step_size = 0;
479 fesettings->max_drift = 0;
483 static void or51211_release(struct dvb_frontend* fe)
485 struct or51211_state* state = fe->demodulator_priv;
486 state->config->sleep(fe);
490 static const struct dvb_frontend_ops or51211_ops;
492 struct dvb_frontend* or51211_attach(const struct or51211_config* config,
493 struct i2c_adapter* i2c)
495 struct or51211_state* state = NULL;
497 /* Allocate memory for the internal state */
498 state = kzalloc(sizeof(struct or51211_state), GFP_KERNEL);
502 /* Setup the state */
503 state->config = config;
505 state->initialized = 0;
506 state->current_frequency = 0;
508 /* Create dvb_frontend */
509 memcpy(&state->frontend.ops, &or51211_ops, sizeof(struct dvb_frontend_ops));
510 state->frontend.demodulator_priv = state;
511 return &state->frontend;
514 static const struct dvb_frontend_ops or51211_ops = {
515 .delsys = { SYS_ATSC, SYS_DVBC_ANNEX_B },
517 .name = "Oren OR51211 VSB Frontend",
518 .frequency_min_hz = 44 * MHz,
519 .frequency_max_hz = 958 * MHz,
520 .frequency_stepsize_hz = 166666,
521 .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
522 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
526 .release = or51211_release,
528 .init = or51211_init,
529 .sleep = or51211_sleep,
531 .set_frontend = or51211_set_parameters,
532 .get_tune_settings = or51211_get_tune_settings,
534 .read_status = or51211_read_status,
535 .read_ber = or51211_read_ber,
536 .read_signal_strength = or51211_read_signal_strength,
537 .read_snr = or51211_read_snr,
538 .read_ucblocks = or51211_read_ucblocks,
541 module_param(debug, int, 0644);
542 MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
544 MODULE_DESCRIPTION("Oren OR51211 VSB [pcHDTV HD-2000] Demodulator Driver");
545 MODULE_AUTHOR("Kirk Lapray");
546 MODULE_LICENSE("GPL");
548 EXPORT_SYMBOL(or51211_attach);