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