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