blob: 80ae0397b05c5cc66c1ffcd5312ed13d9b7e2900 [file] [log] [blame]
Konstantin Dimitrov6fef4fc2012-12-23 19:25:27 -03001/*
2 Montage Technology TS2020 - Silicon Tuner driver
3 Copyright (C) 2009-2012 Konstantin Dimitrov <kosio.dimitrov@gmail.com>
4
5 Copyright (C) 2009-2012 TurboSight.com
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22#include "dvb_frontend.h"
23#include "ts2020.h"
Antti Palosaarif158cbc2015-03-27 18:14:25 -030024#include <linux/regmap.h>
Konstantin Dimitrov6fef4fc2012-12-23 19:25:27 -030025
26#define TS2020_XTAL_FREQ 27000 /* in kHz */
Igor M. Liplianinb858c332012-12-28 19:40:33 -030027#define FREQ_OFFSET_LOW_SYM_RATE 3000
Konstantin Dimitrov6fef4fc2012-12-23 19:25:27 -030028
Igor M. Liplianinb858c332012-12-28 19:40:33 -030029struct ts2020_priv {
Antti Palosaarie6ad9ce2015-03-26 20:20:42 -030030 struct i2c_client *client;
Antti Palosaarif158cbc2015-03-27 18:14:25 -030031 struct mutex regmap_mutex;
32 struct regmap_config regmap_config;
33 struct regmap *regmap;
Antti Palosaaridc245a52015-03-23 18:26:55 -030034 struct dvb_frontend *fe;
David Howells3366cd52015-05-26 12:04:07 -030035 struct delayed_work stat_work;
David Howells0f91c9d2015-05-26 12:04:00 -030036 int (*get_agc_pwm)(struct dvb_frontend *fe, u8 *_agc_pwm);
Igor M. Liplianinb858c332012-12-28 19:40:33 -030037 /* i2c details */
Konstantin Dimitrov6fef4fc2012-12-23 19:25:27 -030038 struct i2c_adapter *i2c;
David Howells3366cd52015-05-26 12:04:07 -030039 int i2c_address;
Antti Palosaariabd90252015-03-23 14:14:40 -030040 u8 clk_out:2;
41 u8 clk_out_div:5;
Antti Palosaariaf9d5252015-03-26 10:04:09 -030042 u32 frequency_div; /* LO output divider switch frequency */
43 u32 frequency_khz; /* actual used LO frequency */
Antti Palosaariabd90252015-03-23 14:14:40 -030044#define TS2020_M88TS2020 0
45#define TS2020_M88TS2022 1
46 u8 tuner;
47 u8 loop_through:1;
48};
49
50struct ts2020_reg_val {
51 u8 reg;
52 u8 val;
Konstantin Dimitrov6fef4fc2012-12-23 19:25:27 -030053};
54
Igor M. Liplianinb858c332012-12-28 19:40:33 -030055static int ts2020_release(struct dvb_frontend *fe)
56{
Antti Palosaarie6ad9ce2015-03-26 20:20:42 -030057 struct ts2020_priv *priv = fe->tuner_priv;
58 struct i2c_client *client = priv->client;
59
60 dev_dbg(&client->dev, "\n");
61
62 i2c_unregister_device(client);
Igor M. Liplianinb858c332012-12-28 19:40:33 -030063 return 0;
64}
65
Igor M. Liplianinb858c332012-12-28 19:40:33 -030066static int ts2020_sleep(struct dvb_frontend *fe)
Konstantin Dimitrov6fef4fc2012-12-23 19:25:27 -030067{
Igor M. Liplianinb858c332012-12-28 19:40:33 -030068 struct ts2020_priv *priv = fe->tuner_priv;
David Howells3366cd52015-05-26 12:04:07 -030069 int ret;
Antti Palosaarib3226f92015-03-24 09:40:58 -030070 u8 u8tmp;
Konstantin Dimitrov6fef4fc2012-12-23 19:25:27 -030071
Antti Palosaarib3226f92015-03-24 09:40:58 -030072 if (priv->tuner == TS2020_M88TS2020)
73 u8tmp = 0x0a; /* XXX: probably wrong */
74 else
75 u8tmp = 0x00;
Antti Palosaariabd90252015-03-23 14:14:40 -030076
David Howells3366cd52015-05-26 12:04:07 -030077 ret = regmap_write(priv->regmap, u8tmp, 0x00);
78 if (ret < 0)
79 return ret;
80
81 /* stop statistics polling */
82 cancel_delayed_work_sync(&priv->stat_work);
83 return 0;
Konstantin Dimitrov6fef4fc2012-12-23 19:25:27 -030084}
85
86static int ts2020_init(struct dvb_frontend *fe)
87{
David Howells3366cd52015-05-26 12:04:07 -030088 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Igor M. Liplianinb858c332012-12-28 19:40:33 -030089 struct ts2020_priv *priv = fe->tuner_priv;
Antti Palosaariabd90252015-03-23 14:14:40 -030090 int i;
91 u8 u8tmp;
Igor M. Liplianinb858c332012-12-28 19:40:33 -030092
Antti Palosaariabd90252015-03-23 14:14:40 -030093 if (priv->tuner == TS2020_M88TS2020) {
Antti Palosaarif158cbc2015-03-27 18:14:25 -030094 regmap_write(priv->regmap, 0x42, 0x73);
95 regmap_write(priv->regmap, 0x05, priv->clk_out_div);
96 regmap_write(priv->regmap, 0x20, 0x27);
97 regmap_write(priv->regmap, 0x07, 0x02);
98 regmap_write(priv->regmap, 0x11, 0xff);
99 regmap_write(priv->regmap, 0x60, 0xf9);
100 regmap_write(priv->regmap, 0x08, 0x01);
101 regmap_write(priv->regmap, 0x00, 0x41);
Antti Palosaariabd90252015-03-23 14:14:40 -0300102 } else {
103 static const struct ts2020_reg_val reg_vals[] = {
104 {0x7d, 0x9d},
105 {0x7c, 0x9a},
106 {0x7a, 0x76},
107 {0x3b, 0x01},
108 {0x63, 0x88},
109 {0x61, 0x85},
110 {0x22, 0x30},
111 {0x30, 0x40},
112 {0x20, 0x23},
113 {0x24, 0x02},
114 {0x12, 0xa0},
115 };
116
Antti Palosaarif158cbc2015-03-27 18:14:25 -0300117 regmap_write(priv->regmap, 0x00, 0x01);
118 regmap_write(priv->regmap, 0x00, 0x03);
Antti Palosaariabd90252015-03-23 14:14:40 -0300119
120 switch (priv->clk_out) {
121 case TS2020_CLK_OUT_DISABLED:
122 u8tmp = 0x60;
123 break;
124 case TS2020_CLK_OUT_ENABLED:
125 u8tmp = 0x70;
Antti Palosaarif158cbc2015-03-27 18:14:25 -0300126 regmap_write(priv->regmap, 0x05, priv->clk_out_div);
Antti Palosaariabd90252015-03-23 14:14:40 -0300127 break;
128 case TS2020_CLK_OUT_ENABLED_XTALOUT:
129 u8tmp = 0x6c;
130 break;
131 default:
132 u8tmp = 0x60;
133 break;
134 }
135
Antti Palosaarif158cbc2015-03-27 18:14:25 -0300136 regmap_write(priv->regmap, 0x42, u8tmp);
Antti Palosaariabd90252015-03-23 14:14:40 -0300137
138 if (priv->loop_through)
139 u8tmp = 0xec;
140 else
141 u8tmp = 0x6c;
142
Antti Palosaarif158cbc2015-03-27 18:14:25 -0300143 regmap_write(priv->regmap, 0x62, u8tmp);
Antti Palosaariabd90252015-03-23 14:14:40 -0300144
145 for (i = 0; i < ARRAY_SIZE(reg_vals); i++)
Antti Palosaarif158cbc2015-03-27 18:14:25 -0300146 regmap_write(priv->regmap, reg_vals[i].reg,
147 reg_vals[i].val);
Antti Palosaariabd90252015-03-23 14:14:40 -0300148 }
Igor M. Liplianinb858c332012-12-28 19:40:33 -0300149
David Howells3366cd52015-05-26 12:04:07 -0300150 /* Initialise v5 stats here */
151 c->strength.len = 1;
152 c->strength.stat[0].scale = FE_SCALE_DECIBEL;
153 c->strength.stat[0].uvalue = 0;
154
155 /* Start statistics polling */
156 schedule_delayed_work(&priv->stat_work, 0);
Konstantin Dimitrov6fef4fc2012-12-23 19:25:27 -0300157 return 0;
158}
159
Igor M. Liplianinb858c332012-12-28 19:40:33 -0300160static int ts2020_tuner_gate_ctrl(struct dvb_frontend *fe, u8 offset)
Konstantin Dimitrov6fef4fc2012-12-23 19:25:27 -0300161{
Antti Palosaarif158cbc2015-03-27 18:14:25 -0300162 struct ts2020_priv *priv = fe->tuner_priv;
Igor M. Liplianinb858c332012-12-28 19:40:33 -0300163 int ret;
Antti Palosaarif158cbc2015-03-27 18:14:25 -0300164 ret = regmap_write(priv->regmap, 0x51, 0x1f - offset);
165 ret |= regmap_write(priv->regmap, 0x51, 0x1f);
166 ret |= regmap_write(priv->regmap, 0x50, offset);
167 ret |= regmap_write(priv->regmap, 0x50, 0x00);
Igor M. Liplianinb858c332012-12-28 19:40:33 -0300168 msleep(20);
169 return ret;
170}
Konstantin Dimitrov6fef4fc2012-12-23 19:25:27 -0300171
Igor M. Liplianinb858c332012-12-28 19:40:33 -0300172static int ts2020_set_tuner_rf(struct dvb_frontend *fe)
173{
Antti Palosaarif158cbc2015-03-27 18:14:25 -0300174 struct ts2020_priv *dev = fe->tuner_priv;
175 int ret;
176 unsigned int utmp;
Konstantin Dimitrov6fef4fc2012-12-23 19:25:27 -0300177
Antti Palosaarif158cbc2015-03-27 18:14:25 -0300178 ret = regmap_read(dev->regmap, 0x3d, &utmp);
179 utmp &= 0x7f;
180 if (utmp < 0x16)
181 utmp = 0xa1;
182 else if (utmp == 0x16)
183 utmp = 0x99;
Igor M. Liplianinb858c332012-12-28 19:40:33 -0300184 else
Antti Palosaarif158cbc2015-03-27 18:14:25 -0300185 utmp = 0xf9;
Konstantin Dimitrov6fef4fc2012-12-23 19:25:27 -0300186
Antti Palosaarif158cbc2015-03-27 18:14:25 -0300187 regmap_write(dev->regmap, 0x60, utmp);
188 ret = ts2020_tuner_gate_ctrl(fe, 0x08);
Konstantin Dimitrov6fef4fc2012-12-23 19:25:27 -0300189
Antti Palosaarif158cbc2015-03-27 18:14:25 -0300190 return ret;
Konstantin Dimitrov6fef4fc2012-12-23 19:25:27 -0300191}
192
193static int ts2020_set_params(struct dvb_frontend *fe)
194{
195 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Malcolm Priestley9898df62013-01-03 16:37:25 -0300196 struct ts2020_priv *priv = fe->tuner_priv;
Igor M. Liplianinb858c332012-12-28 19:40:33 -0300197 int ret;
Antti Palosaarif158cbc2015-03-27 18:14:25 -0300198 unsigned int utmp;
Igor M. Liplianinb858c332012-12-28 19:40:33 -0300199 u32 f3db, gdiv28;
Antti Palosaariaf9d5252015-03-26 10:04:09 -0300200 u16 u16tmp, value, lpf_coeff;
201 u8 buf[3], reg10, lpf_mxdiv, mlpf_max, mlpf_min, nlpf;
202 unsigned int f_ref_khz, f_vco_khz, div_ref, div_out, pll_n;
203 unsigned int frequency_khz = c->frequency;
Konstantin Dimitrov6fef4fc2012-12-23 19:25:27 -0300204
Antti Palosaariaf9d5252015-03-26 10:04:09 -0300205 /*
206 * Integer-N PLL synthesizer
207 * kHz is used for all calculations to keep calculations within 32-bit
208 */
209 f_ref_khz = TS2020_XTAL_FREQ;
210 div_ref = DIV_ROUND_CLOSEST(f_ref_khz, 2000);
211
212 /* select LO output divider */
213 if (frequency_khz < priv->frequency_div) {
214 div_out = 4;
215 reg10 = 0x10;
216 } else {
217 div_out = 2;
218 reg10 = 0x00;
219 }
220
221 f_vco_khz = frequency_khz * div_out;
222 pll_n = f_vco_khz * div_ref / f_ref_khz;
223 pll_n += pll_n % 2;
224 priv->frequency_khz = pll_n * f_ref_khz / div_ref / div_out;
225
226 pr_debug("frequency=%u offset=%d f_vco_khz=%u pll_n=%u div_ref=%u div_out=%u\n",
227 priv->frequency_khz, priv->frequency_khz - c->frequency,
228 f_vco_khz, pll_n, div_ref, div_out);
Konstantin Dimitrov6fef4fc2012-12-23 19:25:27 -0300229
Antti Palosaariabd90252015-03-23 14:14:40 -0300230 if (priv->tuner == TS2020_M88TS2020) {
231 lpf_coeff = 2766;
Antti Palosaariaf9d5252015-03-26 10:04:09 -0300232 reg10 |= 0x01;
Antti Palosaarif158cbc2015-03-27 18:14:25 -0300233 ret = regmap_write(priv->regmap, 0x10, reg10);
Antti Palosaariabd90252015-03-23 14:14:40 -0300234 } else {
235 lpf_coeff = 3200;
Antti Palosaariaf9d5252015-03-26 10:04:09 -0300236 reg10 |= 0x0b;
Antti Palosaarif158cbc2015-03-27 18:14:25 -0300237 ret = regmap_write(priv->regmap, 0x10, reg10);
238 ret |= regmap_write(priv->regmap, 0x11, 0x40);
Antti Palosaariabd90252015-03-23 14:14:40 -0300239 }
Konstantin Dimitrov6fef4fc2012-12-23 19:25:27 -0300240
Antti Palosaariaf9d5252015-03-26 10:04:09 -0300241 u16tmp = pll_n - 1024;
242 buf[0] = (u16tmp >> 8) & 0xff;
243 buf[1] = (u16tmp >> 0) & 0xff;
244 buf[2] = div_ref - 8;
Konstantin Dimitrov6fef4fc2012-12-23 19:25:27 -0300245
Antti Palosaarif158cbc2015-03-27 18:14:25 -0300246 ret |= regmap_write(priv->regmap, 0x01, buf[0]);
247 ret |= regmap_write(priv->regmap, 0x02, buf[1]);
248 ret |= regmap_write(priv->regmap, 0x03, buf[2]);
Antti Palosaariaf9d5252015-03-26 10:04:09 -0300249
Igor M. Liplianinb858c332012-12-28 19:40:33 -0300250 ret |= ts2020_tuner_gate_ctrl(fe, 0x10);
251 if (ret < 0)
252 return -ENODEV;
Konstantin Dimitrov6fef4fc2012-12-23 19:25:27 -0300253
Igor M. Liplianinb858c332012-12-28 19:40:33 -0300254 ret |= ts2020_tuner_gate_ctrl(fe, 0x08);
Konstantin Dimitrov6fef4fc2012-12-23 19:25:27 -0300255
Igor M. Liplianinb858c332012-12-28 19:40:33 -0300256 /* Tuner RF */
Antti Palosaariabd90252015-03-23 14:14:40 -0300257 if (priv->tuner == TS2020_M88TS2020)
258 ret |= ts2020_set_tuner_rf(fe);
Konstantin Dimitrov6fef4fc2012-12-23 19:25:27 -0300259
Igor M. Liplianinb858c332012-12-28 19:40:33 -0300260 gdiv28 = (TS2020_XTAL_FREQ / 1000 * 1694 + 500) / 1000;
Antti Palosaarif158cbc2015-03-27 18:14:25 -0300261 ret |= regmap_write(priv->regmap, 0x04, gdiv28 & 0xff);
Igor M. Liplianinb858c332012-12-28 19:40:33 -0300262 ret |= ts2020_tuner_gate_ctrl(fe, 0x04);
263 if (ret < 0)
264 return -ENODEV;
Konstantin Dimitrov6fef4fc2012-12-23 19:25:27 -0300265
Antti Palosaariabd90252015-03-23 14:14:40 -0300266 if (priv->tuner == TS2020_M88TS2022) {
Antti Palosaarif158cbc2015-03-27 18:14:25 -0300267 ret = regmap_write(priv->regmap, 0x25, 0x00);
268 ret |= regmap_write(priv->regmap, 0x27, 0x70);
269 ret |= regmap_write(priv->regmap, 0x41, 0x09);
270 ret |= regmap_write(priv->regmap, 0x08, 0x0b);
Antti Palosaariabd90252015-03-23 14:14:40 -0300271 if (ret < 0)
272 return -ENODEV;
273 }
274
Antti Palosaarif158cbc2015-03-27 18:14:25 -0300275 regmap_read(priv->regmap, 0x26, &utmp);
276 value = utmp;
Konstantin Dimitrov6fef4fc2012-12-23 19:25:27 -0300277
Antti Palosaari2ca58f42015-03-26 10:49:17 -0300278 f3db = (c->bandwidth_hz / 1000 / 2) + 2000;
279 f3db += FREQ_OFFSET_LOW_SYM_RATE; /* FIXME: ~always too wide filter */
280 f3db = clamp(f3db, 7000U, 40000U);
Konstantin Dimitrov6fef4fc2012-12-23 19:25:27 -0300281
Igor M. Liplianinb858c332012-12-28 19:40:33 -0300282 gdiv28 = gdiv28 * 207 / (value * 2 + 151);
283 mlpf_max = gdiv28 * 135 / 100;
284 mlpf_min = gdiv28 * 78 / 100;
Konstantin Dimitrov6fef4fc2012-12-23 19:25:27 -0300285 if (mlpf_max > 63)
286 mlpf_max = 63;
287
Igor M. Liplianinb858c332012-12-28 19:40:33 -0300288 nlpf = (f3db * gdiv28 * 2 / lpf_coeff /
289 (TS2020_XTAL_FREQ / 1000) + 1) / 2;
Konstantin Dimitrov6fef4fc2012-12-23 19:25:27 -0300290 if (nlpf > 23)
291 nlpf = 23;
292 if (nlpf < 1)
293 nlpf = 1;
294
Igor M. Liplianinb858c332012-12-28 19:40:33 -0300295 lpf_mxdiv = (nlpf * (TS2020_XTAL_FREQ / 1000)
296 * lpf_coeff * 2 / f3db + 1) / 2;
Konstantin Dimitrov6fef4fc2012-12-23 19:25:27 -0300297
Igor M. Liplianinb858c332012-12-28 19:40:33 -0300298 if (lpf_mxdiv < mlpf_min) {
Konstantin Dimitrov6fef4fc2012-12-23 19:25:27 -0300299 nlpf++;
Igor M. Liplianinb858c332012-12-28 19:40:33 -0300300 lpf_mxdiv = (nlpf * (TS2020_XTAL_FREQ / 1000)
301 * lpf_coeff * 2 / f3db + 1) / 2;
Konstantin Dimitrov6fef4fc2012-12-23 19:25:27 -0300302 }
303
Igor M. Liplianinb858c332012-12-28 19:40:33 -0300304 if (lpf_mxdiv > mlpf_max)
305 lpf_mxdiv = mlpf_max;
Konstantin Dimitrov6fef4fc2012-12-23 19:25:27 -0300306
Antti Palosaarif158cbc2015-03-27 18:14:25 -0300307 ret = regmap_write(priv->regmap, 0x04, lpf_mxdiv);
308 ret |= regmap_write(priv->regmap, 0x06, nlpf);
Konstantin Dimitrov6fef4fc2012-12-23 19:25:27 -0300309
Igor M. Liplianinb858c332012-12-28 19:40:33 -0300310 ret |= ts2020_tuner_gate_ctrl(fe, 0x04);
Konstantin Dimitrov6fef4fc2012-12-23 19:25:27 -0300311
Igor M. Liplianinb858c332012-12-28 19:40:33 -0300312 ret |= ts2020_tuner_gate_ctrl(fe, 0x01);
313
314 msleep(80);
Igor M. Liplianinb858c332012-12-28 19:40:33 -0300315
316 return (ret < 0) ? -EINVAL : 0;
Konstantin Dimitrov6fef4fc2012-12-23 19:25:27 -0300317}
318
Igor M. Liplianinb858c332012-12-28 19:40:33 -0300319static int ts2020_get_frequency(struct dvb_frontend *fe, u32 *frequency)
Konstantin Dimitrov6fef4fc2012-12-23 19:25:27 -0300320{
Igor M. Liplianinb858c332012-12-28 19:40:33 -0300321 struct ts2020_priv *priv = fe->tuner_priv;
Antti Palosaariabd90252015-03-23 14:14:40 -0300322
Antti Palosaariaf9d5252015-03-26 10:04:09 -0300323 *frequency = priv->frequency_khz;
Antti Palosaariabd90252015-03-23 14:14:40 -0300324 return 0;
325}
326
327static int ts2020_get_if_frequency(struct dvb_frontend *fe, u32 *frequency)
328{
329 *frequency = 0; /* Zero-IF */
Konstantin Dimitrov6fef4fc2012-12-23 19:25:27 -0300330 return 0;
331}
332
David Howells0f91c9d2015-05-26 12:04:00 -0300333/*
334 * Get the tuner gain.
335 * @fe: The front end for which we're determining the gain
336 * @v_agc: The voltage of the AGC from the demodulator (0-2600mV)
337 * @_gain: Where to store the gain (in 0.001dB units)
338 *
339 * Returns 0 or a negative error code.
340 */
341static int ts2020_read_tuner_gain(struct dvb_frontend *fe, unsigned v_agc,
342 __s64 *_gain)
343{
344 struct ts2020_priv *priv = fe->tuner_priv;
345 unsigned long gain1, gain2, gain3;
346 unsigned utmp;
347 int ret;
348
349 /* Read the RF gain */
350 ret = regmap_read(priv->regmap, 0x3d, &utmp);
351 if (ret < 0)
352 return ret;
353 gain1 = utmp & 0x1f;
354
355 /* Read the baseband gain */
356 ret = regmap_read(priv->regmap, 0x21, &utmp);
357 if (ret < 0)
358 return ret;
359 gain2 = utmp & 0x1f;
360
361 switch (priv->tuner) {
362 case TS2020_M88TS2020:
363 gain1 = clamp_t(long, gain1, 0, 15);
364 gain2 = clamp_t(long, gain2, 0, 13);
365 v_agc = clamp_t(long, v_agc, 400, 1100);
366
367 *_gain = -(gain1 * 2330 +
368 gain2 * 3500 +
369 v_agc * 24 / 10 * 10 +
370 10000);
371 /* gain in range -19600 to -116850 in units of 0.001dB */
372 break;
373
374 case TS2020_M88TS2022:
375 ret = regmap_read(priv->regmap, 0x66, &utmp);
376 if (ret < 0)
377 return ret;
378 gain3 = (utmp >> 3) & 0x07;
379
380 gain1 = clamp_t(long, gain1, 0, 15);
381 gain2 = clamp_t(long, gain2, 2, 16);
382 gain3 = clamp_t(long, gain3, 0, 6);
383 v_agc = clamp_t(long, v_agc, 600, 1600);
384
385 *_gain = -(gain1 * 2650 +
386 gain2 * 3380 +
387 gain3 * 2850 +
388 v_agc * 176 / 100 * 10 -
389 30000);
390 /* gain in range -47320 to -158950 in units of 0.001dB */
391 break;
392 }
393
394 return 0;
395}
396
397/*
398 * Get the AGC information from the demodulator and use that to calculate the
399 * tuner gain.
400 */
401static int ts2020_get_tuner_gain(struct dvb_frontend *fe, __s64 *_gain)
402{
403 struct ts2020_priv *priv = fe->tuner_priv;
404 int v_agc = 0, ret;
405 u8 agc_pwm;
406
407 /* Read the AGC PWM rate from the demodulator */
408 if (priv->get_agc_pwm) {
409 ret = priv->get_agc_pwm(fe, &agc_pwm);
410 if (ret < 0)
411 return ret;
412
413 switch (priv->tuner) {
414 case TS2020_M88TS2020:
415 v_agc = (int)agc_pwm * 20 - 1166;
416 break;
417 case TS2020_M88TS2022:
418 v_agc = (int)agc_pwm * 16 - 670;
419 break;
420 }
421
422 if (v_agc < 0)
423 v_agc = 0;
424 }
425
426 return ts2020_read_tuner_gain(fe, v_agc, _gain);
427}
428
429/*
David Howells3366cd52015-05-26 12:04:07 -0300430 * Gather statistics on a regular basis
431 */
432static void ts2020_stat_work(struct work_struct *work)
433{
434 struct ts2020_priv *priv = container_of(work, struct ts2020_priv,
435 stat_work.work);
436 struct i2c_client *client = priv->client;
437 struct dtv_frontend_properties *c = &priv->fe->dtv_property_cache;
438 int ret;
439
440 dev_dbg(&client->dev, "\n");
441
442 ret = ts2020_get_tuner_gain(priv->fe, &c->strength.stat[0].svalue);
443 if (ret < 0)
444 goto err;
445
446 c->strength.stat[0].scale = FE_SCALE_DECIBEL;
447
448 schedule_delayed_work(&priv->stat_work, msecs_to_jiffies(2000));
449 return;
450err:
451 dev_dbg(&client->dev, "failed=%d\n", ret);
452}
453
454/*
David Howells0f91c9d2015-05-26 12:04:00 -0300455 * Read TS2020 signal strength in v3 format.
456 */
Igor M. Liplianinb858c332012-12-28 19:40:33 -0300457static int ts2020_read_signal_strength(struct dvb_frontend *fe,
David Howells3366cd52015-05-26 12:04:07 -0300458 u16 *_signal_strength)
Konstantin Dimitrov6fef4fc2012-12-23 19:25:27 -0300459{
David Howells3366cd52015-05-26 12:04:07 -0300460 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
David Howells0f91c9d2015-05-26 12:04:00 -0300461 unsigned strength;
462 __s64 gain;
Konstantin Dimitrov6fef4fc2012-12-23 19:25:27 -0300463
David Howells3366cd52015-05-26 12:04:07 -0300464 if (c->strength.stat[0].scale == FE_SCALE_NOT_AVAILABLE) {
465 *_signal_strength = 0;
466 return 0;
467 }
468
469 gain = c->strength.stat[0].svalue;
Konstantin Dimitrov6fef4fc2012-12-23 19:25:27 -0300470
David Howells0f91c9d2015-05-26 12:04:00 -0300471 /* Calculate the signal strength based on the total gain of the tuner */
472 if (gain < -85000)
473 /* 0%: no signal or weak signal */
474 strength = 0;
475 else if (gain < -65000)
476 /* 0% - 60%: weak signal */
477 strength = 0 + (85000 + gain) * 3 / 1000;
478 else if (gain < -45000)
479 /* 60% - 90%: normal signal */
480 strength = 60 + (65000 + gain) * 3 / 2000;
481 else
482 /* 90% - 99%: strong signal */
483 strength = 90 + (45000 + gain) / 5000;
Konstantin Dimitrov6fef4fc2012-12-23 19:25:27 -0300484
David Howells3366cd52015-05-26 12:04:07 -0300485 *_signal_strength = strength * 65535 / 100;
Konstantin Dimitrov6fef4fc2012-12-23 19:25:27 -0300486 return 0;
487}
488
Igor M. Liplianinb858c332012-12-28 19:40:33 -0300489static struct dvb_tuner_ops ts2020_tuner_ops = {
Konstantin Dimitrov6fef4fc2012-12-23 19:25:27 -0300490 .info = {
Igor M. Liplianinb858c332012-12-28 19:40:33 -0300491 .name = "TS2020",
Konstantin Dimitrov6fef4fc2012-12-23 19:25:27 -0300492 .frequency_min = 950000,
Igor M. Liplianinb858c332012-12-28 19:40:33 -0300493 .frequency_max = 2150000
Konstantin Dimitrov6fef4fc2012-12-23 19:25:27 -0300494 },
Konstantin Dimitrov6fef4fc2012-12-23 19:25:27 -0300495 .init = ts2020_init,
496 .release = ts2020_release,
Igor M. Liplianinb858c332012-12-28 19:40:33 -0300497 .sleep = ts2020_sleep,
Konstantin Dimitrov6fef4fc2012-12-23 19:25:27 -0300498 .set_params = ts2020_set_params,
499 .get_frequency = ts2020_get_frequency,
Antti Palosaariabd90252015-03-23 14:14:40 -0300500 .get_if_frequency = ts2020_get_if_frequency,
Igor M. Liplianinb858c332012-12-28 19:40:33 -0300501 .get_rf_strength = ts2020_read_signal_strength,
Konstantin Dimitrov6fef4fc2012-12-23 19:25:27 -0300502};
503
504struct dvb_frontend *ts2020_attach(struct dvb_frontend *fe,
Igor M. Liplianinb858c332012-12-28 19:40:33 -0300505 const struct ts2020_config *config,
506 struct i2c_adapter *i2c)
Konstantin Dimitrov6fef4fc2012-12-23 19:25:27 -0300507{
Antti Palosaarie6ad9ce2015-03-26 20:20:42 -0300508 struct i2c_client *client;
509 struct i2c_board_info board_info;
David Howells80868c82015-04-02 08:03:11 -0300510
511 /* This is only used by ts2020_probe() so can be on the stack */
Antti Palosaarie6ad9ce2015-03-26 20:20:42 -0300512 struct ts2020_config pdata;
Konstantin Dimitrov6fef4fc2012-12-23 19:25:27 -0300513
Antti Palosaarie6ad9ce2015-03-26 20:20:42 -0300514 memcpy(&pdata, config, sizeof(pdata));
515 pdata.fe = fe;
516 pdata.attach_in_use = true;
517
518 memset(&board_info, 0, sizeof(board_info));
519 strlcpy(board_info.type, "ts2020", I2C_NAME_SIZE);
520 board_info.addr = config->tuner_address;
521 board_info.platform_data = &pdata;
522 client = i2c_new_device(i2c, &board_info);
523 if (!client || !client->dev.driver)
Konstantin Dimitrov6fef4fc2012-12-23 19:25:27 -0300524 return NULL;
525
Konstantin Dimitrov6fef4fc2012-12-23 19:25:27 -0300526 return fe;
527}
528EXPORT_SYMBOL(ts2020_attach);
529
Antti Palosaarif158cbc2015-03-27 18:14:25 -0300530/*
531 * We implement own regmap locking due to legacy DVB attach which uses frontend
532 * gate control callback to control I2C bus access. We can open / close gate and
533 * serialize whole open / I2C-operation / close sequence at the same.
534 */
535static void ts2020_regmap_lock(void *__dev)
536{
537 struct ts2020_priv *dev = __dev;
538
539 mutex_lock(&dev->regmap_mutex);
540 if (dev->fe->ops.i2c_gate_ctrl)
541 dev->fe->ops.i2c_gate_ctrl(dev->fe, 1);
542}
543
544static void ts2020_regmap_unlock(void *__dev)
545{
546 struct ts2020_priv *dev = __dev;
547
548 if (dev->fe->ops.i2c_gate_ctrl)
549 dev->fe->ops.i2c_gate_ctrl(dev->fe, 0);
550 mutex_unlock(&dev->regmap_mutex);
551}
552
Antti Palosaaridc245a52015-03-23 18:26:55 -0300553static int ts2020_probe(struct i2c_client *client,
554 const struct i2c_device_id *id)
555{
556 struct ts2020_config *pdata = client->dev.platform_data;
557 struct dvb_frontend *fe = pdata->fe;
558 struct ts2020_priv *dev;
559 int ret;
560 u8 u8tmp;
561 unsigned int utmp;
562 char *chip_str;
563
564 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
565 if (!dev) {
566 ret = -ENOMEM;
567 goto err;
568 }
569
Antti Palosaarif158cbc2015-03-27 18:14:25 -0300570 /* create regmap */
571 mutex_init(&dev->regmap_mutex);
572 dev->regmap_config.reg_bits = 8,
573 dev->regmap_config.val_bits = 8,
574 dev->regmap_config.lock = ts2020_regmap_lock,
575 dev->regmap_config.unlock = ts2020_regmap_unlock,
576 dev->regmap_config.lock_arg = dev,
577 dev->regmap = regmap_init_i2c(client, &dev->regmap_config);
578 if (IS_ERR(dev->regmap)) {
579 ret = PTR_ERR(dev->regmap);
580 goto err_kfree;
581 }
582
Antti Palosaaridc245a52015-03-23 18:26:55 -0300583 dev->i2c = client->adapter;
584 dev->i2c_address = client->addr;
585 dev->clk_out = pdata->clk_out;
586 dev->clk_out_div = pdata->clk_out_div;
587 dev->frequency_div = pdata->frequency_div;
588 dev->fe = fe;
David Howells0f91c9d2015-05-26 12:04:00 -0300589 dev->get_agc_pwm = pdata->get_agc_pwm;
Antti Palosaaridc245a52015-03-23 18:26:55 -0300590 fe->tuner_priv = dev;
Antti Palosaarie6ad9ce2015-03-26 20:20:42 -0300591 dev->client = client;
David Howells3366cd52015-05-26 12:04:07 -0300592 INIT_DELAYED_WORK(&dev->stat_work, ts2020_stat_work);
Antti Palosaaridc245a52015-03-23 18:26:55 -0300593
594 /* check if the tuner is there */
Antti Palosaarif158cbc2015-03-27 18:14:25 -0300595 ret = regmap_read(dev->regmap, 0x00, &utmp);
596 if (ret)
597 goto err_regmap_exit;
Antti Palosaaridc245a52015-03-23 18:26:55 -0300598
599 if ((utmp & 0x03) == 0x00) {
Antti Palosaarif158cbc2015-03-27 18:14:25 -0300600 ret = regmap_write(dev->regmap, 0x00, 0x01);
Antti Palosaaridc245a52015-03-23 18:26:55 -0300601 if (ret)
Antti Palosaarif158cbc2015-03-27 18:14:25 -0300602 goto err_regmap_exit;
Antti Palosaaridc245a52015-03-23 18:26:55 -0300603
604 usleep_range(2000, 50000);
605 }
606
Antti Palosaarif158cbc2015-03-27 18:14:25 -0300607 ret = regmap_write(dev->regmap, 0x00, 0x03);
Antti Palosaaridc245a52015-03-23 18:26:55 -0300608 if (ret)
Antti Palosaarif158cbc2015-03-27 18:14:25 -0300609 goto err_regmap_exit;
Antti Palosaaridc245a52015-03-23 18:26:55 -0300610
611 usleep_range(2000, 50000);
612
Antti Palosaarif158cbc2015-03-27 18:14:25 -0300613 ret = regmap_read(dev->regmap, 0x00, &utmp);
614 if (ret)
615 goto err_regmap_exit;
Antti Palosaaridc245a52015-03-23 18:26:55 -0300616
617 dev_dbg(&client->dev, "chip_id=%02x\n", utmp);
618
619 switch (utmp) {
620 case 0x01:
621 case 0x41:
622 case 0x81:
623 dev->tuner = TS2020_M88TS2020;
624 chip_str = "TS2020";
625 if (!dev->frequency_div)
626 dev->frequency_div = 1060000;
627 break;
628 case 0xc3:
629 case 0x83:
630 dev->tuner = TS2020_M88TS2022;
631 chip_str = "TS2022";
632 if (!dev->frequency_div)
633 dev->frequency_div = 1103000;
634 break;
635 default:
636 ret = -ENODEV;
Antti Palosaarif158cbc2015-03-27 18:14:25 -0300637 goto err_regmap_exit;
Antti Palosaaridc245a52015-03-23 18:26:55 -0300638 }
639
640 if (dev->tuner == TS2020_M88TS2022) {
641 switch (dev->clk_out) {
642 case TS2020_CLK_OUT_DISABLED:
643 u8tmp = 0x60;
644 break;
645 case TS2020_CLK_OUT_ENABLED:
646 u8tmp = 0x70;
Antti Palosaarif158cbc2015-03-27 18:14:25 -0300647 ret = regmap_write(dev->regmap, 0x05, dev->clk_out_div);
Antti Palosaaridc245a52015-03-23 18:26:55 -0300648 if (ret)
Antti Palosaarif158cbc2015-03-27 18:14:25 -0300649 goto err_regmap_exit;
Antti Palosaaridc245a52015-03-23 18:26:55 -0300650 break;
651 case TS2020_CLK_OUT_ENABLED_XTALOUT:
652 u8tmp = 0x6c;
653 break;
654 default:
655 ret = -EINVAL;
Antti Palosaarif158cbc2015-03-27 18:14:25 -0300656 goto err_regmap_exit;
Antti Palosaaridc245a52015-03-23 18:26:55 -0300657 }
658
Antti Palosaarif158cbc2015-03-27 18:14:25 -0300659 ret = regmap_write(dev->regmap, 0x42, u8tmp);
Antti Palosaaridc245a52015-03-23 18:26:55 -0300660 if (ret)
Antti Palosaarif158cbc2015-03-27 18:14:25 -0300661 goto err_regmap_exit;
Antti Palosaaridc245a52015-03-23 18:26:55 -0300662
663 if (dev->loop_through)
664 u8tmp = 0xec;
665 else
666 u8tmp = 0x6c;
667
Antti Palosaarif158cbc2015-03-27 18:14:25 -0300668 ret = regmap_write(dev->regmap, 0x62, u8tmp);
Antti Palosaaridc245a52015-03-23 18:26:55 -0300669 if (ret)
Antti Palosaarif158cbc2015-03-27 18:14:25 -0300670 goto err_regmap_exit;
Antti Palosaaridc245a52015-03-23 18:26:55 -0300671 }
672
673 /* sleep */
Antti Palosaarif158cbc2015-03-27 18:14:25 -0300674 ret = regmap_write(dev->regmap, 0x00, 0x00);
Antti Palosaaridc245a52015-03-23 18:26:55 -0300675 if (ret)
Antti Palosaarif158cbc2015-03-27 18:14:25 -0300676 goto err_regmap_exit;
Antti Palosaaridc245a52015-03-23 18:26:55 -0300677
678 dev_info(&client->dev,
679 "Montage Technology %s successfully identified\n", chip_str);
680
681 memcpy(&fe->ops.tuner_ops, &ts2020_tuner_ops,
682 sizeof(struct dvb_tuner_ops));
Antti Palosaarie6ad9ce2015-03-26 20:20:42 -0300683 if (!pdata->attach_in_use)
684 fe->ops.tuner_ops.release = NULL;
Antti Palosaaridc245a52015-03-23 18:26:55 -0300685
686 i2c_set_clientdata(client, dev);
687 return 0;
Antti Palosaarif158cbc2015-03-27 18:14:25 -0300688err_regmap_exit:
689 regmap_exit(dev->regmap);
690err_kfree:
691 kfree(dev);
Antti Palosaaridc245a52015-03-23 18:26:55 -0300692err:
693 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaaridc245a52015-03-23 18:26:55 -0300694 return ret;
695}
696
697static int ts2020_remove(struct i2c_client *client)
698{
699 struct ts2020_priv *dev = i2c_get_clientdata(client);
Antti Palosaaridc245a52015-03-23 18:26:55 -0300700
701 dev_dbg(&client->dev, "\n");
702
Antti Palosaarif158cbc2015-03-27 18:14:25 -0300703 regmap_exit(dev->regmap);
Antti Palosaaridc245a52015-03-23 18:26:55 -0300704 kfree(dev);
Antti Palosaaridc245a52015-03-23 18:26:55 -0300705 return 0;
706}
707
708static const struct i2c_device_id ts2020_id_table[] = {
709 {"ts2020", 0},
710 {"ts2022", 0},
711 {}
712};
713MODULE_DEVICE_TABLE(i2c, ts2020_id_table);
714
715static struct i2c_driver ts2020_driver = {
716 .driver = {
717 .owner = THIS_MODULE,
718 .name = "ts2020",
719 },
720 .probe = ts2020_probe,
721 .remove = ts2020_remove,
722 .id_table = ts2020_id_table,
723};
724
725module_i2c_driver(ts2020_driver);
726
Konstantin Dimitrov6fef4fc2012-12-23 19:25:27 -0300727MODULE_AUTHOR("Konstantin Dimitrov <kosio.dimitrov@gmail.com>");
728MODULE_DESCRIPTION("Montage Technology TS2020 - Silicon tuner driver module");
729MODULE_LICENSE("GPL");