Merge branch 'asoc-4.19' into asoc-4.20 for rt5682 deps.
[linux-2.6-block.git] / drivers / media / tuners / tda8290.c
CommitLineData
1da177e4 1/*
de48eebc
HH
2
3 i2c tv tuner chip device driver
4 controls the philips tda8290+75 tuner chip combo.
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
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
910bb3e3
MK
19
20 This "tda8290" module was split apart from the original "tuner" module.
de48eebc
HH
21*/
22
1da177e4 23#include <linux/i2c.h>
5a0e3ad6 24#include <linux/slab.h>
1da177e4 25#include <linux/delay.h>
7f6adeaf 26#include <linux/videodev2.h>
ab166050 27#include "tuner-i2c.h"
910bb3e3 28#include "tda8290.h"
746d9732 29#include "tda827x.h"
5bea1cd3 30#include "tda18271.h"
910bb3e3 31
ab166050
MK
32static int debug;
33module_param(debug, int, 0644);
910bb3e3
MK
34MODULE_PARM_DESC(debug, "enable verbose debug messages");
35
9af0ef27 36static int deemphasis_50;
b450b92e 37module_param(deemphasis_50, int, 0644);
9af0ef27
MCC
38MODULE_PARM_DESC(deemphasis_50, "0 - 75us deemphasis; 1 - 50us deemphasis");
39
1da177e4
LT
40/* ---------------------------------------------------------------------- */
41
b2083199 42struct tda8290_priv {
db8a6956
MK
43 struct tuner_i2c_props i2c_props;
44
b2083199 45 unsigned char tda8290_easy_mode;
746d9732 46
b2083199 47 unsigned char tda827x_addr;
8c125f2c
MK
48
49 unsigned char ver;
50#define TDA8290 1
51#define TDA8295 2
52#define TDA8275 4
53#define TDA8275A 8
54#define TDA18271 16
1da177e4 55
746d9732 56 struct tda827x_config cfg;
5b0e5350 57 struct tda18271_std_map *tda18271_std_map;
1da177e4
LT
58};
59
de48eebc 60/*---------------------------------------------------------------------*/
1da177e4 61
a72dd305 62static int tda8290_i2c_bridge(struct dvb_frontend *fe, int close)
de48eebc 63{
4e9154b8 64 struct tda8290_priv *priv = fe->analog_demod_priv;
db8a6956 65
8ff230fb
AB
66 static unsigned char enable[2] = { 0x21, 0xC0 };
67 static unsigned char disable[2] = { 0x21, 0x00 };
de48eebc 68 unsigned char *msg;
a72dd305
MK
69
70 if (close) {
de48eebc 71 msg = enable;
db8a6956 72 tuner_i2c_xfer_send(&priv->i2c_props, msg, 2);
de48eebc
HH
73 /* let the bridge stabilize */
74 msleep(20);
75 } else {
76 msg = disable;
db8a6956 77 tuner_i2c_xfer_send(&priv->i2c_props, msg, 2);
de48eebc 78 }
a72dd305
MK
79
80 return 0;
1da177e4
LT
81}
82
a72dd305 83static int tda8295_i2c_bridge(struct dvb_frontend *fe, int close)
5bea1cd3 84{
4e9154b8 85 struct tda8290_priv *priv = fe->analog_demod_priv;
5bea1cd3 86
8ff230fb
AB
87 static unsigned char enable[2] = { 0x45, 0xc1 };
88 static unsigned char disable[2] = { 0x46, 0x00 };
89 static unsigned char buf[3] = { 0x45, 0x01, 0x00 };
5bea1cd3 90 unsigned char *msg;
a72dd305 91
5bea1cd3
MK
92 if (close) {
93 msg = enable;
94 tuner_i2c_xfer_send(&priv->i2c_props, msg, 2);
95 /* let the bridge stabilize */
96 msleep(20);
97 } else {
98 msg = disable;
567aba0b 99 tuner_i2c_xfer_send_recv(&priv->i2c_props, msg, 1, &msg[1], 1);
5bea1cd3
MK
100
101 buf[2] = msg[1];
102 buf[2] &= ~0x04;
103 tuner_i2c_xfer_send(&priv->i2c_props, buf, 3);
104 msleep(5);
105
106 msg[1] |= 0x04;
107 tuner_i2c_xfer_send(&priv->i2c_props, msg, 2);
108 }
a72dd305
MK
109
110 return 0;
5bea1cd3
MK
111}
112
de48eebc
HH
113/*---------------------------------------------------------------------*/
114
c7919d52
MK
115static void set_audio(struct dvb_frontend *fe,
116 struct analog_parameters *params)
1da177e4 117{
4e9154b8 118 struct tda8290_priv *priv = fe->analog_demod_priv;
910bb3e3
MK
119 char* mode;
120
c7919d52 121 if (params->std & V4L2_STD_MN) {
910bb3e3 122 priv->tda8290_easy_mode = 0x01;
910bb3e3 123 mode = "MN";
c7919d52 124 } else if (params->std & V4L2_STD_B) {
910bb3e3
MK
125 priv->tda8290_easy_mode = 0x02;
126 mode = "B";
c7919d52 127 } else if (params->std & V4L2_STD_GH) {
910bb3e3
MK
128 priv->tda8290_easy_mode = 0x04;
129 mode = "GH";
c7919d52 130 } else if (params->std & V4L2_STD_PAL_I) {
910bb3e3
MK
131 priv->tda8290_easy_mode = 0x08;
132 mode = "I";
c7919d52 133 } else if (params->std & V4L2_STD_DK) {
910bb3e3
MK
134 priv->tda8290_easy_mode = 0x10;
135 mode = "DK";
c7919d52 136 } else if (params->std & V4L2_STD_SECAM_L) {
910bb3e3
MK
137 priv->tda8290_easy_mode = 0x20;
138 mode = "L";
c7919d52 139 } else if (params->std & V4L2_STD_SECAM_LC) {
910bb3e3
MK
140 priv->tda8290_easy_mode = 0x40;
141 mode = "LC";
142 } else {
910bb3e3
MK
143 priv->tda8290_easy_mode = 0x10;
144 mode = "xx";
145 }
146
9af0ef27 147 if (params->mode == V4L2_TUNER_RADIO) {
3f76cf8c
MK
148 /* Set TDA8295 to FM radio; Start TDA8290 with MN values */
149 priv->tda8290_easy_mode = (priv->ver & TDA8295) ? 0x80 : 0x01;
9af0ef27
MCC
150 tuner_dbg("setting to radio FM\n");
151 } else {
152 tuner_dbg("setting tda829x to system %s\n", mode);
153 }
910bb3e3
MK
154}
155
4c27f1a4 156static struct {
9af0ef27
MCC
157 unsigned char seq[2];
158} fm_mode[] = {
159 { { 0x01, 0x81} }, /* Put device into expert mode */
160 { { 0x03, 0x48} }, /* Disable NOTCH and VIDEO filters */
161 { { 0x04, 0x04} }, /* Disable color carrier filter (SSIF) */
162 { { 0x05, 0x04} }, /* ADC headroom */
163 { { 0x06, 0x10} }, /* group delay flat */
164
165 { { 0x07, 0x00} }, /* use the same radio DTO values as a tda8295 */
166 { { 0x08, 0x00} },
167 { { 0x09, 0x80} },
168 { { 0x0a, 0xda} },
169 { { 0x0b, 0x4b} },
170 { { 0x0c, 0x68} },
171
172 { { 0x0d, 0x00} }, /* PLL off, no video carrier detect */
173 { { 0x14, 0x00} }, /* disable auto mute if no video */
174};
175
c7919d52
MK
176static void tda8290_set_params(struct dvb_frontend *fe,
177 struct analog_parameters *params)
910bb3e3 178{
4e9154b8 179 struct tda8290_priv *priv = fe->analog_demod_priv;
4e9154b8 180
8ff230fb 181 static unsigned char soft_reset[] = { 0x00, 0x00 };
b2083199 182 unsigned char easy_mode[] = { 0x01, priv->tda8290_easy_mode };
8ff230fb
AB
183 static unsigned char expert_mode[] = { 0x01, 0x80 };
184 static unsigned char agc_out_on[] = { 0x02, 0x00 };
185 static unsigned char gainset_off[] = { 0x28, 0x14 };
186 static unsigned char if_agc_spd[] = { 0x0f, 0x88 };
187 static unsigned char adc_head_6[] = { 0x05, 0x04 };
188 static unsigned char adc_head_9[] = { 0x05, 0x02 };
189 static unsigned char adc_head_12[] = { 0x05, 0x01 };
190 static unsigned char pll_bw_nom[] = { 0x0d, 0x47 };
191 static unsigned char pll_bw_low[] = { 0x0d, 0x27 };
192 static unsigned char gainset_2[] = { 0x28, 0x64 };
193 static unsigned char agc_rst_on[] = { 0x0e, 0x0b };
194 static unsigned char agc_rst_off[] = { 0x0e, 0x09 };
195 static unsigned char if_agc_set[] = { 0x0f, 0x81 };
196 static unsigned char addr_adc_sat = 0x1a;
197 static unsigned char addr_agc_stat = 0x1d;
198 static unsigned char addr_pll_stat = 0x1b;
199 static unsigned char adc_sat = 0, agc_stat = 0,
9a741ec9 200 pll_stat;
58ef4f92 201 int i;
de48eebc 202
c7919d52 203 set_audio(fe, params);
910bb3e3 204
ab166050 205 if (priv->cfg.config)
7bff4b4d 206 tuner_dbg("tda827xa config is 0x%02x\n", priv->cfg.config);
db8a6956
MK
207 tuner_i2c_xfer_send(&priv->i2c_props, easy_mode, 2);
208 tuner_i2c_xfer_send(&priv->i2c_props, agc_out_on, 2);
209 tuner_i2c_xfer_send(&priv->i2c_props, soft_reset, 2);
de48eebc
HH
210 msleep(1);
211
9af0ef27 212 if (params->mode == V4L2_TUNER_RADIO) {
9af0ef27
MCC
213 unsigned char deemphasis[] = { 0x13, 1 };
214
215 /* FIXME: allow using a different deemphasis */
216
217 if (deemphasis_50)
218 deemphasis[1] = 2;
219
220 for (i = 0; i < ARRAY_SIZE(fm_mode); i++)
221 tuner_i2c_xfer_send(&priv->i2c_props, fm_mode[i].seq, 2);
222
223 tuner_i2c_xfer_send(&priv->i2c_props, deemphasis, 2);
224 } else {
225 expert_mode[1] = priv->tda8290_easy_mode + 0x80;
226 tuner_i2c_xfer_send(&priv->i2c_props, expert_mode, 2);
227 tuner_i2c_xfer_send(&priv->i2c_props, gainset_off, 2);
228 tuner_i2c_xfer_send(&priv->i2c_props, if_agc_spd, 2);
229 if (priv->tda8290_easy_mode & 0x60)
230 tuner_i2c_xfer_send(&priv->i2c_props, adc_head_9, 2);
231 else
232 tuner_i2c_xfer_send(&priv->i2c_props, adc_head_6, 2);
233 tuner_i2c_xfer_send(&priv->i2c_props, pll_bw_nom, 2);
234 }
de48eebc 235
ecb71d26 236
2f719f7a
OZ
237 if (fe->ops.analog_ops.i2c_gate_ctrl)
238 fe->ops.analog_ops.i2c_gate_ctrl(fe, 1);
746d9732 239
4e9154b8 240 if (fe->ops.tuner_ops.set_analog_params)
c7919d52 241 fe->ops.tuner_ops.set_analog_params(fe, params);
746d9732 242
58ef4f92 243 for (i = 0; i < 3; i++) {
567aba0b
MCC
244 tuner_i2c_xfer_send_recv(&priv->i2c_props,
245 &addr_pll_stat, 1, &pll_stat, 1);
58ef4f92 246 if (pll_stat & 0x80) {
567aba0b
MCC
247 tuner_i2c_xfer_send_recv(&priv->i2c_props,
248 &addr_adc_sat, 1,
249 &adc_sat, 1);
250 tuner_i2c_xfer_send_recv(&priv->i2c_props,
251 &addr_agc_stat, 1,
252 &agc_stat, 1);
58ef4f92
HH
253 tuner_dbg("tda8290 is locked, AGC: %d\n", agc_stat);
254 break;
255 } else {
256 tuner_dbg("tda8290 not locked, no signal?\n");
257 msleep(100);
258 }
259 }
de48eebc 260 /* adjust headroom resp. gain */
4ac95af9
HH
261 if ((agc_stat > 115) || (!(pll_stat & 0x80) && (adc_sat < 20))) {
262 tuner_dbg("adjust gain, step 1. Agc: %d, ADC stat: %d, lock: %d\n",
263 agc_stat, adc_sat, pll_stat & 0x80);
db8a6956 264 tuner_i2c_xfer_send(&priv->i2c_props, gainset_2, 2);
de48eebc 265 msleep(100);
567aba0b
MCC
266 tuner_i2c_xfer_send_recv(&priv->i2c_props,
267 &addr_agc_stat, 1, &agc_stat, 1);
268 tuner_i2c_xfer_send_recv(&priv->i2c_props,
269 &addr_pll_stat, 1, &pll_stat, 1);
de48eebc 270 if ((agc_stat > 115) || !(pll_stat & 0x80)) {
4ac95af9
HH
271 tuner_dbg("adjust gain, step 2. Agc: %d, lock: %d\n",
272 agc_stat, pll_stat & 0x80);
746d9732 273 if (priv->cfg.agcf)
4e9154b8 274 priv->cfg.agcf(fe);
de48eebc 275 msleep(100);
567aba0b
MCC
276 tuner_i2c_xfer_send_recv(&priv->i2c_props,
277 &addr_agc_stat, 1,
278 &agc_stat, 1);
279 tuner_i2c_xfer_send_recv(&priv->i2c_props,
280 &addr_pll_stat, 1,
281 &pll_stat, 1);
de48eebc
HH
282 if((agc_stat > 115) || !(pll_stat & 0x80)) {
283 tuner_dbg("adjust gain, step 3. Agc: %d\n", agc_stat);
db8a6956
MK
284 tuner_i2c_xfer_send(&priv->i2c_props, adc_head_12, 2);
285 tuner_i2c_xfer_send(&priv->i2c_props, pll_bw_low, 2);
de48eebc
HH
286 msleep(100);
287 }
288 }
289 }
1da177e4 290
de48eebc 291 /* l/ l' deadlock? */
b2083199 292 if(priv->tda8290_easy_mode & 0x60) {
567aba0b
MCC
293 tuner_i2c_xfer_send_recv(&priv->i2c_props,
294 &addr_adc_sat, 1,
295 &adc_sat, 1);
296 tuner_i2c_xfer_send_recv(&priv->i2c_props,
297 &addr_pll_stat, 1,
298 &pll_stat, 1);
de48eebc 299 if ((adc_sat > 20) || !(pll_stat & 0x80)) {
4ac95af9 300 tuner_dbg("trying to resolve SECAM L deadlock\n");
db8a6956 301 tuner_i2c_xfer_send(&priv->i2c_props, agc_rst_on, 2);
de48eebc 302 msleep(40);
db8a6956 303 tuner_i2c_xfer_send(&priv->i2c_props, agc_rst_off, 2);
de48eebc
HH
304 }
305 }
586b0cab 306
2f719f7a
OZ
307 if (fe->ops.analog_ops.i2c_gate_ctrl)
308 fe->ops.analog_ops.i2c_gate_ctrl(fe, 0);
db8a6956 309 tuner_i2c_xfer_send(&priv->i2c_props, if_agc_set, 2);
1da177e4
LT
310}
311
de48eebc
HH
312/*---------------------------------------------------------------------*/
313
4e9154b8 314static void tda8295_power(struct dvb_frontend *fe, int enable)
5bea1cd3 315{
4e9154b8 316 struct tda8290_priv *priv = fe->analog_demod_priv;
5bea1cd3
MK
317 unsigned char buf[] = { 0x30, 0x00 }; /* clb_stdbt */
318
567aba0b 319 tuner_i2c_xfer_send_recv(&priv->i2c_props, &buf[0], 1, &buf[1], 1);
5bea1cd3
MK
320
321 if (enable)
322 buf[1] = 0x01;
323 else
324 buf[1] = 0x03;
325
326 tuner_i2c_xfer_send(&priv->i2c_props, buf, 2);
327}
328
4e9154b8 329static void tda8295_set_easy_mode(struct dvb_frontend *fe, int enable)
5bea1cd3 330{
4e9154b8 331 struct tda8290_priv *priv = fe->analog_demod_priv;
5bea1cd3
MK
332 unsigned char buf[] = { 0x01, 0x00 };
333
567aba0b 334 tuner_i2c_xfer_send_recv(&priv->i2c_props, &buf[0], 1, &buf[1], 1);
5bea1cd3
MK
335
336 if (enable)
337 buf[1] = 0x01; /* rising edge sets regs 0x02 - 0x23 */
338 else
339 buf[1] = 0x00; /* reset active bit */
340
341 tuner_i2c_xfer_send(&priv->i2c_props, buf, 2);
342}
343
4e9154b8 344static void tda8295_set_video_std(struct dvb_frontend *fe)
5bea1cd3 345{
4e9154b8 346 struct tda8290_priv *priv = fe->analog_demod_priv;
5bea1cd3
MK
347 unsigned char buf[] = { 0x00, priv->tda8290_easy_mode };
348
349 tuner_i2c_xfer_send(&priv->i2c_props, buf, 2);
350
4e9154b8 351 tda8295_set_easy_mode(fe, 1);
5bea1cd3 352 msleep(20);
4e9154b8 353 tda8295_set_easy_mode(fe, 0);
5bea1cd3
MK
354}
355
356/*---------------------------------------------------------------------*/
357
4e9154b8 358static void tda8295_agc1_out(struct dvb_frontend *fe, int enable)
5bea1cd3 359{
4e9154b8 360 struct tda8290_priv *priv = fe->analog_demod_priv;
5bea1cd3
MK
361 unsigned char buf[] = { 0x02, 0x00 }; /* DIV_FUNC */
362
567aba0b 363 tuner_i2c_xfer_send_recv(&priv->i2c_props, &buf[0], 1, &buf[1], 1);
5bea1cd3
MK
364
365 if (enable)
366 buf[1] &= ~0x40;
367 else
368 buf[1] |= 0x40;
369
370 tuner_i2c_xfer_send(&priv->i2c_props, buf, 2);
371}
372
4e9154b8 373static void tda8295_agc2_out(struct dvb_frontend *fe, int enable)
5bea1cd3 374{
4e9154b8 375 struct tda8290_priv *priv = fe->analog_demod_priv;
5bea1cd3
MK
376 unsigned char set_gpio_cf[] = { 0x44, 0x00 };
377 unsigned char set_gpio_val[] = { 0x46, 0x00 };
378
567aba0b
MCC
379 tuner_i2c_xfer_send_recv(&priv->i2c_props,
380 &set_gpio_cf[0], 1, &set_gpio_cf[1], 1);
381 tuner_i2c_xfer_send_recv(&priv->i2c_props,
382 &set_gpio_val[0], 1, &set_gpio_val[1], 1);
5bea1cd3
MK
383
384 set_gpio_cf[1] &= 0xf0; /* clear GPIO_0 bits 3-0 */
385
386 if (enable) {
387 set_gpio_cf[1] |= 0x01; /* config GPIO_0 as Open Drain Out */
388 set_gpio_val[1] &= 0xfe; /* set GPIO_0 pin low */
389 }
390 tuner_i2c_xfer_send(&priv->i2c_props, set_gpio_cf, 2);
391 tuner_i2c_xfer_send(&priv->i2c_props, set_gpio_val, 2);
392}
393
dfc2e12d 394static int tda8295_has_signal(struct dvb_frontend *fe, u16 *signal)
5bea1cd3 395{
4e9154b8 396 struct tda8290_priv *priv = fe->analog_demod_priv;
5bea1cd3
MK
397
398 unsigned char hvpll_stat = 0x26;
399 unsigned char ret;
400
567aba0b 401 tuner_i2c_xfer_send_recv(&priv->i2c_props, &hvpll_stat, 1, &ret, 1);
dfc2e12d
HV
402 *signal = (ret & 0x01) ? 65535 : 0;
403 return 0;
5bea1cd3
MK
404}
405
406/*---------------------------------------------------------------------*/
407
c7919d52
MK
408static void tda8295_set_params(struct dvb_frontend *fe,
409 struct analog_parameters *params)
5bea1cd3 410{
4e9154b8 411 struct tda8290_priv *priv = fe->analog_demod_priv;
dfc2e12d 412 u16 signal = 0;
5bea1cd3
MK
413 unsigned char blanking_mode[] = { 0x1d, 0x00 };
414
c7919d52 415 set_audio(fe, params);
5bea1cd3 416
7e28adb2 417 tuner_dbg("%s: freq = %d\n", __func__, params->frequency);
5bea1cd3 418
4e9154b8
MK
419 tda8295_power(fe, 1);
420 tda8295_agc1_out(fe, 1);
5bea1cd3 421
567aba0b
MCC
422 tuner_i2c_xfer_send_recv(&priv->i2c_props,
423 &blanking_mode[0], 1, &blanking_mode[1], 1);
5bea1cd3 424
4e9154b8 425 tda8295_set_video_std(fe);
5bea1cd3
MK
426
427 blanking_mode[1] = 0x03;
428 tuner_i2c_xfer_send(&priv->i2c_props, blanking_mode, 2);
429 msleep(20);
430
2f719f7a
OZ
431 if (fe->ops.analog_ops.i2c_gate_ctrl)
432 fe->ops.analog_ops.i2c_gate_ctrl(fe, 1);
5bea1cd3 433
4e9154b8 434 if (fe->ops.tuner_ops.set_analog_params)
c7919d52 435 fe->ops.tuner_ops.set_analog_params(fe, params);
5bea1cd3
MK
436
437 if (priv->cfg.agcf)
4e9154b8 438 priv->cfg.agcf(fe);
5bea1cd3 439
dfc2e12d
HV
440 tda8295_has_signal(fe, &signal);
441 if (signal)
5bea1cd3
MK
442 tuner_dbg("tda8295 is locked\n");
443 else
444 tuner_dbg("tda8295 not locked, no signal?\n");
445
2f719f7a
OZ
446 if (fe->ops.analog_ops.i2c_gate_ctrl)
447 fe->ops.analog_ops.i2c_gate_ctrl(fe, 0);
5bea1cd3
MK
448}
449
450/*---------------------------------------------------------------------*/
451
dfc2e12d 452static int tda8290_has_signal(struct dvb_frontend *fe, u16 *signal)
1da177e4 453{
4e9154b8 454 struct tda8290_priv *priv = fe->analog_demod_priv;
1da177e4 455
910bb3e3
MK
456 unsigned char i2c_get_afc[1] = { 0x1B };
457 unsigned char afc = 0;
1da177e4 458
567aba0b
MCC
459 tuner_i2c_xfer_send_recv(&priv->i2c_props,
460 i2c_get_afc, ARRAY_SIZE(i2c_get_afc), &afc, 1);
dfc2e12d
HV
461 *signal = (afc & 0x80) ? 65535 : 0;
462 return 0;
1da177e4
LT
463}
464
de48eebc
HH
465/*---------------------------------------------------------------------*/
466
4e9154b8 467static void tda8290_standby(struct dvb_frontend *fe)
793cf9e6 468{
4e9154b8
MK
469 struct tda8290_priv *priv = fe->analog_demod_priv;
470
8ff230fb
AB
471 static unsigned char cb1[] = { 0x30, 0xD0 };
472 static unsigned char tda8290_standby[] = { 0x00, 0x02 };
473 static unsigned char tda8290_agc_tri[] = { 0x02, 0x20 };
b2083199 474 struct i2c_msg msg = {.addr = priv->tda827x_addr, .flags=0, .buf=cb1, .len = 2};
de48eebc 475
2f719f7a
OZ
476 if (fe->ops.analog_ops.i2c_gate_ctrl)
477 fe->ops.analog_ops.i2c_gate_ctrl(fe, 1);
8c125f2c 478 if (priv->ver & TDA8275A)
de48eebc 479 cb1[1] = 0x90;
db8a6956 480 i2c_transfer(priv->i2c_props.adap, &msg, 1);
2f719f7a
OZ
481 if (fe->ops.analog_ops.i2c_gate_ctrl)
482 fe->ops.analog_ops.i2c_gate_ctrl(fe, 0);
db8a6956
MK
483 tuner_i2c_xfer_send(&priv->i2c_props, tda8290_agc_tri, 2);
484 tuner_i2c_xfer_send(&priv->i2c_props, tda8290_standby, 2);
793cf9e6
MCC
485}
486
4e9154b8 487static void tda8295_standby(struct dvb_frontend *fe)
5bea1cd3 488{
4e9154b8 489 tda8295_agc1_out(fe, 0); /* Put AGC in tri-state */
5bea1cd3 490
4e9154b8 491 tda8295_power(fe, 0);
5bea1cd3
MK
492}
493
4e9154b8 494static void tda8290_init_if(struct dvb_frontend *fe)
de48eebc 495{
4e9154b8 496 struct tda8290_priv *priv = fe->analog_demod_priv;
db8a6956 497
8ff230fb
AB
498 static unsigned char set_VS[] = { 0x30, 0x6F };
499 static unsigned char set_GP00_CF[] = { 0x20, 0x01 };
500 static unsigned char set_GP01_CF[] = { 0x20, 0x0B };
de48eebc 501
1bacb2df
OZ
502 if ((priv->cfg.config == TDA8290_LNA_GP0_HIGH_ON) ||
503 (priv->cfg.config == TDA8290_LNA_GP0_HIGH_OFF))
db8a6956 504 tuner_i2c_xfer_send(&priv->i2c_props, set_GP00_CF, 2);
58ef4f92 505 else
db8a6956
MK
506 tuner_i2c_xfer_send(&priv->i2c_props, set_GP01_CF, 2);
507 tuner_i2c_xfer_send(&priv->i2c_props, set_VS, 2);
de48eebc
HH
508}
509
4e9154b8 510static void tda8295_init_if(struct dvb_frontend *fe)
5bea1cd3 511{
4e9154b8 512 struct tda8290_priv *priv = fe->analog_demod_priv;
5bea1cd3
MK
513
514 static unsigned char set_adc_ctl[] = { 0x33, 0x14 };
515 static unsigned char set_adc_ctl2[] = { 0x34, 0x00 };
516 static unsigned char set_pll_reg6[] = { 0x3e, 0x63 };
517 static unsigned char set_pll_reg0[] = { 0x38, 0x23 };
518 static unsigned char set_pll_reg7[] = { 0x3f, 0x01 };
519 static unsigned char set_pll_reg10[] = { 0x42, 0x61 };
520 static unsigned char set_gpio_reg0[] = { 0x44, 0x0b };
521
4e9154b8 522 tda8295_power(fe, 1);
5bea1cd3 523
4e9154b8
MK
524 tda8295_set_easy_mode(fe, 0);
525 tda8295_set_video_std(fe);
5bea1cd3
MK
526
527 tuner_i2c_xfer_send(&priv->i2c_props, set_adc_ctl, 2);
528 tuner_i2c_xfer_send(&priv->i2c_props, set_adc_ctl2, 2);
529 tuner_i2c_xfer_send(&priv->i2c_props, set_pll_reg6, 2);
530 tuner_i2c_xfer_send(&priv->i2c_props, set_pll_reg0, 2);
531 tuner_i2c_xfer_send(&priv->i2c_props, set_pll_reg7, 2);
532 tuner_i2c_xfer_send(&priv->i2c_props, set_pll_reg10, 2);
533 tuner_i2c_xfer_send(&priv->i2c_props, set_gpio_reg0, 2);
534
4e9154b8
MK
535 tda8295_agc1_out(fe, 0);
536 tda8295_agc2_out(fe, 0);
5bea1cd3
MK
537}
538
4e9154b8 539static void tda8290_init_tuner(struct dvb_frontend *fe)
1da177e4 540{
4e9154b8 541 struct tda8290_priv *priv = fe->analog_demod_priv;
8ff230fb
AB
542 static unsigned char tda8275_init[] =
543 { 0x00, 0x00, 0x00, 0x40, 0xdC, 0x04, 0xAf,
544 0x3F, 0x2A, 0x04, 0xFF, 0x00, 0x00, 0x40 };
545 static unsigned char tda8275a_init[] =
546 { 0x00, 0x00, 0x00, 0x00, 0xdC, 0x05, 0x8b,
547 0x0c, 0x04, 0x20, 0xFF, 0x00, 0x00, 0x4b };
b2083199 548 struct i2c_msg msg = {.addr = priv->tda827x_addr, .flags=0,
c2f6f9d8 549 .buf=tda8275_init, .len = 14};
8c125f2c 550 if (priv->ver & TDA8275A)
de48eebc
HH
551 msg.buf = tda8275a_init;
552
2f719f7a
OZ
553 if (fe->ops.analog_ops.i2c_gate_ctrl)
554 fe->ops.analog_ops.i2c_gate_ctrl(fe, 1);
db8a6956 555 i2c_transfer(priv->i2c_props.adap, &msg, 1);
2f719f7a
OZ
556 if (fe->ops.analog_ops.i2c_gate_ctrl)
557 fe->ops.analog_ops.i2c_gate_ctrl(fe, 0);
de48eebc
HH
558}
559
560/*---------------------------------------------------------------------*/
1da177e4 561
4e9154b8 562static void tda829x_release(struct dvb_frontend *fe)
024cf530 563{
a4f263b5
MK
564 struct tda8290_priv *priv = fe->analog_demod_priv;
565
006ed1ec
MK
566 /* only try to release the tuner if we've
567 * attached it from within this module */
568 if (priv->ver & (TDA18271 | TDA8275 | TDA8275A))
a4f263b5
MK
569 if (fe->ops.tuner_ops.release)
570 fe->ops.tuner_ops.release(fe);
024cf530 571
4e9154b8
MK
572 kfree(fe->analog_demod_priv);
573 fe->analog_demod_priv = NULL;
910bb3e3
MK
574}
575
f21e0d7f
MK
576static struct tda18271_config tda829x_tda18271_config = {
577 .gate = TDA18271_GATE_ANALOG,
578};
579
8c125f2c 580static int tda829x_find_tuner(struct dvb_frontend *fe)
de48eebc 581{
8c125f2c 582 struct tda8290_priv *priv = fe->analog_demod_priv;
de48eebc
HH
583 int i, ret, tuners_found;
584 u32 tuner_addrs;
8c125f2c
MK
585 u8 data;
586 struct i2c_msg msg = { .flags = I2C_M_RD, .buf = &data, .len = 1 };
de48eebc 587
2f719f7a
OZ
588 if (fe->ops.analog_ops.i2c_gate_ctrl)
589 fe->ops.analog_ops.i2c_gate_ctrl(fe, 1);
db8a6956 590
de48eebc
HH
591 /* probe for tuner chip */
592 tuners_found = 0;
593 tuner_addrs = 0;
8c125f2c 594 for (i = 0x60; i <= 0x63; i++) {
de48eebc 595 msg.addr = i;
db8a6956 596 ret = i2c_transfer(priv->i2c_props.adap, &msg, 1);
de48eebc
HH
597 if (ret == 1) {
598 tuners_found++;
599 tuner_addrs = (tuner_addrs << 8) + i;
600 }
601 }
602 /* if there is more than one tuner, we expect the right one is
603 behind the bridge and we choose the highest address that doesn't
604 give a response now
605 */
8c125f2c 606
2f719f7a
OZ
607 if (fe->ops.analog_ops.i2c_gate_ctrl)
608 fe->ops.analog_ops.i2c_gate_ctrl(fe, 0);
8c125f2c
MK
609
610 if (tuners_found > 1)
de48eebc
HH
611 for (i = 0; i < tuners_found; i++) {
612 msg.addr = tuner_addrs & 0xff;
db8a6956 613 ret = i2c_transfer(priv->i2c_props.adap, &msg, 1);
8c125f2c 614 if (ret == 1)
de48eebc
HH
615 tuner_addrs = tuner_addrs >> 8;
616 else
617 break;
618 }
8c125f2c 619
de48eebc 620 if (tuner_addrs == 0) {
8c125f2c 621 tuner_addrs = 0x60;
8ca2e927
MCC
622 tuner_info("could not clearly identify tuner address, defaulting to %x\n",
623 tuner_addrs);
de48eebc
HH
624 } else {
625 tuner_addrs = tuner_addrs & 0xff;
910bb3e3 626 tuner_info("setting tuner address to %x\n", tuner_addrs);
de48eebc 627 }
b2083199 628 priv->tda827x_addr = tuner_addrs;
de48eebc
HH
629 msg.addr = tuner_addrs;
630
2f719f7a
OZ
631 if (fe->ops.analog_ops.i2c_gate_ctrl)
632 fe->ops.analog_ops.i2c_gate_ctrl(fe, 1);
db8a6956 633 ret = i2c_transfer(priv->i2c_props.adap, &msg, 1);
910bb3e3 634
8c125f2c
MK
635 if (ret != 1) {
636 tuner_warn("tuner access failed!\n");
2f719f7a
OZ
637 if (fe->ops.analog_ops.i2c_gate_ctrl)
638 fe->ops.analog_ops.i2c_gate_ctrl(fe, 0);
8c125f2c 639 return -EREMOTEIO;
de48eebc 640 }
746d9732 641
255b5113 642 if ((data == 0x83) || (data == 0x84)) {
8c125f2c 643 priv->ver |= TDA18271;
adcc4b3e 644 tda829x_tda18271_config.config = priv->cfg.config;
5b0e5350 645 tda829x_tda18271_config.std_map = priv->tda18271_std_map;
a07c8779
MK
646 dvb_attach(tda18271_attach, fe, priv->tda827x_addr,
647 priv->i2c_props.adap, &tda829x_tda18271_config);
8c125f2c
MK
648 } else {
649 if ((data & 0x3c) == 0)
650 priv->ver |= TDA8275;
651 else
652 priv->ver |= TDA8275A;
7fd8b263 653
a07c8779
MK
654 dvb_attach(tda827x_attach, fe, priv->tda827x_addr,
655 priv->i2c_props.adap, &priv->cfg);
7bff4b4d 656 priv->cfg.switch_addr = priv->i2c_props.addr;
8c125f2c 657 }
6881647c
MK
658 if (fe->ops.tuner_ops.init)
659 fe->ops.tuner_ops.init(fe);
63c25480 660
6881647c
MK
661 if (fe->ops.tuner_ops.sleep)
662 fe->ops.tuner_ops.sleep(fe);
63c25480 663
2f719f7a
OZ
664 if (fe->ops.analog_ops.i2c_gate_ctrl)
665 fe->ops.analog_ops.i2c_gate_ctrl(fe, 0);
746d9732 666
746d9732 667 return 0;
1da177e4 668}
5bea1cd3 669
f1f32849
MK
670static int tda8290_probe(struct tuner_i2c_props *i2c_props)
671{
672#define TDA8290_ID 0x89
567aba0b
MCC
673 u8 reg = 0x1f, id;
674 struct i2c_msg msg_read[] = {
89a8969a
JW
675 { .addr = i2c_props->addr, .flags = 0, .len = 1, .buf = &reg },
676 { .addr = i2c_props->addr, .flags = I2C_M_RD, .len = 1, .buf = &id },
567aba0b 677 };
f1f32849
MK
678
679 /* detect tda8290 */
567aba0b 680 if (i2c_transfer(i2c_props->adap, msg_read, 2) != 2) {
89a8969a 681 printk(KERN_WARNING "%s: couldn't read register 0x%02x\n",
567aba0b
MCC
682 __func__, reg);
683 return -ENODEV;
684 }
f1f32849 685
567aba0b 686 if (id == TDA8290_ID) {
ab166050 687 if (debug)
f1f32849 688 printk(KERN_DEBUG "%s: tda8290 detected @ %d-%04x\n",
7e28adb2 689 __func__, i2c_adapter_id(i2c_props->adap),
f1f32849
MK
690 i2c_props->addr);
691 return 0;
692 }
1f3a4e32 693 return -ENODEV;
f1f32849
MK
694}
695
696static int tda8295_probe(struct tuner_i2c_props *i2c_props)
697{
698#define TDA8295_ID 0x8a
19f8a6c3 699#define TDA8295C2_ID 0x8b
567aba0b
MCC
700 u8 reg = 0x2f, id;
701 struct i2c_msg msg_read[] = {
89a8969a
JW
702 { .addr = i2c_props->addr, .flags = 0, .len = 1, .buf = &reg },
703 { .addr = i2c_props->addr, .flags = I2C_M_RD, .len = 1, .buf = &id },
567aba0b 704 };
f1f32849 705
89a8969a 706 /* detect tda8295 */
567aba0b 707 if (i2c_transfer(i2c_props->adap, msg_read, 2) != 2) {
89a8969a 708 printk(KERN_WARNING "%s: couldn't read register 0x%02x\n",
567aba0b
MCC
709 __func__, reg);
710 return -ENODEV;
711 }
f1f32849 712
567aba0b 713 if ((id & 0xfe) == TDA8295_ID) {
ab166050 714 if (debug)
19f8a6c3 715 printk(KERN_DEBUG "%s: %s detected @ %d-%04x\n",
567aba0b 716 __func__, (id == TDA8295_ID) ?
19f8a6c3
MK
717 "tda8295c1" : "tda8295c2",
718 i2c_adapter_id(i2c_props->adap),
f1f32849
MK
719 i2c_props->addr);
720 return 0;
721 }
722
1f3a4e32 723 return -ENODEV;
f1f32849
MK
724}
725
bd336e63 726static const struct analog_demod_ops tda8290_ops = {
c7919d52 727 .set_params = tda8290_set_params,
8c125f2c
MK
728 .has_signal = tda8290_has_signal,
729 .standby = tda8290_standby,
730 .release = tda829x_release,
731 .i2c_gate_ctrl = tda8290_i2c_bridge,
732};
733
bd336e63 734static const struct analog_demod_ops tda8295_ops = {
c7919d52 735 .set_params = tda8295_set_params,
8c125f2c
MK
736 .has_signal = tda8295_has_signal,
737 .standby = tda8295_standby,
738 .release = tda829x_release,
739 .i2c_gate_ctrl = tda8295_i2c_bridge,
740};
741
ab166050
MK
742struct dvb_frontend *tda829x_attach(struct dvb_frontend *fe,
743 struct i2c_adapter *i2c_adap, u8 i2c_addr,
744 struct tda829x_config *cfg)
5bea1cd3
MK
745{
746 struct tda8290_priv *priv = NULL;
ab166050 747 char *name;
8c125f2c 748
5bea1cd3
MK
749 priv = kzalloc(sizeof(struct tda8290_priv), GFP_KERNEL);
750 if (priv == NULL)
ab166050 751 return NULL;
8c125f2c 752 fe->analog_demod_priv = priv;
5bea1cd3 753
ab166050
MK
754 priv->i2c_props.addr = i2c_addr;
755 priv->i2c_props.adap = i2c_adap;
2756665c 756 priv->i2c_props.name = "tda829x";
5b0e5350
OZ
757 if (cfg) {
758 priv->cfg.config = cfg->lna_cfg;
759 priv->tda18271_std_map = cfg->tda18271_std_map;
760 }
5bea1cd3 761
f1f32849 762 if (tda8290_probe(&priv->i2c_props) == 0) {
8c125f2c 763 priv->ver = TDA8290;
bc3e5c7f
MK
764 memcpy(&fe->ops.analog_ops, &tda8290_ops,
765 sizeof(struct analog_demod_ops));
5bea1cd3 766 }
8c125f2c 767
f1f32849 768 if (tda8295_probe(&priv->i2c_props) == 0) {
8c125f2c 769 priv->ver = TDA8295;
bc3e5c7f
MK
770 memcpy(&fe->ops.analog_ops, &tda8295_ops,
771 sizeof(struct analog_demod_ops));
5bea1cd3 772 }
5bea1cd3 773
2f719f7a
OZ
774 if (cfg && cfg->no_i2c_gate)
775 fe->ops.analog_ops.i2c_gate_ctrl = NULL;
776
9d700a06
MCC
777 if (!(cfg) || (TDA829X_PROBE_TUNER == cfg->probe_tuner)) {
778 tda8295_power(fe, 1);
779 if (tda829x_find_tuner(fe) < 0)
780 goto fail;
47ab285a 781 }
5bea1cd3 782
ab166050 783 switch (priv->ver) {
c9076279
MK
784 case TDA8290:
785 name = "tda8290";
786 break;
787 case TDA8295:
788 name = "tda8295";
789 break;
ab166050
MK
790 case TDA8290 | TDA8275:
791 name = "tda8290+75";
792 break;
793 case TDA8295 | TDA8275:
794 name = "tda8295+75";
795 break;
796 case TDA8290 | TDA8275A:
797 name = "tda8290+75a";
798 break;
799 case TDA8295 | TDA8275A:
800 name = "tda8295+75a";
801 break;
802 case TDA8290 | TDA18271:
803 name = "tda8290+18271";
804 break;
805 case TDA8295 | TDA18271:
806 name = "tda8295+18271";
807 break;
808 default:
809 goto fail;
810 }
811 tuner_info("type set to %s\n", name);
812
0f2ce983
MK
813 fe->ops.analog_ops.info.name = name;
814
8c125f2c 815 if (priv->ver & TDA8290) {
439b72b6
MK
816 if (priv->ver & (TDA8275 | TDA8275A))
817 tda8290_init_tuner(fe);
8c125f2c
MK
818 tda8290_init_if(fe);
819 } else if (priv->ver & TDA8295)
820 tda8295_init_if(fe);
5bea1cd3 821
ab166050 822 return fe;
fa746aee
MK
823
824fail:
9d700a06
MCC
825 memset(&fe->ops.analog_ops, 0, sizeof(struct analog_demod_ops));
826
fa746aee 827 tda829x_release(fe);
ab166050 828 return NULL;
5bea1cd3 829}
8c125f2c 830EXPORT_SYMBOL_GPL(tda829x_attach);
1da177e4 831
ab166050 832int tda829x_probe(struct i2c_adapter *i2c_adap, u8 i2c_addr)
95736034 833{
910bb3e3 834 struct tuner_i2c_props i2c_props = {
ab166050
MK
835 .adap = i2c_adap,
836 .addr = i2c_addr,
910bb3e3 837 };
db8a6956 838
8ff230fb
AB
839 static unsigned char soft_reset[] = { 0x00, 0x00 };
840 static unsigned char easy_mode_b[] = { 0x01, 0x02 };
841 static unsigned char easy_mode_g[] = { 0x01, 0x04 };
842 static unsigned char restore_9886[] = { 0x00, 0xd6, 0x30 };
843 static unsigned char addr_dto_lsb = 0x07;
95736034 844 unsigned char data;
a818e1c8
MK
845#define PROBE_BUFFER_SIZE 8
846 unsigned char buf[PROBE_BUFFER_SIZE];
847 int i;
848
849 /* rule out tda9887, which would return the same byte repeatedly */
567aba0b
MCC
850 tuner_i2c_xfer_send_recv(&i2c_props,
851 soft_reset, 1, buf, PROBE_BUFFER_SIZE);
a818e1c8 852 for (i = 1; i < PROBE_BUFFER_SIZE; i++) {
bbe1e0ba
MK
853 if (buf[i] != buf[0])
854 break;
a818e1c8
MK
855 }
856
857 /* all bytes are equal, not a tda829x - probably a tda9887 */
858 if (i == PROBE_BUFFER_SIZE)
859 return -ENODEV;
95736034 860
f1f32849
MK
861 if ((tda8290_probe(&i2c_props) == 0) ||
862 (tda8295_probe(&i2c_props) == 0))
863 return 0;
864
865 /* fall back to old probing method */
910bb3e3
MK
866 tuner_i2c_xfer_send(&i2c_props, easy_mode_b, 2);
867 tuner_i2c_xfer_send(&i2c_props, soft_reset, 2);
567aba0b 868 tuner_i2c_xfer_send_recv(&i2c_props, &addr_dto_lsb, 1, &data, 1);
95736034 869 if (data == 0) {
910bb3e3
MK
870 tuner_i2c_xfer_send(&i2c_props, easy_mode_g, 2);
871 tuner_i2c_xfer_send(&i2c_props, soft_reset, 2);
567aba0b
MCC
872 tuner_i2c_xfer_send_recv(&i2c_props,
873 &addr_dto_lsb, 1, &data, 1);
95736034
HH
874 if (data == 0x7b) {
875 return 0;
876 }
877 }
910bb3e3 878 tuner_i2c_xfer_send(&i2c_props, restore_9886, 3);
1f3a4e32 879 return -ENODEV;
95736034 880}
f1f32849 881EXPORT_SYMBOL_GPL(tda829x_probe);
910bb3e3 882
8c125f2c 883MODULE_DESCRIPTION("Philips/NXP TDA8290/TDA8295 analog IF demodulator driver");
5bea1cd3 884MODULE_AUTHOR("Gerd Knorr, Hartmut Hackmann, Michael Krufky");
910bb3e3 885MODULE_LICENSE("GPL");