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