2 * mxl111sf-demod.c - driver for the MaxLinear MXL111SF DVB-T demodulator
4 * Copyright (C) 2010 Michael Krufky <mkrufky@kernellabs.com>
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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include "mxl111sf-demod.h"
22 #include "mxl111sf-reg.h"
25 static int mxl111sf_demod_debug;
26 module_param_named(debug, mxl111sf_demod_debug, int, 0644);
27 MODULE_PARM_DESC(debug, "set debugging level (1=info (or-able)).");
29 #define mxl_dbg(fmt, arg...) \
30 if (mxl111sf_demod_debug) \
31 mxl_printk(KERN_DEBUG, fmt, ##arg)
33 /* ------------------------------------------------------------------------ */
35 struct mxl111sf_demod_state {
36 struct mxl111sf_state *mxl_state;
38 struct mxl111sf_demod_config *cfg;
40 struct dvb_frontend fe;
43 /* ------------------------------------------------------------------------ */
45 static int mxl111sf_demod_read_reg(struct mxl111sf_demod_state *state,
48 return (state->cfg->read_reg) ?
49 state->cfg->read_reg(state->mxl_state, addr, data) :
53 static int mxl111sf_demod_write_reg(struct mxl111sf_demod_state *state,
56 return (state->cfg->write_reg) ?
57 state->cfg->write_reg(state->mxl_state, addr, data) :
62 int mxl111sf_demod_program_regs(struct mxl111sf_demod_state *state,
63 struct mxl111sf_reg_ctrl_info *ctrl_reg_info)
65 return (state->cfg->program_regs) ?
66 state->cfg->program_regs(state->mxl_state, ctrl_reg_info) :
70 /* ------------------------------------------------------------------------ */
74 int mxl1x1sf_demod_get_tps_code_rate(struct mxl111sf_demod_state *state,
75 fe_code_rate_t *code_rate)
78 int ret = mxl111sf_demod_read_reg(state, V6_CODE_RATE_TPS_REG, &val);
79 /* bit<2:0> - 000:1/2, 001:2/3, 010:3/4, 011:5/6, 100:7/8 */
83 switch (val & V6_CODE_RATE_TPS_MASK) {
105 int mxl1x1sf_demod_get_tps_constellation(struct mxl111sf_demod_state *state,
106 fe_modulation_t *constellation)
109 int ret = mxl111sf_demod_read_reg(state, V6_MODORDER_TPS_REG, &val);
110 /* Constellation, 00 : QPSK, 01 : 16QAM, 10:64QAM */
114 switch ((val & V6_PARAM_CONSTELLATION_MASK) >> 4) {
116 *constellation = QPSK;
119 *constellation = QAM_16;
122 *constellation = QAM_64;
130 int mxl1x1sf_demod_get_tps_guard_fft_mode(struct mxl111sf_demod_state *state,
131 fe_transmit_mode_t *fft_mode)
134 int ret = mxl111sf_demod_read_reg(state, V6_MODE_TPS_REG, &val);
135 /* FFT Mode, 00:2K, 01:8K, 10:4K */
139 switch ((val & V6_PARAM_FFT_MODE_MASK) >> 2) {
141 *fft_mode = TRANSMISSION_MODE_2K;
144 *fft_mode = TRANSMISSION_MODE_8K;
147 *fft_mode = TRANSMISSION_MODE_4K;
155 int mxl1x1sf_demod_get_tps_guard_interval(struct mxl111sf_demod_state *state,
156 fe_guard_interval_t *guard)
159 int ret = mxl111sf_demod_read_reg(state, V6_CP_TPS_REG, &val);
160 /* 00:1/32, 01:1/16, 10:1/8, 11:1/4 */
164 switch ((val & V6_PARAM_GI_MASK) >> 4) {
166 *guard = GUARD_INTERVAL_1_32;
169 *guard = GUARD_INTERVAL_1_16;
172 *guard = GUARD_INTERVAL_1_8;
175 *guard = GUARD_INTERVAL_1_4;
183 int mxl1x1sf_demod_get_tps_hierarchy(struct mxl111sf_demod_state *state,
184 fe_hierarchy_t *hierarchy)
187 int ret = mxl111sf_demod_read_reg(state, V6_TPS_HIERACHY_REG, &val);
188 /* bit<6:4> - 000:Non hierarchy, 001:1, 010:2, 011:4 */
192 switch ((val & V6_TPS_HIERARCHY_INFO_MASK) >> 6) {
194 *hierarchy = HIERARCHY_NONE;
197 *hierarchy = HIERARCHY_1;
200 *hierarchy = HIERARCHY_2;
203 *hierarchy = HIERARCHY_4;
210 /* ------------------------------------------------------------------------ */
214 int mxl1x1sf_demod_get_sync_lock_status(struct mxl111sf_demod_state *state,
218 int ret = mxl111sf_demod_read_reg(state, V6_SYNC_LOCK_REG, &val);
221 *sync_lock = (val & SYNC_LOCK_MASK) >> 4;
227 int mxl1x1sf_demod_get_rs_lock_status(struct mxl111sf_demod_state *state,
231 int ret = mxl111sf_demod_read_reg(state, V6_RS_LOCK_DET_REG, &val);
234 *rs_lock = (val & RS_LOCK_DET_MASK) >> 3;
240 int mxl1x1sf_demod_get_tps_lock_status(struct mxl111sf_demod_state *state,
244 int ret = mxl111sf_demod_read_reg(state, V6_TPS_LOCK_REG, &val);
247 *tps_lock = (val & V6_PARAM_TPS_LOCK_MASK) >> 6;
253 int mxl1x1sf_demod_get_fec_lock_status(struct mxl111sf_demod_state *state,
257 int ret = mxl111sf_demod_read_reg(state, V6_IRQ_STATUS_REG, &val);
260 *fec_lock = (val & IRQ_MASK_FEC_LOCK) >> 4;
267 int mxl1x1sf_demod_get_cp_lock_status(struct mxl111sf_demod_state *state,
271 int ret = mxl111sf_demod_read_reg(state, V6_CP_LOCK_DET_REG, &val);
274 *cp_lock = (val & V6_CP_LOCK_DET_MASK) >> 2;
280 static int mxl1x1sf_demod_reset_irq_status(struct mxl111sf_demod_state *state)
282 return mxl111sf_demod_write_reg(state, 0x0e, 0xff);
285 /* ------------------------------------------------------------------------ */
287 static int mxl111sf_demod_set_frontend(struct dvb_frontend *fe,
288 struct dvb_frontend_parameters *param)
290 struct mxl111sf_demod_state *state = fe->demodulator_priv;
293 struct mxl111sf_reg_ctrl_info phy_pll_patch[] = {
294 {0x00, 0xff, 0x01}, /* change page to 1 */
299 {0x00, 0xff, 0x00}, /* change page to 0 */
305 if (fe->ops.tuner_ops.set_params) {
306 ret = fe->ops.tuner_ops.set_params(fe, param);
311 ret = mxl111sf_demod_program_regs(state, phy_pll_patch);
314 ret = mxl1x1sf_demod_reset_irq_status(state);
321 /* ------------------------------------------------------------------------ */
324 /* resets TS Packet error count */
325 /* After setting 7th bit of V5_PER_COUNT_RESET_REG, it should be reset to 0. */
327 int mxl1x1sf_demod_reset_packet_error_count(struct mxl111sf_demod_state *state)
329 struct mxl111sf_reg_ctrl_info reset_per_count[] = {
334 return mxl111sf_demod_program_regs(state, reset_per_count);
338 /* returns TS Packet error count */
339 /* PER Count = FEC_PER_COUNT * (2 ** (FEC_PER_SCALE * 4)) */
340 static int mxl111sf_demod_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
342 struct mxl111sf_demod_state *state = fe->demodulator_priv;
343 u32 fec_per_count, fec_per_scale;
349 /* FEC_PER_COUNT Register */
350 ret = mxl111sf_demod_read_reg(state, V6_FEC_PER_COUNT_REG, &val);
356 /* FEC_PER_SCALE Register */
357 ret = mxl111sf_demod_read_reg(state, V6_FEC_PER_SCALE_REG, &val);
361 val &= V6_FEC_PER_SCALE_MASK;
364 fec_per_scale = 1 << val;
366 fec_per_count *= fec_per_scale;
368 *ucblocks = fec_per_count;
373 #ifdef MXL111SF_DEMOD_ENABLE_CALCULATIONS
374 /* FIXME: leaving this enabled breaks the build on some architectures,
375 * and we shouldn't have any floating point math in the kernel, anyway.
377 * These macros need to be re-written, but it's harmless to simply
378 * return zero for now. */
379 #define CALCULATE_BER(avg_errors, count) \
380 ((u32)(avg_errors * 4)/(count*64*188*8))
381 #define CALCULATE_SNR(data) \
382 ((u32)((10 * (u32)data / 64) - 2.5))
384 #define CALCULATE_BER(avg_errors, count) 0
385 #define CALCULATE_SNR(data) 0
388 static int mxl111sf_demod_read_ber(struct dvb_frontend *fe, u32 *ber)
390 struct mxl111sf_demod_state *state = fe->demodulator_priv;
396 ret = mxl111sf_demod_read_reg(state, V6_RS_AVG_ERRORS_LSB_REG, &val1);
399 ret = mxl111sf_demod_read_reg(state, V6_RS_AVG_ERRORS_MSB_REG, &val2);
402 ret = mxl111sf_demod_read_reg(state, V6_N_ACCUMULATE_REG, &val3);
406 *ber = CALCULATE_BER((val1 | (val2 << 8)), val3);
411 static int mxl111sf_demod_calc_snr(struct mxl111sf_demod_state *state,
419 ret = mxl111sf_demod_read_reg(state, V6_SNR_RB_LSB_REG, &val1);
422 ret = mxl111sf_demod_read_reg(state, V6_SNR_RB_MSB_REG, &val2);
426 *snr = CALCULATE_SNR(val1 | ((val2 & 0x03) << 8));
431 static int mxl111sf_demod_read_snr(struct dvb_frontend *fe, u16 *snr)
433 struct mxl111sf_demod_state *state = fe->demodulator_priv;
435 int ret = mxl111sf_demod_calc_snr(state, snr);
439 *snr /= 10; /* 0.1 dB */
444 static int mxl111sf_demod_read_status(struct dvb_frontend *fe,
447 struct mxl111sf_demod_state *state = fe->demodulator_priv;
448 int ret, locked, cr_lock, sync_lock, fec_lock;
452 ret = mxl1x1sf_demod_get_rs_lock_status(state, &locked);
455 ret = mxl1x1sf_demod_get_tps_lock_status(state, &cr_lock);
458 ret = mxl1x1sf_demod_get_sync_lock_status(state, &sync_lock);
461 ret = mxl1x1sf_demod_get_fec_lock_status(state, &fec_lock);
466 *status |= FE_HAS_SIGNAL;
468 *status |= FE_HAS_CARRIER;
470 *status |= FE_HAS_SYNC;
471 if (fec_lock) /* false positives? */
472 *status |= FE_HAS_VITERBI;
474 if ((locked) && (cr_lock) && (sync_lock))
475 *status |= FE_HAS_LOCK;
480 static int mxl111sf_demod_read_signal_strength(struct dvb_frontend *fe,
481 u16 *signal_strength)
483 struct mxl111sf_demod_state *state = fe->demodulator_priv;
484 fe_modulation_t constellation;
487 mxl111sf_demod_calc_snr(state, &snr);
488 mxl1x1sf_demod_get_tps_constellation(state, &constellation);
490 switch (constellation) {
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 dvb_frontend_parameters *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->u.ofdm.bandwidth);
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->u.ofdm.code_rate_HP);
525 mxl1x1sf_demod_get_tps_code_rate(state, &p->u.ofdm.code_rate_LP);
526 mxl1x1sf_demod_get_tps_constellation(state, &p->u.ofdm.constellation);
527 mxl1x1sf_demod_get_tps_guard_fft_mode(state,
528 &p->u.ofdm.transmission_mode);
529 mxl1x1sf_demod_get_tps_guard_interval(state,
530 &p->u.ofdm.guard_interval);
531 mxl1x1sf_demod_get_tps_hierarchy(state,
532 &p->u.ofdm.hierarchy_information);
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 struct dvb_frontend_ops mxl111sf_demod_ops = {
556 .name = "MaxLinear MxL111SF DVB-T demodulator",
558 .frequency_min = 177000000,
559 .frequency_max = 858000000,
560 .frequency_stepsize = 166666,
561 .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
562 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
563 FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 |
565 FE_CAN_HIERARCHY_AUTO | FE_CAN_GUARD_INTERVAL_AUTO |
566 FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_RECOVER
568 .release = mxl111sf_demod_release,
570 .init = mxl111sf_init,
571 .i2c_gate_ctrl = mxl111sf_i2c_gate_ctrl,
573 .set_frontend = mxl111sf_demod_set_frontend,
574 .get_frontend = mxl111sf_demod_get_frontend,
575 .get_tune_settings = mxl111sf_demod_get_tune_settings,
576 .read_status = mxl111sf_demod_read_status,
577 .read_signal_strength = mxl111sf_demod_read_signal_strength,
578 .read_ber = mxl111sf_demod_read_ber,
579 .read_snr = mxl111sf_demod_read_snr,
580 .read_ucblocks = mxl111sf_demod_read_ucblocks,
583 struct dvb_frontend *mxl111sf_demod_attach(struct mxl111sf_state *mxl_state,
584 struct mxl111sf_demod_config *cfg)
586 struct mxl111sf_demod_state *state = NULL;
590 state = kzalloc(sizeof(struct mxl111sf_demod_state), GFP_KERNEL);
594 state->mxl_state = mxl_state;
597 memcpy(&state->fe.ops, &mxl111sf_demod_ops,
598 sizeof(struct dvb_frontend_ops));
600 state->fe.demodulator_priv = state;
603 EXPORT_SYMBOL_GPL(mxl111sf_demod_attach);
605 MODULE_DESCRIPTION("MaxLinear MxL111SF DVB-T demodulator driver");
606 MODULE_AUTHOR("Michael Krufky <mkrufky@kernellabs.com>");
607 MODULE_LICENSE("GPL");
608 MODULE_VERSION("0.1");