Skip to contentSkip to conversation

Add dvbv5 stats to dib8000 frontend

PATCH New Mauro Carvalho Chehab 1 participant 7 messages Dec 17, 2013 Media (V4L/DVB)

lore
Share & tools
lore
#0 lore raw

cover Add dvbv5 stats to dib8000 frontend

DVBv5 stats is more complete than the DVBv3 ones, and provide scales to the statistics.

Add support for it with dib8000.

After this patchset, the stats will look like:

Lock (0x1f) Quality= Poor Signal= -26.93dBm C/N= 22.45dB UCB= 229 postBER= 16.6x10^-6 PER= 1.12x10^-3

For the first version of dib8076. Newer versions should also have per-layer stats, but I couldn't test here, due to the lack of a newer hardware.

For those that want to test the new improvements on dib8000 frontend, I added them on my experimental tree:
http://git.linuxtv.org/mchehab/experimental.git/shortlog/refs/heads/dib0700_isdb

Mauro Carvalho Chehab (6):
[media] dib8000: add DVBv5 stats
[media] dib8000: estimate strength in dBm
[media] dib8000: make a better estimation for dBm
[media] dib8000: Fix UCB measure with DVBv5 stats
[media] dib8000: be sure that stats are available before reading them [media] dib8000: improve block statistics

 drivers/media/dvb-frontends/dib8000.c | 404 +++++++++++++++++++++++++++++++++-
 1 file changed, 403 insertions(+), 1 deletion(-)
Signature · 1 line
1.8.3.1
#1 lore raw

PATCH 1/6 dib8000: add DVBv5 stats

The advantage of DVBv5 stats is that it allows adding proper scales to all measures. use it for this frontend.

This patch adds a basic set of stats, basically cloning what's already provided by DVBv3 API. Latter patches will improve it.

---
 drivers/media/dvb-frontends/dib8000.c | 142 +++++++++++++++++++++++++++++++++-
 1 file changed, 141 insertions(+), 1 deletion(-)
drivers/media/dvb-frontends/dib8000.c +141 -1
index 13fdc3d5f762..2dbf89365a97 100644
@@ -124,6 +124,8 @@ struct dib8000_state {
 	u16 agc2_max;
 	u16 agc2_min;
 #endif
+
+	unsigned long get_stats_time;
 };
 
 enum dib8000_power_mode {
@@ -804,7 +806,7 @@ int dib8000_update_pll(struct dvb_frontend *fe,
 			dprintk("PLL: Update ratio (prediv: %d, ratio: %d)", state->cfg.pll->pll_prediv, ratio);
 			dib8000_write_word(state, 901, (state->cfg.pll->pll_prediv << 8) | (ratio << 0)); /* only the PLL ratio is updated. */
 		}
-}
+	}
 
 	return 0;
 }
@@ -983,6 +985,32 @@ static u16 dib8000_identify(struct i2c_device *client)
 	return value;
 }
 
+static void dib8000_reset_stats(struct dvb_frontend *fe)
+{
+	struct dib8000_state *state = fe->demodulator_priv;
+	struct dtv_frontend_properties *c = &state->fe[0]->dtv_property_cache;
+
+	memset(&c->strength, 0, sizeof(c->strength));
+	memset(&c->cnr, 0, sizeof(c->cnr));
+	memset(&c->post_bit_error, 0, sizeof(c->post_bit_error));
+	memset(&c->post_bit_count, 0, sizeof(c->post_bit_count));
+	memset(&c->block_error, 0, sizeof(c->block_error));
+
+	c->strength.len = 1;
+	c->cnr.len = 1;
+	c->block_error.len = 1;
+	c->post_bit_error.len = 1;
+	c->post_bit_count.len = 1;
+
+	c->strength.stat[0].scale = FE_SCALE_RELATIVE;
+	c->strength.stat[0].uvalue = 0;
+
+	c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
+	c->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
+	c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
+	c->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
+}
+
 static int dib8000_reset(struct dvb_frontend *fe)
 {
 	struct dib8000_state *state = fe->demodulator_priv;
@@ -1088,6 +1116,8 @@ static int dib8000_reset(struct dvb_frontend *fe)
 
 	dib8000_set_power_mode(state, DIB8000_POWER_INTERFACE_ONLY);
 
+	dib8000_reset_stats(fe);
+
 	return 0;
 }
 
@@ -2983,6 +3013,8 @@ static int dib8000_tune(struct dvb_frontend *fe)
 
 	switch (*tune_state) {
 	case CT_DEMOD_START: /* 30 */
+			dib8000_reset_stats(fe);
+
 			if (state->revision == 0x8090)
 				dib8090p_init_sdram(state);
 			state->status = FE_STATUS_TUNE_PENDING;
@@ -3654,6 +3686,8 @@ static int dib8000_set_frontend(struct dvb_frontend *fe)
 	return 0;
 }
 
+static int dib8000_get_stats(struct dvb_frontend *fe, fe_status_t stat);
+
 static int dib8000_read_status(struct dvb_frontend *fe, fe_status_t * stat)
 {
 	struct dib8000_state *state = fe->demodulator_priv;
@@ -3691,6 +3725,7 @@ static int dib8000_read_status(struct dvb_frontend *fe, fe_status_t * stat)
 		if (lock & 0x01)
 			*stat |= FE_HAS_VITERBI;
 	}
+	dib8000_get_stats(fe, *stat);
 
 	return 0;
 }
@@ -3797,6 +3832,111 @@ static int dib8000_read_snr(struct dvb_frontend *fe, u16 * snr)
 	return 0;
 }
 
+struct per_layer_regs {
+	u16 lock, ber, per;
+};
+
+static const struct per_layer_regs per_layer_regs[] = {
+	{ 554, 560, 562 },
+	{ 555, 576, 578 },
+	{ 556, 581, 583 },
+};
+
+static int dib8000_get_stats(struct dvb_frontend *fe, fe_status_t stat)
+{
+	struct dib8000_state *state = fe->demodulator_priv;
+	struct dtv_frontend_properties *c = &state->fe[0]->dtv_property_cache;
+	int i, lock;
+	u32 snr, val;
+	u16 strength;
+
+	/* Get Signal strength */
+	dib8000_read_signal_strength(fe, &strength);
+	c->strength.stat[0].uvalue = strength;
+
+	/* Check if 1 second was elapsed */
+	if (!time_after(jiffies, state->get_stats_time))
+		return 0;
+	state->get_stats_time = jiffies + msecs_to_jiffies(1000);
+
+	/* Get SNR */
+	snr = dib8000_get_snr(fe);
+	for (i = 1; i < MAX_NUMBER_OF_FRONTENDS; i++) {
+		if (state->fe[i])
+			snr += dib8000_get_snr(state->fe[i]);
+	}
+	snr = snr >> 16;
+
+	if (snr) {
+		snr = 10 * intlog10(snr);
+		snr = (1000L * snr) >> 24;
+	} else {
+		snr = 0;
+	}
+	c->cnr.stat[0].svalue = snr;
+	c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
+
+	/* UCB/BER measures require lock */
+	if (!(stat & FE_HAS_LOCK)) {
+		c->block_error.len = 1;
+		c->post_bit_error.len = 1;
+		c->post_bit_count.len = 1;
+		c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
+		c->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
+		c->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
+		return 0;
+	}
+
+	/* Get UCB and post-BER measures */
+
+	/* FIXME: need to check if 1.25e6 bits already passed */
+	dib8000_read_ber(fe, &val);
+	c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
+	c->post_bit_error.stat[0].uvalue += val;
+
+	c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
+	c->post_bit_count.stat[0].uvalue += 100000000;
+
+	/*
+	 * FIXME: this is refreshed on every second, but a time
+	 * drift between dib8000 and PC clock may cause troubles
+	 */
+	dib8000_read_unc_blocks(fe, &val);
+
+	c->block_error.stat[0].scale = FE_SCALE_COUNTER;
+	c->block_error.stat[0].uvalue += val;
+
+	if (state->revision < 0x8002)
+		return 0;
+
+	c->block_error.len = 4;
+	c->post_bit_error.len = 4;
+	c->post_bit_count.len = 4;
+
+	for (i = 0; i < 3; i++) {
+		lock = dib8000_read_word(state, per_layer_regs[i].lock);
+		if (lock & 0x01) {
+			/* FIXME: need to check if 1.25e6 bits already passed */
+			val = dib8000_read_word(state, per_layer_regs[i].ber);
+			c->post_bit_error.stat[1 + i].scale = FE_SCALE_COUNTER;
+			c->post_bit_error.stat[1 + i].uvalue += val;
+
+			c->post_bit_count.stat[1 + i].scale = FE_SCALE_COUNTER;
+			c->post_bit_count.stat[1 + i].uvalue += 100000000;
+
+			/*
+			 * FIXME: this is refreshed on every second, but a time
+			 * drift between dib8000 and PC clock may cause troubles
+			 */
+			val = dib8000_read_word(state, per_layer_regs[i].per);
+
+			c->block_error.stat[1 + i].scale = FE_SCALE_COUNTER;
+			c->block_error.stat[1 + i].uvalue += val;
+		}
+	}
+	return 0;
+}
+
 int dib8000_set_slave_frontend(struct dvb_frontend *fe, struct dvb_frontend *fe_slave)
 {
 	struct dib8000_state *state = fe->demodulator_priv;
Signature · 1 line
1.8.3.1
#4 lore raw

PATCH 4/6 dib8000: Fix UCB measure with DVBv5 stats

In reply to Mauro Carvalho Chehab

On dib8000, the block error count is a monotonic 32 bits register. With DVBv5 stats, we use a 64 bits counter, that it is reset when a new channel is tuned.

Change the UCB counting start from 0 and to be returned with 64 bits, just like the API requests.

---
 drivers/media/dvb-frontends/dib8000.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)
drivers/media/dvb-frontends/dib8000.c +10 -5
index 7b10b73befbe..ef0d9ec0df23 100644
@@ -119,6 +119,7 @@ struct dib8000_state {
 	u8 longest_intlv_layer;
 	u16 output_mode;
 
+	s64 init_ucb;
 #ifdef DIB8000_AGC_FREEZE
 	u16 agc1_max;
 	u16 agc1_min;
@@ -986,10 +987,13 @@ static u16 dib8000_identify(struct i2c_device *client)
 	return value;
 }
 
+static int dib8000_read_unc_blocks(struct dvb_frontend *fe, u32 *unc);
+
 static void dib8000_reset_stats(struct dvb_frontend *fe)
 {
 	struct dib8000_state *state = fe->demodulator_priv;
 	struct dtv_frontend_properties *c = &state->fe[0]->dtv_property_cache;
+	u32 ucb;
 
 	memset(&c->strength, 0, sizeof(c->strength));
 	memset(&c->cnr, 0, sizeof(c->cnr));
@@ -1010,6 +1014,9 @@ static void dib8000_reset_stats(struct dvb_frontend *fe)
 	c->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
 	c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
 	c->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
+
+	dib8000_read_unc_blocks(fe, &ucb);
+	state->init_ucb = -ucb;
 }
 
 static int dib8000_reset(struct dvb_frontend *fe)
@@ -3989,14 +3996,12 @@ static int dib8000_get_stats(struct dvb_frontend *fe, fe_status_t stat)
 	c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
 	c->post_bit_count.stat[0].uvalue += 100000000;
 
-	/*
-	 * FIXME: this is refreshed on every second, but a time
-	 * drift between dib8000 and PC clock may cause troubles
-	 */
 	dib8000_read_unc_blocks(fe, &val);
+	if (val < state->init_ucb)
+		state->init_ucb += 1L << 32;
 
 	c->block_error.stat[0].scale = FE_SCALE_COUNTER;
-	c->block_error.stat[0].uvalue += val;
+	c->block_error.stat[0].uvalue = val + state->init_ucb;
 
 	if (state->revision < 0x8002)
 		return 0;
Signature · 1 line
1.8.3.1
#5 lore raw

PATCH 5/6 dib8000: be sure that stats are available before reading them

In reply to Mauro Carvalho Chehab

On dib8000, the BER statistics are updated on every 1.25e6 bits. Adjust the code to only update the statistics after having it done.

---
 drivers/media/dvb-frontends/dib8000.c | 240 ++++++++++++++++++++++++++--------
 1 file changed, 182 insertions(+), 58 deletions(-)
drivers/media/dvb-frontends/dib8000.c +182 -58
index ef0d9ec0df23..c67a3dba116c 100644
@@ -119,15 +119,17 @@ struct dib8000_state {
 	u8 longest_intlv_layer;
 	u16 output_mode;
 
+	/* for DVBv5 stats */
 	s64 init_ucb;
+	unsigned long jiffies_stats;
+	unsigned long jiffies_stats_layer[3];
+
 #ifdef DIB8000_AGC_FREEZE
 	u16 agc1_max;
 	u16 agc1_min;
 	u16 agc2_max;
 	u16 agc2_min;
 #endif
-
-	unsigned long get_stats_time;
 };
 
 enum dib8000_power_mode {
@@ -1016,7 +1018,11 @@ static void dib8000_reset_stats(struct dvb_frontend *fe)
 	c->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
 
 	dib8000_read_unc_blocks(fe, &ucb);
+
 	state->init_ucb = -ucb;
+	state->jiffies_stats = 0;
+	memset(&state->jiffies_stats_layer, 0,
+	       sizeof(state->jiffies_stats_layer));
 }
 
 static int dib8000_reset(struct dvb_frontend *fe)
@@ -3936,12 +3942,124 @@ static u32 interpolate_value(u32 value, struct linear_segments *segments,
 	return ret;
 }
 
+static u32 dib8000_get_time_us(struct dvb_frontend *fe, int layer)
+{
+	struct dib8000_state *state = fe->demodulator_priv;
+	struct dtv_frontend_properties *c = &state->fe[0]->dtv_property_cache;
+	int ini_layer, end_layer, i;
+	u64 time_us;
+	u32 tmp, denom;
+	int guard, rate_num, rate_denum, bits_per_symbol, nsegs;
+	int interleaving, fft_div;
+
+	if (layer >= 0) {
+		ini_layer = layer;
+		end_layer = layer + 1;
+	} else {
+		ini_layer = 0;
+		end_layer = 3;
+	}
+
+	switch (c->guard_interval) {
+	case GUARD_INTERVAL_1_4:
+		guard = 4;
+		break;
+	case GUARD_INTERVAL_1_8:
+		guard = 8;
+		break;
+	case GUARD_INTERVAL_1_16:
+		guard = 16;
+		break;
+	default:
+	case GUARD_INTERVAL_1_32:
+		guard = 32;
+		break;
+	}
+
+	switch (c->transmission_mode) {
+	case TRANSMISSION_MODE_2K:
+		fft_div = 4;
+		break;
+	case TRANSMISSION_MODE_4K:
+		fft_div = 2;
+		break;
+	default:
+	case TRANSMISSION_MODE_8K:
+		fft_div = 1;
+		break;
+	}
+
+	denom = 0;
+	for (i = ini_layer; i < end_layer; i++) {
+		nsegs = c->layer[i].segment_count;
+		if (nsegs == 0 || nsegs > 13)
+			continue;
+
+		switch (c->layer[i].modulation) {
+		case DQPSK:
+		case QPSK:
+			bits_per_symbol = 2;
+			break;
+		case QAM_16:
+			bits_per_symbol = 4;
+			break;
+		default:
+		case QAM_64:
+			bits_per_symbol = 6;
+			break;
+		}
+
+		switch (c->layer[i].fec) {
+		case FEC_1_2:
+			rate_num = 1;
+			rate_denum = 2;
+			break;
+		case FEC_2_3:
+			rate_num = 2;
+			rate_denum = 3;
+			break;
+		case FEC_3_4:
+			rate_num = 3;
+			rate_denum = 4;
+			break;
+		case FEC_5_6:
+			rate_num = 5;
+			rate_denum = 6;
+			break;
+		default:
+		case FEC_7_8:
+			rate_num = 7;
+			rate_denum = 8;
+			break;
+		}
+
+		interleaving = c->layer[i].interleaving;
+
+		denom += bits_per_symbol * rate_num * fft_div * nsegs * 384;
+	}
+
+	/* If all goes wrong, wait for 1s for the next stats */
+	if (!denom)
+		return 0;
+
+	/* Estimate the period for the total bit rate */
+	time_us = rate_denum * (1008 * 1562500L);
+	time_us = time_us + time_us / guard;
+	time_us += denom / 2;
+	do_div(time_us, denom);
+
+	tmp = 1008 * 96 * interleaving;
+	time_us += tmp + tmp / guard;
+
+	return time_us;
+}
+
 static int dib8000_get_stats(struct dvb_frontend *fe, fe_status_t stat)
 {
 	struct dib8000_state *state = fe->demodulator_priv;
 	struct dtv_frontend_properties *c = &state->fe[0]->dtv_property_cache;
-	int i, lock;
-	u32 snr, val;
+	int i;
+	u32 time_us, snr, val;
 	s32 db;
 	u16 strength;
 
@@ -3953,55 +4071,59 @@ static int dib8000_get_stats(struct dvb_frontend *fe, fe_status_t stat)
 			       ARRAY_SIZE(strength_to_db_table)) - 131000;
 	c->strength.stat[0].svalue = db;
 
-	/* Check if 1 second was elapsed */
-	if (!time_after(jiffies, state->get_stats_time))
-		return 0;
-	state->get_stats_time = jiffies + msecs_to_jiffies(1000);
-
-	/* Get SNR */
-	snr = dib8000_get_snr(fe);
-	for (i = 1; i < MAX_NUMBER_OF_FRONTENDS; i++) {
-		if (state->fe[i])
-			snr += dib8000_get_snr(state->fe[i]);
-	}
-	snr = snr >> 16;
-
-	if (snr) {
-		snr = 10 * intlog10(snr);
-		snr = (1000L * snr) >> 24;
-	} else {
-		snr = 0;
-	}
-	c->cnr.stat[0].svalue = snr;
-	c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
-
-	/* UCB/BER measures require lock */
+	/* UCB/BER/CNR measures require lock */
 	if (!(stat & FE_HAS_LOCK)) {
+		c->cnr.len = 1;
 		c->block_error.len = 1;
 		c->post_bit_error.len = 1;
 		c->post_bit_count.len = 1;
+		c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
 		c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
 		c->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
 		c->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
 		return 0;
 	}
 
-	/* Get UCB and post-BER measures */
+	/* Check if time for stats was elapsed */
+	if (time_after(jiffies, state->jiffies_stats)) {
+		time_us = dib8000_get_time_us(fe, -1);
+		state->jiffies_stats = jiffies + msecs_to_jiffies((time_us + 500) / 1000);
+
+		dprintk("Next all layers stats available in %u us.\n", time_us);
+
+		/* Get SNR */
+		snr = dib8000_get_snr(fe);
+		for (i = 1; i < MAX_NUMBER_OF_FRONTENDS; i++) {
+			if (state->fe[i])
+				snr += dib8000_get_snr(state->fe[i]);
+		}
+		snr = snr >> 16;
 
-	/* FIXME: need to check if 1.25e6 bits already passed */
-	dib8000_read_ber(fe, &val);
-	c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
-	c->post_bit_error.stat[0].uvalue += val;
+		if (snr) {
+			snr = 10 * intlog10(snr);
+			snr = (1000L * snr) >> 24;
+		} else {
+			snr = 0;
+		}
+		c->cnr.stat[0].svalue = snr;
+		c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
 
-	c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
-	c->post_bit_count.stat[0].uvalue += 100000000;
+		/* Get UCB and post-BER measures */
 
-	dib8000_read_unc_blocks(fe, &val);
-	if (val < state->init_ucb)
-		state->init_ucb += 1L << 32;
+		dib8000_read_ber(fe, &val);
+		c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
+		c->post_bit_error.stat[0].uvalue += val;
 
-	c->block_error.stat[0].scale = FE_SCALE_COUNTER;
-	c->block_error.stat[0].uvalue = val + state->init_ucb;
+		c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
+		c->post_bit_count.stat[0].uvalue += 100000000;
+
+		dib8000_read_unc_blocks(fe, &val);
+		if (val < state->init_ucb)
+			state->init_ucb += 1L << 32;
+
+		c->block_error.stat[0].scale = FE_SCALE_COUNTER;
+		c->block_error.stat[0].uvalue = val + state->init_ucb;
+	}
 
 	if (state->revision < 0x8002)
 		return 0;
@@ -4011,25 +4133,27 @@ static int dib8000_get_stats(struct dvb_frontend *fe, fe_status_t stat)
 	c->post_bit_count.len = 4;
 
 	for (i = 0; i < 3; i++) {
-		lock = dib8000_read_word(state, per_layer_regs[i].lock);
-		if (lock & 0x01) {
-			/* FIXME: need to check if 1.25e6 bits already passed */
-			val = dib8000_read_word(state, per_layer_regs[i].ber);
-			c->post_bit_error.stat[1 + i].scale = FE_SCALE_COUNTER;
-			c->post_bit_error.stat[1 + i].uvalue += val;
-
-			c->post_bit_count.stat[1 + i].scale = FE_SCALE_COUNTER;
-			c->post_bit_count.stat[1 + i].uvalue += 100000000;
-
-			/*
-			 * FIXME: this is refreshed on every second, but a time
-			 * drift between dib8000 and PC clock may cause troubles
-			 */
-			val = dib8000_read_word(state, per_layer_regs[i].per);
-
-			c->block_error.stat[1 + i].scale = FE_SCALE_COUNTER;
-			c->block_error.stat[1 + i].uvalue += val;
-		}
+		if (!time_after(jiffies, state->jiffies_stats_layer[i]))
+			continue;
+		time_us = dib8000_get_time_us(fe, i);
+		if (!time_us)
+			continue;
+
+		state->jiffies_stats_layer[i] = jiffies + msecs_to_jiffies((time_us + 500) / 1000);
+		dprintk("Next layer %c  stats will be available in %u us\n",
+			'A' + i, time_us);
+
+		val = dib8000_read_word(state, per_layer_regs[i].ber);
+		c->post_bit_error.stat[1 + i].scale = FE_SCALE_COUNTER;
+		c->post_bit_error.stat[1 + i].uvalue += val;
+
+		c->post_bit_count.stat[1 + i].scale = FE_SCALE_COUNTER;
+		c->post_bit_count.stat[1 + i].uvalue += 100000000;
+
+		val = dib8000_read_word(state, per_layer_regs[i].per);
+
+		c->block_error.stat[1 + i].scale = FE_SCALE_COUNTER;
+		c->block_error.stat[1 + i].uvalue += val;
 	}
 	return 0;
 }
Signature · 1 line
1.8.3.1
#6 lore raw

PATCH 6/6 dib8000: improve block statistics

In reply to Mauro Carvalho Chehab

PER/UCB statistics are collected once on each 1 second. However, it doesn't provide the total number of packets needed to calculate PER.

Yet, as we know the bit rate, it is possible to estimate such number. So, do it.

---
 drivers/media/dvb-frontends/dib8000.c | 109 +++++++++++++++++++++++-----------
 1 file changed, 75 insertions(+), 34 deletions(-)
drivers/media/dvb-frontends/dib8000.c +75 -34
index c67a3dba116c..7539d7af2cf7 100644
@@ -121,8 +121,9 @@ struct dib8000_state {
 
 	/* for DVBv5 stats */
 	s64 init_ucb;
-	unsigned long jiffies_stats;
-	unsigned long jiffies_stats_layer[3];
+	unsigned long per_jiffies_stats;
+	unsigned long ber_jiffies_stats;
+	unsigned long ber_jiffies_stats_layer[3];
 
 #ifdef DIB8000_AGC_FREEZE
 	u16 agc1_max;
@@ -1006,6 +1007,7 @@ static void dib8000_reset_stats(struct dvb_frontend *fe)
 	c->strength.len = 1;
 	c->cnr.len = 1;
 	c->block_error.len = 1;
+	c->block_count.len = 1;
 	c->post_bit_error.len = 1;
 	c->post_bit_count.len = 1;
 
@@ -1014,15 +1016,17 @@ static void dib8000_reset_stats(struct dvb_frontend *fe)
 
 	c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
 	c->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
+	c->block_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
 	c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
 	c->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
 
 	dib8000_read_unc_blocks(fe, &ucb);
 
 	state->init_ucb = -ucb;
-	state->jiffies_stats = 0;
-	memset(&state->jiffies_stats_layer, 0,
-	       sizeof(state->jiffies_stats_layer));
+	state->ber_jiffies_stats = 0;
+	state->per_jiffies_stats = 0;
+	memset(&state->ber_jiffies_stats_layer, 0,
+	       sizeof(state->ber_jiffies_stats_layer));
 }
 
 static int dib8000_reset(struct dvb_frontend *fe)
@@ -4059,7 +4063,9 @@ static int dib8000_get_stats(struct dvb_frontend *fe, fe_status_t stat)
 	struct dib8000_state *state = fe->demodulator_priv;
 	struct dtv_frontend_properties *c = &state->fe[0]->dtv_property_cache;
 	int i;
-	u32 time_us, snr, val;
+	int show_per_stats = 0;
+	u32 time_us = 0, snr, val;
+	u64 blocks;
 	s32 db;
 	u16 strength;
 
@@ -4074,6 +4080,7 @@ static int dib8000_get_stats(struct dvb_frontend *fe, fe_status_t stat)
 	/* UCB/BER/CNR measures require lock */
 	if (!(stat & FE_HAS_LOCK)) {
 		c->cnr.len = 1;
+		c->block_count.len = 1;
 		c->block_error.len = 1;
 		c->post_bit_error.len = 1;
 		c->post_bit_count.len = 1;
@@ -4081,15 +4088,13 @@ static int dib8000_get_stats(struct dvb_frontend *fe, fe_status_t stat)
 		c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
 		c->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
 		c->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
+		c->block_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
 		return 0;
 	}
 
 	/* Check if time for stats was elapsed */
-	if (time_after(jiffies, state->jiffies_stats)) {
-		time_us = dib8000_get_time_us(fe, -1);
-		state->jiffies_stats = jiffies + msecs_to_jiffies((time_us + 500) / 1000);
-
-		dprintk("Next all layers stats available in %u us.\n", time_us);
+	if (time_after(jiffies, state->per_jiffies_stats)) {
+		state->per_jiffies_stats = jiffies + msecs_to_jiffies(1000);
 
 		/* Get SNR */
 		snr = dib8000_get_snr(fe);
@@ -4108,7 +4113,34 @@ static int dib8000_get_stats(struct dvb_frontend *fe, fe_status_t stat)
 		c->cnr.stat[0].svalue = snr;
 		c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
 
-		/* Get UCB and post-BER measures */
+		/* Get UCB measures */
+		dib8000_read_unc_blocks(fe, &val);
+		if (val < state->init_ucb)
+			state->init_ucb += 0x100000000L;
+
+		c->block_error.stat[0].scale = FE_SCALE_COUNTER;
+		c->block_error.stat[0].uvalue = val + state->init_ucb;
+
+		/* Estimate the number of packets based on bitrate */
+		if (!time_us)
+			time_us = dib8000_get_time_us(fe, -1);
+
+		if (time_us) {
+			blocks = 1250000UL * 1000000UL;
+			do_div(blocks, time_us * 8 * 204);
+			c->block_count.stat[0].scale = FE_SCALE_COUNTER;
+			c->block_count.stat[0].uvalue += blocks;
+		}
+
+		show_per_stats = 1;
+	}
+
+	/* Get post-BER measures */
+	if (time_after(jiffies, state->ber_jiffies_stats)) {
+		time_us = dib8000_get_time_us(fe, -1);
+		state->ber_jiffies_stats = jiffies + msecs_to_jiffies((time_us + 500) / 1000);
+
+		dprintk("Next all layers stats available in %u us.", time_us);
 
 		dib8000_read_ber(fe, &val);
 		c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
@@ -4116,13 +4148,6 @@ static int dib8000_get_stats(struct dvb_frontend *fe, fe_status_t stat)
 
 		c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
 		c->post_bit_count.stat[0].uvalue += 100000000;
-
-		dib8000_read_unc_blocks(fe, &val);
-		if (val < state->init_ucb)
-			state->init_ucb += 1L << 32;
-
-		c->block_error.stat[0].scale = FE_SCALE_COUNTER;
-		c->block_error.stat[0].uvalue = val + state->init_ucb;
 	}
 
 	if (state->revision < 0x8002)
@@ -4133,27 +4158,43 @@ static int dib8000_get_stats(struct dvb_frontend *fe, fe_status_t stat)
 	c->post_bit_count.len = 4;
 
 	for (i = 0; i < 3; i++) {
-		if (!time_after(jiffies, state->jiffies_stats_layer[i]))
-			continue;
-		time_us = dib8000_get_time_us(fe, i);
-		if (!time_us)
+		unsigned nsegs = c->layer[i].segment_count;
+
+		if (nsegs == 0 || nsegs > 13)
 			continue;
 
-		state->jiffies_stats_layer[i] = jiffies + msecs_to_jiffies((time_us + 500) / 1000);
-		dprintk("Next layer %c  stats will be available in %u us\n",
-			'A' + i, time_us);
+		time_us = 0;
+
+		if (time_after(jiffies, state->ber_jiffies_stats_layer[i])) {
+			time_us = dib8000_get_time_us(fe, i);
+
+			state->ber_jiffies_stats_layer[i] = jiffies + msecs_to_jiffies((time_us + 500) / 1000);
+			dprintk("Next layer %c  stats will be available in %u us\n",
+				'A' + i, time_us);
 
-		val = dib8000_read_word(state, per_layer_regs[i].ber);
-		c->post_bit_error.stat[1 + i].scale = FE_SCALE_COUNTER;
-		c->post_bit_error.stat[1 + i].uvalue += val;
+			val = dib8000_read_word(state, per_layer_regs[i].ber);
+			c->post_bit_error.stat[1 + i].scale = FE_SCALE_COUNTER;
+			c->post_bit_error.stat[1 + i].uvalue += val;
 
-		c->post_bit_count.stat[1 + i].scale = FE_SCALE_COUNTER;
-		c->post_bit_count.stat[1 + i].uvalue += 100000000;
+			c->post_bit_count.stat[1 + i].scale = FE_SCALE_COUNTER;
+			c->post_bit_count.stat[1 + i].uvalue += 100000000;
+		}
+
+		if (show_per_stats) {
+			val = dib8000_read_word(state, per_layer_regs[i].per);
 
-		val = dib8000_read_word(state, per_layer_regs[i].per);
+			c->block_error.stat[1 + i].scale = FE_SCALE_COUNTER;
+			c->block_error.stat[1 + i].uvalue += val;
 
-		c->block_error.stat[1 + i].scale = FE_SCALE_COUNTER;
-		c->block_error.stat[1 + i].uvalue += val;
+			if (!time_us)
+				time_us = dib8000_get_time_us(fe, i);
+			if (time_us) {
+				blocks = 1250000UL * 1000000UL;
+				do_div(blocks, time_us * 8 * 204);
+				c->block_count.stat[0].scale = FE_SCALE_COUNTER;
+				c->block_count.stat[0].uvalue += blocks;
+			}
+		}
 	}
 	return 0;
 }
Signature · 1 line
1.8.3.1
#2 lore raw

PATCH 2/6 dib8000: estimate strength in dBm

In reply to Mauro Carvalho Chehab

Better to have Signal strength in dB.
This takes a very rough estimation for the signal strength, that was calibrated using a Dektec DTA-2111 Gold RF generator and a Pixelview dib8076 stick.

It estimates the signal strength using a linear equation where:
- the max is -22.5 dBm, with returns 55953
- the min is -35.0 dBm, with returns 50110

With -22dBm, the signal strengh is returned as 65535. Unfortunately, the min strength generated with DTA-2111 is -35dBm.

It should be noticed that approximating it by a linear equation is not right. I should probably be splitting it into 0.5 dB linear segments, in order to get a higher precision, just like it is done on mb86a20s, but that would force me to add some attenuators, in order to get dB levels below -35dBm, which is, btw, strong enough to get signal lock.

---
 drivers/media/dvb-frontends/dib8000.c | 23 +++++++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)
drivers/media/dvb-frontends/dib8000.c +21 -2
index 2dbf89365a97..faf469d1d437 100644
@@ -11,6 +11,7 @@
 #include <linux/slab.h>
 #include <linux/i2c.h>
 #include <linux/mutex.h>
+#include <asm/div64.h>
 
 #include "dvb_math.h"
 
@@ -1002,7 +1003,7 @@ static void dib8000_reset_stats(struct dvb_frontend *fe)
 	c->post_bit_error.len = 1;
 	c->post_bit_count.len = 1;
 
-	c->strength.stat[0].scale = FE_SCALE_RELATIVE;
+	c->strength.stat[0].scale = FE_SCALE_DECIBEL;
 	c->strength.stat[0].uvalue = 0;
 
 	c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
@@ -3847,12 +3848,30 @@ static int dib8000_get_stats(struct dvb_frontend *fe, fe_status_t stat)
 	struct dib8000_state *state = fe->demodulator_priv;
 	struct dtv_frontend_properties *c = &state->fe[0]->dtv_property_cache;
 	int i, lock;
+	u64 tmp;
 	u32 snr, val;
 	u16 strength;
 
 	/* Get Signal strength */
 	dib8000_read_signal_strength(fe, &strength);
-	c->strength.stat[0].uvalue = strength;
+
+	/*
+	 * Estimate it in dBm
+	 * This calculus was empirically determinated by measuring the signal
+	 * strength generated by a DTA-2111 RF generator directly connected into
+	 * a dib8076 device. The real value can actually be different on other
+	 * devices, depending if LNA is enabled or not, if diversity is enabled,
+	 * etc.
+	 */
+	if (strength == 65535) {
+		c->strength.stat[0].svalue = -22000;
+	} else {
+		tmp = strength * 25000L;
+		do_div(tmp, 11646);
+		c->strength.stat[0].svalue = tmp - 142569;
+		if (c->strength.stat[0].svalue > -22000)
+			c->strength.stat[0].svalue = -22000;
+	}
 
 	/* Check if 1 second was elapsed */
 	if (!time_after(jiffies, state->get_stats_time))
Signature · 1 line
1.8.3.1
#3 lore raw

PATCH 3/6 dib8000: make a better estimation for dBm

In reply to Mauro Carvalho Chehab

Use multiple linear segments to better interpolate the dBm for the signal strength.

The table that converts from linear strength to dB was empirically determinated with the help of a signal generator (DTA-2111).

The entries from -35dBm to -22.5dBm were taken using just the signal generator and the board.

For the entries from -36dBm to -51dBm, a 16 dB tap was used, in order to extend its range.

Signals below to -51dBm are just linearly interpolated.

---
 drivers/media/dvb-frontends/dib8000.c | 111 ++++++++++++++++++++++++++++------
 1 file changed, 92 insertions(+), 19 deletions(-)
drivers/media/dvb-frontends/dib8000.c +92 -19
index faf469d1d437..7b10b73befbe 100644
@@ -3843,35 +3843,108 @@ static const struct per_layer_regs per_layer_regs[] = {
 	{ 556, 581, 583 },
 };
 
+struct linear_segments {
+	unsigned x;
+	signed y;
+};
+
+/*
+ * Table to estimate signal strength in dBm.
+ * This table was empirically determinated by measuring the signal
+ * strength generated by a DTA-2111 RF generator directly connected into
+ * a dib8076 device (a PixelView PV-D231U stick), using a good quality
+ * 3 meters RC6 cable and good RC6 connectors.
+ * The real value can actually be different on other devices, depending
+ * on several factors, like if LNA is enabled or not, if diversity is
+ * enabled, type of connectors, etc.
+ * Yet, it is better to use this measure in dB than a random non-linear
+ * percentage value, especially for antenna adjustments.
+ * On my tests, the precision of the measure using this table is about
+ * 0.5 dB, with sounds reasonable enough.
+ */
+static struct linear_segments strength_to_db_table[] = {
+	{ 55953, 108500 },	/* -22.5 dBm */
+	{ 55394, 108000 },
+	{ 53834, 107000 },
+	{ 52863, 106000 },
+	{ 52239, 105000 },
+	{ 52012, 104000 },
+	{ 51803, 103000 },
+	{ 51566, 102000 },
+	{ 51356, 101000 },
+	{ 51112, 100000 },
+	{ 50869,  99000 },
+	{ 50600,  98000 },
+	{ 50363,  97000 },
+	{ 50117,  96000 },	/* -35 dBm */
+	{ 49889,  95000 },
+	{ 49680,  94000 },
+	{ 49493,  93000 },
+	{ 49302,  92000 },
+	{ 48929,  91000 },
+	{ 48416,  90000 },
+	{ 48035,  89000 },
+	{ 47593,  88000 },
+	{ 47282,  87000 },
+	{ 46953,  86000 },
+	{ 46698,  85000 },
+	{ 45617,  84000 },
+	{ 44773,  83000 },
+	{ 43845,  82000 },
+	{ 43020,  81000 },
+	{ 42010,  80000 },	/* -51 dBm */
+	{     0,      0 },
+};
+
+static u32 interpolate_value(u32 value, struct linear_segments *segments,
+			     unsigned len)
+{
+	u64 tmp64;
+	u32 dx;
+	s32 dy;
+	int i, ret;
+
+	if (value >= segments[0].x)
+		return segments[0].y;
+	if (value < segments[len-1].x)
+		return segments[len-1].y;
+
+	for (i = 1; i < len - 1; i++) {
+		/* If value is identical, no need to interpolate */
+		if (value == segments[i].x)
+			return segments[i].y;
+		if (value > segments[i].x)
+			break;
+	}
+
+	/* Linear interpolation between the two (x,y) points */
+	dy = segments[i - 1].y - segments[i].y;
+	dx = segments[i - 1].x - segments[i].x;
+
+	tmp64 = value - segments[i].x;
+	tmp64 *= dy;
+	do_div(tmp64, dx);
+	ret = segments[i].y + tmp64;
+
+	return ret;
+}
+
 static int dib8000_get_stats(struct dvb_frontend *fe, fe_status_t stat)
 {
 	struct dib8000_state *state = fe->demodulator_priv;
 	struct dtv_frontend_properties *c = &state->fe[0]->dtv_property_cache;
 	int i, lock;
-	u64 tmp;
 	u32 snr, val;
+	s32 db;
 	u16 strength;
 
 	/* Get Signal strength */
 	dib8000_read_signal_strength(fe, &strength);
-
-	/*
-	 * Estimate it in dBm
-	 * This calculus was empirically determinated by measuring the signal
-	 * strength generated by a DTA-2111 RF generator directly connected into
-	 * a dib8076 device. The real value can actually be different on other
-	 * devices, depending if LNA is enabled or not, if diversity is enabled,
-	 * etc.
-	 */
-	if (strength == 65535) {
-		c->strength.stat[0].svalue = -22000;
-	} else {
-		tmp = strength * 25000L;
-		do_div(tmp, 11646);
-		c->strength.stat[0].svalue = tmp - 142569;
-		if (c->strength.stat[0].svalue > -22000)
-			c->strength.stat[0].svalue = -22000;
-	}
+	val = strength;
+	db = interpolate_value(val,
+			       strength_to_db_table,
+			       ARRAY_SIZE(strength_to_db_table)) - 131000;
+	c->strength.stat[0].svalue = db;
 
 	/* Check if 1 second was elapsed */
 	if (!time_after(jiffies, state->get_stats_time))
Signature · 1 line
1.8.3.1