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