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