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