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