From a80abc58feda48f868d748bde8c88592c2892b1d Mon Sep 17 00:00:00 2001 From: Mauro Carvalho Chehab Date: Fri, 5 Apr 2013 14:35:18 -0300 Subject: [media] 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) >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. Signed-off-by: Mauro Carvalho Chehab Tested-by: Antti Palosaari --- drivers/media/tuners/r820t.h | 55 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 drivers/media/tuners/r820t.h (limited to 'drivers/media/tuners/r820t.h') diff --git a/drivers/media/tuners/r820t.h b/drivers/media/tuners/r820t.h new file mode 100644 index 000000000000..a64a7b630729 --- /dev/null +++ b/drivers/media/tuners/r820t.h @@ -0,0 +1,55 @@ +/* + * Elonics R820T silicon tuner driver + * + * Copyright (C) 2012 Antti Palosaari + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef R820T_H +#define R820T_H + +#include +#include "dvb_frontend.h" + +enum r820t_chip { + CHIP_R820T, + CHIP_R828S, + CHIP_R820C, +}; + +struct r820t_config { + u8 i2c_addr; /* 0x34 */ + + u32 xtal; + enum r820t_chip rafael_chip; + unsigned max_i2c_msg_len; +}; + +#if IS_ENABLED(CONFIG_MEDIA_TUNER_R820T) +struct dvb_frontend *r820t_attach(struct dvb_frontend *fe, + struct i2c_adapter *i2c, + const struct r820t_config *cfg); +#else +static inline struct dvb_frontend *r820t_attach(struct dvb_frontend *fe, + struct i2c_adapter *i2c, + const struct r820t_config *cfg) +{ + pr_warn("%s: driver disabled by Kconfig\n", __func__); + return NULL; +} +#endif + +#endif -- cgit v1.2.3 From 84ddc33c20cd026871eb3585ed77badacb0fc113 Mon Sep 17 00:00:00 2001 From: Mauro Carvalho Chehab Date: Wed, 10 Apr 2013 08:51:45 -0300 Subject: [media] r820t: add support for diplexer This is part of the original driver, and adding it doesn't hurt, so add it, to better sync the code. Signed-off-by: Mauro Carvalho Chehab Tested-by: Antti Palosaari --- drivers/media/tuners/r820t.c | 12 ++++++++++++ drivers/media/tuners/r820t.h | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) (limited to 'drivers/media/tuners/r820t.h') diff --git a/drivers/media/tuners/r820t.c b/drivers/media/tuners/r820t.c index bb154449a2cf..5be4635c521d 100644 --- a/drivers/media/tuners/r820t.c +++ b/drivers/media/tuners/r820t.c @@ -101,6 +101,7 @@ struct r820t_freq_range { }; #define VCO_POWER_REF 0x02 +#define DIP_FREQ 32000000 /* * Static constants @@ -751,6 +752,17 @@ static int r820t_sysfreq_sel(struct r820t_priv *priv, u32 freq, break; } + if (priv->cfg->use_diplexer && + ((priv->cfg->rafael_chip == CHIP_R820T) || + (priv->cfg->rafael_chip == CHIP_R828S) || + (priv->cfg->rafael_chip == CHIP_R820C))) { + if (freq > DIP_FREQ) + air_cable1_in = 0x00; + else + air_cable1_in = 0x60; + cable2_in = 0x00; + } + rc = r820t_write_reg_mask(priv, 0x1d, lna_top, 0xc7); if (rc < 0) return rc; diff --git a/drivers/media/tuners/r820t.h b/drivers/media/tuners/r820t.h index a64a7b630729..949575a41d49 100644 --- a/drivers/media/tuners/r820t.h +++ b/drivers/media/tuners/r820t.h @@ -32,10 +32,10 @@ enum r820t_chip { struct r820t_config { u8 i2c_addr; /* 0x34 */ - u32 xtal; enum r820t_chip rafael_chip; unsigned max_i2c_msg_len; + bool use_diplexer; }; #if IS_ENABLED(CONFIG_MEDIA_TUNER_R820T) -- cgit v1.2.3 From d75d538899da00bdf8f152c65a99eda1ab59daa3 Mon Sep 17 00:00:00 2001 From: Mauro Carvalho Chehab Date: Wed, 10 Apr 2013 15:54:46 -0300 Subject: [media] r820t: proper initialize the PLL register The rtl-sdr library, from where this driver was initially based, doesn't use half PLL clock, but this is used on the Realtek Kernel driver. So, also do the same here. Signed-off-by: Mauro Carvalho Chehab Tested-by: Antti Palosaari --- drivers/media/tuners/r820t.c | 43 ++++++++++++++++++++++++++++--------------- drivers/media/tuners/r820t.h | 3 +++ 2 files changed, 31 insertions(+), 15 deletions(-) (limited to 'drivers/media/tuners/r820t.h') diff --git a/drivers/media/tuners/r820t.c b/drivers/media/tuners/r820t.c index 279be4f78e7a..07d032350c1b 100644 --- a/drivers/media/tuners/r820t.c +++ b/drivers/media/tuners/r820t.c @@ -522,10 +522,12 @@ static int r820t_set_mux(struct r820t_priv *priv, u32 freq) return rc; } -static int r820t_set_pll(struct r820t_priv *priv, u32 freq) +static int r820t_set_pll(struct r820t_priv *priv, enum v4l2_tuner_type type, + u32 freq) { u64 tmp64, vco_freq; int rc, i; + unsigned sleep_time = 10000; u32 vco_fra; /* VCO contribution by SDM (kHz) */ u32 vco_min = 1770000; u32 vco_max = vco_min * 2; @@ -535,17 +537,34 @@ static int r820t_set_pll(struct r820t_priv *priv, u32 freq) u8 mix_div = 2; u8 div_buf = 0; u8 div_num = 0; + u8 refdiv2 = 0; u8 ni, si, nint, vco_fine_tune, val; u8 data[5]; - freq = freq / 1000; /* Frequency in kHz */ - + /* Frequency in kHz */ + freq = freq / 1000; pll_ref = priv->cfg->xtal / 1000; - tuner_dbg("set r820t pll for frequency %d kHz = %d\n", freq, pll_ref); + if ((priv->cfg->rafael_chip == CHIP_R620D) || + (priv->cfg->rafael_chip == CHIP_R828D) || + (priv->cfg->rafael_chip == CHIP_R828)) { + /* ref set refdiv2, reffreq = Xtal/2 on ATV application */ + if (type != V4L2_TUNER_DIGITAL_TV) { + pll_ref /= 2; + refdiv2 = 0x10; + sleep_time = 20000; + } + } else { + if (priv->cfg->xtal > 24000000) { + pll_ref /= 2; + refdiv2 = 0x10; + } + } - /* FIXME: this seems to be a hack - probably it can be removed */ - rc = r820t_write_reg_mask(priv, 0x10, 0x00, 0x00); + tuner_dbg("set r820t pll for frequency %d kHz = %d%s\n", + freq, pll_ref, refdiv2 ? " / 2" : ""); + + rc = r820t_write_reg_mask(priv, 0x10, refdiv2, 0x10); if (rc < 0) return rc; @@ -598,8 +617,6 @@ static int r820t_set_pll(struct r820t_priv *priv, u32 freq) do_div(tmp64, 1000); vco_fra = (u16)(tmp64); - pll_ref /= 1000; - /* boundary spur prevention */ if (vco_fra < pll_ref / 64) { vco_fra = 0; @@ -653,11 +670,7 @@ static int r820t_set_pll(struct r820t_priv *priv, u32 freq) return rc; for (i = 0; i < 2; i++) { - /* - * FIXME: Rafael chips R620D, R828D and R828 seems to - * need 20 ms for analog TV - */ - usleep_range(10000, 11000); + usleep_range(sleep_time, sleep_time + 1000); /* Check if PLL has locked */ rc = r820t_read(priv, 0x00, data, 3); @@ -1040,7 +1053,7 @@ static int r820t_set_tv_standard(struct r820t_priv *priv, if (rc < 0) return rc; - rc = r820t_set_pll(priv, filt_cal_lo); + rc = r820t_set_pll(priv, type, filt_cal_lo); if (rc < 0 || !priv->has_lock) return rc; @@ -1244,7 +1257,7 @@ static int generic_set_freq(struct dvb_frontend *fe, if (rc < 0) goto err; - rc = r820t_set_pll(priv, lo_freq); + rc = r820t_set_pll(priv, type, lo_freq); if (rc < 0 || !priv->has_lock) goto err; diff --git a/drivers/media/tuners/r820t.h b/drivers/media/tuners/r820t.h index 949575a41d49..4c0823b21693 100644 --- a/drivers/media/tuners/r820t.h +++ b/drivers/media/tuners/r820t.h @@ -26,6 +26,9 @@ enum r820t_chip { CHIP_R820T, + CHIP_R620D, + CHIP_R828D, + CHIP_R828, CHIP_R828S, CHIP_R820C, }; -- cgit v1.2.3 From 7063c1456fc0a6366f467f10e7a35178ecc881ad Mon Sep 17 00:00:00 2001 From: Mauro Carvalho Chehab Date: Thu, 25 Apr 2013 15:40:21 -0300 Subject: [media] r820t: Remove a warning for an unused value Currently, the driver complains about the pre_detect var: drivers/media/tuners/r820t.c: In function 'r820t_sysfreq_sel': drivers/media/tuners/r820t.c:722:31: warning: variable 'pre_dect' set but not used [-Wunused-but-set-variable] While rtl8232 code comments it, perhaps some other driver may use. So, the better is to keep the code there, allowing to enable it via r820t config data. Signed-off-by: Mauro Carvalho Chehab --- drivers/media/tuners/r820t.c | 7 +++++++ drivers/media/tuners/r820t.h | 1 + 2 files changed, 8 insertions(+) (limited to 'drivers/media/tuners/r820t.h') diff --git a/drivers/media/tuners/r820t.c b/drivers/media/tuners/r820t.c index e6e7a06d2b40..4835021aa3b6 100644 --- a/drivers/media/tuners/r820t.c +++ b/drivers/media/tuners/r820t.c @@ -797,6 +797,13 @@ static int r820t_sysfreq_sel(struct r820t_priv *priv, u32 freq, cable2_in = 0x00; } + + if (priv->cfg->use_predetect) { + rc = r820t_write_reg_mask(priv, 0x06, pre_dect, 0x40); + if (rc < 0) + return rc; + } + rc = r820t_write_reg_mask(priv, 0x1d, lna_top, 0xc7); if (rc < 0) return rc; diff --git a/drivers/media/tuners/r820t.h b/drivers/media/tuners/r820t.h index 4c0823b21693..48af3548027d 100644 --- a/drivers/media/tuners/r820t.h +++ b/drivers/media/tuners/r820t.h @@ -39,6 +39,7 @@ struct r820t_config { enum r820t_chip rafael_chip; unsigned max_i2c_msg_len; bool use_diplexer; + bool use_predetect; }; #if IS_ENABLED(CONFIG_MEDIA_TUNER_R820T) -- cgit v1.2.3