f46d5a46683abd868bcc6d2827dce226e4a6e53b
[safe/jmp/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
227         /* Change only if we are actually changing the channel */
228         if (state->current_frequency != param->frequency) {
229                 if (fe->ops.tuner_ops.set_params) {
230                         fe->ops.tuner_ops.set_params(fe, param);
231                         if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 0);
232                 }
233
234                 /* Set to ATSC mode */
235                 or51211_setmode(fe,0);
236
237                 /* Update current frequency */
238                 state->current_frequency = param->frequency;
239         }
240         return 0;
241 }
242
243 static int or51211_read_status(struct dvb_frontend* fe, fe_status_t* status)
244 {
245         struct or51211_state* state = fe->demodulator_priv;
246         unsigned char rec_buf[2];
247         unsigned char snd_buf[] = {0x04,0x00,0x03,0x00};
248         *status = 0;
249
250         /* Receiver Status */
251         if (i2c_writebytes(state,state->config->demod_address,snd_buf,3)) {
252                 printk(KERN_WARNING "or51132: read_status write error\n");
253                 return -1;
254         }
255         msleep(3);
256         if (i2c_readbytes(state,state->config->demod_address,rec_buf,2)) {
257                 printk(KERN_WARNING "or51132: read_status read error\n");
258                 return -1;
259         }
260         dprintk("read_status %x %x\n",rec_buf[0],rec_buf[1]);
261
262         if (rec_buf[0] &  0x01) { /* Receiver Lock */
263                 *status |= FE_HAS_SIGNAL;
264                 *status |= FE_HAS_CARRIER;
265                 *status |= FE_HAS_VITERBI;
266                 *status |= FE_HAS_SYNC;
267                 *status |= FE_HAS_LOCK;
268         }
269         return 0;
270 }
271
272 /* Calculate SNR estimation (scaled by 2^24)
273
274    8-VSB SNR equation from Oren datasheets
275
276    For 8-VSB:
277      SNR[dB] = 10 * log10(219037.9454 / MSE^2 )
278
279    We re-write the snr equation as:
280      SNR * 2^24 = 10*(c - 2*intlog10(MSE))
281    Where for 8-VSB, c = log10(219037.9454) * 2^24 */
282
283 static u32 calculate_snr(u32 mse, u32 c)
284 {
285         if (mse == 0) /* No signal */
286                 return 0;
287
288         mse = 2*intlog10(mse);
289         if (mse > c) {
290                 /* Negative SNR, which is possible, but realisticly the
291                 demod will lose lock before the signal gets this bad.  The
292                 API only allows for unsigned values, so just return 0 */
293                 return 0;
294         }
295         return 10*(c - mse);
296 }
297
298 static int or51211_read_snr(struct dvb_frontend* fe, u16* snr)
299 {
300         struct or51211_state* state = fe->demodulator_priv;
301         u8 rec_buf[2];
302         u8 snd_buf[3];
303
304         /* SNR after Equalizer */
305         snd_buf[0] = 0x04;
306         snd_buf[1] = 0x00;
307         snd_buf[2] = 0x04;
308
309         if (i2c_writebytes(state,state->config->demod_address,snd_buf,3)) {
310                 printk(KERN_WARNING "%s: error writing snr reg\n",
311                        __FUNCTION__);
312                 return -1;
313         }
314         if (i2c_readbytes(state,state->config->demod_address,rec_buf,2)) {
315                 printk(KERN_WARNING "%s: read_status read error\n",
316                        __FUNCTION__);
317                 return -1;
318         }
319
320         state->snr = calculate_snr(rec_buf[0], 89599047);
321         *snr = (state->snr) >> 16;
322
323         dprintk("%s: noise = 0x%02x, snr = %d.%02d dB\n", __FUNCTION__, rec_buf[0],
324                 state->snr >> 24, (((state->snr>>8) & 0xffff) * 100) >> 16);
325
326         return 0;
327 }
328
329 static int or51211_read_signal_strength(struct dvb_frontend* fe, u16* strength)
330 {
331         /* Calculate Strength from SNR up to 35dB */
332         /* Even though the SNR can go higher than 35dB, there is some comfort */
333         /* factor in having a range of strong signals that can show at 100%   */
334         struct or51211_state* state = (struct or51211_state*)fe->demodulator_priv;
335         u16 snr;
336         int ret;
337
338         ret = fe->ops.read_snr(fe, &snr);
339         if (ret != 0)
340                 return ret;
341         /* Rather than use the 8.8 value snr, use state->snr which is 8.24 */
342         /* scale the range 0 - 35*2^24 into 0 - 65535 */
343         if (state->snr >= 8960 * 0x10000)
344                 *strength = 0xffff;
345         else
346                 *strength = state->snr / 8960;
347
348         return 0;
349 }
350
351 static int or51211_read_ber(struct dvb_frontend* fe, u32* ber)
352 {
353         *ber = -ENOSYS;
354         return 0;
355 }
356
357 static int or51211_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
358 {
359         *ucblocks = -ENOSYS;
360         return 0;
361 }
362
363 static int or51211_sleep(struct dvb_frontend* fe)
364 {
365         return 0;
366 }
367
368 static int or51211_init(struct dvb_frontend* fe)
369 {
370         struct or51211_state* state = fe->demodulator_priv;
371         const struct or51211_config* config = state->config;
372         const struct firmware* fw;
373         unsigned char get_ver_buf[] = {0x04,0x00,0x30,0x00,0x00};
374         unsigned char rec_buf[14];
375         int ret,i;
376
377         if (!state->initialized) {
378                 /* Request the firmware, this will block until it uploads */
379                 printk(KERN_INFO "or51211: Waiting for firmware upload "
380                        "(%s)...\n", OR51211_DEFAULT_FIRMWARE);
381                 ret = config->request_firmware(fe, &fw,
382                                                OR51211_DEFAULT_FIRMWARE);
383                 printk(KERN_INFO "or51211:Got Hotplug firmware\n");
384                 if (ret) {
385                         printk(KERN_WARNING "or51211: No firmware uploaded "
386                                "(timeout or file not found?)\n");
387                         return ret;
388                 }
389
390                 ret = or51211_load_firmware(fe, fw);
391                 release_firmware(fw);
392                 if (ret) {
393                         printk(KERN_WARNING "or51211: Writing firmware to "
394                                "device failed!\n");
395                         return ret;
396                 }
397                 printk(KERN_INFO "or51211: Firmware upload complete.\n");
398
399                 /* Set operation mode in Receiver 1 register;
400                  * type 1:
401                  * data 0x50h  Automatic sets receiver channel conditions
402                  *             Automatic NTSC rejection filter
403                  *             Enable  MPEG serial data output
404                  *             MPEG2tr
405                  *             High tuner phase noise
406                  *             normal +/-150kHz Carrier acquisition range
407                  */
408                 if (i2c_writebytes(state,state->config->demod_address,
409                                    cmd_buf,3)) {
410                         printk(KERN_WARNING "or51211: Load DVR Error 5\n");
411                         return -1;
412                 }
413
414                 /* Read back ucode version to besure we loaded correctly */
415                 /* and are really up and running */
416                 rec_buf[0] = 0x04;
417                 rec_buf[1] = 0x00;
418                 rec_buf[2] = 0x03;
419                 rec_buf[3] = 0x00;
420                 msleep(30);
421                 if (i2c_writebytes(state,state->config->demod_address,
422                                    rec_buf,3)) {
423                         printk(KERN_WARNING "or51211: Load DVR Error A\n");
424                         return -1;
425                 }
426                 msleep(3);
427                 if (i2c_readbytes(state,state->config->demod_address,
428                                   &rec_buf[10],2)) {
429                         printk(KERN_WARNING "or51211: Load DVR Error B\n");
430                         return -1;
431                 }
432
433                 rec_buf[0] = 0x04;
434                 rec_buf[1] = 0x00;
435                 rec_buf[2] = 0x01;
436                 rec_buf[3] = 0x00;
437                 msleep(20);
438                 if (i2c_writebytes(state,state->config->demod_address,
439                                    rec_buf,3)) {
440                         printk(KERN_WARNING "or51211: Load DVR Error C\n");
441                         return -1;
442                 }
443                 msleep(3);
444                 if (i2c_readbytes(state,state->config->demod_address,
445                                   &rec_buf[12],2)) {
446                         printk(KERN_WARNING "or51211: Load DVR Error D\n");
447                         return -1;
448                 }
449
450                 for (i = 0; i < 8; i++)
451                         rec_buf[i]=0xed;
452
453                 for (i = 0; i < 5; i++) {
454                         msleep(30);
455                         get_ver_buf[4] = i+1;
456                         if (i2c_writebytes(state,state->config->demod_address,
457                                            get_ver_buf,5)) {
458                                 printk(KERN_WARNING "or51211:Load DVR Error 6"
459                                        " - %d\n",i);
460                                 return -1;
461                         }
462                         msleep(3);
463
464                         if (i2c_readbytes(state,state->config->demod_address,
465                                           &rec_buf[i*2],2)) {
466                                 printk(KERN_WARNING "or51211:Load DVR Error 7"
467                                        " - %d\n",i);
468                                 return -1;
469                         }
470                         /* If we didn't receive the right index, try again */
471                         if ((int)rec_buf[i*2+1]!=i+1){
472                           i--;
473                         }
474                 }
475                 dprintk("read_fwbits %x %x %x %x %x %x %x %x %x %x\n",
476                         rec_buf[0], rec_buf[1], rec_buf[2], rec_buf[3],
477                         rec_buf[4], rec_buf[5], rec_buf[6], rec_buf[7],
478                         rec_buf[8], rec_buf[9]);
479
480                 printk(KERN_INFO "or51211: ver TU%02x%02x%02x VSB mode %02x"
481                        " Status %02x\n",
482                        rec_buf[2], rec_buf[4],rec_buf[6],
483                        rec_buf[12],rec_buf[10]);
484
485                 rec_buf[0] = 0x04;
486                 rec_buf[1] = 0x00;
487                 rec_buf[2] = 0x03;
488                 rec_buf[3] = 0x00;
489                 msleep(20);
490                 if (i2c_writebytes(state,state->config->demod_address,
491                                    rec_buf,3)) {
492                         printk(KERN_WARNING "or51211: Load DVR Error 8\n");
493                         return -1;
494                 }
495                 msleep(20);
496                 if (i2c_readbytes(state,state->config->demod_address,
497                                   &rec_buf[8],2)) {
498                         printk(KERN_WARNING "or51211: Load DVR Error 9\n");
499                         return -1;
500                 }
501                 state->initialized = 1;
502         }
503
504         return 0;
505 }
506
507 static int or51211_get_tune_settings(struct dvb_frontend* fe,
508                                      struct dvb_frontend_tune_settings* fesettings)
509 {
510         fesettings->min_delay_ms = 500;
511         fesettings->step_size = 0;
512         fesettings->max_drift = 0;
513         return 0;
514 }
515
516 static void or51211_release(struct dvb_frontend* fe)
517 {
518         struct or51211_state* state = fe->demodulator_priv;
519         state->config->sleep(fe);
520         kfree(state);
521 }
522
523 static struct dvb_frontend_ops or51211_ops;
524
525 struct dvb_frontend* or51211_attach(const struct or51211_config* config,
526                                     struct i2c_adapter* i2c)
527 {
528         struct or51211_state* state = NULL;
529
530         /* Allocate memory for the internal state */
531         state = kmalloc(sizeof(struct or51211_state), GFP_KERNEL);
532         if (state == NULL)
533                 goto error;
534
535         /* Setup the state */
536         state->config = config;
537         state->i2c = i2c;
538         state->initialized = 0;
539         state->current_frequency = 0;
540
541         /* Create dvb_frontend */
542         memcpy(&state->frontend.ops, &or51211_ops, sizeof(struct dvb_frontend_ops));
543         state->frontend.demodulator_priv = state;
544         return &state->frontend;
545
546 error:
547         kfree(state);
548         return NULL;
549 }
550
551 static struct dvb_frontend_ops or51211_ops = {
552
553         .info = {
554                 .name               = "Oren OR51211 VSB Frontend",
555                 .type               = FE_ATSC,
556                 .frequency_min      = 44000000,
557                 .frequency_max      = 958000000,
558                 .frequency_stepsize = 166666,
559                 .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
560                         FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
561                         FE_CAN_8VSB
562         },
563
564         .release = or51211_release,
565
566         .init = or51211_init,
567         .sleep = or51211_sleep,
568
569         .set_frontend = or51211_set_parameters,
570         .get_tune_settings = or51211_get_tune_settings,
571
572         .read_status = or51211_read_status,
573         .read_ber = or51211_read_ber,
574         .read_signal_strength = or51211_read_signal_strength,
575         .read_snr = or51211_read_snr,
576         .read_ucblocks = or51211_read_ucblocks,
577 };
578
579 module_param(debug, int, 0644);
580 MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
581
582 MODULE_DESCRIPTION("Oren OR51211 VSB [pcHDTV HD-2000] Demodulator Driver");
583 MODULE_AUTHOR("Kirk Lapray");
584 MODULE_LICENSE("GPL");
585
586 EXPORT_SYMBOL(or51211_attach);
587