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