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