2 * mxl111sf-demod.c - driver for the MaxLinear MXL111SF DVB-T demodulator
4 * Copyright (C) 2010-2014 Michael Krufky <mkrufky@linuxtv.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
17 #include "mxl111sf-demod.h"
18 #include "mxl111sf-reg.h"
21 static int mxl111sf_demod_debug
;
22 module_param_named(debug
, mxl111sf_demod_debug
, int, 0644);
23 MODULE_PARM_DESC(debug
, "set debugging level (1=info (or-able)).");
25 #define mxl_dbg(fmt, arg...) \
26 if (mxl111sf_demod_debug) \
27 mxl_printk(KERN_DEBUG, fmt, ##arg)
29 /* ------------------------------------------------------------------------ */
31 struct mxl111sf_demod_state
{
32 struct mxl111sf_state
*mxl_state
;
34 const struct mxl111sf_demod_config
*cfg
;
36 struct dvb_frontend fe
;
39 /* ------------------------------------------------------------------------ */
41 static int mxl111sf_demod_read_reg(struct mxl111sf_demod_state
*state
,
44 return (state
->cfg
->read_reg
) ?
45 state
->cfg
->read_reg(state
->mxl_state
, addr
, data
) :
49 static int mxl111sf_demod_write_reg(struct mxl111sf_demod_state
*state
,
52 return (state
->cfg
->write_reg
) ?
53 state
->cfg
->write_reg(state
->mxl_state
, addr
, data
) :
58 int mxl111sf_demod_program_regs(struct mxl111sf_demod_state
*state
,
59 struct mxl111sf_reg_ctrl_info
*ctrl_reg_info
)
61 return (state
->cfg
->program_regs
) ?
62 state
->cfg
->program_regs(state
->mxl_state
, ctrl_reg_info
) :
66 /* ------------------------------------------------------------------------ */
70 int mxl1x1sf_demod_get_tps_code_rate(struct mxl111sf_demod_state
*state
,
71 enum fe_code_rate
*code_rate
)
74 int ret
= mxl111sf_demod_read_reg(state
, V6_CODE_RATE_TPS_REG
, &val
);
75 /* bit<2:0> - 000:1/2, 001:2/3, 010:3/4, 011:5/6, 100:7/8 */
79 switch (val
& V6_CODE_RATE_TPS_MASK
) {
101 int mxl1x1sf_demod_get_tps_modulation(struct mxl111sf_demod_state
*state
,
102 enum fe_modulation
*modulation
)
105 int ret
= mxl111sf_demod_read_reg(state
, V6_MODORDER_TPS_REG
, &val
);
106 /* Constellation, 00 : QPSK, 01 : 16QAM, 10:64QAM */
110 switch ((val
& V6_PARAM_CONSTELLATION_MASK
) >> 4) {
115 *modulation
= QAM_16
;
118 *modulation
= QAM_64
;
126 int mxl1x1sf_demod_get_tps_guard_fft_mode(struct mxl111sf_demod_state
*state
,
127 enum fe_transmit_mode
*fft_mode
)
130 int ret
= mxl111sf_demod_read_reg(state
, V6_MODE_TPS_REG
, &val
);
131 /* FFT Mode, 00:2K, 01:8K, 10:4K */
135 switch ((val
& V6_PARAM_FFT_MODE_MASK
) >> 2) {
137 *fft_mode
= TRANSMISSION_MODE_2K
;
140 *fft_mode
= TRANSMISSION_MODE_8K
;
143 *fft_mode
= TRANSMISSION_MODE_4K
;
151 int mxl1x1sf_demod_get_tps_guard_interval(struct mxl111sf_demod_state
*state
,
152 enum fe_guard_interval
*guard
)
155 int ret
= mxl111sf_demod_read_reg(state
, V6_CP_TPS_REG
, &val
);
156 /* 00:1/32, 01:1/16, 10:1/8, 11:1/4 */
160 switch ((val
& V6_PARAM_GI_MASK
) >> 4) {
162 *guard
= GUARD_INTERVAL_1_32
;
165 *guard
= GUARD_INTERVAL_1_16
;
168 *guard
= GUARD_INTERVAL_1_8
;
171 *guard
= GUARD_INTERVAL_1_4
;
179 int mxl1x1sf_demod_get_tps_hierarchy(struct mxl111sf_demod_state
*state
,
180 enum fe_hierarchy
*hierarchy
)
183 int ret
= mxl111sf_demod_read_reg(state
, V6_TPS_HIERACHY_REG
, &val
);
184 /* bit<6:4> - 000:Non hierarchy, 001:1, 010:2, 011:4 */
188 switch ((val
& V6_TPS_HIERARCHY_INFO_MASK
) >> 6) {
190 *hierarchy
= HIERARCHY_NONE
;
193 *hierarchy
= HIERARCHY_1
;
196 *hierarchy
= HIERARCHY_2
;
199 *hierarchy
= HIERARCHY_4
;
206 /* ------------------------------------------------------------------------ */
210 int mxl1x1sf_demod_get_sync_lock_status(struct mxl111sf_demod_state
*state
,
214 int ret
= mxl111sf_demod_read_reg(state
, V6_SYNC_LOCK_REG
, &val
);
217 *sync_lock
= (val
& SYNC_LOCK_MASK
) >> 4;
223 int mxl1x1sf_demod_get_rs_lock_status(struct mxl111sf_demod_state
*state
,
227 int ret
= mxl111sf_demod_read_reg(state
, V6_RS_LOCK_DET_REG
, &val
);
230 *rs_lock
= (val
& RS_LOCK_DET_MASK
) >> 3;
236 int mxl1x1sf_demod_get_tps_lock_status(struct mxl111sf_demod_state
*state
,
240 int ret
= mxl111sf_demod_read_reg(state
, V6_TPS_LOCK_REG
, &val
);
243 *tps_lock
= (val
& V6_PARAM_TPS_LOCK_MASK
) >> 6;
249 int mxl1x1sf_demod_get_fec_lock_status(struct mxl111sf_demod_state
*state
,
253 int ret
= mxl111sf_demod_read_reg(state
, V6_IRQ_STATUS_REG
, &val
);
256 *fec_lock
= (val
& IRQ_MASK_FEC_LOCK
) >> 4;
263 int mxl1x1sf_demod_get_cp_lock_status(struct mxl111sf_demod_state
*state
,
267 int ret
= mxl111sf_demod_read_reg(state
, V6_CP_LOCK_DET_REG
, &val
);
270 *cp_lock
= (val
& V6_CP_LOCK_DET_MASK
) >> 2;
276 static int mxl1x1sf_demod_reset_irq_status(struct mxl111sf_demod_state
*state
)
278 return mxl111sf_demod_write_reg(state
, 0x0e, 0xff);
281 /* ------------------------------------------------------------------------ */
283 static int mxl111sf_demod_set_frontend(struct dvb_frontend
*fe
)
285 struct mxl111sf_demod_state
*state
= fe
->demodulator_priv
;
288 struct mxl111sf_reg_ctrl_info phy_pll_patch
[] = {
289 {0x00, 0xff, 0x01}, /* change page to 1 */
294 {0x00, 0xff, 0x00}, /* change page to 0 */
300 if (fe
->ops
.tuner_ops
.set_params
) {
301 ret
= fe
->ops
.tuner_ops
.set_params(fe
);
306 ret
= mxl111sf_demod_program_regs(state
, phy_pll_patch
);
309 ret
= mxl1x1sf_demod_reset_irq_status(state
);
316 /* ------------------------------------------------------------------------ */
319 /* resets TS Packet error count */
320 /* After setting 7th bit of V5_PER_COUNT_RESET_REG, it should be reset to 0. */
322 int mxl1x1sf_demod_reset_packet_error_count(struct mxl111sf_demod_state
*state
)
324 struct mxl111sf_reg_ctrl_info reset_per_count
[] = {
329 return mxl111sf_demod_program_regs(state
, reset_per_count
);
333 /* returns TS Packet error count */
334 /* PER Count = FEC_PER_COUNT * (2 ** (FEC_PER_SCALE * 4)) */
335 static int mxl111sf_demod_read_ucblocks(struct dvb_frontend
*fe
, u32
*ucblocks
)
337 struct mxl111sf_demod_state
*state
= fe
->demodulator_priv
;
338 u32 fec_per_count
, fec_per_scale
;
344 /* FEC_PER_COUNT Register */
345 ret
= mxl111sf_demod_read_reg(state
, V6_FEC_PER_COUNT_REG
, &val
);
351 /* FEC_PER_SCALE Register */
352 ret
= mxl111sf_demod_read_reg(state
, V6_FEC_PER_SCALE_REG
, &val
);
356 val
&= V6_FEC_PER_SCALE_MASK
;
359 fec_per_scale
= 1 << val
;
361 fec_per_count
*= fec_per_scale
;
363 *ucblocks
= fec_per_count
;
368 #ifdef MXL111SF_DEMOD_ENABLE_CALCULATIONS
369 /* FIXME: leaving this enabled breaks the build on some architectures,
370 * and we shouldn't have any floating point math in the kernel, anyway.
372 * These macros need to be re-written, but it's harmless to simply
373 * return zero for now. */
374 #define CALCULATE_BER(avg_errors, count) \
375 ((u32)(avg_errors * 4)/(count*64*188*8))
376 #define CALCULATE_SNR(data) \
377 ((u32)((10 * (u32)data / 64) - 2.5))
379 #define CALCULATE_BER(avg_errors, count) 0
380 #define CALCULATE_SNR(data) 0
383 static int mxl111sf_demod_read_ber(struct dvb_frontend
*fe
, u32
*ber
)
385 struct mxl111sf_demod_state
*state
= fe
->demodulator_priv
;
391 ret
= mxl111sf_demod_read_reg(state
, V6_RS_AVG_ERRORS_LSB_REG
, &val1
);
394 ret
= mxl111sf_demod_read_reg(state
, V6_RS_AVG_ERRORS_MSB_REG
, &val2
);
397 ret
= mxl111sf_demod_read_reg(state
, V6_N_ACCUMULATE_REG
, &val3
);
401 *ber
= CALCULATE_BER((val1
| (val2
<< 8)), val3
);
406 static int mxl111sf_demod_calc_snr(struct mxl111sf_demod_state
*state
,
414 ret
= mxl111sf_demod_read_reg(state
, V6_SNR_RB_LSB_REG
, &val1
);
417 ret
= mxl111sf_demod_read_reg(state
, V6_SNR_RB_MSB_REG
, &val2
);
421 *snr
= CALCULATE_SNR(val1
| ((val2
& 0x03) << 8));
426 static int mxl111sf_demod_read_snr(struct dvb_frontend
*fe
, u16
*snr
)
428 struct mxl111sf_demod_state
*state
= fe
->demodulator_priv
;
430 int ret
= mxl111sf_demod_calc_snr(state
, snr
);
434 *snr
/= 10; /* 0.1 dB */
439 static int mxl111sf_demod_read_status(struct dvb_frontend
*fe
,
440 enum fe_status
*status
)
442 struct mxl111sf_demod_state
*state
= fe
->demodulator_priv
;
443 int ret
, locked
, cr_lock
, sync_lock
, fec_lock
;
447 ret
= mxl1x1sf_demod_get_rs_lock_status(state
, &locked
);
450 ret
= mxl1x1sf_demod_get_tps_lock_status(state
, &cr_lock
);
453 ret
= mxl1x1sf_demod_get_sync_lock_status(state
, &sync_lock
);
456 ret
= mxl1x1sf_demod_get_fec_lock_status(state
, &fec_lock
);
461 *status
|= FE_HAS_SIGNAL
;
463 *status
|= FE_HAS_CARRIER
;
465 *status
|= FE_HAS_SYNC
;
466 if (fec_lock
) /* false positives? */
467 *status
|= FE_HAS_VITERBI
;
469 if ((locked
) && (cr_lock
) && (sync_lock
))
470 *status
|= FE_HAS_LOCK
;
475 static int mxl111sf_demod_read_signal_strength(struct dvb_frontend
*fe
,
476 u16
*signal_strength
)
478 struct mxl111sf_demod_state
*state
= fe
->demodulator_priv
;
479 enum fe_modulation modulation
;
483 ret
= mxl111sf_demod_calc_snr(state
, &snr
);
486 ret
= mxl1x1sf_demod_get_tps_modulation(state
, &modulation
);
490 switch (modulation
) {
492 *signal_strength
= (snr
>= 1300) ?
493 min(65535, snr
* 44) : snr
* 38;
496 *signal_strength
= (snr
>= 1500) ?
497 min(65535, snr
* 38) : snr
* 33;
500 *signal_strength
= (snr
>= 2000) ?
501 min(65535, snr
* 29) : snr
* 25;
504 *signal_strength
= 0;
511 static int mxl111sf_demod_get_frontend(struct dvb_frontend
*fe
,
512 struct dtv_frontend_properties
*p
)
514 struct mxl111sf_demod_state
*state
= fe
->demodulator_priv
;
518 p
->inversion
= /* FIXME */ ? INVERSION_ON
: INVERSION_OFF
;
520 if (fe
->ops
.tuner_ops
.get_bandwidth
)
521 fe
->ops
.tuner_ops
.get_bandwidth(fe
, &p
->bandwidth_hz
);
522 if (fe
->ops
.tuner_ops
.get_frequency
)
523 fe
->ops
.tuner_ops
.get_frequency(fe
, &p
->frequency
);
524 mxl1x1sf_demod_get_tps_code_rate(state
, &p
->code_rate_HP
);
525 mxl1x1sf_demod_get_tps_code_rate(state
, &p
->code_rate_LP
);
526 mxl1x1sf_demod_get_tps_modulation(state
, &p
->modulation
);
527 mxl1x1sf_demod_get_tps_guard_fft_mode(state
,
528 &p
->transmission_mode
);
529 mxl1x1sf_demod_get_tps_guard_interval(state
,
531 mxl1x1sf_demod_get_tps_hierarchy(state
,
538 int mxl111sf_demod_get_tune_settings(struct dvb_frontend
*fe
,
539 struct dvb_frontend_tune_settings
*tune
)
541 tune
->min_delay_ms
= 1000;
545 static void mxl111sf_demod_release(struct dvb_frontend
*fe
)
547 struct mxl111sf_demod_state
*state
= fe
->demodulator_priv
;
550 fe
->demodulator_priv
= NULL
;
553 static const struct dvb_frontend_ops mxl111sf_demod_ops
= {
554 .delsys
= { SYS_DVBT
},
556 .name
= "MaxLinear MxL111SF DVB-T demodulator",
557 .frequency_min
= 177000000,
558 .frequency_max
= 858000000,
559 .frequency_stepsize
= 166666,
560 .caps
= FE_CAN_FEC_1_2
| FE_CAN_FEC_2_3
| FE_CAN_FEC_3_4
|
561 FE_CAN_FEC_5_6
| FE_CAN_FEC_7_8
| FE_CAN_FEC_AUTO
|
562 FE_CAN_QPSK
| FE_CAN_QAM_16
| FE_CAN_QAM_64
|
564 FE_CAN_HIERARCHY_AUTO
| FE_CAN_GUARD_INTERVAL_AUTO
|
565 FE_CAN_TRANSMISSION_MODE_AUTO
| FE_CAN_RECOVER
567 .release
= mxl111sf_demod_release
,
569 .init
= mxl111sf_init
,
570 .i2c_gate_ctrl
= mxl111sf_i2c_gate_ctrl
,
572 .set_frontend
= mxl111sf_demod_set_frontend
,
573 .get_frontend
= mxl111sf_demod_get_frontend
,
574 .get_tune_settings
= mxl111sf_demod_get_tune_settings
,
575 .read_status
= mxl111sf_demod_read_status
,
576 .read_signal_strength
= mxl111sf_demod_read_signal_strength
,
577 .read_ber
= mxl111sf_demod_read_ber
,
578 .read_snr
= mxl111sf_demod_read_snr
,
579 .read_ucblocks
= mxl111sf_demod_read_ucblocks
,
582 struct dvb_frontend
*mxl111sf_demod_attach(struct mxl111sf_state
*mxl_state
,
583 const struct mxl111sf_demod_config
*cfg
)
585 struct mxl111sf_demod_state
*state
= NULL
;
589 state
= kzalloc(sizeof(struct mxl111sf_demod_state
), GFP_KERNEL
);
593 state
->mxl_state
= mxl_state
;
596 memcpy(&state
->fe
.ops
, &mxl111sf_demod_ops
,
597 sizeof(struct dvb_frontend_ops
));
599 state
->fe
.demodulator_priv
= state
;
602 EXPORT_SYMBOL_GPL(mxl111sf_demod_attach
);
604 MODULE_DESCRIPTION("MaxLinear MxL111SF DVB-T demodulator driver");
605 MODULE_AUTHOR("Michael Krufky <mkrufky@linuxtv.org>");
606 MODULE_LICENSE("GPL");
607 MODULE_VERSION("0.1");