rtl28xxu: add experimental support for r820t
Subsystems (2)
cover rtl28xxu: add experimental support for r820t
Several rtl28xxu are currently shipped with a r820t tuner. Add experimental suppor for it.
NOTE: I don't have DVB-T signal here, so I couldn't fully test the driver. By sniffing the USB traffic from rtl-sdr and comparing with this driver's traffic, it seems to be working fine, at least up to tuner callibration. After that, I would need a DVB-T lock to test the rest of the driver.
Mauro Carvalho Chehab (2):
r820t: Add a tuner driver for Rafael Micro R820T silicon tuner
rtl28xxu: add support for Rafael Micro r820t
drivers/media/tuners/Kconfig | 7 + drivers/media/tuners/Makefile | 1 + drivers/media/tuners/r820t.c | 1486 +++++++++++++++++++++++++++++++ drivers/media/tuners/r820t.h | 55 ++ drivers/media/usb/dvb-usb-v2/Kconfig | 1 + drivers/media/usb/dvb-usb-v2/rtl28xxu.c | 30 + drivers/media/usb/dvb-usb-v2/rtl28xxu.h | 1 + 7 files changed, 1581 insertions(+) create mode 100644 drivers/media/tuners/r820t.c create mode 100644 drivers/media/tuners/r820t.h
Signature · 1 line
1.8.1.4
RFC PATCH 1/2 r820t: Add a tuner driver for Rafael Micro R820T silicon tuner
This driver was written from scratch, based on an existing driver that it is part of rtl-sdr git tree, released under GPLv2:
https://groups.google.com/forum/#!topic/ultra-cheap-sdr/Y3rBEOFtHug https://github.com/n1gp/gr-baz http://cgit.osmocom.org/rtl-sdr/plain/src/tuner_r820t.c (there are also other variants of it out there)
Quoted 1 line
From what I understood from the threads, the original driver was converted
to userspace from a Realtek tree. I couldn't find the original tree. However, the original driver look awkward on my eyes. So, I decided to write a new version from it from the scratch, while trying to reproduce everything found there.
TODO:
- After locking, the original driver seems to have some routines to improve reception. This was not implemented here yet.
- RF Gain set/get is not implemented.
--- drivers/media/tuners/Kconfig | 7 + drivers/media/tuners/Makefile | 1 + drivers/media/tuners/r820t.c | 1486 +++++++++++++++++++++++++++++++++++++++++ drivers/media/tuners/r820t.h | 55 ++ 4 files changed, 1549 insertions(+) create mode 100644 drivers/media/tuners/r820t.c create mode 100644 drivers/media/tuners/r820t.h
drivers/media/tuners/Kconfig +7 -0
@@ -241,4 +241,11 @@ config MEDIA_TUNER_TUA9001 default m if !MEDIA_SUBDRV_AUTOSELECT help Infineon TUA 9001 silicon tuner driver. + +config MEDIA_TUNER_R820T + tristate "Rafael Micro R820T silicon tuner" + depends on MEDIA_SUPPORT && I2C + default m if !MEDIA_SUBDRV_AUTOSELECT + help + Rafael Micro R820T silicon tuner driver. endmenu
drivers/media/tuners/Makefile +1 -0
@@ -34,6 +34,7 @@ obj-$(CONFIG_MEDIA_TUNER_TUA9001) += tua9001.o obj-$(CONFIG_MEDIA_TUNER_FC0011) += fc0011.o obj-$(CONFIG_MEDIA_TUNER_FC0012) += fc0012.o obj-$(CONFIG_MEDIA_TUNER_FC0013) += fc0013.o +obj-$(CONFIG_MEDIA_TUNER_R820T) += r820t.o ccflags-y += -I$(srctree)/drivers/media/dvb-core ccflags-y += -I$(srctree)/drivers/media/dvb-frontends
drivers/media/tuners/r820t.c new file +1486 -0
1,486 lines not on this page · raw message
drivers/media/tuners/r820t.h new file +55 -0
55 lines not on this page · raw message
Signature · 1 line
1.8.1.4
RFC PATCH 2/2 rtl28xxu: add support for Rafael Micro r820t
In reply to Mauro Carvalho ChehabThis tuner is used on some rtl2882 dongles. Add it to the driver.
--- drivers/media/usb/dvb-usb-v2/Kconfig | 1 + drivers/media/usb/dvb-usb-v2/rtl28xxu.c | 30 ++++++++++++++++++++++++++++++ drivers/media/usb/dvb-usb-v2/rtl28xxu.h | 1 + 3 files changed, 32 insertions(+)
drivers/media/usb/dvb-usb-v2/Kconfig +1 -0
@@ -144,6 +144,7 @@ config DVB_USB_RTL28XXU select MEDIA_TUNER_FC0013 if MEDIA_SUBDRV_AUTOSELECT select MEDIA_TUNER_E4000 if MEDIA_SUBDRV_AUTOSELECT select MEDIA_TUNER_FC2580 if MEDIA_SUBDRV_AUTOSELECT + select MEDIA_TUNER_R820T if MEDIA_SUBDRV_AUTOSELECT help Say Y here to support the Realtek RTL28xxU DVB USB receiver.
drivers/media/usb/dvb-usb-v2/rtl28xxu.c +30 -0
@@ -33,6 +33,7 @@#include "e4000.h" #include "fc2580.h" #include "tua9001.h" +#include "r820t.h" DVB_DEFINE_MOD_OPT_ADAPTER_NR (adapter_nr);
@@ -375,6 +376,7 @@ static int rtl2832u_read_config(struct dvb_usb_device *d)struct rtl28xxu_req req_mxl5007t = {0xd9c0 ,CMD_I2C_RD ,1 , buf};struct rtl28xxu_req req_e4000 = {0x02c8 ,CMD_I2C_RD ,1 , buf};struct rtl28xxu_req req_tda18272 = {0x00c0 ,CMD_I2C_RD ,2 , buf}; +struct rtl28xxu_req req_r820t = {0x0034 ,CMD_I2C_RD ,5 , buf};dev_dbg (&d->udev->dev,"%s:\n" , __func__);
@@ -479,6 +481,14 @@ static int rtl2832u_read_config(struct dvb_usb_device *d) goto found; } +/* check R820T by reading tuner stats at I2C addr 0x1a */ + ret =rtl28xxu_ctrl_msg (d, &req_r820t); +if (ret ==0 ) { + priv->tuner =TUNER_RTL2832_R820T ; + priv->tuner_name ="R820T" ; + goto found; + } + found:dev_dbg (&d->udev->dev,"%s: tuner=%s\n" , __func__, priv->tuner_name);
@@ -589,6 +599,12 @@ static struct rtl2832_config rtl28xxu_rtl2832_e4000_config = { .tuner = TUNER_RTL2832_E4000, }; +static struct rtl2832_config rtl28xxu_rtl2832_r820t_config = { + .i2c_addr =0x10 , + .xtal =28800000 , + .tuner =TUNER_RTL2832_R820T , +}; +static int rtl2832u_fc0012_tuner_callback (struct dvb_usb_device *d,int cmd,int arg) {
@@ -728,6 +744,9 @@ static int rtl2832u_frontend_attach(struct dvb_usb_adapter *adap)case TUNER_RTL2832_E4000 : rtl2832_config = &rtl28xxu_rtl2832_e4000_config;break ; +case TUNER_RTL2832_R820T : + rtl2832_config = &rtl28xxu_rtl2832_r820t_config; +break ;default :dev_err (&d->udev->dev,"%s: unknown tuner=%s\n" ,KBUILD_MODNAME , priv->tuner_name);
@@ -835,6 +854,13 @@ static struct tua9001_config rtl2832u_tua9001_config = { .i2c_addr = 0x60, }; +static const struct r820t_config rtl2832u_r820t_config = { + .i2c_addr =0x1a , + .xtal =28800000 , + .max_i2c_msg_len =2 , + .rafael_chip =CHIP_R820T , +}; +static int rtl2832u_tuner_attach (struct dvb_usb_adapter *adap) {int ret;
@@ -884,6 +910,10 @@ static int rtl2832u_tuner_attach(struct dvb_usb_adapter *adap) fe =dvb_attach (tua9001_attach, adap->fe[0 ], &d->i2c_adap, &rtl2832u_tua9001_config);break ; +case TUNER_RTL2832_R820T : + fe =dvb_attach (r820t_attach, adap->fe[0 ], &d->i2c_adap, + &rtl2832u_r820t_config); +break ;default : fe =NULL ; dev_err(&d->udev->dev, "%s: unknown tuner=%d\n", KBUILD_MODNAME,
drivers/media/usb/dvb-usb-v2/rtl28xxu.h +1 -0
@@ -82,6 +82,7 @@ enum rtl28xxu_tuner {TUNER_RTL2832_E4000 ,TUNER_RTL2832_TDA18272 ,TUNER_RTL2832_FC0013 , + TUNER_RTL2832_R820T, }; struct rtl28xxu_req {
Signature · 1 line
1.8.1.4
RFC PATCH 1/5 r820t: Give a better estimation of the signal strength
In reply to Mauro Carvalho ChehabInstead of a binary signal strength measure, use the tuner gain to obtain a better estimation of the signal strength.
--- drivers/media/tuners/r820t.c | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-)
drivers/media/tuners/r820t.c +27 -3
@@ -1082,6 +1082,18 @@ static int r820t_set_tv_standard(struct r820t_priv *priv,return 0 ; } +static int r820t_read_gain (struct r820t_priv *priv) +{ +u8 data[4 ]; +int rc; + + rc =r820_read (priv,0x00 , data,sizeof (data)); +if (rc <0 ) +return rc; + +return ((data[3 ] &0x0f ) <<1 ) + ((data[3 ] &0xf0 ) >>4 ); +} + static int generic_set_freq(struct dvb_frontend *fe, u32 freq /* in HZ */, unsigned bw,
@@ -1353,11 +1365,23 @@ static int r820t_set_params(struct dvb_frontend *fe)static int r820t_signal (struct dvb_frontend *fe,u16 *strength) {struct r820t_priv *priv = fe->tuner_priv; +int rc =0 ; -if (priv->has_lock) - *strength =0xffff ; -else +if (priv->has_lock) { + rc =r820t_read_gain (priv); +if (rc <0 ) +return rc; + +/* A higher gain at LNA means a lower signal strength */ + *strength = (45 - rc) <<4 |0xff ; + }else { *strength =0 ; + } + +tuner_dbg ("%s: %s, gain=%d strength=%d\n" , + __func__, + priv->has_lock ?"PLL locked" :"no signal" , + rc, *strength);return 0 ; }
Signature · 1 line
1.8.1.4
RFC PATCH 2/5 r820t: Set gain mode to auto
This tuner works with 2 modes: automatic gain mode and manual gain mode. Put it into automatic mode, as we currently don't have any API for manual gain adjustment.
The logic to allow setting the manual mode is there, as it is just a few extra code. This way, if/when we latter add support for setting the gain mode, the code is already there.
--- drivers/media/tuners/r820t.c | 91 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+)
drivers/media/tuners/r820t.c +91 -0
@@ -321,6 +321,20 @@ static int r820t_xtal_capacitor[][2] = { };/* +* measured with a Racal 6103E GSM test set at 928 MHz with -60 dBm +* input power, for raw results see: +* http://steve-m.de/projects/rtl-sdr/gain_measurement/r820t/ +*/ + +static const int r820t_lna_gain_steps[] = { +0 ,9 ,13 ,40 ,38 ,13 ,31 ,22 ,26 ,31 ,26 ,14 ,19 ,5 ,35 ,13 +}; + +static const int r820t_mixer_gain_steps[] = { +0 ,5 ,10 ,10 ,19 ,9 ,10 ,25 ,17 ,10 ,8 ,16 ,13 ,6 ,3 ,-8 +}; + +/* * I2C read/write code and shadow registers logic */ static void shadow_store(struct r820t_priv *priv, u8 reg, const u8 *val,
@@ -1094,6 +1108,78 @@ static int r820t_read_gain(struct r820t_priv *priv)return ((data[3 ] &0x0f ) <<1 ) + ((data[3 ] &0xf0 ) >>4 ); } +static int r820t_set_gain_mode (struct r820t_priv *priv, +bool set_manual_gain, +int gain) +{ +int rc; + +if (set_manual_gain) { +int i, total_gain =0 ; +uint8_t mix_index =0 , lna_index =0 ; +u8 data[4 ]; + +/* LNA auto off */ + rc =r820t_write_reg_mask (priv,0x05 ,0x10 ,0x10 ); +if (rc <0 ) +return rc; + +/* Mixer auto off */ + rc =r820t_write_reg_mask (priv,0x07 ,0 ,0x10 ); +if (rc <0 ) +return rc; + + rc =r820_read (priv,0x00 , data,sizeof (data)); +if (rc <0 ) +return rc; + +/* set fixed VGA gain for now (16.3 dB) */ + rc =r820t_write_reg_mask (priv,0x0c ,0x08 ,0x9f ); +if (rc <0 ) +return rc; + +for (i =0 ; i <15 ; i++) { +if (total_gain >= gain) +break ; + + total_gain += r820t_lna_gain_steps[++lna_index]; + +if (total_gain >= gain) +break ; + + total_gain += r820t_mixer_gain_steps[++mix_index]; + } + +/* set LNA gain */ + rc =r820t_write_reg_mask (priv,0x05 , lna_index,0x0f ); +if (rc <0 ) +return rc; + +/* set Mixer gain */ + rc =r820t_write_reg_mask (priv,0x07 , mix_index,0x0f ); +if (rc <0 ) +return rc; + }else { +/* LNA */ + rc =r820t_write_reg_mask (priv,0x05 ,0 ,0xef ); +if (rc <0 ) +return rc; + +/* Mixer */ + rc =r820t_write_reg_mask (priv,0x07 ,0x10 ,0xef ); +if (rc <0 ) +return rc; + +/* set fixed VGA gain for now (26.5 dB) */ + rc =r820t_write_reg_mask (priv,0x0c ,0x0b ,0x9f ); +if (rc <0 ) +return rc; + } + +return 0 ; +} + + static int generic_set_freq(struct dvb_frontend *fe, u32 freq /* in HZ */, unsigned bw,
@@ -1121,6 +1207,11 @@ static int generic_set_freq(struct dvb_frontend *fe, rc =r820t_set_mux (priv, lo_freq);if (rc <0 ) goto err; + + rc =r820t_set_gain_mode (priv, true,0 ); +if (rc <0 ) + goto err; + rc =r820t_set_pll (priv, lo_freq);if (rc <0 || !priv->has_lock) goto err;
Signature · 1 line
1.8.1.4
RFC PATCH 3/5 rtl28xxu: use r820t to obtain the signal strength
In reply to Mauro Carvalho ChehabNow that we can get the strength from r820t, use it.
--- drivers/media/usb/dvb-usb-v2/rtl28xxu.c | 4 ++++ 1 file changed, 4 insertions(+)
drivers/media/usb/dvb-usb-v2/rtl28xxu.c +4 -0
@@ -913,6 +913,10 @@ static int rtl2832u_tuner_attach(struct dvb_usb_adapter *adap)case TUNER_RTL2832_R820T : fe =dvb_attach (r820t_attach, adap->fe[0 ], &d->i2c_adap, &rtl2832u_r820t_config); + +/* Use tuner to get the signal strength */ + adap->fe[0 ]->ops.read_signal_strength = + adap->fe[0 ]->ops.tuner_ops.get_rf_strength;break ;default : fe =NULL ;
Signature · 1 line
1.8.1.4
RFC PATCH 4/5 r820t: proper lock and set the I2C gate
In reply to Mauro Carvalho ChehabAs this tuner can be used by analog and digital parts of the driver, be sure that all ops that access the hardware will be be properly locked.
--- drivers/media/tuners/r820t.c | 50 +++++++++++++++++++++++++++++--------------- 1 file changed, 33 insertions(+), 17 deletions(-)
drivers/media/tuners/r820t.c +33 -17
@@ -1193,8 +1193,6 @@ static int generic_set_freq(struct dvb_frontend *fe,tuner_dbg ("should set frequency to %d kHz, bw %d MHz\n" , freq /1000 , bw); -mutex_lock (&priv->lock); -if ((type ==V4L2_TUNER_ANALOG_TV ) && (std ==V4L2_STD_SECAM_LC )) lo_freq = freq - priv->int_freq;else
@@ -1218,7 +1216,6 @@ static int generic_set_freq(struct dvb_frontend *fe, rc =r820t_sysfreq_sel (priv, freq, type, std, delsys); err: -mutex_unlock (&priv->lock);if (rc <0 )tuner_dbg ("%s: failed=%d\n" , __func__, rc);
@@ -1335,6 +1332,8 @@ static int r820t_xtal_check(struct r820t_priv *priv)/* * r820t frontend operations and tuner attach code +* +* All driver locks and i2c control are only in this part of the code */ static int r820t_init (struct dvb_frontend *fe)
@@ -1345,11 +1344,10 @@ static int r820t_init(struct dvb_frontend *fe)tuner_dbg ("%s:\n" , __func__); +mutex_lock (&priv->lock);if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl (fe,1 ); -mutex_lock (&priv->lock); -if ((priv->cfg->rafael_chip ==CHIP_R820T ) || (priv->cfg->rafael_chip ==CHIP_R828S ) || (priv->cfg->rafael_chip ==CHIP_R820C )) {
@@ -1369,17 +1367,13 @@ static int r820t_init(struct dvb_frontend *fe) rc =r820t_write (priv,0x05 , r820t_init_array,sizeof (r820t_init_array)); -mutex_unlock (&priv->lock); - -if (fe->ops.i2c_gate_ctrl) - fe->ops.i2c_gate_ctrl (fe,0 ); - -return rc; err:if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl (fe,0 ); +mutex_unlock (&priv->lock); -tuner_dbg ("%s: failed=%d\n" , __func__, rc); +if (rc <0 ) +tuner_dbg ("%s: failed=%d\n" , __func__, rc);return rc; }
@@ -1390,15 +1384,15 @@ static int r820t_sleep(struct dvb_frontend *fe)tuner_dbg ("%s:\n" , __func__); +mutex_lock (&priv->lock);if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl (fe,1 ); -mutex_lock (&priv->lock); rc =r820t_standby (priv); -mutex_unlock (&priv->lock);if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl (fe,0 ); +mutex_unlock (&priv->lock);tuner_dbg ("%s: failed=%d\n" , __func__, rc);return rc;
@@ -1409,6 +1403,7 @@ static int r820t_set_analog_freq(struct dvb_frontend *fe, {struct r820t_priv *priv = fe->tuner_priv;unsigned bw; +int rc;tuner_dbg ("%s called\n" , __func__);
@@ -1421,8 +1416,18 @@ static int r820t_set_analog_freq(struct dvb_frontend *fe,else bw =8 ; -return generic_set_freq (fe,62500l * p->frequency, bw, -V4L2_TUNER_ANALOG_TV , p->std,SYS_UNDEFINED ); +mutex_lock (&priv->lock); +if (fe->ops.i2c_gate_ctrl) + fe->ops.i2c_gate_ctrl (fe,1 ); + + rc =generic_set_freq (fe,62500l * p->frequency, bw, +V4L2_TUNER_ANALOG_TV , p->std,SYS_UNDEFINED ); + +if (fe->ops.i2c_gate_ctrl) + fe->ops.i2c_gate_ctrl (fe,0 ); +mutex_unlock (&priv->lock); + +return rc; }static int r820t_set_params (struct dvb_frontend *fe)
@@ -1435,6 +1440,7 @@ static int r820t_set_params(struct dvb_frontend *fe)tuner_dbg ("%s: delivery_system=%d frequency=%d bandwidth_hz=%d\n" , __func__, c->delivery_system, c->frequency, c->bandwidth_hz); +mutex_lock (&priv->lock);if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl (fe,1 );
@@ -1447,6 +1453,7 @@ static int r820t_set_params(struct dvb_frontend *fe)if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl (fe,0 ); +mutex_unlock (&priv->lock);if (rc)tuner_dbg ("%s: failed=%d\n" , __func__, rc);
@@ -1458,10 +1465,14 @@ static int r820t_signal(struct dvb_frontend *fe, u16 *strength)struct r820t_priv *priv = fe->tuner_priv;int rc =0 ; +mutex_lock (&priv->lock); +if (fe->ops.i2c_gate_ctrl) + fe->ops.i2c_gate_ctrl (fe,1 ); +if (priv->has_lock) { rc =r820t_read_gain (priv);if (rc <0 ) -return rc; + goto err;/* A higher gain at LNA means a lower signal strength */ *strength = (45 - rc) <<4 |0xff ;
@@ -1469,6 +1480,11 @@ static int r820t_signal(struct dvb_frontend *fe, u16 *strength) *strength =0 ; } +err: +if (fe->ops.i2c_gate_ctrl) + fe->ops.i2c_gate_ctrl (fe,0 ); +mutex_unlock (&priv->lock); + tuner_dbg("%s: %s, gain=%d strength=%d\n", __func__, priv->has_lock ? "PLL locked" : "no signal",
Signature · 1 line
1.8.1.4
RFC PATCH 5/5 rtl820t: Add a debug msg when PLL gets locked
In reply to Mauro Carvalho Chehab[ 2255.342797] r820t 3-001a: generic_set_freq: PLL locked on frequency 725476191 Hz, gain=45
--- drivers/media/tuners/r820t.c | 6 ++++++ 1 file changed, 6 insertions(+)
drivers/media/tuners/r820t.c +6 -0
@@ -1215,6 +1215,12 @@ static int generic_set_freq(struct dvb_frontend *fe, goto err; rc =r820t_sysfreq_sel (priv, freq, type, std, delsys); +if (rc <0 ) + goto err; + +tuner_dbg ("%s: PLL locked on frequency %d Hz, gain=%d\n" , + __func__, freq,r820t_read_gain (priv)); + err:if (rc <0 )
Signature · 1 line
1.8.1.4
I tested it and it didn't work at all. Latest commit was:
rtl820t: Add a debug msg when PLL gets locked
I used your git tree and rebased it top of master. There was some merge conflicts, but I was naturally able to fix.
regards
Antti
Quoted 24 lines · Mauro Carvalho Chehab
Several rtl28xxu are currently shipped with a r820t tuner. Add experimental suppor for it.
NOTE: I don't have DVB-T signal here, so I couldn't fully test the driver. By sniffing the USB traffic from rtl-sdr and comparing with this driver's traffic, it seems to be working fine, at least up to tuner callibration. After that, I would need a DVB-T lock to test the rest of the driver.
Mauro Carvalho Chehab (2):
r820t: Add a tuner driver for Rafael Micro R820T silicon tuner
rtl28xxu: add support for Rafael Micro r820tdrivers/media/tuners/Kconfig | 7 + drivers/media/tuners/Makefile | 1 + drivers/media/tuners/r820t.c | 1486 +++++++++++++++++++++++++++++++ drivers/media/tuners/r820t.h | 55 ++ drivers/media/usb/dvb-usb-v2/Kconfig | 1 + drivers/media/usb/dvb-usb-v2/rtl28xxu.c | 30 + drivers/media/usb/dvb-usb-v2/rtl28xxu.h | 1 + 7 files changed, 1581 insertions(+)create mode 100644
drivers/media/tuners/r820t.c
create mode 100644drivers/media/tuners/r820t.h
Signature · 1 line
http://palosaari.fi/