Linux 4.16.11
[linux/fpc-iii.git] / drivers / media / dvb-frontends / nxt6000.c
blob109a635d166a867d81f2dc6551a5a76322ece0be
1 /*
2 NxtWave Communications - NXT6000 demodulator driver
4 Copyright (C) 2002-2003 Florian Schirmer <jolt@tuxbox.org>
5 Copyright (C) 2003 Paul Andreassen <paul@andreassen.com.au>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24 #include <linux/init.h>
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/string.h>
28 #include <linux/slab.h>
30 #include <media/dvb_frontend.h>
31 #include "nxt6000_priv.h"
32 #include "nxt6000.h"
36 struct nxt6000_state {
37 struct i2c_adapter* i2c;
38 /* configuration settings */
39 const struct nxt6000_config* config;
40 struct dvb_frontend frontend;
43 static int debug;
44 #define dprintk(fmt, arg...) do { \
45 if (debug) \
46 printk(KERN_DEBUG pr_fmt("%s: " fmt), \
47 __func__, ##arg); \
48 } while (0)
50 static int nxt6000_writereg(struct nxt6000_state* state, u8 reg, u8 data)
52 u8 buf[] = { reg, data };
53 struct i2c_msg msg = {.addr = state->config->demod_address,.flags = 0,.buf = buf,.len = 2 };
54 int ret;
56 if ((ret = i2c_transfer(state->i2c, &msg, 1)) != 1)
57 dprintk("nxt6000: nxt6000_write error (reg: 0x%02X, data: 0x%02X, ret: %d)\n", reg, data, ret);
59 return (ret != 1) ? -EIO : 0;
62 static u8 nxt6000_readreg(struct nxt6000_state* state, u8 reg)
64 int ret;
65 u8 b0[] = { reg };
66 u8 b1[] = { 0 };
67 struct i2c_msg msgs[] = {
68 {.addr = state->config->demod_address,.flags = 0,.buf = b0,.len = 1},
69 {.addr = state->config->demod_address,.flags = I2C_M_RD,.buf = b1,.len = 1}
72 ret = i2c_transfer(state->i2c, msgs, 2);
74 if (ret != 2)
75 dprintk("nxt6000: nxt6000_read error (reg: 0x%02X, ret: %d)\n", reg, ret);
77 return b1[0];
80 static void nxt6000_reset(struct nxt6000_state* state)
82 u8 val;
84 val = nxt6000_readreg(state, OFDM_COR_CTL);
86 nxt6000_writereg(state, OFDM_COR_CTL, val & ~COREACT);
87 nxt6000_writereg(state, OFDM_COR_CTL, val | COREACT);
90 static int nxt6000_set_bandwidth(struct nxt6000_state *state, u32 bandwidth)
92 u16 nominal_rate;
93 int result;
95 switch (bandwidth) {
96 case 6000000:
97 nominal_rate = 0x55B7;
98 break;
100 case 7000000:
101 nominal_rate = 0x6400;
102 break;
104 case 8000000:
105 nominal_rate = 0x7249;
106 break;
108 default:
109 return -EINVAL;
112 if ((result = nxt6000_writereg(state, OFDM_TRL_NOMINALRATE_1, nominal_rate & 0xFF)) < 0)
113 return result;
115 return nxt6000_writereg(state, OFDM_TRL_NOMINALRATE_2, (nominal_rate >> 8) & 0xFF);
118 static int nxt6000_set_guard_interval(struct nxt6000_state *state,
119 enum fe_guard_interval guard_interval)
121 switch (guard_interval) {
123 case GUARD_INTERVAL_1_32:
124 return nxt6000_writereg(state, OFDM_COR_MODEGUARD, 0x00 | (nxt6000_readreg(state, OFDM_COR_MODEGUARD) & ~0x03));
126 case GUARD_INTERVAL_1_16:
127 return nxt6000_writereg(state, OFDM_COR_MODEGUARD, 0x01 | (nxt6000_readreg(state, OFDM_COR_MODEGUARD) & ~0x03));
129 case GUARD_INTERVAL_AUTO:
130 case GUARD_INTERVAL_1_8:
131 return nxt6000_writereg(state, OFDM_COR_MODEGUARD, 0x02 | (nxt6000_readreg(state, OFDM_COR_MODEGUARD) & ~0x03));
133 case GUARD_INTERVAL_1_4:
134 return nxt6000_writereg(state, OFDM_COR_MODEGUARD, 0x03 | (nxt6000_readreg(state, OFDM_COR_MODEGUARD) & ~0x03));
136 default:
137 return -EINVAL;
141 static int nxt6000_set_inversion(struct nxt6000_state *state,
142 enum fe_spectral_inversion inversion)
144 switch (inversion) {
146 case INVERSION_OFF:
147 return nxt6000_writereg(state, OFDM_ITB_CTL, 0x00);
149 case INVERSION_ON:
150 return nxt6000_writereg(state, OFDM_ITB_CTL, ITBINV);
152 default:
153 return -EINVAL;
158 static int
159 nxt6000_set_transmission_mode(struct nxt6000_state *state,
160 enum fe_transmit_mode transmission_mode)
162 int result;
164 switch (transmission_mode) {
166 case TRANSMISSION_MODE_2K:
167 if ((result = nxt6000_writereg(state, EN_DMD_RACQ, 0x00 | (nxt6000_readreg(state, EN_DMD_RACQ) & ~0x03))) < 0)
168 return result;
170 return nxt6000_writereg(state, OFDM_COR_MODEGUARD, (0x00 << 2) | (nxt6000_readreg(state, OFDM_COR_MODEGUARD) & ~0x04));
172 case TRANSMISSION_MODE_8K:
173 case TRANSMISSION_MODE_AUTO:
174 if ((result = nxt6000_writereg(state, EN_DMD_RACQ, 0x02 | (nxt6000_readreg(state, EN_DMD_RACQ) & ~0x03))) < 0)
175 return result;
177 return nxt6000_writereg(state, OFDM_COR_MODEGUARD, (0x01 << 2) | (nxt6000_readreg(state, OFDM_COR_MODEGUARD) & ~0x04));
179 default:
180 return -EINVAL;
185 static void nxt6000_setup(struct dvb_frontend* fe)
187 struct nxt6000_state* state = fe->demodulator_priv;
189 nxt6000_writereg(state, RS_COR_SYNC_PARAM, SYNC_PARAM);
190 nxt6000_writereg(state, BER_CTRL, /*(1 << 2) | */ (0x01 << 1) | 0x01);
191 nxt6000_writereg(state, VIT_BERTIME_2, 0x00); // BER Timer = 0x000200 * 256 = 131072 bits
192 nxt6000_writereg(state, VIT_BERTIME_1, 0x02); //
193 nxt6000_writereg(state, VIT_BERTIME_0, 0x00); //
194 nxt6000_writereg(state, VIT_COR_INTEN, 0x98); // Enable BER interrupts
195 nxt6000_writereg(state, VIT_COR_CTL, 0x82); // Enable BER measurement
196 nxt6000_writereg(state, VIT_COR_CTL, VIT_COR_RESYNC | 0x02 );
197 nxt6000_writereg(state, OFDM_COR_CTL, (0x01 << 5) | (nxt6000_readreg(state, OFDM_COR_CTL) & 0x0F));
198 nxt6000_writereg(state, OFDM_COR_MODEGUARD, FORCEMODE8K | 0x02);
199 nxt6000_writereg(state, OFDM_AGC_CTL, AGCLAST | INITIAL_AGC_BW);
200 nxt6000_writereg(state, OFDM_ITB_FREQ_1, 0x06);
201 nxt6000_writereg(state, OFDM_ITB_FREQ_2, 0x31);
202 nxt6000_writereg(state, OFDM_CAS_CTL, (0x01 << 7) | (0x02 << 3) | 0x04);
203 nxt6000_writereg(state, CAS_FREQ, 0xBB); /* CHECKME */
204 nxt6000_writereg(state, OFDM_SYR_CTL, 1 << 2);
205 nxt6000_writereg(state, OFDM_PPM_CTL_1, PPM256);
206 nxt6000_writereg(state, OFDM_TRL_NOMINALRATE_1, 0x49);
207 nxt6000_writereg(state, OFDM_TRL_NOMINALRATE_2, 0x72);
208 nxt6000_writereg(state, ANALOG_CONTROL_0, 1 << 5);
209 nxt6000_writereg(state, EN_DMD_RACQ, (1 << 7) | (3 << 4) | 2);
210 nxt6000_writereg(state, DIAG_CONFIG, TB_SET);
212 if (state->config->clock_inversion)
213 nxt6000_writereg(state, SUB_DIAG_MODE_SEL, CLKINVERSION);
214 else
215 nxt6000_writereg(state, SUB_DIAG_MODE_SEL, 0);
217 nxt6000_writereg(state, TS_FORMAT, 0);
220 static void nxt6000_dump_status(struct nxt6000_state *state)
222 u8 val;
224 #if 0
225 pr_info("RS_COR_STAT: 0x%02X\n",
226 nxt6000_readreg(fe, RS_COR_STAT));
227 pr_info("VIT_SYNC_STATUS: 0x%02X\n",
228 nxt6000_readreg(fe, VIT_SYNC_STATUS));
229 pr_info("OFDM_COR_STAT: 0x%02X\n",
230 nxt6000_readreg(fe, OFDM_COR_STAT));
231 pr_info("OFDM_SYR_STAT: 0x%02X\n",
232 nxt6000_readreg(fe, OFDM_SYR_STAT));
233 pr_info("OFDM_TPS_RCVD_1: 0x%02X\n",
234 nxt6000_readreg(fe, OFDM_TPS_RCVD_1));
235 pr_info("OFDM_TPS_RCVD_2: 0x%02X\n",
236 nxt6000_readreg(fe, OFDM_TPS_RCVD_2));
237 pr_info("OFDM_TPS_RCVD_3: 0x%02X\n",
238 nxt6000_readreg(fe, OFDM_TPS_RCVD_3));
239 pr_info("OFDM_TPS_RCVD_4: 0x%02X\n",
240 nxt6000_readreg(fe, OFDM_TPS_RCVD_4));
241 pr_info("OFDM_TPS_RESERVED_1: 0x%02X\n",
242 nxt6000_readreg(fe, OFDM_TPS_RESERVED_1));
243 pr_info("OFDM_TPS_RESERVED_2: 0x%02X\n",
244 nxt6000_readreg(fe, OFDM_TPS_RESERVED_2));
245 #endif
246 pr_info("NXT6000 status:");
248 val = nxt6000_readreg(state, RS_COR_STAT);
250 pr_cont(" DATA DESCR LOCK: %d,", val & 0x01);
251 pr_cont(" DATA SYNC LOCK: %d,", (val >> 1) & 0x01);
253 val = nxt6000_readreg(state, VIT_SYNC_STATUS);
255 pr_cont(" VITERBI LOCK: %d,", (val >> 7) & 0x01);
257 switch ((val >> 4) & 0x07) {
259 case 0x00:
260 pr_cont(" VITERBI CODERATE: 1/2,");
261 break;
263 case 0x01:
264 pr_cont(" VITERBI CODERATE: 2/3,");
265 break;
267 case 0x02:
268 pr_cont(" VITERBI CODERATE: 3/4,");
269 break;
271 case 0x03:
272 pr_cont(" VITERBI CODERATE: 5/6,");
273 break;
275 case 0x04:
276 pr_cont(" VITERBI CODERATE: 7/8,");
277 break;
279 default:
280 pr_cont(" VITERBI CODERATE: Reserved,");
284 val = nxt6000_readreg(state, OFDM_COR_STAT);
286 pr_cont(" CHCTrack: %d,", (val >> 7) & 0x01);
287 pr_cont(" TPSLock: %d,", (val >> 6) & 0x01);
288 pr_cont(" SYRLock: %d,", (val >> 5) & 0x01);
289 pr_cont(" AGCLock: %d,", (val >> 4) & 0x01);
291 switch (val & 0x0F) {
293 case 0x00:
294 pr_cont(" CoreState: IDLE,");
295 break;
297 case 0x02:
298 pr_cont(" CoreState: WAIT_AGC,");
299 break;
301 case 0x03:
302 pr_cont(" CoreState: WAIT_SYR,");
303 break;
305 case 0x04:
306 pr_cont(" CoreState: WAIT_PPM,");
307 break;
309 case 0x01:
310 pr_cont(" CoreState: WAIT_TRL,");
311 break;
313 case 0x05:
314 pr_cont(" CoreState: WAIT_TPS,");
315 break;
317 case 0x06:
318 pr_cont(" CoreState: MONITOR_TPS,");
319 break;
321 default:
322 pr_cont(" CoreState: Reserved,");
326 val = nxt6000_readreg(state, OFDM_SYR_STAT);
328 pr_cont(" SYRLock: %d,", (val >> 4) & 0x01);
329 pr_cont(" SYRMode: %s,", (val >> 2) & 0x01 ? "8K" : "2K");
331 switch ((val >> 4) & 0x03) {
333 case 0x00:
334 pr_cont(" SYRGuard: 1/32,");
335 break;
337 case 0x01:
338 pr_cont(" SYRGuard: 1/16,");
339 break;
341 case 0x02:
342 pr_cont(" SYRGuard: 1/8,");
343 break;
345 case 0x03:
346 pr_cont(" SYRGuard: 1/4,");
347 break;
350 val = nxt6000_readreg(state, OFDM_TPS_RCVD_3);
352 switch ((val >> 4) & 0x07) {
354 case 0x00:
355 pr_cont(" TPSLP: 1/2,");
356 break;
358 case 0x01:
359 pr_cont(" TPSLP: 2/3,");
360 break;
362 case 0x02:
363 pr_cont(" TPSLP: 3/4,");
364 break;
366 case 0x03:
367 pr_cont(" TPSLP: 5/6,");
368 break;
370 case 0x04:
371 pr_cont(" TPSLP: 7/8,");
372 break;
374 default:
375 pr_cont(" TPSLP: Reserved,");
379 switch (val & 0x07) {
381 case 0x00:
382 pr_cont(" TPSHP: 1/2,");
383 break;
385 case 0x01:
386 pr_cont(" TPSHP: 2/3,");
387 break;
389 case 0x02:
390 pr_cont(" TPSHP: 3/4,");
391 break;
393 case 0x03:
394 pr_cont(" TPSHP: 5/6,");
395 break;
397 case 0x04:
398 pr_cont(" TPSHP: 7/8,");
399 break;
401 default:
402 pr_cont(" TPSHP: Reserved,");
406 val = nxt6000_readreg(state, OFDM_TPS_RCVD_4);
408 pr_cont(" TPSMode: %s,", val & 0x01 ? "8K" : "2K");
410 switch ((val >> 4) & 0x03) {
412 case 0x00:
413 pr_cont(" TPSGuard: 1/32,");
414 break;
416 case 0x01:
417 pr_cont(" TPSGuard: 1/16,");
418 break;
420 case 0x02:
421 pr_cont(" TPSGuard: 1/8,");
422 break;
424 case 0x03:
425 pr_cont(" TPSGuard: 1/4,");
426 break;
430 /* Strange magic required to gain access to RF_AGC_STATUS */
431 nxt6000_readreg(state, RF_AGC_VAL_1);
432 val = nxt6000_readreg(state, RF_AGC_STATUS);
433 val = nxt6000_readreg(state, RF_AGC_STATUS);
435 pr_cont(" RF AGC LOCK: %d,", (val >> 4) & 0x01);
436 pr_cont("\n");
439 static int nxt6000_read_status(struct dvb_frontend *fe, enum fe_status *status)
441 u8 core_status;
442 struct nxt6000_state* state = fe->demodulator_priv;
444 *status = 0;
446 core_status = nxt6000_readreg(state, OFDM_COR_STAT);
448 if (core_status & AGCLOCKED)
449 *status |= FE_HAS_SIGNAL;
451 if (nxt6000_readreg(state, OFDM_SYR_STAT) & GI14_SYR_LOCK)
452 *status |= FE_HAS_CARRIER;
454 if (nxt6000_readreg(state, VIT_SYNC_STATUS) & VITINSYNC)
455 *status |= FE_HAS_VITERBI;
457 if (nxt6000_readreg(state, RS_COR_STAT) & RSCORESTATUS)
458 *status |= FE_HAS_SYNC;
460 if ((core_status & TPSLOCKED) && (*status == (FE_HAS_SIGNAL | FE_HAS_CARRIER | FE_HAS_VITERBI | FE_HAS_SYNC)))
461 *status |= FE_HAS_LOCK;
463 if (debug)
464 nxt6000_dump_status(state);
466 return 0;
469 static int nxt6000_init(struct dvb_frontend* fe)
471 struct nxt6000_state* state = fe->demodulator_priv;
473 nxt6000_reset(state);
474 nxt6000_setup(fe);
476 return 0;
479 static int nxt6000_set_frontend(struct dvb_frontend *fe)
481 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
482 struct nxt6000_state* state = fe->demodulator_priv;
483 int result;
485 if (fe->ops.tuner_ops.set_params) {
486 fe->ops.tuner_ops.set_params(fe);
487 if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 0);
490 result = nxt6000_set_bandwidth(state, p->bandwidth_hz);
491 if (result < 0)
492 return result;
494 result = nxt6000_set_guard_interval(state, p->guard_interval);
495 if (result < 0)
496 return result;
498 result = nxt6000_set_transmission_mode(state, p->transmission_mode);
499 if (result < 0)
500 return result;
502 result = nxt6000_set_inversion(state, p->inversion);
503 if (result < 0)
504 return result;
506 msleep(500);
507 return 0;
510 static void nxt6000_release(struct dvb_frontend* fe)
512 struct nxt6000_state* state = fe->demodulator_priv;
513 kfree(state);
516 static int nxt6000_read_snr(struct dvb_frontend* fe, u16* snr)
518 struct nxt6000_state* state = fe->demodulator_priv;
520 *snr = nxt6000_readreg( state, OFDM_CHC_SNR) / 8;
522 return 0;
525 static int nxt6000_read_ber(struct dvb_frontend* fe, u32* ber)
527 struct nxt6000_state* state = fe->demodulator_priv;
529 nxt6000_writereg( state, VIT_COR_INTSTAT, 0x18 );
531 *ber = (nxt6000_readreg( state, VIT_BER_1 ) << 8 ) |
532 nxt6000_readreg( state, VIT_BER_0 );
534 nxt6000_writereg( state, VIT_COR_INTSTAT, 0x18); // Clear BER Done interrupts
536 return 0;
539 static int nxt6000_read_signal_strength(struct dvb_frontend* fe, u16* signal_strength)
541 struct nxt6000_state* state = fe->demodulator_priv;
543 *signal_strength = (short) (511 -
544 (nxt6000_readreg(state, AGC_GAIN_1) +
545 ((nxt6000_readreg(state, AGC_GAIN_2) & 0x03) << 8)));
547 return 0;
550 static int nxt6000_fe_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings *tune)
552 tune->min_delay_ms = 500;
553 return 0;
556 static int nxt6000_i2c_gate_ctrl(struct dvb_frontend* fe, int enable)
558 struct nxt6000_state* state = fe->demodulator_priv;
560 if (enable) {
561 return nxt6000_writereg(state, ENABLE_TUNER_IIC, 0x01);
562 } else {
563 return nxt6000_writereg(state, ENABLE_TUNER_IIC, 0x00);
567 static const struct dvb_frontend_ops nxt6000_ops;
569 struct dvb_frontend* nxt6000_attach(const struct nxt6000_config* config,
570 struct i2c_adapter* i2c)
572 struct nxt6000_state* state = NULL;
574 /* allocate memory for the internal state */
575 state = kzalloc(sizeof(struct nxt6000_state), GFP_KERNEL);
576 if (state == NULL) goto error;
578 /* setup the state */
579 state->config = config;
580 state->i2c = i2c;
582 /* check if the demod is there */
583 if (nxt6000_readreg(state, OFDM_MSC_REV) != NXT6000ASICDEVICE) goto error;
585 /* create dvb_frontend */
586 memcpy(&state->frontend.ops, &nxt6000_ops, sizeof(struct dvb_frontend_ops));
587 state->frontend.demodulator_priv = state;
588 return &state->frontend;
590 error:
591 kfree(state);
592 return NULL;
595 static const struct dvb_frontend_ops nxt6000_ops = {
596 .delsys = { SYS_DVBT },
597 .info = {
598 .name = "NxtWave NXT6000 DVB-T",
599 .frequency_min = 0,
600 .frequency_max = 863250000,
601 .frequency_stepsize = 62500,
602 /*.frequency_tolerance = *//* FIXME: 12% of SR */
603 .symbol_rate_min = 0, /* FIXME */
604 .symbol_rate_max = 9360000, /* FIXME */
605 .symbol_rate_tolerance = 4000,
606 .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
607 FE_CAN_FEC_4_5 | FE_CAN_FEC_5_6 | FE_CAN_FEC_6_7 |
608 FE_CAN_FEC_7_8 | FE_CAN_FEC_8_9 | FE_CAN_FEC_AUTO |
609 FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
610 FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_GUARD_INTERVAL_AUTO |
611 FE_CAN_HIERARCHY_AUTO,
614 .release = nxt6000_release,
616 .init = nxt6000_init,
617 .i2c_gate_ctrl = nxt6000_i2c_gate_ctrl,
619 .get_tune_settings = nxt6000_fe_get_tune_settings,
621 .set_frontend = nxt6000_set_frontend,
623 .read_status = nxt6000_read_status,
624 .read_ber = nxt6000_read_ber,
625 .read_signal_strength = nxt6000_read_signal_strength,
626 .read_snr = nxt6000_read_snr,
629 module_param(debug, int, 0644);
630 MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
632 MODULE_DESCRIPTION("NxtWave NXT6000 DVB-T demodulator driver");
633 MODULE_AUTHOR("Florian Schirmer");
634 MODULE_LICENSE("GPL");
636 EXPORT_SYMBOL(nxt6000_attach);