[ARM] Support register switch in nommu mode
[linux-2.6/verdex.git] / drivers / media / dvb / frontends / or51211.c
blob7c3aed1f546be2609631206639271fbd6f9b178b
1 /*
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"
43 #include "or51211.h"
45 static int debug;
46 #define dprintk(args...) \
47 do { \
48 if (debug) printk(KERN_DEBUG "or51211: " args); \
49 } while (0)
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;
63 struct bt878* bt;
65 /* Demodulator private data */
66 u8 initialized:1;
68 /* Tuner private data */
69 u32 current_frequency;
72 static int i2c_writebytes (struct or51211_state* state, u8 reg, u8 *buf,
73 int len)
75 int err;
76 struct i2c_msg msg;
77 msg.addr = reg;
78 msg.flags = 0;
79 msg.len = len;
80 msg.buf = 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);
85 return -EREMOTEIO;
88 return 0;
91 static u8 i2c_readbytes (struct or51211_state* state, u8 reg, u8* buf, int len)
93 int err;
94 struct i2c_msg msg;
95 msg.addr = reg;
96 msg.flags = I2C_M_RD;
97 msg.len = len;
98 msg.buf = buf;
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);
103 return -EREMOTEIO;
106 return 0;
109 static int or51211_load_firmware (struct dvb_frontend* fe,
110 const struct firmware *fw)
112 struct or51211_state* state = fe->demodulator_priv;
113 u8 tudata[585];
114 int i;
116 dprintk("Firmware is %zd bytes\n",fw->size);
118 /* Get eprom data */
119 tudata[0] = 17;
120 if (i2c_writebytes(state,0x50,tudata,1)) {
121 printk(KERN_WARNING "or51211:load_firmware error eprom addr\n");
122 return -1;
124 if (i2c_readbytes(state,0x50,&tudata[145],192)) {
125 printk(KERN_WARNING "or51211: load_firmware error eprom\n");
126 return -1;
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");
140 return -1;
142 msleep(1);
144 if (i2c_writebytes(state,state->config->demod_address,
145 &fw->data[393],8125)) {
146 printk(KERN_WARNING "or51211: load_firmware error 2\n");
147 return -1;
149 msleep(1);
151 if (i2c_writebytes(state,state->config->demod_address,run_buf,2)) {
152 printk(KERN_WARNING "or51211: load_firmware error 3\n");
153 return -1;
156 /* Wait at least 5 msec */
157 msleep(10);
158 if (i2c_writebytes(state,state->config->demod_address,run_buf,2)) {
159 printk(KERN_WARNING "or51211: load_firmware error 4\n");
160 return -1;
162 msleep(10);
164 printk("or51211: Done.\n");
165 return 0;
168 static int or51211_setmode(struct dvb_frontend* fe, int mode)
170 struct or51211_state* state = fe->demodulator_priv;
171 u8 rec_buf[14];
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");
177 return -1;
180 /* Wait at least 5 msec */
181 msleep(10);
182 if (i2c_writebytes(state,state->config->demod_address,run_buf,2)) {
183 printk(KERN_WARNING "or51211: setmode error 2\n");
184 return -1;
187 msleep(10);
189 /* Set operation mode in Receiver 1 register;
190 * type 1:
191 * data 0x50h Automatic sets receiver channel conditions
192 * Automatic NTSC rejection filter
193 * Enable MPEG serial data output
194 * MPEG2tr
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");
200 return -1;
203 rec_buf[0] = 0x04;
204 rec_buf[1] = 0x00;
205 rec_buf[2] = 0x03;
206 rec_buf[3] = 0x00;
207 msleep(20);
208 if (i2c_writebytes(state,state->config->demod_address,rec_buf,3)) {
209 printk(KERN_WARNING "or51211: setmode error 5\n");
211 msleep(3);
212 if (i2c_readbytes(state,state->config->demod_address,&rec_buf[10],2)) {
213 printk(KERN_WARNING "or51211: setmode error 6");
214 return -1;
216 dprintk("setmode rec status %02x %02x\n",rec_buf[10],rec_buf[11]);
218 return 0;
221 static int or51211_set_parameters(struct dvb_frontend* fe,
222 struct dvb_frontend_parameters *param)
224 struct or51211_state* state = fe->demodulator_priv;
225 u32 freq = 0;
226 u16 tunerfreq = 0;
227 u8 buf[4];
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);
239 buf[2] = 0x8E;
241 if (param->frequency < 157250000) {
242 buf[3] = 0xA0;
243 dprintk("set_parameters VHF low range\n");
244 } else if (param->frequency < 454000000) {
245 buf[3] = 0x90;
246 dprintk("set_parameters VHF high range\n");
247 } else {
248 buf[3] = 0x30;
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;
264 return 0;
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};
272 *status = 0;
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");
277 return -1;
279 msleep(3);
280 if (i2c_readbytes(state,state->config->demod_address,rec_buf,2)) {
281 printk(KERN_WARNING "or51132: read_status read error\n");
282 return -1;
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;
293 return 0;
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];
334 return rntval;
337 static int or51211_read_signal_strength(struct dvb_frontend* fe, u16* strength)
339 struct or51211_state* state = fe->demodulator_priv;
340 u8 rec_buf[2];
341 u8 snd_buf[4];
342 u8 snr_equ;
343 u32 signal_strength;
345 /* SNR after Equalizer */
346 snd_buf[0] = 0x04;
347 snd_buf[1] = 0x00;
348 snd_buf[2] = 0x04;
349 snd_buf[3] = 0x00;
351 if (i2c_writebytes(state,state->config->demod_address,snd_buf,3)) {
352 printk(KERN_WARNING "or51211: read_status write error\n");
353 return -1;
355 msleep(3);
356 if (i2c_readbytes(state,state->config->demod_address,rec_buf,2)) {
357 printk(KERN_WARNING "or51211: read_status read error\n");
358 return -1;
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)
365 *strength = 0xffff;
366 else
367 *strength = signal_strength;
368 dprintk("read_signal_strength %i\n",*strength);
370 return 0;
373 static int or51211_read_snr(struct dvb_frontend* fe, u16* snr)
375 struct or51211_state* state = fe->demodulator_priv;
376 u8 rec_buf[2];
377 u8 snd_buf[4];
379 /* SNR after Equalizer */
380 snd_buf[0] = 0x04;
381 snd_buf[1] = 0x00;
382 snd_buf[2] = 0x04;
383 snd_buf[3] = 0x00;
385 if (i2c_writebytes(state,state->config->demod_address,snd_buf,3)) {
386 printk(KERN_WARNING "or51211: read_status write error\n");
387 return -1;
389 msleep(3);
390 if (i2c_readbytes(state,state->config->demod_address,rec_buf,2)) {
391 printk(KERN_WARNING "or51211: read_status read error\n");
392 return -1;
394 *snr = rec_buf[0] & 0xff;
396 dprintk("read_snr %i\n",*snr);
398 return 0;
401 static int or51211_read_ber(struct dvb_frontend* fe, u32* ber)
403 *ber = -ENOSYS;
404 return 0;
407 static int or51211_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
409 *ucblocks = -ENOSYS;
410 return 0;
413 static int or51211_sleep(struct dvb_frontend* fe)
415 return 0;
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];
425 int ret,i;
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");
434 if (ret) {
435 printk(KERN_WARNING "or51211: No firmware uploaded "
436 "(timeout or file not found?)\n");
437 return ret;
440 ret = or51211_load_firmware(fe, fw);
441 if (ret) {
442 printk(KERN_WARNING "or51211: Writing firmware to "
443 "device failed!\n");
444 release_firmware(fw);
445 return ret;
447 printk(KERN_INFO "or51211: Firmware upload complete.\n");
449 /* Set operation mode in Receiver 1 register;
450 * type 1:
451 * data 0x50h Automatic sets receiver channel conditions
452 * Automatic NTSC rejection filter
453 * Enable MPEG serial data output
454 * MPEG2tr
455 * High tuner phase noise
456 * normal +/-150kHz Carrier acquisition range
458 if (i2c_writebytes(state,state->config->demod_address,
459 cmd_buf,3)) {
460 printk(KERN_WARNING "or51211: Load DVR Error 5\n");
461 return -1;
464 /* Read back ucode version to besure we loaded correctly */
465 /* and are really up and running */
466 rec_buf[0] = 0x04;
467 rec_buf[1] = 0x00;
468 rec_buf[2] = 0x03;
469 rec_buf[3] = 0x00;
470 msleep(30);
471 if (i2c_writebytes(state,state->config->demod_address,
472 rec_buf,3)) {
473 printk(KERN_WARNING "or51211: Load DVR Error A\n");
474 return -1;
476 msleep(3);
477 if (i2c_readbytes(state,state->config->demod_address,
478 &rec_buf[10],2)) {
479 printk(KERN_WARNING "or51211: Load DVR Error B\n");
480 return -1;
483 rec_buf[0] = 0x04;
484 rec_buf[1] = 0x00;
485 rec_buf[2] = 0x01;
486 rec_buf[3] = 0x00;
487 msleep(20);
488 if (i2c_writebytes(state,state->config->demod_address,
489 rec_buf,3)) {
490 printk(KERN_WARNING "or51211: Load DVR Error C\n");
491 return -1;
493 msleep(3);
494 if (i2c_readbytes(state,state->config->demod_address,
495 &rec_buf[12],2)) {
496 printk(KERN_WARNING "or51211: Load DVR Error D\n");
497 return -1;
500 for (i = 0; i < 8; i++)
501 rec_buf[i]=0xed;
503 for (i = 0; i < 5; i++) {
504 msleep(30);
505 get_ver_buf[4] = i+1;
506 if (i2c_writebytes(state,state->config->demod_address,
507 get_ver_buf,5)) {
508 printk(KERN_WARNING "or51211:Load DVR Error 6"
509 " - %d\n",i);
510 return -1;
512 msleep(3);
514 if (i2c_readbytes(state,state->config->demod_address,
515 &rec_buf[i*2],2)) {
516 printk(KERN_WARNING "or51211:Load DVR Error 7"
517 " - %d\n",i);
518 return -1;
520 /* If we didn't receive the right index, try again */
521 if ((int)rec_buf[i*2+1]!=i+1){
522 i--;
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"
531 " Status %02x\n",
532 rec_buf[2], rec_buf[4],rec_buf[6],
533 rec_buf[12],rec_buf[10]);
535 rec_buf[0] = 0x04;
536 rec_buf[1] = 0x00;
537 rec_buf[2] = 0x03;
538 rec_buf[3] = 0x00;
539 msleep(20);
540 if (i2c_writebytes(state,state->config->demod_address,
541 rec_buf,3)) {
542 printk(KERN_WARNING "or51211: Load DVR Error 8\n");
543 return -1;
545 msleep(20);
546 if (i2c_readbytes(state,state->config->demod_address,
547 &rec_buf[8],2)) {
548 printk(KERN_WARNING "or51211: Load DVR Error 9\n");
549 return -1;
551 state->initialized = 1;
554 return 0;
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;
563 return 0;
566 static void or51211_release(struct dvb_frontend* fe)
568 struct or51211_state* state = fe->demodulator_priv;
569 state->config->sleep(fe);
570 kfree(state);
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);
582 if (state == NULL)
583 goto error;
585 /* Setup the state */
586 state->config = config;
587 state->i2c = i2c;
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;
597 error:
598 kfree(state);
599 return NULL;
602 static struct dvb_frontend_ops or51211_ops = {
604 .info = {
605 .name = "Oren OR51211 VSB Frontend",
606 .type = FE_ATSC,
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 |
612 FE_CAN_8VSB
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);