Merge tag 'io_uring-5.11-2021-01-16' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / media / dvb-frontends / stv0297.c
blob6d5962d5697ac95e94ba1507075614bd27e8cea3
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 Driver for STV0297 demodulator
5 Copyright (C) 2004 Andrew de Quincey <adq_dvb@lidskialf.net>
6 Copyright (C) 2003-2004 Dennis Noermann <dennis.noermann@noernet.de>
8 */
10 #include <linux/init.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/string.h>
14 #include <linux/delay.h>
15 #include <linux/jiffies.h>
16 #include <linux/slab.h>
18 #include <media/dvb_frontend.h>
19 #include "stv0297.h"
21 struct stv0297_state {
22 struct i2c_adapter *i2c;
23 const struct stv0297_config *config;
24 struct dvb_frontend frontend;
26 unsigned long last_ber;
27 unsigned long base_freq;
30 #if 1
31 #define dprintk(x...) printk(x)
32 #else
33 #define dprintk(x...)
34 #endif
36 #define STV0297_CLOCK_KHZ 28900
39 static int stv0297_writereg(struct stv0297_state *state, u8 reg, u8 data)
41 int ret;
42 u8 buf[] = { reg, data };
43 struct i2c_msg msg = {.addr = state->config->demod_address,.flags = 0,.buf = buf,.len = 2 };
45 ret = i2c_transfer(state->i2c, &msg, 1);
47 if (ret != 1)
48 dprintk("%s: writereg error (reg == 0x%02x, val == 0x%02x, ret == %i)\n",
49 __func__, reg, data, ret);
51 return (ret != 1) ? -1 : 0;
54 static int stv0297_readreg(struct stv0297_state *state, u8 reg)
56 int ret;
57 u8 b0[] = { reg };
58 u8 b1[] = { 0 };
59 struct i2c_msg msg[] = { {.addr = state->config->demod_address,.flags = 0,.buf = b0,.len = 1},
60 {.addr = state->config->demod_address,.flags = I2C_M_RD,.buf = b1,.len = 1}
63 // this device needs a STOP between the register and data
64 if (state->config->stop_during_read) {
65 if ((ret = i2c_transfer(state->i2c, &msg[0], 1)) != 1) {
66 dprintk("%s: readreg error (reg == 0x%02x, ret == %i)\n", __func__, reg, ret);
67 return -1;
69 if ((ret = i2c_transfer(state->i2c, &msg[1], 1)) != 1) {
70 dprintk("%s: readreg error (reg == 0x%02x, ret == %i)\n", __func__, reg, ret);
71 return -1;
73 } else {
74 if ((ret = i2c_transfer(state->i2c, msg, 2)) != 2) {
75 dprintk("%s: readreg error (reg == 0x%02x, ret == %i)\n", __func__, reg, ret);
76 return -1;
80 return b1[0];
83 static int stv0297_writereg_mask(struct stv0297_state *state, u8 reg, u8 mask, u8 data)
85 int val;
87 val = stv0297_readreg(state, reg);
88 val &= ~mask;
89 val |= (data & mask);
90 stv0297_writereg(state, reg, val);
92 return 0;
95 static int stv0297_readregs(struct stv0297_state *state, u8 reg1, u8 * b, u8 len)
97 int ret;
98 struct i2c_msg msg[] = { {.addr = state->config->demod_address,.flags = 0,.buf =
99 &reg1,.len = 1},
100 {.addr = state->config->demod_address,.flags = I2C_M_RD,.buf = b,.len = len}
103 // this device needs a STOP between the register and data
104 if (state->config->stop_during_read) {
105 if ((ret = i2c_transfer(state->i2c, &msg[0], 1)) != 1) {
106 dprintk("%s: readreg error (reg == 0x%02x, ret == %i)\n", __func__, reg1, ret);
107 return -1;
109 if ((ret = i2c_transfer(state->i2c, &msg[1], 1)) != 1) {
110 dprintk("%s: readreg error (reg == 0x%02x, ret == %i)\n", __func__, reg1, ret);
111 return -1;
113 } else {
114 if ((ret = i2c_transfer(state->i2c, msg, 2)) != 2) {
115 dprintk("%s: readreg error (reg == 0x%02x, ret == %i)\n", __func__, reg1, ret);
116 return -1;
120 return 0;
123 static u32 stv0297_get_symbolrate(struct stv0297_state *state)
125 u64 tmp;
127 tmp = (u64)(stv0297_readreg(state, 0x55)
128 | (stv0297_readreg(state, 0x56) << 8)
129 | (stv0297_readreg(state, 0x57) << 16)
130 | (stv0297_readreg(state, 0x58) << 24));
132 tmp *= STV0297_CLOCK_KHZ;
133 tmp >>= 32;
135 return (u32) tmp;
138 static void stv0297_set_symbolrate(struct stv0297_state *state, u32 srate)
140 long tmp;
142 tmp = 131072L * srate; /* 131072 = 2^17 */
143 tmp = tmp / (STV0297_CLOCK_KHZ / 4); /* 1/4 = 2^-2 */
144 tmp = tmp * 8192L; /* 8192 = 2^13 */
146 stv0297_writereg(state, 0x55, (unsigned char) (tmp & 0xFF));
147 stv0297_writereg(state, 0x56, (unsigned char) (tmp >> 8));
148 stv0297_writereg(state, 0x57, (unsigned char) (tmp >> 16));
149 stv0297_writereg(state, 0x58, (unsigned char) (tmp >> 24));
152 static void stv0297_set_sweeprate(struct stv0297_state *state, short fshift, long symrate)
154 long tmp;
156 tmp = (long) fshift *262144L; /* 262144 = 2*18 */
157 tmp /= symrate;
158 tmp *= 1024; /* 1024 = 2*10 */
160 // adjust
161 if (tmp >= 0) {
162 tmp += 500000;
163 } else {
164 tmp -= 500000;
166 tmp /= 1000000;
168 stv0297_writereg(state, 0x60, tmp & 0xFF);
169 stv0297_writereg_mask(state, 0x69, 0xF0, (tmp >> 4) & 0xf0);
172 static void stv0297_set_carrieroffset(struct stv0297_state *state, long offset)
174 long tmp;
176 /* symrate is hardcoded to 10000 */
177 tmp = offset * 26844L; /* (2**28)/10000 */
178 if (tmp < 0)
179 tmp += 0x10000000;
180 tmp &= 0x0FFFFFFF;
182 stv0297_writereg(state, 0x66, (unsigned char) (tmp & 0xFF));
183 stv0297_writereg(state, 0x67, (unsigned char) (tmp >> 8));
184 stv0297_writereg(state, 0x68, (unsigned char) (tmp >> 16));
185 stv0297_writereg_mask(state, 0x69, 0x0F, (tmp >> 24) & 0x0f);
189 static long stv0297_get_carrieroffset(struct stv0297_state *state)
191 s64 tmp;
193 stv0297_writereg(state, 0x6B, 0x00);
195 tmp = stv0297_readreg(state, 0x66);
196 tmp |= (stv0297_readreg(state, 0x67) << 8);
197 tmp |= (stv0297_readreg(state, 0x68) << 16);
198 tmp |= (stv0297_readreg(state, 0x69) & 0x0F) << 24;
200 tmp *= stv0297_get_symbolrate(state);
201 tmp >>= 28;
203 return (s32) tmp;
207 static void stv0297_set_initialdemodfreq(struct stv0297_state *state, long freq)
209 s32 tmp;
211 if (freq > 10000)
212 freq -= STV0297_CLOCK_KHZ;
214 tmp = (STV0297_CLOCK_KHZ * 1000) / (1 << 16);
215 tmp = (freq * 1000) / tmp;
216 if (tmp > 0xffff)
217 tmp = 0xffff;
219 stv0297_writereg_mask(state, 0x25, 0x80, 0x80);
220 stv0297_writereg(state, 0x21, tmp >> 8);
221 stv0297_writereg(state, 0x20, tmp);
224 static int stv0297_set_qam(struct stv0297_state *state,
225 enum fe_modulation modulation)
227 int val = 0;
229 switch (modulation) {
230 case QAM_16:
231 val = 0;
232 break;
234 case QAM_32:
235 val = 1;
236 break;
238 case QAM_64:
239 val = 4;
240 break;
242 case QAM_128:
243 val = 2;
244 break;
246 case QAM_256:
247 val = 3;
248 break;
250 default:
251 return -EINVAL;
254 stv0297_writereg_mask(state, 0x00, 0x70, val << 4);
256 return 0;
259 static int stv0297_set_inversion(struct stv0297_state *state,
260 enum fe_spectral_inversion inversion)
262 int val = 0;
264 switch (inversion) {
265 case INVERSION_OFF:
266 val = 0;
267 break;
269 case INVERSION_ON:
270 val = 1;
271 break;
273 default:
274 return -EINVAL;
277 stv0297_writereg_mask(state, 0x83, 0x08, val << 3);
279 return 0;
282 static int stv0297_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
284 struct stv0297_state *state = fe->demodulator_priv;
286 if (enable) {
287 stv0297_writereg(state, 0x87, 0x78);
288 stv0297_writereg(state, 0x86, 0xc8);
291 return 0;
294 static int stv0297_init(struct dvb_frontend *fe)
296 struct stv0297_state *state = fe->demodulator_priv;
297 int i;
299 /* load init table */
300 for (i=0; !(state->config->inittab[i] == 0xff && state->config->inittab[i+1] == 0xff); i+=2)
301 stv0297_writereg(state, state->config->inittab[i], state->config->inittab[i+1]);
302 msleep(200);
304 state->last_ber = 0;
306 return 0;
309 static int stv0297_sleep(struct dvb_frontend *fe)
311 struct stv0297_state *state = fe->demodulator_priv;
313 stv0297_writereg_mask(state, 0x80, 1, 1);
315 return 0;
318 static int stv0297_read_status(struct dvb_frontend *fe,
319 enum fe_status *status)
321 struct stv0297_state *state = fe->demodulator_priv;
323 u8 sync = stv0297_readreg(state, 0xDF);
325 *status = 0;
326 if (sync & 0x80)
327 *status |=
328 FE_HAS_SYNC | FE_HAS_SIGNAL | FE_HAS_CARRIER | FE_HAS_VITERBI | FE_HAS_LOCK;
329 return 0;
332 static int stv0297_read_ber(struct dvb_frontend *fe, u32 * ber)
334 struct stv0297_state *state = fe->demodulator_priv;
335 u8 BER[3];
337 stv0297_readregs(state, 0xA0, BER, 3);
338 if (!(BER[0] & 0x80)) {
339 state->last_ber = BER[2] << 8 | BER[1];
340 stv0297_writereg_mask(state, 0xA0, 0x80, 0x80);
343 *ber = state->last_ber;
345 return 0;
349 static int stv0297_read_signal_strength(struct dvb_frontend *fe, u16 * strength)
351 struct stv0297_state *state = fe->demodulator_priv;
352 u8 STRENGTH[3];
353 u16 tmp;
355 stv0297_readregs(state, 0x41, STRENGTH, 3);
356 tmp = (STRENGTH[1] & 0x03) << 8 | STRENGTH[0];
357 if (STRENGTH[2] & 0x20) {
358 if (tmp < 0x200)
359 tmp = 0;
360 else
361 tmp = tmp - 0x200;
362 } else {
363 if (tmp > 0x1ff)
364 tmp = 0;
365 else
366 tmp = 0x1ff - tmp;
368 *strength = (tmp << 7) | (tmp >> 2);
369 return 0;
372 static int stv0297_read_snr(struct dvb_frontend *fe, u16 * snr)
374 struct stv0297_state *state = fe->demodulator_priv;
375 u8 SNR[2];
377 stv0297_readregs(state, 0x07, SNR, 2);
378 *snr = SNR[1] << 8 | SNR[0];
380 return 0;
383 static int stv0297_read_ucblocks(struct dvb_frontend *fe, u32 * ucblocks)
385 struct stv0297_state *state = fe->demodulator_priv;
387 stv0297_writereg_mask(state, 0xDF, 0x03, 0x03); /* freeze the counters */
389 *ucblocks = (stv0297_readreg(state, 0xD5) << 8)
390 | stv0297_readreg(state, 0xD4);
392 stv0297_writereg_mask(state, 0xDF, 0x03, 0x02); /* clear the counters */
393 stv0297_writereg_mask(state, 0xDF, 0x03, 0x01); /* re-enable the counters */
395 return 0;
398 static int stv0297_set_frontend(struct dvb_frontend *fe)
400 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
401 struct stv0297_state *state = fe->demodulator_priv;
402 int u_threshold;
403 int initial_u;
404 int blind_u;
405 int delay;
406 int sweeprate;
407 int carrieroffset;
408 unsigned long timeout;
409 enum fe_spectral_inversion inversion;
411 switch (p->modulation) {
412 case QAM_16:
413 case QAM_32:
414 case QAM_64:
415 delay = 100;
416 sweeprate = 1000;
417 break;
419 case QAM_128:
420 case QAM_256:
421 delay = 200;
422 sweeprate = 500;
423 break;
425 default:
426 return -EINVAL;
429 // determine inversion dependent parameters
430 inversion = p->inversion;
431 if (state->config->invert)
432 inversion = (inversion == INVERSION_ON) ? INVERSION_OFF : INVERSION_ON;
433 carrieroffset = -330;
434 switch (inversion) {
435 case INVERSION_OFF:
436 break;
438 case INVERSION_ON:
439 sweeprate = -sweeprate;
440 carrieroffset = -carrieroffset;
441 break;
443 default:
444 return -EINVAL;
447 stv0297_init(fe);
448 if (fe->ops.tuner_ops.set_params) {
449 fe->ops.tuner_ops.set_params(fe);
450 if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 0);
453 /* clear software interrupts */
454 stv0297_writereg(state, 0x82, 0x0);
456 /* set initial demodulation frequency */
457 stv0297_set_initialdemodfreq(state, 7250);
459 /* setup AGC */
460 stv0297_writereg_mask(state, 0x43, 0x10, 0x00);
461 stv0297_writereg(state, 0x41, 0x00);
462 stv0297_writereg_mask(state, 0x42, 0x03, 0x01);
463 stv0297_writereg_mask(state, 0x36, 0x60, 0x00);
464 stv0297_writereg_mask(state, 0x36, 0x18, 0x00);
465 stv0297_writereg_mask(state, 0x71, 0x80, 0x80);
466 stv0297_writereg(state, 0x72, 0x00);
467 stv0297_writereg(state, 0x73, 0x00);
468 stv0297_writereg_mask(state, 0x74, 0x0F, 0x00);
469 stv0297_writereg_mask(state, 0x43, 0x08, 0x00);
470 stv0297_writereg_mask(state, 0x71, 0x80, 0x00);
472 /* setup STL */
473 stv0297_writereg_mask(state, 0x5a, 0x20, 0x20);
474 stv0297_writereg_mask(state, 0x5b, 0x02, 0x02);
475 stv0297_writereg_mask(state, 0x5b, 0x02, 0x00);
476 stv0297_writereg_mask(state, 0x5b, 0x01, 0x00);
477 stv0297_writereg_mask(state, 0x5a, 0x40, 0x40);
479 /* disable frequency sweep */
480 stv0297_writereg_mask(state, 0x6a, 0x01, 0x00);
482 /* reset deinterleaver */
483 stv0297_writereg_mask(state, 0x81, 0x01, 0x01);
484 stv0297_writereg_mask(state, 0x81, 0x01, 0x00);
486 /* ??? */
487 stv0297_writereg_mask(state, 0x83, 0x20, 0x20);
488 stv0297_writereg_mask(state, 0x83, 0x20, 0x00);
490 /* reset equaliser */
491 u_threshold = stv0297_readreg(state, 0x00) & 0xf;
492 initial_u = stv0297_readreg(state, 0x01) >> 4;
493 blind_u = stv0297_readreg(state, 0x01) & 0xf;
494 stv0297_writereg_mask(state, 0x84, 0x01, 0x01);
495 stv0297_writereg_mask(state, 0x84, 0x01, 0x00);
496 stv0297_writereg_mask(state, 0x00, 0x0f, u_threshold);
497 stv0297_writereg_mask(state, 0x01, 0xf0, initial_u << 4);
498 stv0297_writereg_mask(state, 0x01, 0x0f, blind_u);
500 /* data comes from internal A/D */
501 stv0297_writereg_mask(state, 0x87, 0x80, 0x00);
503 /* clear phase registers */
504 stv0297_writereg(state, 0x63, 0x00);
505 stv0297_writereg(state, 0x64, 0x00);
506 stv0297_writereg(state, 0x65, 0x00);
507 stv0297_writereg(state, 0x66, 0x00);
508 stv0297_writereg(state, 0x67, 0x00);
509 stv0297_writereg(state, 0x68, 0x00);
510 stv0297_writereg_mask(state, 0x69, 0x0f, 0x00);
512 /* set parameters */
513 stv0297_set_qam(state, p->modulation);
514 stv0297_set_symbolrate(state, p->symbol_rate / 1000);
515 stv0297_set_sweeprate(state, sweeprate, p->symbol_rate / 1000);
516 stv0297_set_carrieroffset(state, carrieroffset);
517 stv0297_set_inversion(state, inversion);
519 /* kick off lock */
520 /* Disable corner detection for higher QAMs */
521 if (p->modulation == QAM_128 ||
522 p->modulation == QAM_256)
523 stv0297_writereg_mask(state, 0x88, 0x08, 0x00);
524 else
525 stv0297_writereg_mask(state, 0x88, 0x08, 0x08);
527 stv0297_writereg_mask(state, 0x5a, 0x20, 0x00);
528 stv0297_writereg_mask(state, 0x6a, 0x01, 0x01);
529 stv0297_writereg_mask(state, 0x43, 0x40, 0x40);
530 stv0297_writereg_mask(state, 0x5b, 0x30, 0x00);
531 stv0297_writereg_mask(state, 0x03, 0x0c, 0x0c);
532 stv0297_writereg_mask(state, 0x03, 0x03, 0x03);
533 stv0297_writereg_mask(state, 0x43, 0x10, 0x10);
535 /* wait for WGAGC lock */
536 timeout = jiffies + msecs_to_jiffies(2000);
537 while (time_before(jiffies, timeout)) {
538 msleep(10);
539 if (stv0297_readreg(state, 0x43) & 0x08)
540 break;
542 if (time_after(jiffies, timeout)) {
543 goto timeout;
545 msleep(20);
547 /* wait for equaliser partial convergence */
548 timeout = jiffies + msecs_to_jiffies(500);
549 while (time_before(jiffies, timeout)) {
550 msleep(10);
552 if (stv0297_readreg(state, 0x82) & 0x04) {
553 break;
556 if (time_after(jiffies, timeout)) {
557 goto timeout;
560 /* wait for equaliser full convergence */
561 timeout = jiffies + msecs_to_jiffies(delay);
562 while (time_before(jiffies, timeout)) {
563 msleep(10);
565 if (stv0297_readreg(state, 0x82) & 0x08) {
566 break;
569 if (time_after(jiffies, timeout)) {
570 goto timeout;
573 /* disable sweep */
574 stv0297_writereg_mask(state, 0x6a, 1, 0);
575 stv0297_writereg_mask(state, 0x88, 8, 0);
577 /* wait for main lock */
578 timeout = jiffies + msecs_to_jiffies(20);
579 while (time_before(jiffies, timeout)) {
580 msleep(10);
582 if (stv0297_readreg(state, 0xDF) & 0x80) {
583 break;
586 if (time_after(jiffies, timeout)) {
587 goto timeout;
589 msleep(100);
591 /* is it still locked after that delay? */
592 if (!(stv0297_readreg(state, 0xDF) & 0x80)) {
593 goto timeout;
596 /* success!! */
597 stv0297_writereg_mask(state, 0x5a, 0x40, 0x00);
598 state->base_freq = p->frequency;
599 return 0;
601 timeout:
602 stv0297_writereg_mask(state, 0x6a, 0x01, 0x00);
603 return 0;
606 static int stv0297_get_frontend(struct dvb_frontend *fe,
607 struct dtv_frontend_properties *p)
609 struct stv0297_state *state = fe->demodulator_priv;
610 int reg_00, reg_83;
612 reg_00 = stv0297_readreg(state, 0x00);
613 reg_83 = stv0297_readreg(state, 0x83);
615 p->frequency = state->base_freq;
616 p->inversion = (reg_83 & 0x08) ? INVERSION_ON : INVERSION_OFF;
617 if (state->config->invert)
618 p->inversion = (p->inversion == INVERSION_ON) ? INVERSION_OFF : INVERSION_ON;
619 p->symbol_rate = stv0297_get_symbolrate(state) * 1000;
620 p->fec_inner = FEC_NONE;
622 switch ((reg_00 >> 4) & 0x7) {
623 case 0:
624 p->modulation = QAM_16;
625 break;
626 case 1:
627 p->modulation = QAM_32;
628 break;
629 case 2:
630 p->modulation = QAM_128;
631 break;
632 case 3:
633 p->modulation = QAM_256;
634 break;
635 case 4:
636 p->modulation = QAM_64;
637 break;
640 return 0;
643 static void stv0297_release(struct dvb_frontend *fe)
645 struct stv0297_state *state = fe->demodulator_priv;
646 kfree(state);
649 static const struct dvb_frontend_ops stv0297_ops;
651 struct dvb_frontend *stv0297_attach(const struct stv0297_config *config,
652 struct i2c_adapter *i2c)
654 struct stv0297_state *state = NULL;
656 /* allocate memory for the internal state */
657 state = kzalloc(sizeof(struct stv0297_state), GFP_KERNEL);
658 if (state == NULL)
659 goto error;
661 /* setup the state */
662 state->config = config;
663 state->i2c = i2c;
664 state->last_ber = 0;
665 state->base_freq = 0;
667 /* check if the demod is there */
668 if ((stv0297_readreg(state, 0x80) & 0x70) != 0x20)
669 goto error;
671 /* create dvb_frontend */
672 memcpy(&state->frontend.ops, &stv0297_ops, sizeof(struct dvb_frontend_ops));
673 state->frontend.demodulator_priv = state;
674 return &state->frontend;
676 error:
677 kfree(state);
678 return NULL;
681 static const struct dvb_frontend_ops stv0297_ops = {
682 .delsys = { SYS_DVBC_ANNEX_A },
683 .info = {
684 .name = "ST STV0297 DVB-C",
685 .frequency_min_hz = 47 * MHz,
686 .frequency_max_hz = 862 * MHz,
687 .frequency_stepsize_hz = 62500,
688 .symbol_rate_min = 870000,
689 .symbol_rate_max = 11700000,
690 .caps = FE_CAN_QAM_16 | FE_CAN_QAM_32 | FE_CAN_QAM_64 |
691 FE_CAN_QAM_128 | FE_CAN_QAM_256 | FE_CAN_FEC_AUTO},
693 .release = stv0297_release,
695 .init = stv0297_init,
696 .sleep = stv0297_sleep,
697 .i2c_gate_ctrl = stv0297_i2c_gate_ctrl,
699 .set_frontend = stv0297_set_frontend,
700 .get_frontend = stv0297_get_frontend,
702 .read_status = stv0297_read_status,
703 .read_ber = stv0297_read_ber,
704 .read_signal_strength = stv0297_read_signal_strength,
705 .read_snr = stv0297_read_snr,
706 .read_ucblocks = stv0297_read_ucblocks,
709 MODULE_DESCRIPTION("ST STV0297 DVB-C Demodulator driver");
710 MODULE_AUTHOR("Dennis Noermann and Andrew de Quincey");
711 MODULE_LICENSE("GPL");
713 EXPORT_SYMBOL(stv0297_attach);