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.
30 #define OR51211_DEFAULT_FIRMWARE "dvb-fe-or51211.fw"
32 #include <linux/kernel.h>
33 #include <linux/module.h>
34 #include <linux/moduleparam.h>
35 #include <linux/device.h>
36 #include <linux/firmware.h>
37 #include <asm/byteorder.h>
39 #include "dvb_frontend.h"
43 #define dprintk(args...) \
45 if (debug) printk(KERN_DEBUG "or51211: " args); \
48 static u8 run_buf
[] = {0x7f,0x01};
49 static u8 cmd_buf
[] = {0x04,0x01,0x50,0x80,0x06}; // ATSC
51 struct or51211_state
{
53 struct i2c_adapter
* i2c
;
54 struct dvb_frontend_ops ops
;
56 /* Configuration settings */
57 const struct or51211_config
* config
;
59 struct dvb_frontend frontend
;
62 /* Demodulator private data */
65 /* Tuner private data */
66 u32 current_frequency
;
69 static int i2c_writebytes (struct or51211_state
* state
, u8 reg
, u8
*buf
,
79 if ((err
= i2c_transfer (state
->i2c
, &msg
, 1)) != 1) {
80 printk(KERN_WARNING
"or51211: i2c_writebytes error "
81 "(addr %02x, err == %i)\n", reg
, err
);
88 static u8
i2c_readbytes (struct or51211_state
* state
, u8 reg
, u8
* buf
, int len
)
97 if ((err
= i2c_transfer (state
->i2c
, &msg
, 1)) != 1) {
98 printk(KERN_WARNING
"or51211: i2c_readbytes error "
99 "(addr %02x, err == %i)\n", reg
, err
);
106 static int or51211_load_firmware (struct dvb_frontend
* fe
,
107 const struct firmware
*fw
)
109 struct or51211_state
* state
= fe
->demodulator_priv
;
113 dprintk("Firmware is %d bytes\n",fw
->size
);
117 if (i2c_writebytes(state
,0x50,tudata
,1)) {
118 printk(KERN_WARNING
"or51211:load_firmware error eprom addr\n");
121 if (i2c_readbytes(state
,0x50,&tudata
[145],192)) {
122 printk(KERN_WARNING
"or51211: load_firmware error eprom\n");
126 /* Create firmware buffer */
127 for (i
= 0; i
< 145; i
++)
128 tudata
[i
] = fw
->data
[i
];
130 for (i
= 0; i
< 248; i
++)
131 tudata
[i
+337] = fw
->data
[145+i
];
133 state
->config
->reset(fe
);
135 if (i2c_writebytes(state
,state
->config
->demod_address
,tudata
,585)) {
136 printk(KERN_WARNING
"or51211: load_firmware error 1\n");
141 if (i2c_writebytes(state
,state
->config
->demod_address
,
142 &fw
->data
[393],8125)) {
143 printk(KERN_WARNING
"or51211: load_firmware error 2\n");
148 if (i2c_writebytes(state
,state
->config
->demod_address
,run_buf
,2)) {
149 printk(KERN_WARNING
"or51211: load_firmware error 3\n");
153 /* Wait at least 5 msec */
155 if (i2c_writebytes(state
,state
->config
->demod_address
,run_buf
,2)) {
156 printk(KERN_WARNING
"or51211: load_firmware error 4\n");
161 printk("or51211: Done.\n");
165 static int or51211_setmode(struct dvb_frontend
* fe
, int mode
)
167 struct or51211_state
* state
= fe
->demodulator_priv
;
170 state
->config
->setmode(fe
, mode
);
172 if (i2c_writebytes(state
,state
->config
->demod_address
,run_buf
,2)) {
173 printk(KERN_WARNING
"or51211: setmode error 1\n");
177 /* Wait at least 5 msec */
179 if (i2c_writebytes(state
,state
->config
->demod_address
,run_buf
,2)) {
180 printk(KERN_WARNING
"or51211: setmode error 2\n");
186 /* Set operation mode in Receiver 1 register;
188 * data 0x50h Automatic sets receiver channel conditions
189 * Automatic NTSC rejection filter
190 * Enable MPEG serial data output
192 * High tuner phase noise
193 * normal +/-150kHz Carrier acquisition range
195 if (i2c_writebytes(state
,state
->config
->demod_address
,cmd_buf
,3)) {
196 printk(KERN_WARNING
"or51211: setmode error 3\n");
205 if (i2c_writebytes(state
,state
->config
->demod_address
,rec_buf
,3)) {
206 printk(KERN_WARNING
"or51211: setmode error 5\n");
209 if (i2c_readbytes(state
,state
->config
->demod_address
,&rec_buf
[10],2)) {
210 printk(KERN_WARNING
"or51211: setmode error 6");
213 dprintk("setmode rec status %02x %02x\n",rec_buf
[10],rec_buf
[11]);
218 static int or51211_set_parameters(struct dvb_frontend
* fe
,
219 struct dvb_frontend_parameters
*param
)
221 struct or51211_state
* state
= fe
->demodulator_priv
;
226 /* Change only if we are actually changing the channel */
227 if (state
->current_frequency
!= param
->frequency
) {
228 freq
= 44000 + (param
->frequency
/1000);
229 tunerfreq
= freq
* 16/1000;
231 dprintk("set_parameters frequency = %d (tunerfreq = %d)\n",
232 param
->frequency
,tunerfreq
);
234 buf
[0] = (tunerfreq
>> 8) & 0x7F;
235 buf
[1] = (tunerfreq
& 0xFF);
238 if (param
->frequency
< 157250000) {
240 dprintk("set_parameters VHF low range\n");
241 } else if (param
->frequency
< 454000000) {
243 dprintk("set_parameters VHF high range\n");
246 dprintk("set_parameters UHF range\n");
248 dprintk("set_parameters tuner bytes: 0x%02x 0x%02x "
249 "0x%02x 0x%02x\n",buf
[0],buf
[1],buf
[2],buf
[3]);
251 if (i2c_writebytes(state
,0xC2>>1,buf
,4))
252 printk(KERN_WARNING
"or51211:set_parameters error "
253 "writing to tuner\n");
255 /* Set to ATSC mode */
256 or51211_setmode(fe
,0);
258 /* Update current frequency */
259 state
->current_frequency
= param
->frequency
;
264 static int or51211_read_status(struct dvb_frontend
* fe
, fe_status_t
* status
)
266 struct or51211_state
* state
= fe
->demodulator_priv
;
267 unsigned char rec_buf
[2];
268 unsigned char snd_buf
[] = {0x04,0x00,0x03,0x00};
271 /* Receiver Status */
272 if (i2c_writebytes(state
,state
->config
->demod_address
,snd_buf
,3)) {
273 printk(KERN_WARNING
"or51132: read_status write error\n");
277 if (i2c_readbytes(state
,state
->config
->demod_address
,rec_buf
,2)) {
278 printk(KERN_WARNING
"or51132: read_status read error\n");
281 dprintk("read_status %x %x\n",rec_buf
[0],rec_buf
[1]);
283 if (rec_buf
[0] & 0x01) { /* Receiver Lock */
284 *status
|= FE_HAS_SIGNAL
;
285 *status
|= FE_HAS_CARRIER
;
286 *status
|= FE_HAS_VITERBI
;
287 *status
|= FE_HAS_SYNC
;
288 *status
|= FE_HAS_LOCK
;
293 /* log10-1 table at .5 increments from 1 to 100.5 */
294 static unsigned int i100x20log10
[] = {
295 0, 352, 602, 795, 954, 1088, 1204, 1306, 1397, 1480,
296 1556, 1625, 1690, 1750, 1806, 1858, 1908, 1955, 2000, 2042,
297 2082, 2121, 2158, 2193, 2227, 2260, 2292, 2322, 2352, 2380,
298 2408, 2434, 2460, 2486, 2510, 2534, 2557, 2580, 2602, 2623,
299 2644, 2664, 2684, 2704, 2723, 2742, 2760, 2778, 2795, 2813,
300 2829, 2846, 2862, 2878, 2894, 2909, 2924, 2939, 2954, 2968,
301 2982, 2996, 3010, 3023, 3037, 3050, 3062, 3075, 3088, 3100,
302 3112, 3124, 3136, 3148, 3159, 3170, 3182, 3193, 3204, 3214,
303 3225, 3236, 3246, 3256, 3266, 3276, 3286, 3296, 3306, 3316,
304 3325, 3334, 3344, 3353, 3362, 3371, 3380, 3389, 3397, 3406,
305 3415, 3423, 3432, 3440, 3448, 3456, 3464, 3472, 3480, 3488,
306 3496, 3504, 3511, 3519, 3526, 3534, 3541, 3549, 3556, 3563,
307 3570, 3577, 3584, 3591, 3598, 3605, 3612, 3619, 3625, 3632,
308 3639, 3645, 3652, 3658, 3665, 3671, 3677, 3683, 3690, 3696,
309 3702, 3708, 3714, 3720, 3726, 3732, 3738, 3744, 3750, 3755,
310 3761, 3767, 3772, 3778, 3784, 3789, 3795, 3800, 3806, 3811,
311 3816, 3822, 3827, 3832, 3838, 3843, 3848, 3853, 3858, 3863,
312 3868, 3874, 3879, 3884, 3888, 3893, 3898, 3903, 3908, 3913,
313 3918, 3922, 3927, 3932, 3936, 3941, 3946, 3950, 3955, 3960,
314 3964, 3969, 3973, 3978, 3982, 3986, 3991, 3995, 4000, 4004,
317 static unsigned int denom
[] = {1,1,100,1000,10000,100000,1000000,10000000,100000000};
319 static unsigned int i20Log10(unsigned short val
)
321 unsigned int rntval
= 100;
322 unsigned int tmp
= val
;
323 unsigned int exp
= 1;
325 while(tmp
> 100) {tmp
/= 100; exp
++;}
327 val
= (2 * val
)/denom
[exp
];
328 if (exp
> 1) rntval
= 2000*exp
;
330 rntval
+= i100x20log10
[val
];
334 static int or51211_read_signal_strength(struct dvb_frontend
* fe
, u16
* strength
)
336 struct or51211_state
* state
= fe
->demodulator_priv
;
341 /* SNR after Equalizer */
347 if (i2c_writebytes(state
,state
->config
->demod_address
,snd_buf
,3)) {
348 printk(KERN_WARNING
"or51211: read_status write error\n");
352 if (i2c_readbytes(state
,state
->config
->demod_address
,rec_buf
,2)) {
353 printk(KERN_WARNING
"or51211: read_status read error\n");
356 snr_equ
= rec_buf
[0] & 0xff;
358 /* The value reported back from the frontend will be FFFF=100% 0000=0% */
359 *strength
= (((5334 - i20Log10(snr_equ
))/3+5)*65535)/1000;
361 dprintk("read_signal_strength %i\n",*strength
);
366 static int or51211_read_snr(struct dvb_frontend
* fe
, u16
* snr
)
368 struct or51211_state
* state
= fe
->demodulator_priv
;
372 /* SNR after Equalizer */
378 if (i2c_writebytes(state
,state
->config
->demod_address
,snd_buf
,3)) {
379 printk(KERN_WARNING
"or51211: read_status write error\n");
383 if (i2c_readbytes(state
,state
->config
->demod_address
,rec_buf
,2)) {
384 printk(KERN_WARNING
"or51211: read_status read error\n");
387 *snr
= rec_buf
[0] & 0xff;
389 dprintk("read_snr %i\n",*snr
);
394 static int or51211_read_ber(struct dvb_frontend
* fe
, u32
* ber
)
400 static int or51211_read_ucblocks(struct dvb_frontend
* fe
, u32
* ucblocks
)
406 static int or51211_sleep(struct dvb_frontend
* fe
)
411 static int or51211_init(struct dvb_frontend
* fe
)
413 struct or51211_state
* state
= fe
->demodulator_priv
;
414 const struct or51211_config
* config
= state
->config
;
415 const struct firmware
* fw
;
416 unsigned char get_ver_buf
[] = {0x04,0x00,0x30,0x00,0x00};
417 unsigned char rec_buf
[14];
420 if (!state
->initialized
) {
421 /* Request the firmware, this will block until it uploads */
422 printk(KERN_INFO
"or51211: Waiting for firmware upload "
423 "(%s)...\n", OR51211_DEFAULT_FIRMWARE
);
424 ret
= config
->request_firmware(fe
, &fw
,
425 OR51211_DEFAULT_FIRMWARE
);
426 printk(KERN_INFO
"or51211:Got Hotplug firmware\n");
428 printk(KERN_WARNING
"or51211: No firmware uploaded "
429 "(timeout or file not found?)\n");
433 ret
= or51211_load_firmware(fe
, fw
);
435 printk(KERN_WARNING
"or51211: Writing firmware to "
437 release_firmware(fw
);
440 printk(KERN_INFO
"or51211: Firmware upload complete.\n");
442 /* Set operation mode in Receiver 1 register;
444 * data 0x50h Automatic sets receiver channel conditions
445 * Automatic NTSC rejection filter
446 * Enable MPEG serial data output
448 * High tuner phase noise
449 * normal +/-150kHz Carrier acquisition range
451 if (i2c_writebytes(state
,state
->config
->demod_address
,
453 printk(KERN_WARNING
"or51211: Load DVR Error 5\n");
457 /* Read back ucode version to besure we loaded correctly */
458 /* and are really up and running */
464 if (i2c_writebytes(state
,state
->config
->demod_address
,
466 printk(KERN_WARNING
"or51211: Load DVR Error A\n");
470 if (i2c_readbytes(state
,state
->config
->demod_address
,
472 printk(KERN_WARNING
"or51211: Load DVR Error B\n");
481 if (i2c_writebytes(state
,state
->config
->demod_address
,
483 printk(KERN_WARNING
"or51211: Load DVR Error C\n");
487 if (i2c_readbytes(state
,state
->config
->demod_address
,
489 printk(KERN_WARNING
"or51211: Load DVR Error D\n");
493 for (i
= 0; i
< 8; i
++)
496 for (i
= 0; i
< 5; i
++) {
498 get_ver_buf
[4] = i
+1;
499 if (i2c_writebytes(state
,state
->config
->demod_address
,
501 printk(KERN_WARNING
"or51211:Load DVR Error 6"
507 if (i2c_readbytes(state
,state
->config
->demod_address
,
509 printk(KERN_WARNING
"or51211:Load DVR Error 7"
513 /* If we didn't receive the right index, try again */
514 if ((int)rec_buf
[i
*2+1]!=i
+1){
518 dprintk("read_fwbits %x %x %x %x %x %x %x %x %x %x\n",
519 rec_buf
[0], rec_buf
[1], rec_buf
[2], rec_buf
[3],
520 rec_buf
[4], rec_buf
[5], rec_buf
[6], rec_buf
[7],
521 rec_buf
[8], rec_buf
[9]);
523 printk(KERN_INFO
"or51211: ver TU%02x%02x%02x VSB mode %02x"
525 rec_buf
[2], rec_buf
[4],rec_buf
[6],
526 rec_buf
[12],rec_buf
[10]);
533 if (i2c_writebytes(state
,state
->config
->demod_address
,
535 printk(KERN_WARNING
"or51211: Load DVR Error 8\n");
539 if (i2c_readbytes(state
,state
->config
->demod_address
,
541 printk(KERN_WARNING
"or51211: Load DVR Error 9\n");
544 state
->initialized
= 1;
550 static int or51211_get_tune_settings(struct dvb_frontend
* fe
,
551 struct dvb_frontend_tune_settings
* fesettings
)
553 fesettings
->min_delay_ms
= 500;
554 fesettings
->step_size
= 0;
555 fesettings
->max_drift
= 0;
559 static void or51211_release(struct dvb_frontend
* fe
)
561 struct or51211_state
* state
= fe
->demodulator_priv
;
562 state
->config
->sleep(fe
);
566 static struct dvb_frontend_ops or51211_ops
;
568 struct dvb_frontend
* or51211_attach(const struct or51211_config
* config
,
569 struct i2c_adapter
* i2c
)
571 struct or51211_state
* state
= NULL
;
573 /* Allocate memory for the internal state */
574 state
= kmalloc(sizeof(struct or51211_state
), GFP_KERNEL
);
578 /* Setup the state */
579 state
->config
= config
;
581 memcpy(&state
->ops
, &or51211_ops
, sizeof(struct dvb_frontend_ops
));
582 state
->initialized
= 0;
583 state
->current_frequency
= 0;
585 /* Create dvb_frontend */
586 state
->frontend
.ops
= &state
->ops
;
587 state
->frontend
.demodulator_priv
= state
;
588 return &state
->frontend
;
595 static struct dvb_frontend_ops or51211_ops
= {
598 .name
= "Oren OR51211 VSB Frontend",
600 .frequency_min
= 44000000,
601 .frequency_max
= 958000000,
602 .frequency_stepsize
= 166666,
603 .caps
= FE_CAN_FEC_1_2
| FE_CAN_FEC_2_3
| FE_CAN_FEC_3_4
|
604 FE_CAN_FEC_5_6
| FE_CAN_FEC_7_8
| FE_CAN_FEC_AUTO
|
608 .release
= or51211_release
,
610 .init
= or51211_init
,
611 .sleep
= or51211_sleep
,
613 .set_frontend
= or51211_set_parameters
,
614 .get_tune_settings
= or51211_get_tune_settings
,
616 .read_status
= or51211_read_status
,
617 .read_ber
= or51211_read_ber
,
618 .read_signal_strength
= or51211_read_signal_strength
,
619 .read_snr
= or51211_read_snr
,
620 .read_ucblocks
= or51211_read_ucblocks
,
623 module_param(debug
, int, 0644);
624 MODULE_PARM_DESC(debug
, "Turn on/off frontend debugging (default:off).");
626 MODULE_DESCRIPTION("Oren OR51211 VSB [pcHDTV HD-2000] Demodulator Driver");
627 MODULE_AUTHOR("Kirk Lapray");
628 MODULE_LICENSE("GPL");
630 EXPORT_SYMBOL(or51211_attach
);