···207207 help208208 A DVB-S tuner module. Say Y when you want to support this frontend.209209210210+config DVB_TS2020211211+ tristate "Montage Tehnology TS2020 based tuners"212212+ depends on DVB_CORE && I2C213213+ default m if DVB_FE_CUSTOMISE214214+ help215215+ A DVB-S/S2 silicon tuner. Say Y when you want to support this tuner.216216+210217config DVB_DS3000211218 tristate "Montage Tehnology DS3000 based"212219 depends on DVB_CORE && I2C
···11+/*22+ Montage Technology TS2020 - Silicon Tuner driver33+ Copyright (C) 2009-2012 Konstantin Dimitrov <kosio.dimitrov@gmail.com>44+55+ Copyright (C) 2009-2012 TurboSight.com66+77+ This program is free software; you can redistribute it and/or modify88+ it under the terms of the GNU General Public License as published by99+ the Free Software Foundation; either version 2 of the License, or1010+ (at your option) any later version.1111+1212+ This program is distributed in the hope that it will be useful,1313+ but WITHOUT ANY WARRANTY; without even the implied warranty of1414+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the1515+ GNU General Public License for more details.1616+1717+ You should have received a copy of the GNU General Public License1818+ along with this program; if not, write to the Free Software1919+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.2020+ */2121+2222+#include "dvb_frontend.h"2323+#include "ts2020.h"2424+2525+#define TS2020_XTAL_FREQ 27000 /* in kHz */2626+2727+struct ts2020_state {2828+ u8 tuner_address;2929+ struct i2c_adapter *i2c;3030+};3131+3232+static int ts2020_readreg(struct dvb_frontend *fe, u8 reg)3333+{3434+ struct ts2020_state *state = fe->tuner_priv;3535+3636+ int ret;3737+ u8 b0[] = { reg };3838+ u8 b1[] = { 0 };3939+ struct i2c_msg msg[] = {4040+ {4141+ .addr = state->tuner_address,4242+ .flags = 0,4343+ .buf = b0,4444+ .len = 14545+ }, {4646+ .addr = state->tuner_address,4747+ .flags = I2C_M_RD,4848+ .buf = b1,4949+ .len = 15050+ }5151+ };5252+5353+ if (fe->ops.i2c_gate_ctrl)5454+ fe->ops.i2c_gate_ctrl(fe, 1);5555+5656+ ret = i2c_transfer(state->i2c, msg, 2);5757+5858+ if (fe->ops.i2c_gate_ctrl)5959+ fe->ops.i2c_gate_ctrl(fe, 0);6060+6161+ if (ret != 2) {6262+ printk(KERN_ERR "%s: reg=0x%x(error=%d)\n", __func__, reg, ret);6363+ return ret;6464+ }6565+6666+ return b1[0];6767+}6868+6969+static int ts2020_writereg(struct dvb_frontend *fe, int reg, int data)7070+{7171+ struct ts2020_state *state = fe->tuner_priv;7272+7373+ u8 buf[] = { reg, data };7474+ struct i2c_msg msg = { .addr = state->tuner_address,7575+ .flags = 0, .buf = buf, .len = 2 };7676+ int err;7777+7878+7979+ if (fe->ops.i2c_gate_ctrl)8080+ fe->ops.i2c_gate_ctrl(fe, 1);8181+8282+ err = i2c_transfer(state->i2c, &msg, 1);8383+8484+ if (fe->ops.i2c_gate_ctrl)8585+ fe->ops.i2c_gate_ctrl(fe, 0);8686+8787+ if (err != 1) {8888+ printk(KERN_ERR "%s: writereg error(err == %i, reg == 0x%02x,"8989+ " value == 0x%02x)\n", __func__, err, reg, data);9090+ return -EREMOTEIO;9191+ }9292+9393+ return 0;9494+}9595+9696+static int ts2020_init(struct dvb_frontend *fe)9797+{9898+ ts2020_writereg(fe, 0x42, 0x73);9999+ ts2020_writereg(fe, 0x05, 0x01);100100+ ts2020_writereg(fe, 0x62, 0xf5);101101+ return 0;102102+}103103+104104+static int ts2020_get_frequency(struct dvb_frontend *fe, u32 *frequency)105105+{106106+ u16 ndiv, div4;107107+108108+ div4 = (ts2020_readreg(fe, 0x10) & 0x10) >> 4;109109+110110+ ndiv = ts2020_readreg(fe, 0x01);111111+ ndiv &= 0x0f;112112+ ndiv <<= 8;113113+ ndiv |= ts2020_readreg(fe, 0x02);114114+115115+ /* actual tuned frequency, i.e. including the offset */116116+ *frequency = (ndiv - ndiv % 2 + 1024) * TS2020_XTAL_FREQ117117+ / (6 + 8) / (div4 + 1) / 2;118118+119119+ return 0;120120+}121121+122122+static int ts2020_set_params(struct dvb_frontend *fe)123123+{124124+ struct dtv_frontend_properties *c = &fe->dtv_property_cache;125125+126126+ u8 mlpf, mlpf_new, mlpf_max, mlpf_min, nlpf, div4;127127+ u16 value, ndiv;128128+ u32 srate = 0, f3db;129129+130130+ ts2020_init(fe);131131+132132+ /* unknown */133133+ ts2020_writereg(fe, 0x07, 0x02);134134+ ts2020_writereg(fe, 0x10, 0x00);135135+ ts2020_writereg(fe, 0x60, 0x79);136136+ ts2020_writereg(fe, 0x08, 0x01);137137+ ts2020_writereg(fe, 0x00, 0x01);138138+ div4 = 0;139139+140140+ /* calculate and set freq divider */141141+ if (c->frequency < 1146000) {142142+ ts2020_writereg(fe, 0x10, 0x11);143143+ div4 = 1;144144+ ndiv = ((c->frequency * (6 + 8) * 4) +145145+ (TS2020_XTAL_FREQ / 2)) /146146+ TS2020_XTAL_FREQ - 1024;147147+ } else {148148+ ts2020_writereg(fe, 0x10, 0x01);149149+ ndiv = ((c->frequency * (6 + 8) * 2) +150150+ (TS2020_XTAL_FREQ / 2)) /151151+ TS2020_XTAL_FREQ - 1024;152152+ }153153+154154+ ts2020_writereg(fe, 0x01, (ndiv & 0x0f00) >> 8);155155+ ts2020_writereg(fe, 0x02, ndiv & 0x00ff);156156+157157+ /* set pll */158158+ ts2020_writereg(fe, 0x03, 0x06);159159+ ts2020_writereg(fe, 0x51, 0x0f);160160+ ts2020_writereg(fe, 0x51, 0x1f);161161+ ts2020_writereg(fe, 0x50, 0x10);162162+ ts2020_writereg(fe, 0x50, 0x00);163163+ msleep(5);164164+165165+ /* unknown */166166+ ts2020_writereg(fe, 0x51, 0x17);167167+ ts2020_writereg(fe, 0x51, 0x1f);168168+ ts2020_writereg(fe, 0x50, 0x08);169169+ ts2020_writereg(fe, 0x50, 0x00);170170+ msleep(5);171171+172172+ value = ts2020_readreg(fe, 0x3d);173173+ value &= 0x0f;174174+ if ((value > 4) && (value < 15)) {175175+ value -= 3;176176+ if (value < 4)177177+ value = 4;178178+ value = ((value << 3) | 0x01) & 0x79;179179+ }180180+181181+ ts2020_writereg(fe, 0x60, value);182182+ ts2020_writereg(fe, 0x51, 0x17);183183+ ts2020_writereg(fe, 0x51, 0x1f);184184+ ts2020_writereg(fe, 0x50, 0x08);185185+ ts2020_writereg(fe, 0x50, 0x00);186186+187187+ /* set low-pass filter period */188188+ ts2020_writereg(fe, 0x04, 0x2e);189189+ ts2020_writereg(fe, 0x51, 0x1b);190190+ ts2020_writereg(fe, 0x51, 0x1f);191191+ ts2020_writereg(fe, 0x50, 0x04);192192+ ts2020_writereg(fe, 0x50, 0x00);193193+ msleep(5);194194+195195+ srate = c->symbol_rate / 1000;196196+197197+ f3db = (srate << 2) / 5 + 2000;198198+ if (srate < 5000)199199+ f3db += 3000;200200+ if (f3db < 7000)201201+ f3db = 7000;202202+ if (f3db > 40000)203203+ f3db = 40000;204204+205205+ /* set low-pass filter baseband */206206+ value = ts2020_readreg(fe, 0x26);207207+ mlpf = 0x2e * 207 / ((value << 1) + 151);208208+ mlpf_max = mlpf * 135 / 100;209209+ mlpf_min = mlpf * 78 / 100;210210+ if (mlpf_max > 63)211211+ mlpf_max = 63;212212+213213+ /* rounded to the closest integer */214214+ nlpf = ((mlpf * f3db * 1000) + (2766 * TS2020_XTAL_FREQ / 2))215215+ / (2766 * TS2020_XTAL_FREQ);216216+ if (nlpf > 23)217217+ nlpf = 23;218218+ if (nlpf < 1)219219+ nlpf = 1;220220+221221+ /* rounded to the closest integer */222222+ mlpf_new = ((TS2020_XTAL_FREQ * nlpf * 2766) +223223+ (1000 * f3db / 2)) / (1000 * f3db);224224+225225+ if (mlpf_new < mlpf_min) {226226+ nlpf++;227227+ mlpf_new = ((TS2020_XTAL_FREQ * nlpf * 2766) +228228+ (1000 * f3db / 2)) / (1000 * f3db);229229+ }230230+231231+ if (mlpf_new > mlpf_max)232232+ mlpf_new = mlpf_max;233233+234234+ ts2020_writereg(fe, 0x04, mlpf_new);235235+ ts2020_writereg(fe, 0x06, nlpf);236236+ ts2020_writereg(fe, 0x51, 0x1b);237237+ ts2020_writereg(fe, 0x51, 0x1f);238238+ ts2020_writereg(fe, 0x50, 0x04);239239+ ts2020_writereg(fe, 0x50, 0x00);240240+ msleep(5);241241+242242+ /* unknown */243243+ ts2020_writereg(fe, 0x51, 0x1e);244244+ ts2020_writereg(fe, 0x51, 0x1f);245245+ ts2020_writereg(fe, 0x50, 0x01);246246+ ts2020_writereg(fe, 0x50, 0x00);247247+ msleep(60);248248+249249+ return 0;250250+}251251+252252+static int ts2020_release(struct dvb_frontend *fe)253253+{254254+ struct ts2020_state *state = fe->tuner_priv;255255+256256+ fe->tuner_priv = NULL;257257+ kfree(state);258258+259259+ return 0;260260+}261261+262262+int ts2020_get_signal_strength(struct dvb_frontend *fe,263263+ u16 *signal_strength)264264+{265265+ u16 sig_reading, sig_strength;266266+ u8 rfgain, bbgain;267267+268268+ rfgain = ts2020_readreg(fe, 0x3d) & 0x1f;269269+ bbgain = ts2020_readreg(fe, 0x21) & 0x1f;270270+271271+ if (rfgain > 15)272272+ rfgain = 15;273273+ if (bbgain > 13)274274+ bbgain = 13;275275+276276+ sig_reading = rfgain * 2 + bbgain * 3;277277+278278+ sig_strength = 40 + (64 - sig_reading) * 50 / 64 ;279279+280280+ /* cook the value to be suitable for szap-s2 human readable output */281281+ *signal_strength = sig_strength * 1000;282282+283283+ return 0;284284+}285285+286286+static struct dvb_tuner_ops ts2020_ops = {287287+ .info = {288288+ .name = "Montage Technology TS2020 Silicon Tuner",289289+ .frequency_min = 950000,290290+ .frequency_max = 2150000,291291+ },292292+293293+ .init = ts2020_init,294294+ .release = ts2020_release,295295+ .set_params = ts2020_set_params,296296+ .get_frequency = ts2020_get_frequency,297297+ .get_rf_strength = ts2020_get_signal_strength298298+};299299+300300+struct dvb_frontend *ts2020_attach(struct dvb_frontend *fe,301301+ const struct ts2020_config *config, struct i2c_adapter *i2c)302302+{303303+ struct ts2020_state *state = NULL;304304+305305+ /* allocate memory for the internal state */306306+ state = kzalloc(sizeof(struct ts2020_state), GFP_KERNEL);307307+ if (!state)308308+ return NULL;309309+310310+ /* setup the state */311311+ state->tuner_address = config->tuner_address;312312+ state->i2c = i2c;313313+ fe->tuner_priv = state;314314+ fe->ops.tuner_ops = ts2020_ops;315315+ fe->ops.read_signal_strength = fe->ops.tuner_ops.get_rf_strength;316316+317317+ return fe;318318+}319319+EXPORT_SYMBOL(ts2020_attach);320320+321321+MODULE_AUTHOR("Konstantin Dimitrov <kosio.dimitrov@gmail.com>");322322+MODULE_DESCRIPTION("Montage Technology TS2020 - Silicon tuner driver module");323323+MODULE_LICENSE("GPL");
+49
drivers/media/dvb-frontends/ts2020.h
···11+/*22+ Montage Technology TS2020 - Silicon Tuner driver33+ Copyright (C) 2009-2012 Konstantin Dimitrov <kosio.dimitrov@gmail.com>44+55+ Copyright (C) 2009-2012 TurboSight.com66+77+ This program is free software; you can redistribute it and/or modify88+ it under the terms of the GNU General Public License as published by99+ the Free Software Foundation; either version 2 of the License, or1010+ (at your option) any later version.1111+1212+ This program is distributed in the hope that it will be useful,1313+ but WITHOUT ANY WARRANTY; without even the implied warranty of1414+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the1515+ GNU General Public License for more details.1616+1717+ You should have received a copy of the GNU General Public License1818+ along with this program; if not, write to the Free Software1919+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.2020+ */2121+2222+#ifndef TS2020_H2323+#define TS2020_H2424+2525+#include <linux/dvb/frontend.h>2626+2727+struct ts2020_config {2828+ u8 tuner_address;2929+};3030+3131+#if defined(CONFIG_DVB_TS2020) || \3232+ (defined(CONFIG_DVB_TS2020_MODULE) && defined(MODULE))3333+3434+extern struct dvb_frontend *ts2020_attach(3535+ struct dvb_frontend *fe,3636+ const struct ts2020_config *config,3737+ struct i2c_adapter *i2c);3838+#else3939+static inline struct dvb_frontend *ts2020_attach(4040+ struct dvb_frontend *fe,4141+ const struct ts2020_config *config,4242+ struct i2c_adapter *i2c)4343+{4444+ printk(KERN_WARNING "%s: driver disabled by Kconfig\n", __func__);4545+ return NULL;4646+}4747+#endif4848+4949+#endif /* TS2020_H */