Merge master.kernel.org:/home/rmk/linux-2.6-mmc
[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         struct dvb_frontend_ops ops;
58
59         /* Configuration settings */
60         const struct or51211_config* config;
61
62         struct dvb_frontend frontend;
63         struct bt878* bt;
64
65         /* Demodulator private data */
66         u8 initialized:1;
67
68         /* Tuner private data */
69         u32 current_frequency;
70 };
71
72 static int i2c_writebytes (struct or51211_state* state, u8 reg, u8 *buf,
73                            int len)
74 {
75         int err;
76         struct i2c_msg msg;
77         msg.addr        = reg;
78         msg.flags       = 0;
79         msg.len         = len;
80         msg.buf         = buf;
81
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;
86         }
87
88         return 0;
89 }
90
91 static u8 i2c_readbytes (struct or51211_state* state, u8 reg, u8* buf, int len)
92 {
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;
99
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;
104         }
105
106         return 0;
107 }
108
109 static int or51211_load_firmware (struct dvb_frontend* fe,
110                                   const struct firmware *fw)
111 {
112         struct or51211_state* state = fe->demodulator_priv;
113         u8 tudata[585];
114         int i;
115
116         dprintk("Firmware is %zd bytes\n",fw->size);
117
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;
123         }
124         if (i2c_readbytes(state,0x50,&tudata[145],192)) {
125                 printk(KERN_WARNING "or51211: load_firmware error eprom\n");
126                 return -1;
127         }
128
129         /* Create firmware buffer */
130         for (i = 0; i < 145; i++)
131                 tudata[i] = fw->data[i];
132
133         for (i = 0; i < 248; i++)
134                 tudata[i+337] = fw->data[145+i];
135
136         state->config->reset(fe);
137
138         if (i2c_writebytes(state,state->config->demod_address,tudata,585)) {
139                 printk(KERN_WARNING "or51211: load_firmware error 1\n");
140                 return -1;
141         }
142         msleep(1);
143
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;
148         }
149         msleep(1);
150
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;
154         }
155
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;
161         }
162         msleep(10);
163
164         printk("or51211: Done.\n");
165         return 0;
166 };
167
168 static int or51211_setmode(struct dvb_frontend* fe, int mode)
169 {
170         struct or51211_state* state = fe->demodulator_priv;
171         u8 rec_buf[14];
172
173         state->config->setmode(fe, mode);
174
175         if (i2c_writebytes(state,state->config->demod_address,run_buf,2)) {
176                 printk(KERN_WARNING "or51211: setmode error 1\n");
177                 return -1;
178         }
179
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;
185         }
186
187         msleep(10);
188
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
197          */
198         if (i2c_writebytes(state,state->config->demod_address,cmd_buf,3)) {
199                 printk(KERN_WARNING "or51211: setmode error 3\n");
200                 return -1;
201         }
202
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");
210         }
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;
215         }
216         dprintk("setmode rec status %02x %02x\n",rec_buf[10],rec_buf[11]);
217
218         return 0;
219 }
220
221 static int or51211_set_parameters(struct dvb_frontend* fe,
222                                   struct dvb_frontend_parameters *param)
223 {
224         struct or51211_state* state = fe->demodulator_priv;
225         u32 freq = 0;
226         u16 tunerfreq = 0;
227         u8 buf[4];
228
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;
233
234                 dprintk("set_parameters frequency = %d (tunerfreq = %d)\n",
235                         param->frequency,tunerfreq);
236
237                 buf[0] = (tunerfreq >> 8) & 0x7F;
238                 buf[1] = (tunerfreq & 0xFF);
239                 buf[2] = 0x8E;
240
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");
250                 }
251                 dprintk("set_parameters tuner bytes: 0x%02x 0x%02x "
252                         "0x%02x 0x%02x\n",buf[0],buf[1],buf[2],buf[3]);
253
254                 if (i2c_writebytes(state,0xC2>>1,buf,4))
255                         printk(KERN_WARNING "or51211:set_parameters error "
256                                "writing to tuner\n");
257
258                 /* Set to ATSC mode */
259                 or51211_setmode(fe,0);
260
261                 /* Update current frequency */
262                 state->current_frequency = param->frequency;
263         }
264         return 0;
265 }
266
267 static int or51211_read_status(struct dvb_frontend* fe, fe_status_t* status)
268 {
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;
273
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;
278         }
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;
283         }
284         dprintk("read_status %x %x\n",rec_buf[0],rec_buf[1]);
285
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;
292         }
293         return 0;
294 }
295
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,
318 };
319
320 static unsigned int denom[] = {1,1,100,1000,10000,100000,1000000,10000000,100000000};
321
322 static unsigned int i20Log10(unsigned short val)
323 {
324         unsigned int rntval = 100;
325         unsigned int tmp = val;
326         unsigned int exp = 1;
327
328         while(tmp > 100) {tmp /= 100; exp++;}
329
330         val = (2 * val)/denom[exp];
331         if (exp > 1) rntval = 2000*exp;
332
333         rntval += i100x20log10[val];
334         return rntval;
335 }
336
337 static int or51211_read_signal_strength(struct dvb_frontend* fe, u16* strength)
338 {
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;
344
345         /* SNR after Equalizer */
346         snd_buf[0] = 0x04;
347         snd_buf[1] = 0x00;
348         snd_buf[2] = 0x04;
349         snd_buf[3] = 0x00;
350
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;
354         }
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;
359         }
360         snr_equ = rec_buf[0] & 0xff;
361
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);
369
370         return 0;
371 }
372
373 static int or51211_read_snr(struct dvb_frontend* fe, u16* snr)
374 {
375         struct or51211_state* state = fe->demodulator_priv;
376         u8 rec_buf[2];
377         u8 snd_buf[4];
378
379         /* SNR after Equalizer */
380         snd_buf[0] = 0x04;
381         snd_buf[1] = 0x00;
382         snd_buf[2] = 0x04;
383         snd_buf[3] = 0x00;
384
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;
388         }
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;
393         }
394         *snr = rec_buf[0] & 0xff;
395
396         dprintk("read_snr %i\n",*snr);
397
398         return 0;
399 }
400
401 static int or51211_read_ber(struct dvb_frontend* fe, u32* ber)
402 {
403         *ber = -ENOSYS;
404         return 0;
405 }
406
407 static int or51211_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
408 {
409         *ucblocks = -ENOSYS;
410         return 0;
411 }
412
413 static int or51211_sleep(struct dvb_frontend* fe)
414 {
415         return 0;
416 }
417
418 static int or51211_init(struct dvb_frontend* fe)
419 {
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;
426
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;
438                 }
439
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;
446                 }
447                 printk(KERN_INFO "or51211: Firmware upload complete.\n");
448
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
457                  */
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;
462                 }
463
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;
475                 }
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;
481                 }
482
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;
492                 }
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;
498                 }
499
500                 for (i = 0; i < 8; i++)
501                         rec_buf[i]=0xed;
502
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;
511                         }
512                         msleep(3);
513
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;
519                         }
520                         /* If we didn't receive the right index, try again */
521                         if ((int)rec_buf[i*2+1]!=i+1){
522                           i--;
523                         }
524                 }
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]);
529
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]);
534
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;
544                 }
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;
550                 }
551                 state->initialized = 1;
552         }
553
554         return 0;
555 }
556
557 static int or51211_get_tune_settings(struct dvb_frontend* fe,
558                                      struct dvb_frontend_tune_settings* fesettings)
559 {
560         fesettings->min_delay_ms = 500;
561         fesettings->step_size = 0;
562         fesettings->max_drift = 0;
563         return 0;
564 }
565
566 static void or51211_release(struct dvb_frontend* fe)
567 {
568         struct or51211_state* state = fe->demodulator_priv;
569         state->config->sleep(fe);
570         kfree(state);
571 }
572
573 static struct dvb_frontend_ops or51211_ops;
574
575 struct dvb_frontend* or51211_attach(const struct or51211_config* config,
576                                     struct i2c_adapter* i2c)
577 {
578         struct or51211_state* state = NULL;
579
580         /* Allocate memory for the internal state */
581         state = kmalloc(sizeof(struct or51211_state), GFP_KERNEL);
582         if (state == NULL)
583                 goto error;
584
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;
591
592         /* Create dvb_frontend */
593         state->frontend.ops = &state->ops;
594         state->frontend.demodulator_priv = state;
595         return &state->frontend;
596
597 error:
598         kfree(state);
599         return NULL;
600 }
601
602 static struct dvb_frontend_ops or51211_ops = {
603
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
613         },
614
615         .release = or51211_release,
616
617         .init = or51211_init,
618         .sleep = or51211_sleep,
619
620         .set_frontend = or51211_set_parameters,
621         .get_tune_settings = or51211_get_tune_settings,
622
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,
628 };
629
630 module_param(debug, int, 0644);
631 MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
632
633 MODULE_DESCRIPTION("Oren OR51211 VSB [pcHDTV HD-2000] Demodulator Driver");
634 MODULE_AUTHOR("Kirk Lapray");
635 MODULE_LICENSE("GPL");
636
637 EXPORT_SYMBOL(or51211_attach);
638