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.
26 * This driver needs external firmware. Please use the command
27 * "<kerneldir>/Documentation/dvb/get_dvb_firmware or51211" to
28 * download/extract it, and then copy it to /usr/lib/hotplug/firmware
29 * or /lib/firmware (depending on configuration of firmware hotplug).
31 #define OR51211_DEFAULT_FIRMWARE "dvb-fe-or51211.fw"
33 #include <linux/kernel.h>
34 #include <linux/module.h>
35 #include <linux/device.h>
36 #include <linux/firmware.h>
37 #include <linux/string.h>
38 #include <linux/slab.h>
39 #include <asm/byteorder.h>
42 #include "dvb_frontend.h"
46 #define dprintk(args...) \
48 if (debug) printk(KERN_DEBUG "or51211: " args); \
51 static u8 run_buf
[] = {0x7f,0x01};
52 static u8 cmd_buf
[] = {0x04,0x01,0x50,0x80,0x06}; // ATSC
54 struct or51211_state
{
56 struct i2c_adapter
* i2c
;
58 /* Configuration settings */
59 const struct or51211_config
* config
;
61 struct dvb_frontend frontend
;
64 /* Demodulator private data */
66 u32 snr
; /* Result of last SNR claculation */
68 /* Tuner private data */
69 u32 current_frequency
;
72 static int i2c_writebytes (struct or51211_state
* state
, u8 reg
, const u8
*buf
,
82 if ((err
= i2c_transfer (state
->i2c
, &msg
, 1)) != 1) {
83 printk(KERN_WARNING
"or51211: i2c_writebytes error "
84 "(addr %02x, err == %i)\n", reg
, err
);
91 static int i2c_readbytes(struct or51211_state
*state
, u8 reg
, u8
*buf
, int len
)
100 if ((err
= i2c_transfer (state
->i2c
, &msg
, 1)) != 1) {
101 printk(KERN_WARNING
"or51211: i2c_readbytes error "
102 "(addr %02x, err == %i)\n", reg
, err
);
109 static int or51211_load_firmware (struct dvb_frontend
* fe
,
110 const struct firmware
*fw
)
112 struct or51211_state
* state
= fe
->demodulator_priv
;
116 dprintk("Firmware is %zd bytes\n",fw
->size
);
120 if (i2c_writebytes(state
,0x50,tudata
,1)) {
121 printk(KERN_WARNING
"or51211:load_firmware error eprom addr\n");
124 if (i2c_readbytes(state
,0x50,&tudata
[145],192)) {
125 printk(KERN_WARNING
"or51211: load_firmware error eprom\n");
129 /* Create firmware buffer */
130 for (i
= 0; i
< 145; i
++)
131 tudata
[i
] = fw
->data
[i
];
133 for (i
= 0; i
< 248; i
++)
134 tudata
[i
+337] = fw
->data
[145+i
];
136 state
->config
->reset(fe
);
138 if (i2c_writebytes(state
,state
->config
->demod_address
,tudata
,585)) {
139 printk(KERN_WARNING
"or51211: load_firmware error 1\n");
144 if (i2c_writebytes(state
,state
->config
->demod_address
,
145 &fw
->data
[393],8125)) {
146 printk(KERN_WARNING
"or51211: load_firmware error 2\n");
151 if (i2c_writebytes(state
,state
->config
->demod_address
,run_buf
,2)) {
152 printk(KERN_WARNING
"or51211: load_firmware error 3\n");
156 /* Wait at least 5 msec */
158 if (i2c_writebytes(state
,state
->config
->demod_address
,run_buf
,2)) {
159 printk(KERN_WARNING
"or51211: load_firmware error 4\n");
164 printk("or51211: Done.\n");
168 static int or51211_setmode(struct dvb_frontend
* fe
, int mode
)
170 struct or51211_state
* state
= fe
->demodulator_priv
;
173 state
->config
->setmode(fe
, mode
);
175 if (i2c_writebytes(state
,state
->config
->demod_address
,run_buf
,2)) {
176 printk(KERN_WARNING
"or51211: setmode error 1\n");
180 /* Wait at least 5 msec */
182 if (i2c_writebytes(state
,state
->config
->demod_address
,run_buf
,2)) {
183 printk(KERN_WARNING
"or51211: setmode error 2\n");
189 /* Set operation mode in Receiver 1 register;
191 * data 0x50h Automatic sets receiver channel conditions
192 * Automatic NTSC rejection filter
193 * Enable MPEG serial data output
195 * High tuner phase noise
196 * normal +/-150kHz Carrier acquisition range
198 if (i2c_writebytes(state
,state
->config
->demod_address
,cmd_buf
,3)) {
199 printk(KERN_WARNING
"or51211: setmode error 3\n");
208 if (i2c_writebytes(state
,state
->config
->demod_address
,rec_buf
,3)) {
209 printk(KERN_WARNING
"or51211: setmode error 5\n");
212 if (i2c_readbytes(state
,state
->config
->demod_address
,&rec_buf
[10],2)) {
213 printk(KERN_WARNING
"or51211: setmode error 6");
216 dprintk("setmode rec status %02x %02x\n",rec_buf
[10],rec_buf
[11]);
221 static int or51211_set_parameters(struct dvb_frontend
*fe
)
223 struct dtv_frontend_properties
*p
= &fe
->dtv_property_cache
;
224 struct or51211_state
* state
= fe
->demodulator_priv
;
226 /* Change only if we are actually changing the channel */
227 if (state
->current_frequency
!= p
->frequency
) {
228 if (fe
->ops
.tuner_ops
.set_params
) {
229 fe
->ops
.tuner_ops
.set_params(fe
);
230 if (fe
->ops
.i2c_gate_ctrl
) fe
->ops
.i2c_gate_ctrl(fe
, 0);
233 /* Set to ATSC mode */
234 or51211_setmode(fe
,0);
236 /* Update current frequency */
237 state
->current_frequency
= p
->frequency
;
242 static int or51211_read_status(struct dvb_frontend
* fe
, fe_status_t
* status
)
244 struct or51211_state
* state
= fe
->demodulator_priv
;
245 unsigned char rec_buf
[2];
246 unsigned char snd_buf
[] = {0x04,0x00,0x03,0x00};
249 /* Receiver Status */
250 if (i2c_writebytes(state
,state
->config
->demod_address
,snd_buf
,3)) {
251 printk(KERN_WARNING
"or51132: read_status write error\n");
255 if (i2c_readbytes(state
,state
->config
->demod_address
,rec_buf
,2)) {
256 printk(KERN_WARNING
"or51132: read_status read error\n");
259 dprintk("read_status %x %x\n",rec_buf
[0],rec_buf
[1]);
261 if (rec_buf
[0] & 0x01) { /* Receiver Lock */
262 *status
|= FE_HAS_SIGNAL
;
263 *status
|= FE_HAS_CARRIER
;
264 *status
|= FE_HAS_VITERBI
;
265 *status
|= FE_HAS_SYNC
;
266 *status
|= FE_HAS_LOCK
;
271 /* Calculate SNR estimation (scaled by 2^24)
273 8-VSB SNR equation from Oren datasheets
276 SNR[dB] = 10 * log10(219037.9454 / MSE^2 )
278 We re-write the snr equation as:
279 SNR * 2^24 = 10*(c - 2*intlog10(MSE))
280 Where for 8-VSB, c = log10(219037.9454) * 2^24 */
282 static u32
calculate_snr(u32 mse
, u32 c
)
284 if (mse
== 0) /* No signal */
287 mse
= 2*intlog10(mse
);
289 /* Negative SNR, which is possible, but realisticly the
290 demod will lose lock before the signal gets this bad. The
291 API only allows for unsigned values, so just return 0 */
297 static int or51211_read_snr(struct dvb_frontend
* fe
, u16
* snr
)
299 struct or51211_state
* state
= fe
->demodulator_priv
;
303 /* SNR after Equalizer */
308 if (i2c_writebytes(state
,state
->config
->demod_address
,snd_buf
,3)) {
309 printk(KERN_WARNING
"%s: error writing snr reg\n",
313 if (i2c_readbytes(state
,state
->config
->demod_address
,rec_buf
,2)) {
314 printk(KERN_WARNING
"%s: read_status read error\n",
319 state
->snr
= calculate_snr(rec_buf
[0], 89599047);
320 *snr
= (state
->snr
) >> 16;
322 dprintk("%s: noise = 0x%02x, snr = %d.%02d dB\n", __func__
, rec_buf
[0],
323 state
->snr
>> 24, (((state
->snr
>>8) & 0xffff) * 100) >> 16);
328 static int or51211_read_signal_strength(struct dvb_frontend
* fe
, u16
* strength
)
330 /* Calculate Strength from SNR up to 35dB */
331 /* Even though the SNR can go higher than 35dB, there is some comfort */
332 /* factor in having a range of strong signals that can show at 100% */
333 struct or51211_state
* state
= (struct or51211_state
*)fe
->demodulator_priv
;
337 ret
= fe
->ops
.read_snr(fe
, &snr
);
340 /* Rather than use the 8.8 value snr, use state->snr which is 8.24 */
341 /* scale the range 0 - 35*2^24 into 0 - 65535 */
342 if (state
->snr
>= 8960 * 0x10000)
345 *strength
= state
->snr
/ 8960;
350 static int or51211_read_ber(struct dvb_frontend
* fe
, u32
* ber
)
356 static int or51211_read_ucblocks(struct dvb_frontend
* fe
, u32
* ucblocks
)
362 static int or51211_sleep(struct dvb_frontend
* fe
)
367 static int or51211_init(struct dvb_frontend
* fe
)
369 struct or51211_state
* state
= fe
->demodulator_priv
;
370 const struct or51211_config
* config
= state
->config
;
371 const struct firmware
* fw
;
372 unsigned char get_ver_buf
[] = {0x04,0x00,0x30,0x00,0x00};
373 unsigned char rec_buf
[14];
376 if (!state
->initialized
) {
377 /* Request the firmware, this will block until it uploads */
378 printk(KERN_INFO
"or51211: Waiting for firmware upload "
379 "(%s)...\n", OR51211_DEFAULT_FIRMWARE
);
380 ret
= config
->request_firmware(fe
, &fw
,
381 OR51211_DEFAULT_FIRMWARE
);
382 printk(KERN_INFO
"or51211:Got Hotplug firmware\n");
384 printk(KERN_WARNING
"or51211: No firmware uploaded "
385 "(timeout or file not found?)\n");
389 ret
= or51211_load_firmware(fe
, fw
);
390 release_firmware(fw
);
392 printk(KERN_WARNING
"or51211: Writing firmware to "
396 printk(KERN_INFO
"or51211: Firmware upload complete.\n");
398 /* Set operation mode in Receiver 1 register;
400 * data 0x50h Automatic sets receiver channel conditions
401 * Automatic NTSC rejection filter
402 * Enable MPEG serial data output
404 * High tuner phase noise
405 * normal +/-150kHz Carrier acquisition range
407 if (i2c_writebytes(state
,state
->config
->demod_address
,
409 printk(KERN_WARNING
"or51211: Load DVR Error 5\n");
413 /* Read back ucode version to besure we loaded correctly */
414 /* and are really up and running */
420 if (i2c_writebytes(state
,state
->config
->demod_address
,
422 printk(KERN_WARNING
"or51211: Load DVR Error A\n");
426 if (i2c_readbytes(state
,state
->config
->demod_address
,
428 printk(KERN_WARNING
"or51211: Load DVR Error B\n");
437 if (i2c_writebytes(state
,state
->config
->demod_address
,
439 printk(KERN_WARNING
"or51211: Load DVR Error C\n");
443 if (i2c_readbytes(state
,state
->config
->demod_address
,
445 printk(KERN_WARNING
"or51211: Load DVR Error D\n");
449 for (i
= 0; i
< 8; i
++)
452 for (i
= 0; i
< 5; i
++) {
454 get_ver_buf
[4] = i
+1;
455 if (i2c_writebytes(state
,state
->config
->demod_address
,
457 printk(KERN_WARNING
"or51211:Load DVR Error 6"
463 if (i2c_readbytes(state
,state
->config
->demod_address
,
465 printk(KERN_WARNING
"or51211:Load DVR Error 7"
469 /* If we didn't receive the right index, try again */
470 if ((int)rec_buf
[i
*2+1]!=i
+1){
474 dprintk("read_fwbits %x %x %x %x %x %x %x %x %x %x\n",
475 rec_buf
[0], rec_buf
[1], rec_buf
[2], rec_buf
[3],
476 rec_buf
[4], rec_buf
[5], rec_buf
[6], rec_buf
[7],
477 rec_buf
[8], rec_buf
[9]);
479 printk(KERN_INFO
"or51211: ver TU%02x%02x%02x VSB mode %02x"
481 rec_buf
[2], rec_buf
[4],rec_buf
[6],
482 rec_buf
[12],rec_buf
[10]);
489 if (i2c_writebytes(state
,state
->config
->demod_address
,
491 printk(KERN_WARNING
"or51211: Load DVR Error 8\n");
495 if (i2c_readbytes(state
,state
->config
->demod_address
,
497 printk(KERN_WARNING
"or51211: Load DVR Error 9\n");
500 state
->initialized
= 1;
506 static int or51211_get_tune_settings(struct dvb_frontend
* fe
,
507 struct dvb_frontend_tune_settings
* fesettings
)
509 fesettings
->min_delay_ms
= 500;
510 fesettings
->step_size
= 0;
511 fesettings
->max_drift
= 0;
515 static void or51211_release(struct dvb_frontend
* fe
)
517 struct or51211_state
* state
= fe
->demodulator_priv
;
518 state
->config
->sleep(fe
);
522 static struct dvb_frontend_ops or51211_ops
;
524 struct dvb_frontend
* or51211_attach(const struct or51211_config
* config
,
525 struct i2c_adapter
* i2c
)
527 struct or51211_state
* state
= NULL
;
529 /* Allocate memory for the internal state */
530 state
= kzalloc(sizeof(struct or51211_state
), GFP_KERNEL
);
534 /* Setup the state */
535 state
->config
= config
;
537 state
->initialized
= 0;
538 state
->current_frequency
= 0;
540 /* Create dvb_frontend */
541 memcpy(&state
->frontend
.ops
, &or51211_ops
, sizeof(struct dvb_frontend_ops
));
542 state
->frontend
.demodulator_priv
= state
;
543 return &state
->frontend
;
546 static struct dvb_frontend_ops or51211_ops
= {
547 .delsys
= { SYS_ATSC
, SYS_DVBC_ANNEX_B
},
549 .name
= "Oren OR51211 VSB Frontend",
550 .frequency_min
= 44000000,
551 .frequency_max
= 958000000,
552 .frequency_stepsize
= 166666,
553 .caps
= FE_CAN_FEC_1_2
| FE_CAN_FEC_2_3
| FE_CAN_FEC_3_4
|
554 FE_CAN_FEC_5_6
| FE_CAN_FEC_7_8
| FE_CAN_FEC_AUTO
|
558 .release
= or51211_release
,
560 .init
= or51211_init
,
561 .sleep
= or51211_sleep
,
563 .set_frontend
= or51211_set_parameters
,
564 .get_tune_settings
= or51211_get_tune_settings
,
566 .read_status
= or51211_read_status
,
567 .read_ber
= or51211_read_ber
,
568 .read_signal_strength
= or51211_read_signal_strength
,
569 .read_snr
= or51211_read_snr
,
570 .read_ucblocks
= or51211_read_ucblocks
,
573 module_param(debug
, int, 0644);
574 MODULE_PARM_DESC(debug
, "Turn on/off frontend debugging (default:off).");
576 MODULE_DESCRIPTION("Oren OR51211 VSB [pcHDTV HD-2000] Demodulator Driver");
577 MODULE_AUTHOR("Kirk Lapray");
578 MODULE_LICENSE("GPL");
580 EXPORT_SYMBOL(or51211_attach
);