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/moduleparam.h>
36 #include <linux/device.h>
37 #include <linux/firmware.h>
38 #include <linux/string.h>
39 #include <linux/slab.h>
40 #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
;
57 struct dvb_frontend_ops ops
;
59 /* Configuration settings */
60 const struct or51211_config
* config
;
62 struct dvb_frontend frontend
;
65 /* Demodulator private data */
68 /* Tuner private data */
69 u32 current_frequency
;
72 static int i2c_writebytes (struct or51211_state
* state
, u8 reg
, 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 u8
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
,
222 struct dvb_frontend_parameters
*param
)
224 struct or51211_state
* state
= fe
->demodulator_priv
;
229 /* Change only if we are actually changing the channel */
230 if (state
->current_frequency
!= param
->frequency
) {
231 freq
= 44000 + (param
->frequency
/1000);
232 tunerfreq
= freq
* 16/1000;
234 dprintk("set_parameters frequency = %d (tunerfreq = %d)\n",
235 param
->frequency
,tunerfreq
);
237 buf
[0] = (tunerfreq
>> 8) & 0x7F;
238 buf
[1] = (tunerfreq
& 0xFF);
241 if (param
->frequency
< 157250000) {
243 dprintk("set_parameters VHF low range\n");
244 } else if (param
->frequency
< 454000000) {
246 dprintk("set_parameters VHF high range\n");
249 dprintk("set_parameters UHF range\n");
251 dprintk("set_parameters tuner bytes: 0x%02x 0x%02x "
252 "0x%02x 0x%02x\n",buf
[0],buf
[1],buf
[2],buf
[3]);
254 if (i2c_writebytes(state
,0xC2>>1,buf
,4))
255 printk(KERN_WARNING
"or51211:set_parameters error "
256 "writing to tuner\n");
258 /* Set to ATSC mode */
259 or51211_setmode(fe
,0);
261 /* Update current frequency */
262 state
->current_frequency
= param
->frequency
;
267 static int or51211_read_status(struct dvb_frontend
* fe
, fe_status_t
* status
)
269 struct or51211_state
* state
= fe
->demodulator_priv
;
270 unsigned char rec_buf
[2];
271 unsigned char snd_buf
[] = {0x04,0x00,0x03,0x00};
274 /* Receiver Status */
275 if (i2c_writebytes(state
,state
->config
->demod_address
,snd_buf
,3)) {
276 printk(KERN_WARNING
"or51132: read_status write error\n");
280 if (i2c_readbytes(state
,state
->config
->demod_address
,rec_buf
,2)) {
281 printk(KERN_WARNING
"or51132: read_status read error\n");
284 dprintk("read_status %x %x\n",rec_buf
[0],rec_buf
[1]);
286 if (rec_buf
[0] & 0x01) { /* Receiver Lock */
287 *status
|= FE_HAS_SIGNAL
;
288 *status
|= FE_HAS_CARRIER
;
289 *status
|= FE_HAS_VITERBI
;
290 *status
|= FE_HAS_SYNC
;
291 *status
|= FE_HAS_LOCK
;
296 /* log10-1 table at .5 increments from 1 to 100.5 */
297 static unsigned int i100x20log10
[] = {
298 0, 352, 602, 795, 954, 1088, 1204, 1306, 1397, 1480,
299 1556, 1625, 1690, 1750, 1806, 1858, 1908, 1955, 2000, 2042,
300 2082, 2121, 2158, 2193, 2227, 2260, 2292, 2322, 2352, 2380,
301 2408, 2434, 2460, 2486, 2510, 2534, 2557, 2580, 2602, 2623,
302 2644, 2664, 2684, 2704, 2723, 2742, 2760, 2778, 2795, 2813,
303 2829, 2846, 2862, 2878, 2894, 2909, 2924, 2939, 2954, 2968,
304 2982, 2996, 3010, 3023, 3037, 3050, 3062, 3075, 3088, 3100,
305 3112, 3124, 3136, 3148, 3159, 3170, 3182, 3193, 3204, 3214,
306 3225, 3236, 3246, 3256, 3266, 3276, 3286, 3296, 3306, 3316,
307 3325, 3334, 3344, 3353, 3362, 3371, 3380, 3389, 3397, 3406,
308 3415, 3423, 3432, 3440, 3448, 3456, 3464, 3472, 3480, 3488,
309 3496, 3504, 3511, 3519, 3526, 3534, 3541, 3549, 3556, 3563,
310 3570, 3577, 3584, 3591, 3598, 3605, 3612, 3619, 3625, 3632,
311 3639, 3645, 3652, 3658, 3665, 3671, 3677, 3683, 3690, 3696,
312 3702, 3708, 3714, 3720, 3726, 3732, 3738, 3744, 3750, 3755,
313 3761, 3767, 3772, 3778, 3784, 3789, 3795, 3800, 3806, 3811,
314 3816, 3822, 3827, 3832, 3838, 3843, 3848, 3853, 3858, 3863,
315 3868, 3874, 3879, 3884, 3888, 3893, 3898, 3903, 3908, 3913,
316 3918, 3922, 3927, 3932, 3936, 3941, 3946, 3950, 3955, 3960,
317 3964, 3969, 3973, 3978, 3982, 3986, 3991, 3995, 4000, 4004,
320 static unsigned int denom
[] = {1,1,100,1000,10000,100000,1000000,10000000,100000000};
322 static unsigned int i20Log10(unsigned short val
)
324 unsigned int rntval
= 100;
325 unsigned int tmp
= val
;
326 unsigned int exp
= 1;
328 while(tmp
> 100) {tmp
/= 100; exp
++;}
330 val
= (2 * val
)/denom
[exp
];
331 if (exp
> 1) rntval
= 2000*exp
;
333 rntval
+= i100x20log10
[val
];
337 static int or51211_read_signal_strength(struct dvb_frontend
* fe
, u16
* strength
)
339 struct or51211_state
* state
= fe
->demodulator_priv
;
345 /* SNR after Equalizer */
351 if (i2c_writebytes(state
,state
->config
->demod_address
,snd_buf
,3)) {
352 printk(KERN_WARNING
"or51211: read_status write error\n");
356 if (i2c_readbytes(state
,state
->config
->demod_address
,rec_buf
,2)) {
357 printk(KERN_WARNING
"or51211: read_status read error\n");
360 snr_equ
= rec_buf
[0] & 0xff;
362 /* The value reported back from the frontend will be FFFF=100% 0000=0% */
363 signal_strength
= (((5334 - i20Log10(snr_equ
))/3+5)*65535)/1000;
364 if (signal_strength
> 0xffff)
367 *strength
= signal_strength
;
368 dprintk("read_signal_strength %i\n",*strength
);
373 static int or51211_read_snr(struct dvb_frontend
* fe
, u16
* snr
)
375 struct or51211_state
* state
= fe
->demodulator_priv
;
379 /* SNR after Equalizer */
385 if (i2c_writebytes(state
,state
->config
->demod_address
,snd_buf
,3)) {
386 printk(KERN_WARNING
"or51211: read_status write error\n");
390 if (i2c_readbytes(state
,state
->config
->demod_address
,rec_buf
,2)) {
391 printk(KERN_WARNING
"or51211: read_status read error\n");
394 *snr
= rec_buf
[0] & 0xff;
396 dprintk("read_snr %i\n",*snr
);
401 static int or51211_read_ber(struct dvb_frontend
* fe
, u32
* ber
)
407 static int or51211_read_ucblocks(struct dvb_frontend
* fe
, u32
* ucblocks
)
413 static int or51211_sleep(struct dvb_frontend
* fe
)
418 static int or51211_init(struct dvb_frontend
* fe
)
420 struct or51211_state
* state
= fe
->demodulator_priv
;
421 const struct or51211_config
* config
= state
->config
;
422 const struct firmware
* fw
;
423 unsigned char get_ver_buf
[] = {0x04,0x00,0x30,0x00,0x00};
424 unsigned char rec_buf
[14];
427 if (!state
->initialized
) {
428 /* Request the firmware, this will block until it uploads */
429 printk(KERN_INFO
"or51211: Waiting for firmware upload "
430 "(%s)...\n", OR51211_DEFAULT_FIRMWARE
);
431 ret
= config
->request_firmware(fe
, &fw
,
432 OR51211_DEFAULT_FIRMWARE
);
433 printk(KERN_INFO
"or51211:Got Hotplug firmware\n");
435 printk(KERN_WARNING
"or51211: No firmware uploaded "
436 "(timeout or file not found?)\n");
440 ret
= or51211_load_firmware(fe
, fw
);
442 printk(KERN_WARNING
"or51211: Writing firmware to "
444 release_firmware(fw
);
447 printk(KERN_INFO
"or51211: Firmware upload complete.\n");
449 /* Set operation mode in Receiver 1 register;
451 * data 0x50h Automatic sets receiver channel conditions
452 * Automatic NTSC rejection filter
453 * Enable MPEG serial data output
455 * High tuner phase noise
456 * normal +/-150kHz Carrier acquisition range
458 if (i2c_writebytes(state
,state
->config
->demod_address
,
460 printk(KERN_WARNING
"or51211: Load DVR Error 5\n");
464 /* Read back ucode version to besure we loaded correctly */
465 /* and are really up and running */
471 if (i2c_writebytes(state
,state
->config
->demod_address
,
473 printk(KERN_WARNING
"or51211: Load DVR Error A\n");
477 if (i2c_readbytes(state
,state
->config
->demod_address
,
479 printk(KERN_WARNING
"or51211: Load DVR Error B\n");
488 if (i2c_writebytes(state
,state
->config
->demod_address
,
490 printk(KERN_WARNING
"or51211: Load DVR Error C\n");
494 if (i2c_readbytes(state
,state
->config
->demod_address
,
496 printk(KERN_WARNING
"or51211: Load DVR Error D\n");
500 for (i
= 0; i
< 8; i
++)
503 for (i
= 0; i
< 5; i
++) {
505 get_ver_buf
[4] = i
+1;
506 if (i2c_writebytes(state
,state
->config
->demod_address
,
508 printk(KERN_WARNING
"or51211:Load DVR Error 6"
514 if (i2c_readbytes(state
,state
->config
->demod_address
,
516 printk(KERN_WARNING
"or51211:Load DVR Error 7"
520 /* If we didn't receive the right index, try again */
521 if ((int)rec_buf
[i
*2+1]!=i
+1){
525 dprintk("read_fwbits %x %x %x %x %x %x %x %x %x %x\n",
526 rec_buf
[0], rec_buf
[1], rec_buf
[2], rec_buf
[3],
527 rec_buf
[4], rec_buf
[5], rec_buf
[6], rec_buf
[7],
528 rec_buf
[8], rec_buf
[9]);
530 printk(KERN_INFO
"or51211: ver TU%02x%02x%02x VSB mode %02x"
532 rec_buf
[2], rec_buf
[4],rec_buf
[6],
533 rec_buf
[12],rec_buf
[10]);
540 if (i2c_writebytes(state
,state
->config
->demod_address
,
542 printk(KERN_WARNING
"or51211: Load DVR Error 8\n");
546 if (i2c_readbytes(state
,state
->config
->demod_address
,
548 printk(KERN_WARNING
"or51211: Load DVR Error 9\n");
551 state
->initialized
= 1;
557 static int or51211_get_tune_settings(struct dvb_frontend
* fe
,
558 struct dvb_frontend_tune_settings
* fesettings
)
560 fesettings
->min_delay_ms
= 500;
561 fesettings
->step_size
= 0;
562 fesettings
->max_drift
= 0;
566 static void or51211_release(struct dvb_frontend
* fe
)
568 struct or51211_state
* state
= fe
->demodulator_priv
;
569 state
->config
->sleep(fe
);
573 static struct dvb_frontend_ops or51211_ops
;
575 struct dvb_frontend
* or51211_attach(const struct or51211_config
* config
,
576 struct i2c_adapter
* i2c
)
578 struct or51211_state
* state
= NULL
;
580 /* Allocate memory for the internal state */
581 state
= kmalloc(sizeof(struct or51211_state
), GFP_KERNEL
);
585 /* Setup the state */
586 state
->config
= config
;
588 memcpy(&state
->ops
, &or51211_ops
, sizeof(struct dvb_frontend_ops
));
589 state
->initialized
= 0;
590 state
->current_frequency
= 0;
592 /* Create dvb_frontend */
593 state
->frontend
.ops
= &state
->ops
;
594 state
->frontend
.demodulator_priv
= state
;
595 return &state
->frontend
;
602 static struct dvb_frontend_ops or51211_ops
= {
605 .name
= "Oren OR51211 VSB Frontend",
607 .frequency_min
= 44000000,
608 .frequency_max
= 958000000,
609 .frequency_stepsize
= 166666,
610 .caps
= FE_CAN_FEC_1_2
| FE_CAN_FEC_2_3
| FE_CAN_FEC_3_4
|
611 FE_CAN_FEC_5_6
| FE_CAN_FEC_7_8
| FE_CAN_FEC_AUTO
|
615 .release
= or51211_release
,
617 .init
= or51211_init
,
618 .sleep
= or51211_sleep
,
620 .set_frontend
= or51211_set_parameters
,
621 .get_tune_settings
= or51211_get_tune_settings
,
623 .read_status
= or51211_read_status
,
624 .read_ber
= or51211_read_ber
,
625 .read_signal_strength
= or51211_read_signal_strength
,
626 .read_snr
= or51211_read_snr
,
627 .read_ucblocks
= or51211_read_ucblocks
,
630 module_param(debug
, int, 0644);
631 MODULE_PARM_DESC(debug
, "Turn on/off frontend debugging (default:off).");
633 MODULE_DESCRIPTION("Oren OR51211 VSB [pcHDTV HD-2000] Demodulator Driver");
634 MODULE_AUTHOR("Kirk Lapray");
635 MODULE_LICENSE("GPL");
637 EXPORT_SYMBOL(or51211_attach
);