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__
14 * This driver needs external firmware. Please use the command
15 * "<kerneldir>/scripts/get_dvb_firmware or51211" to
16 * download/extract it, and then copy it to /usr/lib/hotplug/firmware
17 * or /lib/firmware (depending on configuration of firmware hotplug).
19 #define OR51211_DEFAULT_FIRMWARE "dvb-fe-or51211.fw"
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/device.h>
24 #include <linux/firmware.h>
25 #include <linux/string.h>
26 #include <linux/slab.h>
27 #include <asm/byteorder.h>
29 #include <media/dvb_math.h>
30 #include <media/dvb_frontend.h>
34 #define dprintk(args...) \
35 do { if (debug) pr_debug(args); } while (0)
37 static u8 run_buf
[] = {0x7f,0x01};
38 static u8 cmd_buf
[] = {0x04,0x01,0x50,0x80,0x06}; // ATSC
40 struct or51211_state
{
42 struct i2c_adapter
* i2c
;
44 /* Configuration settings */
45 const struct or51211_config
* config
;
47 struct dvb_frontend frontend
;
50 /* Demodulator private data */
52 u32 snr
; /* Result of last SNR calculation */
54 /* Tuner private data */
55 u32 current_frequency
;
58 static int i2c_writebytes (struct or51211_state
* state
, u8 reg
, const u8
*buf
,
68 if ((err
= i2c_transfer (state
->i2c
, &msg
, 1)) != 1) {
69 pr_warn("error (addr %02x, err == %i)\n", reg
, err
);
76 static int i2c_readbytes(struct or51211_state
*state
, u8 reg
, u8
*buf
, int len
)
85 if ((err
= i2c_transfer (state
->i2c
, &msg
, 1)) != 1) {
86 pr_warn("error (addr %02x, err == %i)\n", reg
, err
);
93 static int or51211_load_firmware (struct dvb_frontend
* fe
,
94 const struct firmware
*fw
)
96 struct or51211_state
* state
= fe
->demodulator_priv
;
100 dprintk("Firmware is %zu bytes\n", fw
->size
);
104 if (i2c_writebytes(state
,0x50,tudata
,1)) {
105 pr_warn("error eprom addr\n");
108 if (i2c_readbytes(state
,0x50,&tudata
[145],192)) {
109 pr_warn("error eprom\n");
113 /* Create firmware buffer */
114 for (i
= 0; i
< 145; i
++)
115 tudata
[i
] = fw
->data
[i
];
117 for (i
= 0; i
< 248; i
++)
118 tudata
[i
+337] = fw
->data
[145+i
];
120 state
->config
->reset(fe
);
122 if (i2c_writebytes(state
,state
->config
->demod_address
,tudata
,585)) {
123 pr_warn("error 1\n");
128 if (i2c_writebytes(state
,state
->config
->demod_address
,
129 &fw
->data
[393],8125)) {
130 pr_warn("error 2\n");
135 if (i2c_writebytes(state
,state
->config
->demod_address
,run_buf
,2)) {
136 pr_warn("error 3\n");
140 /* Wait at least 5 msec */
142 if (i2c_writebytes(state
,state
->config
->demod_address
,run_buf
,2)) {
143 pr_warn("error 4\n");
152 static int or51211_setmode(struct dvb_frontend
* fe
, int mode
)
154 struct or51211_state
* state
= fe
->demodulator_priv
;
157 state
->config
->setmode(fe
, mode
);
159 if (i2c_writebytes(state
,state
->config
->demod_address
,run_buf
,2)) {
160 pr_warn("error 1\n");
164 /* Wait at least 5 msec */
166 if (i2c_writebytes(state
,state
->config
->demod_address
,run_buf
,2)) {
167 pr_warn("error 2\n");
173 /* Set operation mode in Receiver 1 register;
175 * data 0x50h Automatic sets receiver channel conditions
176 * Automatic NTSC rejection filter
177 * Enable MPEG serial data output
179 * High tuner phase noise
180 * normal +/-150kHz Carrier acquisition range
182 if (i2c_writebytes(state
,state
->config
->demod_address
,cmd_buf
,3)) {
183 pr_warn("error 3\n");
192 if (i2c_writebytes(state
,state
->config
->demod_address
,rec_buf
,3)) {
193 pr_warn("error 5\n");
196 if (i2c_readbytes(state
,state
->config
->demod_address
,&rec_buf
[10],2)) {
197 pr_warn("error 6\n");
200 dprintk("rec status %02x %02x\n", rec_buf
[10], rec_buf
[11]);
205 static int or51211_set_parameters(struct dvb_frontend
*fe
)
207 struct dtv_frontend_properties
*p
= &fe
->dtv_property_cache
;
208 struct or51211_state
* state
= fe
->demodulator_priv
;
210 /* Change only if we are actually changing the channel */
211 if (state
->current_frequency
!= p
->frequency
) {
212 if (fe
->ops
.tuner_ops
.set_params
) {
213 fe
->ops
.tuner_ops
.set_params(fe
);
214 if (fe
->ops
.i2c_gate_ctrl
) fe
->ops
.i2c_gate_ctrl(fe
, 0);
217 /* Set to ATSC mode */
218 or51211_setmode(fe
,0);
220 /* Update current frequency */
221 state
->current_frequency
= p
->frequency
;
226 static int or51211_read_status(struct dvb_frontend
*fe
, enum fe_status
*status
)
228 struct or51211_state
* state
= fe
->demodulator_priv
;
229 unsigned char rec_buf
[2];
230 unsigned char snd_buf
[] = {0x04,0x00,0x03,0x00};
233 /* Receiver Status */
234 if (i2c_writebytes(state
,state
->config
->demod_address
,snd_buf
,3)) {
235 pr_warn("write error\n");
239 if (i2c_readbytes(state
,state
->config
->demod_address
,rec_buf
,2)) {
240 pr_warn("read error\n");
243 dprintk("%x %x\n", rec_buf
[0], rec_buf
[1]);
245 if (rec_buf
[0] & 0x01) { /* Receiver Lock */
246 *status
|= FE_HAS_SIGNAL
;
247 *status
|= FE_HAS_CARRIER
;
248 *status
|= FE_HAS_VITERBI
;
249 *status
|= FE_HAS_SYNC
;
250 *status
|= FE_HAS_LOCK
;
255 /* Calculate SNR estimation (scaled by 2^24)
257 8-VSB SNR equation from Oren datasheets
260 SNR[dB] = 10 * log10(219037.9454 / MSE^2 )
262 We re-write the snr equation as:
263 SNR * 2^24 = 10*(c - 2*intlog10(MSE))
264 Where for 8-VSB, c = log10(219037.9454) * 2^24 */
266 static u32
calculate_snr(u32 mse
, u32 c
)
268 if (mse
== 0) /* No signal */
271 mse
= 2*intlog10(mse
);
273 /* Negative SNR, which is possible, but realisticly the
274 demod will lose lock before the signal gets this bad. The
275 API only allows for unsigned values, so just return 0 */
281 static int or51211_read_snr(struct dvb_frontend
* fe
, u16
* snr
)
283 struct or51211_state
* state
= fe
->demodulator_priv
;
287 /* SNR after Equalizer */
292 if (i2c_writebytes(state
,state
->config
->demod_address
,snd_buf
,3)) {
293 pr_warn("error writing snr reg\n");
296 if (i2c_readbytes(state
,state
->config
->demod_address
,rec_buf
,2)) {
297 pr_warn("read_status read error\n");
301 state
->snr
= calculate_snr(rec_buf
[0], 89599047);
302 *snr
= (state
->snr
) >> 16;
304 dprintk("noise = 0x%02x, snr = %d.%02d dB\n", rec_buf
[0],
305 state
->snr
>> 24, (((state
->snr
>>8) & 0xffff) * 100) >> 16);
310 static int or51211_read_signal_strength(struct dvb_frontend
* fe
, u16
* strength
)
312 /* Calculate Strength from SNR up to 35dB */
313 /* Even though the SNR can go higher than 35dB, there is some comfort */
314 /* factor in having a range of strong signals that can show at 100% */
315 struct or51211_state
* state
= (struct or51211_state
*)fe
->demodulator_priv
;
319 ret
= fe
->ops
.read_snr(fe
, &snr
);
322 /* Rather than use the 8.8 value snr, use state->snr which is 8.24 */
323 /* scale the range 0 - 35*2^24 into 0 - 65535 */
324 if (state
->snr
>= 8960 * 0x10000)
327 *strength
= state
->snr
/ 8960;
332 static int or51211_read_ber(struct dvb_frontend
* fe
, u32
* ber
)
338 static int or51211_read_ucblocks(struct dvb_frontend
* fe
, u32
* ucblocks
)
344 static int or51211_sleep(struct dvb_frontend
* fe
)
349 static int or51211_init(struct dvb_frontend
* fe
)
351 struct or51211_state
* state
= fe
->demodulator_priv
;
352 const struct or51211_config
* config
= state
->config
;
353 const struct firmware
* fw
;
354 unsigned char get_ver_buf
[] = {0x04,0x00,0x30,0x00,0x00};
355 unsigned char rec_buf
[14];
358 if (!state
->initialized
) {
359 /* Request the firmware, this will block until it uploads */
360 pr_info("Waiting for firmware upload (%s)...\n",
361 OR51211_DEFAULT_FIRMWARE
);
362 ret
= config
->request_firmware(fe
, &fw
,
363 OR51211_DEFAULT_FIRMWARE
);
364 pr_info("Got Hotplug firmware\n");
366 pr_warn("No firmware uploaded (timeout or file not found?)\n");
370 ret
= or51211_load_firmware(fe
, fw
);
371 release_firmware(fw
);
373 pr_warn("Writing firmware to device failed!\n");
376 pr_info("Firmware upload complete.\n");
378 /* Set operation mode in Receiver 1 register;
380 * data 0x50h Automatic sets receiver channel conditions
381 * Automatic NTSC rejection filter
382 * Enable MPEG serial data output
384 * High tuner phase noise
385 * normal +/-150kHz Carrier acquisition range
387 if (i2c_writebytes(state
,state
->config
->demod_address
,
389 pr_warn("Load DVR Error 5\n");
393 /* Read back ucode version to besure we loaded correctly */
394 /* and are really up and running */
400 if (i2c_writebytes(state
,state
->config
->demod_address
,
402 pr_warn("Load DVR Error A\n");
406 if (i2c_readbytes(state
,state
->config
->demod_address
,
408 pr_warn("Load DVR Error B\n");
417 if (i2c_writebytes(state
,state
->config
->demod_address
,
419 pr_warn("Load DVR Error C\n");
423 if (i2c_readbytes(state
,state
->config
->demod_address
,
425 pr_warn("Load DVR Error D\n");
429 for (i
= 0; i
< 8; i
++)
432 for (i
= 0; i
< 5; i
++) {
434 get_ver_buf
[4] = i
+1;
435 if (i2c_writebytes(state
,state
->config
->demod_address
,
437 pr_warn("Load DVR Error 6 - %d\n", i
);
442 if (i2c_readbytes(state
,state
->config
->demod_address
,
444 pr_warn("Load DVR Error 7 - %d\n", i
);
447 /* If we didn't receive the right index, try again */
448 if ((int)rec_buf
[i
*2+1]!=i
+1){
452 dprintk("read_fwbits %10ph\n", rec_buf
);
454 pr_info("ver TU%02x%02x%02x VSB mode %02x Status %02x\n",
455 rec_buf
[2], rec_buf
[4], rec_buf
[6], rec_buf
[12],
463 if (i2c_writebytes(state
,state
->config
->demod_address
,
465 pr_warn("Load DVR Error 8\n");
469 if (i2c_readbytes(state
,state
->config
->demod_address
,
471 pr_warn("Load DVR Error 9\n");
474 state
->initialized
= 1;
480 static int or51211_get_tune_settings(struct dvb_frontend
* fe
,
481 struct dvb_frontend_tune_settings
* fesettings
)
483 fesettings
->min_delay_ms
= 500;
484 fesettings
->step_size
= 0;
485 fesettings
->max_drift
= 0;
489 static void or51211_release(struct dvb_frontend
* fe
)
491 struct or51211_state
* state
= fe
->demodulator_priv
;
492 state
->config
->sleep(fe
);
496 static const struct dvb_frontend_ops or51211_ops
;
498 struct dvb_frontend
* or51211_attach(const struct or51211_config
* config
,
499 struct i2c_adapter
* i2c
)
501 struct or51211_state
* state
= NULL
;
503 /* Allocate memory for the internal state */
504 state
= kzalloc(sizeof(struct or51211_state
), GFP_KERNEL
);
508 /* Setup the state */
509 state
->config
= config
;
511 state
->initialized
= 0;
512 state
->current_frequency
= 0;
514 /* Create dvb_frontend */
515 memcpy(&state
->frontend
.ops
, &or51211_ops
, sizeof(struct dvb_frontend_ops
));
516 state
->frontend
.demodulator_priv
= state
;
517 return &state
->frontend
;
520 static const struct dvb_frontend_ops or51211_ops
= {
521 .delsys
= { SYS_ATSC
, SYS_DVBC_ANNEX_B
},
523 .name
= "Oren OR51211 VSB Frontend",
524 .frequency_min_hz
= 44 * MHz
,
525 .frequency_max_hz
= 958 * MHz
,
526 .frequency_stepsize_hz
= 166666,
527 .caps
= FE_CAN_FEC_1_2
| FE_CAN_FEC_2_3
| FE_CAN_FEC_3_4
|
528 FE_CAN_FEC_5_6
| FE_CAN_FEC_7_8
| FE_CAN_FEC_AUTO
|
532 .release
= or51211_release
,
534 .init
= or51211_init
,
535 .sleep
= or51211_sleep
,
537 .set_frontend
= or51211_set_parameters
,
538 .get_tune_settings
= or51211_get_tune_settings
,
540 .read_status
= or51211_read_status
,
541 .read_ber
= or51211_read_ber
,
542 .read_signal_strength
= or51211_read_signal_strength
,
543 .read_snr
= or51211_read_snr
,
544 .read_ucblocks
= or51211_read_ucblocks
,
547 module_param(debug
, int, 0644);
548 MODULE_PARM_DESC(debug
, "Turn on/off frontend debugging (default:off).");
550 MODULE_DESCRIPTION("Oren OR51211 VSB [pcHDTV HD-2000] Demodulator Driver");
551 MODULE_AUTHOR("Kirk Lapray");
552 MODULE_LICENSE("GPL");
554 EXPORT_SYMBOL(or51211_attach
);