Merge rsync://rsync.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6 into for...
[linux-2.6] / drivers / media / dvb / frontends / or51211.c
1 /*
2  *    Support for OR51211 (pcHDTV HD-2000) - VSB
3  *
4  *    Copyright (C) 2005 Kirk Lapray <kirk_lapray@bigfoot.com>
5  *
6  *    Based on code from Jack Kelliher (kelliher@xmission.com)
7  *                           Copyright (C) 2002 & pcHDTV, inc.
8  *
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.
13  *
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.
18  *
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.
22  *
23 */
24
25 /*
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).
30  */
31 #define OR51211_DEFAULT_FIRMWARE "dvb-fe-or51211.fw"
32
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>
41
42 #include "dvb_frontend.h"
43 #include "or51211.h"
44
45 static int debug;
46 #define dprintk(args...) \
47         do { \
48                 if (debug) printk(KERN_DEBUG "or51211: " args); \
49         } while (0)
50
51 static u8 run_buf[] = {0x7f,0x01};
52 static u8 cmd_buf[] = {0x04,0x01,0x50,0x80,0x06}; // ATSC
53
54 struct or51211_state {
55
56         struct i2c_adapter* i2c;
57
58         /* Configuration settings */
59         const struct or51211_config* config;
60
61         struct dvb_frontend frontend;
62         struct bt878* bt;
63
64         /* Demodulator private data */
65         u8 initialized:1;
66
67         /* Tuner private data */
68         u32 current_frequency;
69 };
70
71 static int i2c_writebytes (struct or51211_state* state, u8 reg, u8 *buf,
72                            int len)
73 {
74         int err;
75         struct i2c_msg msg;
76         msg.addr        = reg;
77         msg.flags       = 0;
78         msg.len         = len;
79         msg.buf         = buf;
80
81         if ((err = i2c_transfer (state->i2c, &msg, 1)) != 1) {
82                 printk(KERN_WARNING "or51211: i2c_writebytes error "
83                        "(addr %02x, err == %i)\n", reg, err);
84                 return -EREMOTEIO;
85         }
86
87         return 0;
88 }
89
90 static u8 i2c_readbytes (struct or51211_state* state, u8 reg, u8* buf, int len)
91 {
92         int err;
93         struct i2c_msg msg;
94         msg.addr        = reg;
95         msg.flags       = I2C_M_RD;
96         msg.len         = len;
97         msg.buf         = buf;
98
99         if ((err = i2c_transfer (state->i2c, &msg, 1)) != 1) {
100                 printk(KERN_WARNING "or51211: i2c_readbytes error "
101                        "(addr %02x, err == %i)\n", reg, err);
102                 return -EREMOTEIO;
103         }
104
105         return 0;
106 }
107
108 static int or51211_load_firmware (struct dvb_frontend* fe,
109                                   const struct firmware *fw)
110 {
111         struct or51211_state* state = fe->demodulator_priv;
112         u8 tudata[585];
113         int i;
114
115         dprintk("Firmware is %zd bytes\n",fw->size);
116
117         /* Get eprom data */
118         tudata[0] = 17;
119         if (i2c_writebytes(state,0x50,tudata,1)) {
120                 printk(KERN_WARNING "or51211:load_firmware error eprom addr\n");
121                 return -1;
122         }
123         if (i2c_readbytes(state,0x50,&tudata[145],192)) {
124                 printk(KERN_WARNING "or51211: load_firmware error eprom\n");
125                 return -1;
126         }
127
128         /* Create firmware buffer */
129         for (i = 0; i < 145; i++)
130                 tudata[i] = fw->data[i];
131
132         for (i = 0; i < 248; i++)
133                 tudata[i+337] = fw->data[145+i];
134
135         state->config->reset(fe);
136
137         if (i2c_writebytes(state,state->config->demod_address,tudata,585)) {
138                 printk(KERN_WARNING "or51211: load_firmware error 1\n");
139                 return -1;
140         }
141         msleep(1);
142
143         if (i2c_writebytes(state,state->config->demod_address,
144                            &fw->data[393],8125)) {
145                 printk(KERN_WARNING "or51211: load_firmware error 2\n");
146                 return -1;
147         }
148         msleep(1);
149
150         if (i2c_writebytes(state,state->config->demod_address,run_buf,2)) {
151                 printk(KERN_WARNING "or51211: load_firmware error 3\n");
152                 return -1;
153         }
154
155         /* Wait at least 5 msec */
156         msleep(10);
157         if (i2c_writebytes(state,state->config->demod_address,run_buf,2)) {
158                 printk(KERN_WARNING "or51211: load_firmware error 4\n");
159                 return -1;
160         }
161         msleep(10);
162
163         printk("or51211: Done.\n");
164         return 0;
165 };
166
167 static int or51211_setmode(struct dvb_frontend* fe, int mode)
168 {
169         struct or51211_state* state = fe->demodulator_priv;
170         u8 rec_buf[14];
171
172         state->config->setmode(fe, mode);
173
174         if (i2c_writebytes(state,state->config->demod_address,run_buf,2)) {
175                 printk(KERN_WARNING "or51211: setmode error 1\n");
176                 return -1;
177         }
178
179         /* Wait at least 5 msec */
180         msleep(10);
181         if (i2c_writebytes(state,state->config->demod_address,run_buf,2)) {
182                 printk(KERN_WARNING "or51211: setmode error 2\n");
183                 return -1;
184         }
185
186         msleep(10);
187
188         /* Set operation mode in Receiver 1 register;
189          * type 1:
190          * data 0x50h  Automatic sets receiver channel conditions
191          *             Automatic NTSC rejection filter
192          *             Enable  MPEG serial data output
193          *             MPEG2tr
194          *             High tuner phase noise
195          *             normal +/-150kHz Carrier acquisition range
196          */
197         if (i2c_writebytes(state,state->config->demod_address,cmd_buf,3)) {
198                 printk(KERN_WARNING "or51211: setmode error 3\n");
199                 return -1;
200         }
201
202         rec_buf[0] = 0x04;
203         rec_buf[1] = 0x00;
204         rec_buf[2] = 0x03;
205         rec_buf[3] = 0x00;
206         msleep(20);
207         if (i2c_writebytes(state,state->config->demod_address,rec_buf,3)) {
208                 printk(KERN_WARNING "or51211: setmode error 5\n");
209         }
210         msleep(3);
211         if (i2c_readbytes(state,state->config->demod_address,&rec_buf[10],2)) {
212                 printk(KERN_WARNING "or51211: setmode error 6");
213                 return -1;
214         }
215         dprintk("setmode rec status %02x %02x\n",rec_buf[10],rec_buf[11]);
216
217         return 0;
218 }
219
220 static int or51211_set_parameters(struct dvb_frontend* fe,
221                                   struct dvb_frontend_parameters *param)
222 {
223         struct or51211_state* state = fe->demodulator_priv;
224         u32 freq = 0;
225         u16 tunerfreq = 0;
226         u8 buf[4];
227
228         /* Change only if we are actually changing the channel */
229         if (state->current_frequency != param->frequency) {
230                 freq = 44000 + (param->frequency/1000);
231                 tunerfreq = freq * 16/1000;
232
233                 dprintk("set_parameters frequency = %d (tunerfreq = %d)\n",
234                         param->frequency,tunerfreq);
235
236                 buf[0] = (tunerfreq >> 8) & 0x7F;
237                 buf[1] = (tunerfreq & 0xFF);
238                 buf[2] = 0x8E;
239
240                 if (param->frequency < 157250000) {
241                         buf[3] = 0xA0;
242                         dprintk("set_parameters VHF low range\n");
243                 } else if (param->frequency < 454000000) {
244                         buf[3] = 0x90;
245                         dprintk("set_parameters VHF high range\n");
246                 } else {
247                         buf[3] = 0x30;
248                         dprintk("set_parameters UHF range\n");
249                 }
250                 dprintk("set_parameters tuner bytes: 0x%02x 0x%02x "
251                         "0x%02x 0x%02x\n",buf[0],buf[1],buf[2],buf[3]);
252
253                 if (i2c_writebytes(state,0xC2>>1,buf,4))
254                         printk(KERN_WARNING "or51211:set_parameters error "
255                                "writing to tuner\n");
256
257                 /* Set to ATSC mode */
258                 or51211_setmode(fe,0);
259
260                 /* Update current frequency */
261                 state->current_frequency = param->frequency;
262         }
263         return 0;
264 }
265
266 static int or51211_read_status(struct dvb_frontend* fe, fe_status_t* status)
267 {
268         struct or51211_state* state = fe->demodulator_priv;
269         unsigned char rec_buf[2];
270         unsigned char snd_buf[] = {0x04,0x00,0x03,0x00};
271         *status = 0;
272
273         /* Receiver Status */
274         if (i2c_writebytes(state,state->config->demod_address,snd_buf,3)) {
275                 printk(KERN_WARNING "or51132: read_status write error\n");
276                 return -1;
277         }
278         msleep(3);
279         if (i2c_readbytes(state,state->config->demod_address,rec_buf,2)) {
280                 printk(KERN_WARNING "or51132: read_status read error\n");
281                 return -1;
282         }
283         dprintk("read_status %x %x\n",rec_buf[0],rec_buf[1]);
284
285         if (rec_buf[0] &  0x01) { /* Receiver Lock */
286                 *status |= FE_HAS_SIGNAL;
287                 *status |= FE_HAS_CARRIER;
288                 *status |= FE_HAS_VITERBI;
289                 *status |= FE_HAS_SYNC;
290                 *status |= FE_HAS_LOCK;
291         }
292         return 0;
293 }
294
295 /* log10-1 table at .5 increments from 1 to 100.5 */
296 static unsigned int i100x20log10[] = {
297                 0,  352,  602,  795,  954, 1088, 1204, 1306, 1397, 1480,
298          1556, 1625, 1690, 1750, 1806, 1858, 1908, 1955, 2000, 2042,
299          2082, 2121, 2158, 2193, 2227, 2260, 2292, 2322, 2352, 2380,
300          2408, 2434, 2460, 2486, 2510, 2534, 2557, 2580, 2602, 2623,
301          2644, 2664, 2684, 2704, 2723, 2742, 2760, 2778, 2795, 2813,
302          2829, 2846, 2862, 2878, 2894, 2909, 2924, 2939, 2954, 2968,
303          2982, 2996, 3010, 3023, 3037, 3050, 3062, 3075, 3088, 3100,
304          3112, 3124, 3136, 3148, 3159, 3170, 3182, 3193, 3204, 3214,
305          3225, 3236, 3246, 3256, 3266, 3276, 3286, 3296, 3306, 3316,
306          3325, 3334, 3344, 3353, 3362, 3371, 3380, 3389, 3397, 3406,
307          3415, 3423, 3432, 3440, 3448, 3456, 3464, 3472, 3480, 3488,
308          3496, 3504, 3511, 3519, 3526, 3534, 3541, 3549, 3556, 3563,
309          3570, 3577, 3584, 3591, 3598, 3605, 3612, 3619, 3625, 3632,
310          3639, 3645, 3652, 3658, 3665, 3671, 3677, 3683, 3690, 3696,
311          3702, 3708, 3714, 3720, 3726, 3732, 3738, 3744, 3750, 3755,
312          3761, 3767, 3772, 3778, 3784, 3789, 3795, 3800, 3806, 3811,
313          3816, 3822, 3827, 3832, 3838, 3843, 3848, 3853, 3858, 3863,
314          3868, 3874, 3879, 3884, 3888, 3893, 3898, 3903, 3908, 3913,
315          3918, 3922, 3927, 3932, 3936, 3941, 3946, 3950, 3955, 3960,
316          3964, 3969, 3973, 3978, 3982, 3986, 3991, 3995, 4000, 4004,
317 };
318
319 static unsigned int denom[] = {1,1,100,1000,10000,100000,1000000,10000000,100000000};
320
321 static unsigned int i20Log10(unsigned short val)
322 {
323         unsigned int rntval = 100;
324         unsigned int tmp = val;
325         unsigned int exp = 1;
326
327         while(tmp > 100) {tmp /= 100; exp++;}
328
329         val = (2 * val)/denom[exp];
330         if (exp > 1) rntval = 2000*exp;
331
332         rntval += i100x20log10[val];
333         return rntval;
334 }
335
336 static int or51211_read_signal_strength(struct dvb_frontend* fe, u16* strength)
337 {
338         struct or51211_state* state = fe->demodulator_priv;
339         u8 rec_buf[2];
340         u8 snd_buf[4];
341         u8 snr_equ;
342         u32 signal_strength;
343
344         /* SNR after Equalizer */
345         snd_buf[0] = 0x04;
346         snd_buf[1] = 0x00;
347         snd_buf[2] = 0x04;
348         snd_buf[3] = 0x00;
349
350         if (i2c_writebytes(state,state->config->demod_address,snd_buf,3)) {
351                 printk(KERN_WARNING "or51211: read_status write error\n");
352                 return -1;
353         }
354         msleep(3);
355         if (i2c_readbytes(state,state->config->demod_address,rec_buf,2)) {
356                 printk(KERN_WARNING "or51211: read_status read error\n");
357                 return -1;
358         }
359         snr_equ = rec_buf[0] & 0xff;
360
361         /* The value reported back from the frontend will be FFFF=100% 0000=0% */
362         signal_strength = (((5334 - i20Log10(snr_equ))/3+5)*65535)/1000;
363         if (signal_strength > 0xffff)
364                 *strength = 0xffff;
365         else
366                 *strength = signal_strength;
367         dprintk("read_signal_strength %i\n",*strength);
368
369         return 0;
370 }
371
372 static int or51211_read_snr(struct dvb_frontend* fe, u16* snr)
373 {
374         struct or51211_state* state = fe->demodulator_priv;
375         u8 rec_buf[2];
376         u8 snd_buf[4];
377
378         /* SNR after Equalizer */
379         snd_buf[0] = 0x04;
380         snd_buf[1] = 0x00;
381         snd_buf[2] = 0x04;
382         snd_buf[3] = 0x00;
383
384         if (i2c_writebytes(state,state->config->demod_address,snd_buf,3)) {
385                 printk(KERN_WARNING "or51211: read_status write error\n");
386                 return -1;
387         }
388         msleep(3);
389         if (i2c_readbytes(state,state->config->demod_address,rec_buf,2)) {
390                 printk(KERN_WARNING "or51211: read_status read error\n");
391                 return -1;
392         }
393         *snr = rec_buf[0] & 0xff;
394
395         dprintk("read_snr %i\n",*snr);
396
397         return 0;
398 }
399
400 static int or51211_read_ber(struct dvb_frontend* fe, u32* ber)
401 {
402         *ber = -ENOSYS;
403         return 0;
404 }
405
406 static int or51211_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
407 {
408         *ucblocks = -ENOSYS;
409         return 0;
410 }
411
412 static int or51211_sleep(struct dvb_frontend* fe)
413 {
414         return 0;
415 }
416
417 static int or51211_init(struct dvb_frontend* fe)
418 {
419         struct or51211_state* state = fe->demodulator_priv;
420         const struct or51211_config* config = state->config;
421         const struct firmware* fw;
422         unsigned char get_ver_buf[] = {0x04,0x00,0x30,0x00,0x00};
423         unsigned char rec_buf[14];
424         int ret,i;
425
426         if (!state->initialized) {
427                 /* Request the firmware, this will block until it uploads */
428                 printk(KERN_INFO "or51211: Waiting for firmware upload "
429                        "(%s)...\n", OR51211_DEFAULT_FIRMWARE);
430                 ret = config->request_firmware(fe, &fw,
431                                                OR51211_DEFAULT_FIRMWARE);
432                 printk(KERN_INFO "or51211:Got Hotplug firmware\n");
433                 if (ret) {
434                         printk(KERN_WARNING "or51211: No firmware uploaded "
435                                "(timeout or file not found?)\n");
436                         return ret;
437                 }
438
439                 ret = or51211_load_firmware(fe, fw);
440                 release_firmware(fw);
441                 if (ret) {
442                         printk(KERN_WARNING "or51211: Writing firmware to "
443                                "device failed!\n");
444                         return ret;
445                 }
446                 printk(KERN_INFO "or51211: Firmware upload complete.\n");
447
448                 /* Set operation mode in Receiver 1 register;
449                  * type 1:
450                  * data 0x50h  Automatic sets receiver channel conditions
451                  *             Automatic NTSC rejection filter
452                  *             Enable  MPEG serial data output
453                  *             MPEG2tr
454                  *             High tuner phase noise
455                  *             normal +/-150kHz Carrier acquisition range
456                  */
457                 if (i2c_writebytes(state,state->config->demod_address,
458                                    cmd_buf,3)) {
459                         printk(KERN_WARNING "or51211: Load DVR Error 5\n");
460                         return -1;
461                 }
462
463                 /* Read back ucode version to besure we loaded correctly */
464                 /* and are really up and running */
465                 rec_buf[0] = 0x04;
466                 rec_buf[1] = 0x00;
467                 rec_buf[2] = 0x03;
468                 rec_buf[3] = 0x00;
469                 msleep(30);
470                 if (i2c_writebytes(state,state->config->demod_address,
471                                    rec_buf,3)) {
472                         printk(KERN_WARNING "or51211: Load DVR Error A\n");
473                         return -1;
474                 }
475                 msleep(3);
476                 if (i2c_readbytes(state,state->config->demod_address,
477                                   &rec_buf[10],2)) {
478                         printk(KERN_WARNING "or51211: Load DVR Error B\n");
479                         return -1;
480                 }
481
482                 rec_buf[0] = 0x04;
483                 rec_buf[1] = 0x00;
484                 rec_buf[2] = 0x01;
485                 rec_buf[3] = 0x00;
486                 msleep(20);
487                 if (i2c_writebytes(state,state->config->demod_address,
488                                    rec_buf,3)) {
489                         printk(KERN_WARNING "or51211: Load DVR Error C\n");
490                         return -1;
491                 }
492                 msleep(3);
493                 if (i2c_readbytes(state,state->config->demod_address,
494                                   &rec_buf[12],2)) {
495                         printk(KERN_WARNING "or51211: Load DVR Error D\n");
496                         return -1;
497                 }
498
499                 for (i = 0; i < 8; i++)
500                         rec_buf[i]=0xed;
501
502                 for (i = 0; i < 5; i++) {
503                         msleep(30);
504                         get_ver_buf[4] = i+1;
505                         if (i2c_writebytes(state,state->config->demod_address,
506                                            get_ver_buf,5)) {
507                                 printk(KERN_WARNING "or51211:Load DVR Error 6"
508                                        " - %d\n",i);
509                                 return -1;
510                         }
511                         msleep(3);
512
513                         if (i2c_readbytes(state,state->config->demod_address,
514                                           &rec_buf[i*2],2)) {
515                                 printk(KERN_WARNING "or51211:Load DVR Error 7"
516                                        " - %d\n",i);
517                                 return -1;
518                         }
519                         /* If we didn't receive the right index, try again */
520                         if ((int)rec_buf[i*2+1]!=i+1){
521                           i--;
522                         }
523                 }
524                 dprintk("read_fwbits %x %x %x %x %x %x %x %x %x %x\n",
525                         rec_buf[0], rec_buf[1], rec_buf[2], rec_buf[3],
526                         rec_buf[4], rec_buf[5], rec_buf[6], rec_buf[7],
527                         rec_buf[8], rec_buf[9]);
528
529                 printk(KERN_INFO "or51211: ver TU%02x%02x%02x VSB mode %02x"
530                        " Status %02x\n",
531                        rec_buf[2], rec_buf[4],rec_buf[6],
532                        rec_buf[12],rec_buf[10]);
533
534                 rec_buf[0] = 0x04;
535                 rec_buf[1] = 0x00;
536                 rec_buf[2] = 0x03;
537                 rec_buf[3] = 0x00;
538                 msleep(20);
539                 if (i2c_writebytes(state,state->config->demod_address,
540                                    rec_buf,3)) {
541                         printk(KERN_WARNING "or51211: Load DVR Error 8\n");
542                         return -1;
543                 }
544                 msleep(20);
545                 if (i2c_readbytes(state,state->config->demod_address,
546                                   &rec_buf[8],2)) {
547                         printk(KERN_WARNING "or51211: Load DVR Error 9\n");
548                         return -1;
549                 }
550                 state->initialized = 1;
551         }
552
553         return 0;
554 }
555
556 static int or51211_get_tune_settings(struct dvb_frontend* fe,
557                                      struct dvb_frontend_tune_settings* fesettings)
558 {
559         fesettings->min_delay_ms = 500;
560         fesettings->step_size = 0;
561         fesettings->max_drift = 0;
562         return 0;
563 }
564
565 static void or51211_release(struct dvb_frontend* fe)
566 {
567         struct or51211_state* state = fe->demodulator_priv;
568         state->config->sleep(fe);
569         kfree(state);
570 }
571
572 static struct dvb_frontend_ops or51211_ops;
573
574 struct dvb_frontend* or51211_attach(const struct or51211_config* config,
575                                     struct i2c_adapter* i2c)
576 {
577         struct or51211_state* state = NULL;
578
579         /* Allocate memory for the internal state */
580         state = kmalloc(sizeof(struct or51211_state), GFP_KERNEL);
581         if (state == NULL)
582                 goto error;
583
584         /* Setup the state */
585         state->config = config;
586         state->i2c = i2c;
587         state->initialized = 0;
588         state->current_frequency = 0;
589
590         /* Create dvb_frontend */
591         memcpy(&state->frontend.ops, &or51211_ops, sizeof(struct dvb_frontend_ops));
592         state->frontend.demodulator_priv = state;
593         return &state->frontend;
594
595 error:
596         kfree(state);
597         return NULL;
598 }
599
600 static struct dvb_frontend_ops or51211_ops = {
601
602         .info = {
603                 .name               = "Oren OR51211 VSB Frontend",
604                 .type               = FE_ATSC,
605                 .frequency_min      = 44000000,
606                 .frequency_max      = 958000000,
607                 .frequency_stepsize = 166666,
608                 .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
609                         FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
610                         FE_CAN_8VSB
611         },
612
613         .release = or51211_release,
614
615         .init = or51211_init,
616         .sleep = or51211_sleep,
617
618         .set_frontend = or51211_set_parameters,
619         .get_tune_settings = or51211_get_tune_settings,
620
621         .read_status = or51211_read_status,
622         .read_ber = or51211_read_ber,
623         .read_signal_strength = or51211_read_signal_strength,
624         .read_snr = or51211_read_snr,
625         .read_ucblocks = or51211_read_ucblocks,
626 };
627
628 module_param(debug, int, 0644);
629 MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
630
631 MODULE_DESCRIPTION("Oren OR51211 VSB [pcHDTV HD-2000] Demodulator Driver");
632 MODULE_AUTHOR("Kirk Lapray");
633 MODULE_LICENSE("GPL");
634
635 EXPORT_SYMBOL(or51211_attach);
636