more Mirics MSi3101 changes
Subsystems (2)
cover more Mirics MSi3101 changes
I will pull-request these soon as well those sent earlier.
br Antti
Antti Palosaari (3):
msi3101: implement stream format 504
msi3101: change stream format 384
msi3101: few improvements for RF tuner
drivers/staging/media/msi3101/sdr-msi3101.c | 172 ++++++++++++++++++++++------ 1 file changed, 139 insertions(+), 33 deletions(-)
Signature · 1 line
1.7.11.7
PATCH 1/3 msi3101: implement stream format 504
That stream format carries 504 x I+Q samples per 1024 USB frame. Sample resolution is 8-bit signed. Default it when sampling rate is 9Msps or over.
--- drivers/staging/media/msi3101/sdr-msi3101.c | 94 ++++++++++++++++++++++++++++- 1 file changed, 92 insertions(+), 2 deletions(-)
drivers/staging/media/msi3101/sdr-msi3101.c +92 -2
@@ -448,7 +448,7 @@ leave:/* * +=========================================================================== -* | 00-1023 | USB packet +* | 00-1023 | USB packet type '384' * +=========================================================================== * | 00- 03 | sequence number of first sample in that USB packet * +---------------------------------------------------------------------------
@@ -502,6 +502,93 @@ leave:#define I2F_MASK ((1 << I2F_FRAC_BITS) - 1)/* +* Converts signed 8-bit integer into 32-bit IEEE floating point +* representation. +*/ +static u32 msi3101_convert_sample_504 (struct msi3101_state *s,u16 x) +{ +u32 msb, exponent, fraction, sign; + +/* Zero is special */ +if (!x) +return 0 ; + +/* Negative / positive value */ +if (x & (1 <<7 )) { + x = -x; + x &=0x7f ;/* result is 7 bit ... + sign */ + sign =1 <<31 ; + }else { + sign =0 <<31 ; + } + +/* Get location of the most significant bit */ + msb =__fls (x); + + fraction =ror32 (x, (msb -I2F_FRAC_BITS ) &0x1f ) &I2F_MASK ; + exponent = (127 + msb) <<I2F_FRAC_BITS ; + +return (fraction + exponent) | sign; +} + +static int msi3101_convert_stream_504 (struct msi3101_state *s,u32 *dst, +u8 *src,unsigned int src_len) +{ +int i, j, i_max, dst_len =0 ; +u16 sample[2 ]; +u32 sample_num[3 ]; + +/* There could be 1-3 1024 bytes URB frames */ + i_max = src_len /1024 ; + +for (i =0 ; i < i_max; i++) { + sample_num[i] = src[3 ] <<24 | src[2 ] <<16 | src[1 ] <<8 | src[0 ] <<0 ; +if (i ==0 && s->next_sample != sample_num[0 ]) { +dev_dbg_ratelimited (&s->udev->dev, +"%d samples lost, %d %08x:%08x\n" , + sample_num[0 ] - s->next_sample, + src_len, s->next_sample, sample_num[0 ]); + } + +/* +* Dump all unknown 'garbage' data - maybe we will discover +* someday if there is something rational... +*/ +dev_dbg_ratelimited (&s->udev->dev,"%*ph\n" ,12 , &src[4 ]); + + src +=16 ; +for (j =0 ; j <1008 ; j +=2 ) { + sample[0 ] = src[j +0 ]; + sample[1 ] = src[j +1 ]; + + *dst++ =msi3101_convert_sample_504 (s, sample[0 ]); + *dst++ =msi3101_convert_sample_504 (s, sample[1 ]); + } +/* 504 x I+Q 32bit float samples */ + dst_len +=504 *2 *4 ; + src +=1008 ; + } + +/* calculate samping rate and output it in 10 seconds intervals */ +if ((s->jiffies +msecs_to_jiffies (10000 )) <= jiffies) { +unsigned long jiffies_now = jiffies; +unsigned long msecs =jiffies_to_msecs (jiffies_now) -jiffies_to_msecs (s->jiffies); +unsigned int samples = sample_num[i_max -1 ] - s->sample; + s->jiffies = jiffies_now; + s->sample = sample_num[i_max -1 ]; +dev_dbg (&s->udev->dev, +"slen=%d samples=%u msecs=%lu sampling rate=%lu\n" , + src_len, samples, msecs, + samples *1000UL / msecs); + } + +/* next sample (sample = sample + i * 504) */ + s->next_sample = sample_num[i_max -1 ] +504 ; + +return dst_len; +} + +/* * Converts signed ~10+3-bit integer into 32-bit IEEE floating point * representation. */
@@ -1134,9 +1221,12 @@ static int msi3101_set_usb_adc(struct msi3101_state *s) }else if (f_sr <8000000 ) { s->convert_stream = msi3101_convert_stream_336; reg7 =0x00008507 ; - }else { + }else if (f_sr <9000000 ) { s->convert_stream = msi3101_convert_stream_384; reg7 =0x0000a507 ; + }else { + s->convert_stream = msi3101_convert_stream_504; + reg7 =0x000c9407 ; }/*
Signature · 1 line
1.7.11.7
PATCH 2/3 msi3101: change stream format 384
In reply to Antti PalosaariAfter feeding different signal levels using RF generator and looking GNU Radio FFT sink I made decision to change bit shift 3 to bit shift 2 as there was very (too) huge visible leap in FFT sink GUI. Now it looks more natural.
--- drivers/staging/media/msi3101/sdr-msi3101.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-)
drivers/staging/media/msi3101/sdr-msi3101.c +6 -3
@@ -589,7 +589,7 @@ static int msi3101_convert_stream_504(struct msi3101_state *s, u32 *dst, }/* -* Converts signed ~10+3-bit integer into 32-bit IEEE floating point +* Converts signed ~10+2-bit integer into 32-bit IEEE floating point * representation. */ static u32 msi3101_convert_sample_384 (struct msi3101_state *s,u16 x,int shift)
@@ -601,12 +601,15 @@ static u32 msi3101_convert_sample_384(struct msi3101_state *s, u16 x, int shift)if (!x)return 0 ; -/* Convert 10-bit two's complement to 13-bit */ +if (shift ==3 ) + shift =2 ; + +/* Convert 10-bit two's complement to 12-bit */ if (x & (1 <<9 )) { x |= ~0U <<10 ;/* set all the rest bits to one */ x <<= shift; x = -x; - x &=0xfff ;/* result is 12 bit ... + sign */ + x &=0x7ff ;/* result is 11 bit ... + sign */ sign =1 <<31 ; }else { x <<= shift;
Signature · 1 line
1.7.11.7
PATCH 3/3 msi3101: few improvements for RF tuner
In reply to Antti Palosaari* Fix AM_MODE. Now it could work at least in theory, cannot test.
* Use greatest common divisor algo to divide PLL fractional parts.
* Fix IF frequency mode.
* + some very minor "style" issues
--- drivers/staging/media/msi3101/sdr-msi3101.c | 69 +++++++++++++++++------------ 1 file changed, 41 insertions(+), 28 deletions(-)
drivers/staging/media/msi3101/sdr-msi3101.c +41 -28
@@ -29,11 +29,9 @@* http://git.linuxtv.org/anttip/gr-kernel.git * * TODO: -* I will look these: -* - split RF tuner and USB ADC interface to own drivers (msi2500 and msi001) -* - move controls to V4L2 API -* * Help is very highly welcome for these + all the others you could imagine: +* - split USB ADC interface and RF tuner to own drivers (msi2500 and msi001) +* - move controls to V4L2 API * - use libv4l2 for stream format conversions * - gr-kernel: switch to v4l2_mmap (current read eats a lot of cpu) * - SDRSharp support
@@ -41,6 +39,7 @@#include <linux/module.h> #include <linux/slab.h> +#include <linux/gcd.h> #include <media/v4l2-device.h> #include <media/v4l2-ioctl.h> #include <media/v4l2-ctrls.h>
@@ -1331,25 +1330,25 @@ err:static int msi3101_set_tuner (struct msi3101_state *s) {int ret, i, len; -unsigned int n, m, thresh, frac, vco_step, tmp; +unsigned int n, m, thresh, frac, vco_step, tmp, f_if1;u32 reg;u64 f_vco; -u8 mode, lo_div; +u8 mode, filter_mode, lo_div;const struct msi3101_gain *gain_lut;static const struct {u32 rf;u8 mode;u8 lo_div; } band_lut[] = { - {47000000 ,0x01 ,16 },/* AM_MODE1 */ - {108000000 ,0x02 ,32 },/* VHF_MODE */ - {330000000 ,0x04 ,16 },/* B3_MODE */ - {960000000 ,0x08 ,4 },/* B45_MODE */ - { ~0U ,0x10 ,2 },/* BL_MODE */ + {50000000 ,0xe1 ,16 },/* AM_MODE2, antenna 2 */ + {108000000 ,0x42 ,32 },/* VHF_MODE */ + {330000000 ,0x44 ,16 },/* B3_MODE */ + {960000000 ,0x48 ,4 },/* B45_MODE */ + { ~0U ,0x50 ,2 },/* BL_MODE */ };static const struct {u32 freq; -u8 val; +u8 filter_mode; } if_freq_lut[] = { {0 ,0x03 },/* Zero IF */ {450000 ,0x02 },/* 450 kHz IF */
@@ -1370,19 +1369,19 @@ static int msi3101_set_tuner(struct msi3101_state *s) {8000000, 0x07}, /* 8 MHz */ }; -unsigned int rf_freq = s->ctrl_tuner_rf->val64; +unsigned int f_rf = s->ctrl_tuner_rf->val64;/* * bandwidth (Hz) * 200000, 300000, 600000, 1536000, 5000000, 6000000, 7000000, 8000000 */ -int bandwidth = s->ctrl_tuner_bw->val; +unsigned int bandwidth = s->ctrl_tuner_bw->val;/* * intermediate frequency (Hz) * 0, 450000, 1620000, 2048000 */ -int if_freq = s->ctrl_tuner_if->val; +unsigned int f_if = s->ctrl_tuner_if->val;/* * gain reduction (dB)
@@ -1392,13 +1391,13 @@ static int msi3101_set_tuner(struct msi3101_state *s)int gain = s->ctrl_tuner_gain->val;dev_dbg (&s->udev->dev, -"%s: rf_freq=%d bandwidth=%d if_freq=%d gain=%d\n" , - __func__, rf_freq, bandwidth, if_freq, gain); +"%s: f_rf=%d bandwidth=%d f_if=%d gain=%d\n" , + __func__, f_rf, bandwidth, f_if, gain); ret = -EINVAL ;for (i =0 ; i <ARRAY_SIZE (band_lut); i++) { -if (rf_freq <= band_lut[i].rf) { +if (f_rf <= band_lut[i].rf) { mode = band_lut[i].mode; lo_div = band_lut[i].lo_div;break ;
@@ -1408,9 +1407,15 @@ static int msi3101_set_tuner(struct msi3101_state *s)if (i ==ARRAY_SIZE (band_lut)) goto err; +/* AM_MODE is upconverted */ +if ((mode >>0 ) &0x1 ) + f_if1 =5 *F_REF ; +else + f_if1 =0 ; +for (i =0 ; i <ARRAY_SIZE (if_freq_lut); i++) { -if (if_freq == if_freq_lut[i].freq) { - if_freq = if_freq_lut[i].val; +if (f_if == if_freq_lut[i].freq) { + filter_mode = if_freq_lut[i].filter_mode;break ; } }
@@ -1430,8 +1435,7 @@ static int msi3101_set_tuner(struct msi3101_state *s)#define F_OUT_STEP 1#define R_REF 4 -#define F_IF 0 - f_vco = (rf_freq +F_IF ) * lo_div; + f_vco = (f_rf + f_if + f_if1) * lo_div; n = f_vco / (F_REF *R_REF ); m = f_vco % (F_REF *R_REF );
@@ -1439,7 +1443,12 @@ static int msi3101_set_tuner(struct msi3101_state *s) thresh = (F_REF *R_REF ) / vco_step; frac =1ul * thresh * m / (F_REF *R_REF ); -/* Divide to reg max. After that RF resolution will be +-500Hz. */ +/* Find out greatest common divisor and divide to smaller. */ + tmp =gcd (thresh, frac); + thresh /= tmp; + frac /= tmp; + +/* Force divide to reg max. Resolution will be reduced. */ tmp =DIV_ROUND_UP (thresh,4095 ); thresh =DIV_ROUND_CLOSEST (thresh, tmp); frac =DIV_ROUND_CLOSEST (frac, tmp);
@@ -1451,15 +1460,19 @@ static int msi3101_set_tuner(struct msi3101_state *s)dev_dbg (&s->udev->dev,"%s: rf=%u:%u n=%d thresh=%d frac=%d\n" , - __func__, rf_freq, tmp, n, thresh, frac); + __func__, f_rf, tmp, n, thresh, frac); ret =msi3101_tuner_write (s,0x00000e ); +if (ret) + goto err; + ret =msi3101_tuner_write (s,0x000003 ); +if (ret) + goto err; reg =0 <<0 ; reg |= mode <<4 ; - reg |=1 <<10 ; - reg |= if_freq <<12 ; + reg |= filter_mode <<12 ; reg |= bandwidth <<14 ; reg |=0x02 <<17 ; reg |=0x00 <<20 ;
@@ -1482,10 +1495,10 @@ static int msi3101_set_tuner(struct msi3101_state *s)if (ret) goto err; -if (rf_freq <120000000 ) { +if (f_rf <120000000 ) { gain_lut = msi3101_gain_lut_120; len =ARRAY_SIZE (msi3101_gain_lut_120); - }else if (rf_freq <245000000 ) { + }else if (f_rf <245000000 ) { gain_lut = msi3101_gain_lut_245; len =ARRAY_SIZE (msi3101_gain_lut_120); }else {
Signature · 1 line
1.7.11.7