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