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