V4L/DVB (5457): Dvb-pll: Replace sleep function with a more capable one
[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_math.h"
43 #include "dvb_frontend.h"
44 #include "or51211.h"
45
46 static int debug;
47 #define dprintk(args...) \
48         do { \
49                 if (debug) printk(KERN_DEBUG "or51211: " args); \
50         } while (0)
51
52 static u8 run_buf[] = {0x7f,0x01};
53 static u8 cmd_buf[] = {0x04,0x01,0x50,0x80,0x06}; // ATSC
54
55 struct or51211_state {
56
57         struct i2c_adapter* i2c;
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         u32 snr; /* Result of last SNR claculation */
68
69         /* Tuner private data */
70         u32 current_frequency;
71 };
72
73 static int i2c_writebytes (struct or51211_state* state, u8 reg, u8 *buf,
74                            int len)
75 {
76         int err;
77         struct i2c_msg msg;
78         msg.addr        = reg;
79         msg.flags       = 0;
80         msg.len         = len;
81         msg.buf         = buf;
82
83         if ((err = i2c_transfer (state->i2c, &msg, 1)) != 1) {
84                 printk(KERN_WARNING "or51211: i2c_writebytes error "
85                        "(addr %02x, err == %i)\n", reg, err);
86                 return -EREMOTEIO;
87         }
88
89         return 0;
90 }
91
92 static u8 i2c_readbytes (struct or51211_state* state, u8 reg, u8* buf, int len)
93 {
94         int err;
95         struct i2c_msg msg;
96         msg.addr        = reg;
97         msg.flags       = I2C_M_RD;
98         msg.len         = len;
99         msg.buf         = buf;
100
101         if ((err = i2c_transfer (state->i2c, &msg, 1)) != 1) {
102                 printk(KERN_WARNING "or51211: i2c_readbytes error "
103                        "(addr %02x, err == %i)\n", reg, err);
104                 return -EREMOTEIO;
105         }
106
107         return 0;
108 }
109
110 static int or51211_load_firmware (struct dvb_frontend* fe,
111                                   const struct firmware *fw)
112 {
113         struct or51211_state* state = fe->demodulator_priv;
114         u8 tudata[585];
115         int i;
116
117         dprintk("Firmware is %zd bytes\n",fw->size);
118
119         /* Get eprom data */
120         tudata[0] = 17;
121         if (i2c_writebytes(state,0x50,tudata,1)) {
122                 printk(KERN_WARNING "or51211:load_firmware error eprom addr\n");
123                 return -1;
124         }
125         if (i2c_readbytes(state,0x50,&tudata[145],192)) {
126                 printk(KERN_WARNING "or51211: load_firmware error eprom\n");
127                 return -1;
128         }
129
130         /* Create firmware buffer */
131         for (i = 0; i < 145; i++)
132                 tudata[i] = fw->data[i];
133
134         for (i = 0; i < 248; i++)
135                 tudata[i+337] = fw->data[145+i];
136
137         state->config->reset(fe);
138
139         if (i2c_writebytes(state,state->config->demod_address,tudata,585)) {
140                 printk(KERN_WARNING "or51211: load_firmware error 1\n");
141                 return -1;
142         }
143         msleep(1);
144
145         if (i2c_writebytes(state,state->config->demod_address,
146                            &fw->data[393],8125)) {
147                 printk(KERN_WARNING "or51211: load_firmware error 2\n");
148                 return -1;
149         }
150         msleep(1);
151
152         if (i2c_writebytes(state,state->config->demod_address,run_buf,2)) {
153                 printk(KERN_WARNING "or51211: load_firmware error 3\n");
154                 return -1;
155         }
156
157         /* Wait at least 5 msec */
158         msleep(10);
159         if (i2c_writebytes(state,state->config->demod_address,run_buf,2)) {
160                 printk(KERN_WARNING "or51211: load_firmware error 4\n");
161                 return -1;
162         }
163         msleep(10);
164
165         printk("or51211: Done.\n");
166         return 0;
167 };
168
169 static int or51211_setmode(struct dvb_frontend* fe, int mode)
170 {
171         struct or51211_state* state = fe->demodulator_priv;
172         u8 rec_buf[14];
173
174         state->config->setmode(fe, mode);
175
176         if (i2c_writebytes(state,state->config->demod_address,run_buf,2)) {
177                 printk(KERN_WARNING "or51211: setmode error 1\n");
178                 return -1;
179         }
180
181         /* Wait at least 5 msec */
182         msleep(10);
183         if (i2c_writebytes(state,state->config->demod_address,run_buf,2)) {
184                 printk(KERN_WARNING "or51211: setmode error 2\n");
185                 return -1;
186         }
187
188         msleep(10);
189
190         /* Set operation mode in Receiver 1 register;
191          * type 1:
192          * data 0x50h  Automatic sets receiver channel conditions
193          *             Automatic NTSC rejection filter
194          *             Enable  MPEG serial data output
195          *             MPEG2tr
196          *             High tuner phase noise
197          *             normal +/-150kHz Carrier acquisition range
198          */
199         if (i2c_writebytes(state,state->config->demod_address,cmd_buf,3)) {
200                 printk(KERN_WARNING "or51211: setmode error 3\n");
201                 return -1;
202         }
203
204         rec_buf[0] = 0x04;
205         rec_buf[1] = 0x00;
206         rec_buf[2] = 0x03;
207         rec_buf[3] = 0x00;
208         msleep(20);
209         if (i2c_writebytes(state,state->config->demod_address,rec_buf,3)) {
210                 printk(KERN_WARNING "or51211: setmode error 5\n");
211         }
212         msleep(3);
213         if (i2c_readbytes(state,state->config->demod_address,&rec_buf[10],2)) {
214                 printk(KERN_WARNING "or51211: setmode error 6");
215                 return -1;
216         }
217         dprintk("setmode rec status %02x %02x\n",rec_buf[10],rec_buf[11]);
218
219         return 0;
220 }
221
222 static int or51211_set_parameters(struct dvb_frontend* fe,
223                                   struct dvb_frontend_parameters *param)
224 {
225         struct or51211_state* state = fe->demodulator_priv;
226         u32 freq = 0;
227         u16 tunerfreq = 0;
228         u8 buf[4];
229
230         /* Change only if we are actually changing the channel */
231         if (state->current_frequency != param->frequency) {
232                 freq = 44000 + (param->frequency/1000);
233                 tunerfreq = freq * 16/1000;
234
235                 dprintk("set_parameters frequency = %d (tunerfreq = %d)\n",
236                         param->frequency,tunerfreq);
237
238                 buf[0] = (tunerfreq >> 8) & 0x7F;
239                 buf[1] = (tunerfreq & 0xFF);
240                 buf[2] = 0x8E;
241
242                 if (param->frequency < 157250000) {
243                         buf[3] = 0xA0;
244                         dprintk("set_parameters VHF low range\n");
245                 } else if (param->frequency < 454000000) {
246                         buf[3] = 0x90;
247                         dprintk("set_parameters VHF high range\n");
248                 } else {
249                         buf[3] = 0x30;
250                         dprintk("set_parameters UHF range\n");
251                 }
252                 dprintk("set_parameters tuner bytes: 0x%02x 0x%02x "
253                         "0x%02x 0x%02x\n",buf[0],buf[1],buf[2],buf[3]);
254
255                 if (i2c_writebytes(state,0xC2>>1,buf,4))
256                         printk(KERN_WARNING "or51211:set_parameters error "
257                                "writing to tuner\n");
258
259                 /* Set to ATSC mode */
260                 or51211_setmode(fe,0);
261
262                 /* Update current frequency */
263                 state->current_frequency = param->frequency;
264         }
265         return 0;
266 }
267
268 static int or51211_read_status(struct dvb_frontend* fe, fe_status_t* status)
269 {
270         struct or51211_state* state = fe->demodulator_priv;
271         unsigned char rec_buf[2];
272         unsigned char snd_buf[] = {0x04,0x00,0x03,0x00};
273         *status = 0;
274
275         /* Receiver Status */
276         if (i2c_writebytes(state,state->config->demod_address,snd_buf,3)) {
277                 printk(KERN_WARNING "or51132: read_status write error\n");
278                 return -1;
279         }
280         msleep(3);
281         if (i2c_readbytes(state,state->config->demod_address,rec_buf,2)) {
282                 printk(KERN_WARNING "or51132: read_status read error\n");
283                 return -1;
284         }
285         dprintk("read_status %x %x\n",rec_buf[0],rec_buf[1]);
286
287         if (rec_buf[0] &  0x01) { /* Receiver Lock */
288                 *status |= FE_HAS_SIGNAL;
289                 *status |= FE_HAS_CARRIER;
290                 *status |= FE_HAS_VITERBI;
291                 *status |= FE_HAS_SYNC;
292                 *status |= FE_HAS_LOCK;
293         }
294         return 0;
295 }
296
297 /* Calculate SNR estimation (scaled by 2^24)
298
299    8-VSB SNR equation from Oren datasheets
300
301    For 8-VSB:
302      SNR[dB] = 10 * log10(219037.9454 / MSE^2 )
303
304    We re-write the snr equation as:
305      SNR * 2^24 = 10*(c - 2*intlog10(MSE))
306    Where for 8-VSB, c = log10(219037.9454) * 2^24 */
307
308 static u32 calculate_snr(u32 mse, u32 c)
309 {
310         if (mse == 0) /* No signal */
311                 return 0;
312
313         mse = 2*intlog10(mse);
314         if (mse > c) {
315                 /* Negative SNR, which is possible, but realisticly the
316                 demod will lose lock before the signal gets this bad.  The
317                 API only allows for unsigned values, so just return 0 */
318                 return 0;
319         }
320         return 10*(c - mse);
321 }
322
323 static int or51211_read_snr(struct dvb_frontend* fe, u16* snr)
324 {
325         struct or51211_state* state = fe->demodulator_priv;
326         u8 rec_buf[2];
327         u8 snd_buf[3];
328
329         /* SNR after Equalizer */
330         snd_buf[0] = 0x04;
331         snd_buf[1] = 0x00;
332         snd_buf[2] = 0x04;
333
334         if (i2c_writebytes(state,state->config->demod_address,snd_buf,3)) {
335                 printk(KERN_WARNING "%s: error writing snr reg\n",
336                        __FUNCTION__);
337                 return -1;
338         }
339         if (i2c_readbytes(state,state->config->demod_address,rec_buf,2)) {
340                 printk(KERN_WARNING "%s: read_status read error\n",
341                        __FUNCTION__);
342                 return -1;
343         }
344
345         state->snr = calculate_snr(rec_buf[0], 89599047);
346         *snr = (state->snr) >> 16;
347
348         dprintk("%s: noise = 0x%02x, snr = %d.%02d dB\n", __FUNCTION__, rec_buf[0],
349                 state->snr >> 24, (((state->snr>>8) & 0xffff) * 100) >> 16);
350
351         return 0;
352 }
353
354 static int or51211_read_signal_strength(struct dvb_frontend* fe, u16* strength)
355 {
356         /* Calculate Strength from SNR up to 35dB */
357         /* Even though the SNR can go higher than 35dB, there is some comfort */
358         /* factor in having a range of strong signals that can show at 100%   */
359         struct or51211_state* state = (struct or51211_state*)fe->demodulator_priv;
360         u16 snr;
361         int ret;
362
363         ret = fe->ops.read_snr(fe, &snr);
364         if (ret != 0)
365                 return ret;
366         /* Rather than use the 8.8 value snr, use state->snr which is 8.24 */
367         /* scale the range 0 - 35*2^24 into 0 - 65535 */
368         if (state->snr >= 8960 * 0x10000)
369                 *strength = 0xffff;
370         else
371                 *strength = state->snr / 8960;
372
373         return 0;
374 }
375
376 static int or51211_read_ber(struct dvb_frontend* fe, u32* ber)
377 {
378         *ber = -ENOSYS;
379         return 0;
380 }
381
382 static int or51211_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
383 {
384         *ucblocks = -ENOSYS;
385         return 0;
386 }
387
388 static int or51211_sleep(struct dvb_frontend* fe)
389 {
390         return 0;
391 }
392
393 static int or51211_init(struct dvb_frontend* fe)
394 {
395         struct or51211_state* state = fe->demodulator_priv;
396         const struct or51211_config* config = state->config;
397         const struct firmware* fw;
398         unsigned char get_ver_buf[] = {0x04,0x00,0x30,0x00,0x00};
399         unsigned char rec_buf[14];
400         int ret,i;
401
402         if (!state->initialized) {
403                 /* Request the firmware, this will block until it uploads */
404                 printk(KERN_INFO "or51211: Waiting for firmware upload "
405                        "(%s)...\n", OR51211_DEFAULT_FIRMWARE);
406                 ret = config->request_firmware(fe, &fw,
407                                                OR51211_DEFAULT_FIRMWARE);
408                 printk(KERN_INFO "or51211:Got Hotplug firmware\n");
409                 if (ret) {
410                         printk(KERN_WARNING "or51211: No firmware uploaded "
411                                "(timeout or file not found?)\n");
412                         return ret;
413                 }
414
415                 ret = or51211_load_firmware(fe, fw);
416                 release_firmware(fw);
417                 if (ret) {
418                         printk(KERN_WARNING "or51211: Writing firmware to "
419                                "device failed!\n");
420                         return ret;
421                 }
422                 printk(KERN_INFO "or51211: Firmware upload complete.\n");
423
424                 /* Set operation mode in Receiver 1 register;
425                  * type 1:
426                  * data 0x50h  Automatic sets receiver channel conditions
427                  *             Automatic NTSC rejection filter
428                  *             Enable  MPEG serial data output
429                  *             MPEG2tr
430                  *             High tuner phase noise
431                  *             normal +/-150kHz Carrier acquisition range
432                  */
433                 if (i2c_writebytes(state,state->config->demod_address,
434                                    cmd_buf,3)) {
435                         printk(KERN_WARNING "or51211: Load DVR Error 5\n");
436                         return -1;
437                 }
438
439                 /* Read back ucode version to besure we loaded correctly */
440                 /* and are really up and running */
441                 rec_buf[0] = 0x04;
442                 rec_buf[1] = 0x00;
443                 rec_buf[2] = 0x03;
444                 rec_buf[3] = 0x00;
445                 msleep(30);
446                 if (i2c_writebytes(state,state->config->demod_address,
447                                    rec_buf,3)) {
448                         printk(KERN_WARNING "or51211: Load DVR Error A\n");
449                         return -1;
450                 }
451                 msleep(3);
452                 if (i2c_readbytes(state,state->config->demod_address,
453                                   &rec_buf[10],2)) {
454                         printk(KERN_WARNING "or51211: Load DVR Error B\n");
455                         return -1;
456                 }
457
458                 rec_buf[0] = 0x04;
459                 rec_buf[1] = 0x00;
460                 rec_buf[2] = 0x01;
461                 rec_buf[3] = 0x00;
462                 msleep(20);
463                 if (i2c_writebytes(state,state->config->demod_address,
464                                    rec_buf,3)) {
465                         printk(KERN_WARNING "or51211: Load DVR Error C\n");
466                         return -1;
467                 }
468                 msleep(3);
469                 if (i2c_readbytes(state,state->config->demod_address,
470                                   &rec_buf[12],2)) {
471                         printk(KERN_WARNING "or51211: Load DVR Error D\n");
472                         return -1;
473                 }
474
475                 for (i = 0; i < 8; i++)
476                         rec_buf[i]=0xed;
477
478                 for (i = 0; i < 5; i++) {
479                         msleep(30);
480                         get_ver_buf[4] = i+1;
481                         if (i2c_writebytes(state,state->config->demod_address,
482                                            get_ver_buf,5)) {
483                                 printk(KERN_WARNING "or51211:Load DVR Error 6"
484                                        " - %d\n",i);
485                                 return -1;
486                         }
487                         msleep(3);
488
489                         if (i2c_readbytes(state,state->config->demod_address,
490                                           &rec_buf[i*2],2)) {
491                                 printk(KERN_WARNING "or51211:Load DVR Error 7"
492                                        " - %d\n",i);
493                                 return -1;
494                         }
495                         /* If we didn't receive the right index, try again */
496                         if ((int)rec_buf[i*2+1]!=i+1){
497                           i--;
498                         }
499                 }
500                 dprintk("read_fwbits %x %x %x %x %x %x %x %x %x %x\n",
501                         rec_buf[0], rec_buf[1], rec_buf[2], rec_buf[3],
502                         rec_buf[4], rec_buf[5], rec_buf[6], rec_buf[7],
503                         rec_buf[8], rec_buf[9]);
504
505                 printk(KERN_INFO "or51211: ver TU%02x%02x%02x VSB mode %02x"
506                        " Status %02x\n",
507                        rec_buf[2], rec_buf[4],rec_buf[6],
508                        rec_buf[12],rec_buf[10]);
509
510                 rec_buf[0] = 0x04;
511                 rec_buf[1] = 0x00;
512                 rec_buf[2] = 0x03;
513                 rec_buf[3] = 0x00;
514                 msleep(20);
515                 if (i2c_writebytes(state,state->config->demod_address,
516                                    rec_buf,3)) {
517                         printk(KERN_WARNING "or51211: Load DVR Error 8\n");
518                         return -1;
519                 }
520                 msleep(20);
521                 if (i2c_readbytes(state,state->config->demod_address,
522                                   &rec_buf[8],2)) {
523                         printk(KERN_WARNING "or51211: Load DVR Error 9\n");
524                         return -1;
525                 }
526                 state->initialized = 1;
527         }
528
529         return 0;
530 }
531
532 static int or51211_get_tune_settings(struct dvb_frontend* fe,
533                                      struct dvb_frontend_tune_settings* fesettings)
534 {
535         fesettings->min_delay_ms = 500;
536         fesettings->step_size = 0;
537         fesettings->max_drift = 0;
538         return 0;
539 }
540
541 static void or51211_release(struct dvb_frontend* fe)
542 {
543         struct or51211_state* state = fe->demodulator_priv;
544         state->config->sleep(fe);
545         kfree(state);
546 }
547
548 static struct dvb_frontend_ops or51211_ops;
549
550 struct dvb_frontend* or51211_attach(const struct or51211_config* config,
551                                     struct i2c_adapter* i2c)
552 {
553         struct or51211_state* state = NULL;
554
555         /* Allocate memory for the internal state */
556         state = kmalloc(sizeof(struct or51211_state), GFP_KERNEL);
557         if (state == NULL)
558                 goto error;
559
560         /* Setup the state */
561         state->config = config;
562         state->i2c = i2c;
563         state->initialized = 0;
564         state->current_frequency = 0;
565
566         /* Create dvb_frontend */
567         memcpy(&state->frontend.ops, &or51211_ops, sizeof(struct dvb_frontend_ops));
568         state->frontend.demodulator_priv = state;
569         return &state->frontend;
570
571 error:
572         kfree(state);
573         return NULL;
574 }
575
576 static struct dvb_frontend_ops or51211_ops = {
577
578         .info = {
579                 .name               = "Oren OR51211 VSB Frontend",
580                 .type               = FE_ATSC,
581                 .frequency_min      = 44000000,
582                 .frequency_max      = 958000000,
583                 .frequency_stepsize = 166666,
584                 .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
585                         FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
586                         FE_CAN_8VSB
587         },
588
589         .release = or51211_release,
590
591         .init = or51211_init,
592         .sleep = or51211_sleep,
593
594         .set_frontend = or51211_set_parameters,
595         .get_tune_settings = or51211_get_tune_settings,
596
597         .read_status = or51211_read_status,
598         .read_ber = or51211_read_ber,
599         .read_signal_strength = or51211_read_signal_strength,
600         .read_snr = or51211_read_snr,
601         .read_ucblocks = or51211_read_ucblocks,
602 };
603
604 module_param(debug, int, 0644);
605 MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
606
607 MODULE_DESCRIPTION("Oren OR51211 VSB [pcHDTV HD-2000] Demodulator Driver");
608 MODULE_AUTHOR("Kirk Lapray");
609 MODULE_LICENSE("GPL");
610
611 EXPORT_SYMBOL(or51211_attach);
612