blob: 40c42dec721b9de556dacc6cadc12a80ba3cc06b [file] [log] [blame]
Antti Palosaari695efd02013-02-25 08:43:32 -03001/*
2 * Montage M88TS2022 silicon tuner driver
3 *
4 * Copyright (C) 2013 Antti Palosaari <crope@iki.fi>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
Antti Palosaari695efd02013-02-25 08:43:32 -030016 * Some calculations are taken from existing TS2020 driver.
17 */
18
19#include "m88ts2022_priv.h"
20
21/* write multiple registers */
22static int m88ts2022_wr_regs(struct m88ts2022_priv *priv,
23 u8 reg, const u8 *val, int len)
24{
Antti Palosaari050ae822013-11-07 17:01:31 -030025#define MAX_WR_LEN 3
26#define MAX_WR_XFER_LEN (MAX_WR_LEN + 1)
Antti Palosaari695efd02013-02-25 08:43:32 -030027 int ret;
Antti Palosaari050ae822013-11-07 17:01:31 -030028 u8 buf[MAX_WR_XFER_LEN];
Antti Palosaari695efd02013-02-25 08:43:32 -030029 struct i2c_msg msg[1] = {
30 {
Antti Palosaarieafa2ad2013-12-02 18:38:41 -030031 .addr = priv->client->addr,
Antti Palosaari695efd02013-02-25 08:43:32 -030032 .flags = 0,
Antti Palosaari050ae822013-11-07 17:01:31 -030033 .len = 1 + len,
Antti Palosaari695efd02013-02-25 08:43:32 -030034 .buf = buf,
35 }
36 };
37
Antti Palosaari050ae822013-11-07 17:01:31 -030038 if (WARN_ON(len > MAX_WR_LEN))
39 return -EINVAL;
40
Antti Palosaari695efd02013-02-25 08:43:32 -030041 buf[0] = reg;
42 memcpy(&buf[1], val, len);
43
Antti Palosaarieafa2ad2013-12-02 18:38:41 -030044 ret = i2c_transfer(priv->client->adapter, msg, 1);
Antti Palosaari695efd02013-02-25 08:43:32 -030045 if (ret == 1) {
46 ret = 0;
47 } else {
Antti Palosaarieafa2ad2013-12-02 18:38:41 -030048 dev_warn(&priv->client->dev,
Antti Palosaari695efd02013-02-25 08:43:32 -030049 "%s: i2c wr failed=%d reg=%02x len=%d\n",
50 KBUILD_MODNAME, ret, reg, len);
51 ret = -EREMOTEIO;
52 }
53
54 return ret;
55}
56
57/* read multiple registers */
58static int m88ts2022_rd_regs(struct m88ts2022_priv *priv, u8 reg,
59 u8 *val, int len)
60{
Antti Palosaari050ae822013-11-07 17:01:31 -030061#define MAX_RD_LEN 1
62#define MAX_RD_XFER_LEN (MAX_RD_LEN)
Antti Palosaari695efd02013-02-25 08:43:32 -030063 int ret;
Antti Palosaari050ae822013-11-07 17:01:31 -030064 u8 buf[MAX_RD_XFER_LEN];
Antti Palosaari695efd02013-02-25 08:43:32 -030065 struct i2c_msg msg[2] = {
66 {
Antti Palosaarieafa2ad2013-12-02 18:38:41 -030067 .addr = priv->client->addr,
Antti Palosaari695efd02013-02-25 08:43:32 -030068 .flags = 0,
69 .len = 1,
70 .buf = &reg,
71 }, {
Antti Palosaarieafa2ad2013-12-02 18:38:41 -030072 .addr = priv->client->addr,
Antti Palosaari695efd02013-02-25 08:43:32 -030073 .flags = I2C_M_RD,
Antti Palosaari050ae822013-11-07 17:01:31 -030074 .len = len,
Antti Palosaari695efd02013-02-25 08:43:32 -030075 .buf = buf,
76 }
77 };
78
Antti Palosaari050ae822013-11-07 17:01:31 -030079 if (WARN_ON(len > MAX_RD_LEN))
80 return -EINVAL;
81
Antti Palosaarieafa2ad2013-12-02 18:38:41 -030082 ret = i2c_transfer(priv->client->adapter, msg, 2);
Antti Palosaari695efd02013-02-25 08:43:32 -030083 if (ret == 2) {
84 memcpy(val, buf, len);
85 ret = 0;
86 } else {
Antti Palosaarieafa2ad2013-12-02 18:38:41 -030087 dev_warn(&priv->client->dev,
Antti Palosaari695efd02013-02-25 08:43:32 -030088 "%s: i2c rd failed=%d reg=%02x len=%d\n",
89 KBUILD_MODNAME, ret, reg, len);
90 ret = -EREMOTEIO;
91 }
92
93 return ret;
94}
95
96/* write single register */
97static int m88ts2022_wr_reg(struct m88ts2022_priv *priv, u8 reg, u8 val)
98{
99 return m88ts2022_wr_regs(priv, reg, &val, 1);
100}
101
102/* read single register */
103static int m88ts2022_rd_reg(struct m88ts2022_priv *priv, u8 reg, u8 *val)
104{
105 return m88ts2022_rd_regs(priv, reg, val, 1);
106}
107
108/* write single register with mask */
109static int m88ts2022_wr_reg_mask(struct m88ts2022_priv *priv,
110 u8 reg, u8 val, u8 mask)
111{
112 int ret;
113 u8 u8tmp;
114
115 /* no need for read if whole reg is written */
116 if (mask != 0xff) {
117 ret = m88ts2022_rd_regs(priv, reg, &u8tmp, 1);
118 if (ret)
119 return ret;
120
121 val &= mask;
122 u8tmp &= ~mask;
123 val |= u8tmp;
124 }
125
126 return m88ts2022_wr_regs(priv, reg, &val, 1);
127}
128
129static int m88ts2022_cmd(struct dvb_frontend *fe,
130 int op, int sleep, u8 reg, u8 mask, u8 val, u8 *reg_val)
131{
132 struct m88ts2022_priv *priv = fe->tuner_priv;
133 int ret, i;
134 u8 u8tmp;
135 struct m88ts2022_reg_val reg_vals[] = {
136 {0x51, 0x1f - op},
137 {0x51, 0x1f},
138 {0x50, 0x00 + op},
139 {0x50, 0x00},
140 };
141
142 for (i = 0; i < 2; i++) {
Antti Palosaarieafa2ad2013-12-02 18:38:41 -0300143 dev_dbg(&priv->client->dev,
Antti Palosaari695efd02013-02-25 08:43:32 -0300144 "%s: i=%d op=%02x reg=%02x mask=%02x val=%02x\n",
145 __func__, i, op, reg, mask, val);
146
147 for (i = 0; i < ARRAY_SIZE(reg_vals); i++) {
148 ret = m88ts2022_wr_reg(priv, reg_vals[i].reg,
149 reg_vals[i].val);
150 if (ret)
151 goto err;
152 }
153
154 usleep_range(sleep * 1000, sleep * 10000);
155
156 ret = m88ts2022_rd_reg(priv, reg, &u8tmp);
157 if (ret)
158 goto err;
159
160 if ((u8tmp & mask) != val)
161 break;
162 }
163
164 if (reg_val)
165 *reg_val = u8tmp;
166err:
167 return ret;
168}
169
170static int m88ts2022_set_params(struct dvb_frontend *fe)
171{
172 struct m88ts2022_priv *priv = fe->tuner_priv;
173 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Antti Palosaaric19314b2013-12-02 01:39:24 -0300174 int ret;
175 unsigned int frequency_khz, frequency_offset_khz, f_3db_hz;
176 unsigned int f_ref_khz, f_vco_khz, div_ref, div_out, pll_n, gdiv28;
177 u8 buf[3], u8tmp, cap_code, lpf_gm, lpf_mxdiv, div_max, div_min;
178 u16 u16tmp;
Antti Palosaarieafa2ad2013-12-02 18:38:41 -0300179 dev_dbg(&priv->client->dev,
Antti Palosaari695efd02013-02-25 08:43:32 -0300180 "%s: frequency=%d symbol_rate=%d rolloff=%d\n",
181 __func__, c->frequency, c->symbol_rate, c->rolloff);
Antti Palosaaric19314b2013-12-02 01:39:24 -0300182 /*
183 * Integer-N PLL synthesizer
184 * kHz is used for all calculations to keep calculations within 32-bit
185 */
Antti Palosaarieafa2ad2013-12-02 18:38:41 -0300186 f_ref_khz = DIV_ROUND_CLOSEST(priv->cfg.clock, 1000);
Antti Palosaaric19314b2013-12-02 01:39:24 -0300187 div_ref = DIV_ROUND_CLOSEST(f_ref_khz, 2000);
Antti Palosaari695efd02013-02-25 08:43:32 -0300188
189 if (c->symbol_rate < 5000000)
Antti Palosaaric19314b2013-12-02 01:39:24 -0300190 frequency_offset_khz = 3000; /* 3 MHz */
Antti Palosaari695efd02013-02-25 08:43:32 -0300191 else
Antti Palosaaric19314b2013-12-02 01:39:24 -0300192 frequency_offset_khz = 0;
Antti Palosaari695efd02013-02-25 08:43:32 -0300193
Antti Palosaaric19314b2013-12-02 01:39:24 -0300194 frequency_khz = c->frequency + frequency_offset_khz;
Antti Palosaari695efd02013-02-25 08:43:32 -0300195
196 if (frequency_khz < 1103000) {
Antti Palosaaric19314b2013-12-02 01:39:24 -0300197 div_out = 4;
Antti Palosaari695efd02013-02-25 08:43:32 -0300198 u8tmp = 0x1b;
199 } else {
Antti Palosaaric19314b2013-12-02 01:39:24 -0300200 div_out = 2;
Antti Palosaari695efd02013-02-25 08:43:32 -0300201 u8tmp = 0x0b;
202 }
203
204 buf[0] = u8tmp;
205 buf[1] = 0x40;
206 ret = m88ts2022_wr_regs(priv, 0x10, buf, 2);
207 if (ret)
208 goto err;
209
Antti Palosaaric19314b2013-12-02 01:39:24 -0300210 f_vco_khz = frequency_khz * div_out;
211 pll_n = f_vco_khz * div_ref / f_ref_khz;
212 pll_n += pll_n % 2;
213 priv->frequency_khz = pll_n * f_ref_khz / div_ref / div_out;
Antti Palosaari695efd02013-02-25 08:43:32 -0300214
Antti Palosaaric19314b2013-12-02 01:39:24 -0300215 if (pll_n < 4095)
216 u16tmp = pll_n - 1024;
217 else if (pll_n < 6143)
218 u16tmp = pll_n + 1024;
Antti Palosaari695efd02013-02-25 08:43:32 -0300219 else
Antti Palosaaric19314b2013-12-02 01:39:24 -0300220 u16tmp = pll_n + 3072;
Antti Palosaari695efd02013-02-25 08:43:32 -0300221
Antti Palosaaric19314b2013-12-02 01:39:24 -0300222 buf[0] = (u16tmp >> 8) & 0x3f;
223 buf[1] = (u16tmp >> 0) & 0xff;
224 buf[2] = div_ref - 8;
Antti Palosaari695efd02013-02-25 08:43:32 -0300225 ret = m88ts2022_wr_regs(priv, 0x01, buf, 3);
226 if (ret)
227 goto err;
228
Antti Palosaarieafa2ad2013-12-02 18:38:41 -0300229 dev_dbg(&priv->client->dev,
Antti Palosaaric19314b2013-12-02 01:39:24 -0300230 "%s: frequency=%u offset=%d f_vco_khz=%u pll_n=%u div_ref=%u div_out=%u\n",
Antti Palosaari695efd02013-02-25 08:43:32 -0300231 __func__, priv->frequency_khz,
Antti Palosaaric19314b2013-12-02 01:39:24 -0300232 priv->frequency_khz - c->frequency, f_vco_khz, pll_n,
233 div_ref, div_out);
Antti Palosaari695efd02013-02-25 08:43:32 -0300234
235 ret = m88ts2022_cmd(fe, 0x10, 5, 0x15, 0x40, 0x00, NULL);
236 if (ret)
237 goto err;
238
239 ret = m88ts2022_rd_reg(priv, 0x14, &u8tmp);
240 if (ret)
241 goto err;
242
243 u8tmp &= 0x7f;
244 if (u8tmp < 64) {
245 ret = m88ts2022_wr_reg_mask(priv, 0x10, 0x80, 0x80);
246 if (ret)
247 goto err;
248
249 ret = m88ts2022_wr_reg(priv, 0x11, 0x6f);
250 if (ret)
251 goto err;
252
253 ret = m88ts2022_cmd(fe, 0x10, 5, 0x15, 0x40, 0x00, NULL);
254 if (ret)
255 goto err;
256 }
257
258 ret = m88ts2022_rd_reg(priv, 0x14, &u8tmp);
259 if (ret)
260 goto err;
261
262 u8tmp &= 0x1f;
263 if (u8tmp > 19) {
264 ret = m88ts2022_wr_reg_mask(priv, 0x10, 0x00, 0x02);
265 if (ret)
266 goto err;
267 }
268
269 ret = m88ts2022_cmd(fe, 0x08, 5, 0x3c, 0xff, 0x00, NULL);
270 if (ret)
271 goto err;
272
273 ret = m88ts2022_wr_reg(priv, 0x25, 0x00);
274 if (ret)
275 goto err;
276
277 ret = m88ts2022_wr_reg(priv, 0x27, 0x70);
278 if (ret)
279 goto err;
280
281 ret = m88ts2022_wr_reg(priv, 0x41, 0x09);
282 if (ret)
283 goto err;
284
285 ret = m88ts2022_wr_reg(priv, 0x08, 0x0b);
286 if (ret)
287 goto err;
288
Antti Palosaaric19314b2013-12-02 01:39:24 -0300289 /* filters */
290 gdiv28 = DIV_ROUND_CLOSEST(f_ref_khz * 1694U, 1000000U);
Antti Palosaari695efd02013-02-25 08:43:32 -0300291
292 ret = m88ts2022_wr_reg(priv, 0x04, gdiv28);
293 if (ret)
294 goto err;
295
296 ret = m88ts2022_cmd(fe, 0x04, 2, 0x26, 0xff, 0x00, &u8tmp);
297 if (ret)
298 goto err;
299
300 cap_code = u8tmp & 0x3f;
301
302 ret = m88ts2022_wr_reg(priv, 0x41, 0x0d);
303 if (ret)
304 goto err;
305
306 ret = m88ts2022_cmd(fe, 0x04, 2, 0x26, 0xff, 0x00, &u8tmp);
307 if (ret)
308 goto err;
309
310 u8tmp &= 0x3f;
311 cap_code = (cap_code + u8tmp) / 2;
312 gdiv28 = gdiv28 * 207 / (cap_code * 2 + 151);
313 div_max = gdiv28 * 135 / 100;
314 div_min = gdiv28 * 78 / 100;
Antti Palosaaric19314b2013-12-02 01:39:24 -0300315 div_max = clamp_val(div_max, 0U, 63U);
Antti Palosaari695efd02013-02-25 08:43:32 -0300316
Antti Palosaaric19314b2013-12-02 01:39:24 -0300317 f_3db_hz = c->symbol_rate * 135UL / 200UL;
318 f_3db_hz += 2000000U + (frequency_offset_khz * 1000U);
319 f_3db_hz = clamp(f_3db_hz, 7000000U, 40000000U);
Antti Palosaari695efd02013-02-25 08:43:32 -0300320
Antti Palosaaric19314b2013-12-02 01:39:24 -0300321#define LPF_COEFF 3200U
322 lpf_gm = DIV_ROUND_CLOSEST(f_3db_hz * gdiv28, LPF_COEFF * f_ref_khz);
323 lpf_gm = clamp_val(lpf_gm, 1U, 23U);
Antti Palosaari695efd02013-02-25 08:43:32 -0300324
Antti Palosaaric19314b2013-12-02 01:39:24 -0300325 lpf_mxdiv = DIV_ROUND_CLOSEST(lpf_gm * LPF_COEFF * f_ref_khz, f_3db_hz);
326 if (lpf_mxdiv < div_min)
327 lpf_mxdiv = DIV_ROUND_CLOSEST(++lpf_gm * LPF_COEFF * f_ref_khz, f_3db_hz);
328 lpf_mxdiv = clamp_val(lpf_mxdiv, 0U, div_max);
Antti Palosaari695efd02013-02-25 08:43:32 -0300329
330 ret = m88ts2022_wr_reg(priv, 0x04, lpf_mxdiv);
331 if (ret)
332 goto err;
333
334 ret = m88ts2022_wr_reg(priv, 0x06, lpf_gm);
335 if (ret)
336 goto err;
337
338 ret = m88ts2022_cmd(fe, 0x04, 2, 0x26, 0xff, 0x00, &u8tmp);
339 if (ret)
340 goto err;
341
342 cap_code = u8tmp & 0x3f;
343
344 ret = m88ts2022_wr_reg(priv, 0x41, 0x09);
345 if (ret)
346 goto err;
347
348 ret = m88ts2022_cmd(fe, 0x04, 2, 0x26, 0xff, 0x00, &u8tmp);
349 if (ret)
350 goto err;
351
352 u8tmp &= 0x3f;
353 cap_code = (cap_code + u8tmp) / 2;
354
355 u8tmp = cap_code | 0x80;
356 ret = m88ts2022_wr_reg(priv, 0x25, u8tmp);
357 if (ret)
358 goto err;
359
360 ret = m88ts2022_wr_reg(priv, 0x27, 0x30);
361 if (ret)
362 goto err;
363
364 ret = m88ts2022_wr_reg(priv, 0x08, 0x09);
365 if (ret)
366 goto err;
367
368 ret = m88ts2022_cmd(fe, 0x01, 20, 0x21, 0xff, 0x00, NULL);
369 if (ret)
370 goto err;
371err:
372 if (ret)
Antti Palosaarieafa2ad2013-12-02 18:38:41 -0300373 dev_dbg(&priv->client->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari695efd02013-02-25 08:43:32 -0300374
375 return ret;
376}
377
378static int m88ts2022_init(struct dvb_frontend *fe)
379{
380 struct m88ts2022_priv *priv = fe->tuner_priv;
381 int ret, i;
382 u8 u8tmp;
383 static const struct m88ts2022_reg_val reg_vals[] = {
384 {0x7d, 0x9d},
385 {0x7c, 0x9a},
386 {0x7a, 0x76},
387 {0x3b, 0x01},
388 {0x63, 0x88},
389 {0x61, 0x85},
390 {0x22, 0x30},
391 {0x30, 0x40},
392 {0x20, 0x23},
393 {0x24, 0x02},
394 {0x12, 0xa0},
395 };
Antti Palosaarieafa2ad2013-12-02 18:38:41 -0300396 dev_dbg(&priv->client->dev, "%s:\n", __func__);
Antti Palosaari695efd02013-02-25 08:43:32 -0300397
398 ret = m88ts2022_wr_reg(priv, 0x00, 0x01);
399 if (ret)
400 goto err;
401
402 ret = m88ts2022_wr_reg(priv, 0x00, 0x03);
403 if (ret)
404 goto err;
405
Antti Palosaarieafa2ad2013-12-02 18:38:41 -0300406 switch (priv->cfg.clock_out) {
Antti Palosaari695efd02013-02-25 08:43:32 -0300407 case M88TS2022_CLOCK_OUT_DISABLED:
408 u8tmp = 0x60;
409 break;
410 case M88TS2022_CLOCK_OUT_ENABLED:
411 u8tmp = 0x70;
Antti Palosaarieafa2ad2013-12-02 18:38:41 -0300412 ret = m88ts2022_wr_reg(priv, 0x05, priv->cfg.clock_out_div);
Antti Palosaari695efd02013-02-25 08:43:32 -0300413 if (ret)
414 goto err;
415 break;
416 case M88TS2022_CLOCK_OUT_ENABLED_XTALOUT:
417 u8tmp = 0x6c;
418 break;
419 default:
420 goto err;
421 }
422
423 ret = m88ts2022_wr_reg(priv, 0x42, u8tmp);
424 if (ret)
425 goto err;
426
Antti Palosaarieafa2ad2013-12-02 18:38:41 -0300427 if (priv->cfg.loop_through)
Antti Palosaari695efd02013-02-25 08:43:32 -0300428 u8tmp = 0xec;
429 else
430 u8tmp = 0x6c;
431
432 ret = m88ts2022_wr_reg(priv, 0x62, u8tmp);
433 if (ret)
434 goto err;
435
436 for (i = 0; i < ARRAY_SIZE(reg_vals); i++) {
437 ret = m88ts2022_wr_reg(priv, reg_vals[i].reg, reg_vals[i].val);
438 if (ret)
439 goto err;
440 }
441err:
442 if (ret)
Antti Palosaarieafa2ad2013-12-02 18:38:41 -0300443 dev_dbg(&priv->client->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari695efd02013-02-25 08:43:32 -0300444 return ret;
445}
446
447static int m88ts2022_sleep(struct dvb_frontend *fe)
448{
449 struct m88ts2022_priv *priv = fe->tuner_priv;
450 int ret;
Antti Palosaarieafa2ad2013-12-02 18:38:41 -0300451 dev_dbg(&priv->client->dev, "%s:\n", __func__);
Antti Palosaari695efd02013-02-25 08:43:32 -0300452
453 ret = m88ts2022_wr_reg(priv, 0x00, 0x00);
454 if (ret)
455 goto err;
456err:
457 if (ret)
Antti Palosaarieafa2ad2013-12-02 18:38:41 -0300458 dev_dbg(&priv->client->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari695efd02013-02-25 08:43:32 -0300459 return ret;
460}
461
462static int m88ts2022_get_frequency(struct dvb_frontend *fe, u32 *frequency)
463{
464 struct m88ts2022_priv *priv = fe->tuner_priv;
Antti Palosaarieafa2ad2013-12-02 18:38:41 -0300465 dev_dbg(&priv->client->dev, "%s:\n", __func__);
Antti Palosaari695efd02013-02-25 08:43:32 -0300466
467 *frequency = priv->frequency_khz;
468 return 0;
469}
470
471static int m88ts2022_get_if_frequency(struct dvb_frontend *fe, u32 *frequency)
472{
473 struct m88ts2022_priv *priv = fe->tuner_priv;
Antti Palosaarieafa2ad2013-12-02 18:38:41 -0300474 dev_dbg(&priv->client->dev, "%s:\n", __func__);
Antti Palosaari695efd02013-02-25 08:43:32 -0300475
476 *frequency = 0; /* Zero-IF */
477 return 0;
478}
479
480static int m88ts2022_get_rf_strength(struct dvb_frontend *fe, u16 *strength)
481{
482 struct m88ts2022_priv *priv = fe->tuner_priv;
Antti Palosaari695efd02013-02-25 08:43:32 -0300483 int ret;
Antti Palosaaric19314b2013-12-02 01:39:24 -0300484 u8 u8tmp;
485 u16 gain, u16tmp;
486 unsigned int gain1, gain2, gain3;
Antti Palosaari695efd02013-02-25 08:43:32 -0300487
488 ret = m88ts2022_rd_reg(priv, 0x3d, &u8tmp);
489 if (ret)
490 goto err;
491
492 gain1 = (u8tmp >> 0) & 0x1f;
Antti Palosaaric19314b2013-12-02 01:39:24 -0300493 gain1 = clamp(gain1, 0U, 15U);
Antti Palosaari695efd02013-02-25 08:43:32 -0300494
495 ret = m88ts2022_rd_reg(priv, 0x21, &u8tmp);
496 if (ret)
497 goto err;
498
499 gain2 = (u8tmp >> 0) & 0x1f;
Antti Palosaaric19314b2013-12-02 01:39:24 -0300500 gain2 = clamp(gain2, 2U, 16U);
Antti Palosaari695efd02013-02-25 08:43:32 -0300501
502 ret = m88ts2022_rd_reg(priv, 0x66, &u8tmp);
503 if (ret)
504 goto err;
505
506 gain3 = (u8tmp >> 3) & 0x07;
Antti Palosaaric19314b2013-12-02 01:39:24 -0300507 gain3 = clamp(gain3, 0U, 6U);
Antti Palosaari695efd02013-02-25 08:43:32 -0300508
509 gain = gain1 * 265 + gain2 * 338 + gain3 * 285;
510
511 /* scale value to 0x0000-0xffff */
512 u16tmp = (0xffff - gain);
Antti Palosaaric19314b2013-12-02 01:39:24 -0300513 u16tmp = clamp_val(u16tmp, 59000U, 61500U);
Antti Palosaari695efd02013-02-25 08:43:32 -0300514
515 *strength = (u16tmp - 59000) * 0xffff / (61500 - 59000);
516err:
517 if (ret)
Antti Palosaarieafa2ad2013-12-02 18:38:41 -0300518 dev_dbg(&priv->client->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari695efd02013-02-25 08:43:32 -0300519 return ret;
520}
521
Antti Palosaari695efd02013-02-25 08:43:32 -0300522static const struct dvb_tuner_ops m88ts2022_tuner_ops = {
523 .info = {
524 .name = "Montage M88TS2022",
525 .frequency_min = 950000,
526 .frequency_max = 2150000,
527 },
528
Antti Palosaari695efd02013-02-25 08:43:32 -0300529 .init = m88ts2022_init,
530 .sleep = m88ts2022_sleep,
531 .set_params = m88ts2022_set_params,
532
533 .get_frequency = m88ts2022_get_frequency,
534 .get_if_frequency = m88ts2022_get_if_frequency,
535 .get_rf_strength = m88ts2022_get_rf_strength,
536};
537
Antti Palosaarieafa2ad2013-12-02 18:38:41 -0300538static int m88ts2022_probe(struct i2c_client *client,
539 const struct i2c_device_id *id)
Antti Palosaari695efd02013-02-25 08:43:32 -0300540{
Antti Palosaarieafa2ad2013-12-02 18:38:41 -0300541 struct m88ts2022_config *cfg = client->dev.platform_data;
542 struct dvb_frontend *fe = cfg->fe;
Antti Palosaari695efd02013-02-25 08:43:32 -0300543 struct m88ts2022_priv *priv;
544 int ret;
545 u8 chip_id, u8tmp;
546
Antti Palosaarib03129f2013-12-18 12:46:45 -0300547 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
Antti Palosaari695efd02013-02-25 08:43:32 -0300548 if (!priv) {
549 ret = -ENOMEM;
Antti Palosaarieafa2ad2013-12-02 18:38:41 -0300550 dev_err(&client->dev, "%s: kzalloc() failed\n", KBUILD_MODNAME);
Antti Palosaari695efd02013-02-25 08:43:32 -0300551 goto err;
552 }
553
Antti Palosaarieafa2ad2013-12-02 18:38:41 -0300554 memcpy(&priv->cfg, cfg, sizeof(struct m88ts2022_config));
555 priv->client = client;
Antti Palosaari695efd02013-02-25 08:43:32 -0300556
557 /* check if the tuner is there */
558 ret = m88ts2022_rd_reg(priv, 0x00, &u8tmp);
559 if (ret)
560 goto err;
561
562 if ((u8tmp & 0x03) == 0x00) {
563 ret = m88ts2022_wr_reg(priv, 0x00, 0x01);
564 if (ret < 0)
565 goto err;
566
567 usleep_range(2000, 50000);
568 }
569
570 ret = m88ts2022_wr_reg(priv, 0x00, 0x03);
571 if (ret)
572 goto err;
573
574 usleep_range(2000, 50000);
575
576 ret = m88ts2022_rd_reg(priv, 0x00, &chip_id);
577 if (ret)
578 goto err;
579
Antti Palosaarieafa2ad2013-12-02 18:38:41 -0300580 dev_dbg(&priv->client->dev, "%s: chip_id=%02x\n", __func__, chip_id);
Antti Palosaari695efd02013-02-25 08:43:32 -0300581
582 switch (chip_id) {
583 case 0xc3:
584 case 0x83:
585 break;
586 default:
587 goto err;
588 }
589
Antti Palosaarieafa2ad2013-12-02 18:38:41 -0300590 switch (priv->cfg.clock_out) {
Antti Palosaari695efd02013-02-25 08:43:32 -0300591 case M88TS2022_CLOCK_OUT_DISABLED:
592 u8tmp = 0x60;
593 break;
594 case M88TS2022_CLOCK_OUT_ENABLED:
595 u8tmp = 0x70;
Antti Palosaarieafa2ad2013-12-02 18:38:41 -0300596 ret = m88ts2022_wr_reg(priv, 0x05, priv->cfg.clock_out_div);
Antti Palosaari695efd02013-02-25 08:43:32 -0300597 if (ret)
598 goto err;
599 break;
600 case M88TS2022_CLOCK_OUT_ENABLED_XTALOUT:
601 u8tmp = 0x6c;
602 break;
603 default:
604 goto err;
605 }
606
607 ret = m88ts2022_wr_reg(priv, 0x42, u8tmp);
608 if (ret)
609 goto err;
610
Antti Palosaarieafa2ad2013-12-02 18:38:41 -0300611 if (priv->cfg.loop_through)
Antti Palosaari695efd02013-02-25 08:43:32 -0300612 u8tmp = 0xec;
613 else
614 u8tmp = 0x6c;
615
616 ret = m88ts2022_wr_reg(priv, 0x62, u8tmp);
617 if (ret)
618 goto err;
619
620 /* sleep */
621 ret = m88ts2022_wr_reg(priv, 0x00, 0x00);
622 if (ret)
623 goto err;
624
Antti Palosaarieafa2ad2013-12-02 18:38:41 -0300625 dev_info(&priv->client->dev,
Antti Palosaari695efd02013-02-25 08:43:32 -0300626 "%s: Montage M88TS2022 successfully identified\n",
627 KBUILD_MODNAME);
628
629 fe->tuner_priv = priv;
630 memcpy(&fe->ops.tuner_ops, &m88ts2022_tuner_ops,
631 sizeof(struct dvb_tuner_ops));
Antti Palosaari695efd02013-02-25 08:43:32 -0300632
Antti Palosaarieafa2ad2013-12-02 18:38:41 -0300633 i2c_set_clientdata(client, priv);
634 return 0;
635err:
636 dev_dbg(&client->dev, "%s: failed=%d\n", __func__, ret);
637 kfree(priv);
638 return ret;
Antti Palosaari695efd02013-02-25 08:43:32 -0300639}
Antti Palosaarieafa2ad2013-12-02 18:38:41 -0300640
641static int m88ts2022_remove(struct i2c_client *client)
642{
643 struct m88ts2022_priv *priv = i2c_get_clientdata(client);
644 struct dvb_frontend *fe = priv->cfg.fe;
645 dev_dbg(&client->dev, "%s:\n", __func__);
646
647 memset(&fe->ops.tuner_ops, 0, sizeof(struct dvb_tuner_ops));
648 fe->tuner_priv = NULL;
649 kfree(priv);
650
651 return 0;
652}
653
654static const struct i2c_device_id m88ts2022_id[] = {
655 {"m88ts2022", 0},
656 {}
657};
658MODULE_DEVICE_TABLE(i2c, m88ts2022_id);
659
660static struct i2c_driver m88ts2022_driver = {
661 .driver = {
662 .owner = THIS_MODULE,
663 .name = "m88ts2022",
664 },
665 .probe = m88ts2022_probe,
666 .remove = m88ts2022_remove,
667 .id_table = m88ts2022_id,
668};
669
670module_i2c_driver(m88ts2022_driver);
Antti Palosaari695efd02013-02-25 08:43:32 -0300671
672MODULE_DESCRIPTION("Montage M88TS2022 silicon tuner driver");
673MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
674MODULE_LICENSE("GPL");