Merge branch 'work.regset' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs
[linux/fpc-iii.git] / drivers / media / tuners / tda8290.c
blob98851482c0ccdf17c14285598fb2ecd61af8b57c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
4 i2c tv tuner chip device driver
5 controls the philips tda8290+75 tuner chip combo.
8 This "tda8290" module was split apart from the original "tuner" module.
9 */
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"
20 static int debug;
21 module_param(debug, int, 0644);
22 MODULE_PARM_DESC(debug, "enable verbose debug messages");
24 static int deemphasis_50;
25 module_param(deemphasis_50, int, 0644);
26 MODULE_PARM_DESC(deemphasis_50, "0 - 75us deemphasis; 1 - 50us deemphasis");
28 /* ---------------------------------------------------------------------- */
30 struct tda8290_priv {
31 struct tuner_i2c_props i2c_props;
33 unsigned char tda8290_easy_mode;
35 unsigned char tda827x_addr;
37 unsigned char ver;
38 #define TDA8290 1
39 #define TDA8295 2
40 #define TDA8275 4
41 #define TDA8275A 8
42 #define TDA18271 16
44 struct tda827x_config cfg;
45 struct tda18271_std_map *tda18271_std_map;
48 /*---------------------------------------------------------------------*/
50 static int tda8290_i2c_bridge(struct dvb_frontend *fe, int close)
52 struct tda8290_priv *priv = fe->analog_demod_priv;
54 static unsigned char enable[2] = { 0x21, 0xC0 };
55 static unsigned char disable[2] = { 0x21, 0x00 };
56 unsigned char *msg;
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);
68 return 0;
71 static int tda8295_i2c_bridge(struct dvb_frontend *fe, int close)
73 struct tda8290_priv *priv = fe->analog_demod_priv;
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;
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);
89 buf[2] = msg[1];
90 buf[2] &= ~0x04;
91 tuner_i2c_xfer_send(&priv->i2c_props, buf, 3);
92 msleep(5);
94 msg[1] |= 0x04;
95 tuner_i2c_xfer_send(&priv->i2c_props, msg, 2);
98 return 0;
101 /*---------------------------------------------------------------------*/
103 static void set_audio(struct dvb_frontend *fe,
104 struct analog_parameters *params)
106 struct tda8290_priv *priv = fe->analog_demod_priv;
107 char* mode;
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";
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);
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 */
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} },
160 { { 0x0d, 0x00} }, /* PLL off, no video carrier detect */
161 { { 0x14, 0x00} }, /* disable auto mute if no video */
164 static void tda8290_set_params(struct dvb_frontend *fe,
165 struct analog_parameters *params)
167 struct tda8290_priv *priv = fe->analog_demod_priv;
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;
191 set_audio(fe, params);
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);
200 if (params->mode == V4L2_TUNER_RADIO) {
201 unsigned char deemphasis[] = { 0x13, 1 };
203 /* FIXME: allow using a different deemphasis */
205 if (deemphasis_50)
206 deemphasis[1] = 2;
208 for (i = 0; i < ARRAY_SIZE(fm_mode); i++)
209 tuner_i2c_xfer_send(&priv->i2c_props, fm_mode[i].seq, 2);
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);
225 if (fe->ops.analog_ops.i2c_gate_ctrl)
226 fe->ops.analog_ops.i2c_gate_ctrl(fe, 1);
228 if (fe->ops.tuner_ops.set_analog_params)
229 fe->ops.tuner_ops.set_analog_params(fe, params);
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);
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);
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);
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);
300 /*---------------------------------------------------------------------*/
302 static void tda8295_power(struct dvb_frontend *fe, int enable)
304 struct tda8290_priv *priv = fe->analog_demod_priv;
305 unsigned char buf[] = { 0x30, 0x00 }; /* clb_stdbt */
307 tuner_i2c_xfer_send_recv(&priv->i2c_props, &buf[0], 1, &buf[1], 1);
309 if (enable)
310 buf[1] = 0x01;
311 else
312 buf[1] = 0x03;
314 tuner_i2c_xfer_send(&priv->i2c_props, buf, 2);
317 static void tda8295_set_easy_mode(struct dvb_frontend *fe, int enable)
319 struct tda8290_priv *priv = fe->analog_demod_priv;
320 unsigned char buf[] = { 0x01, 0x00 };
322 tuner_i2c_xfer_send_recv(&priv->i2c_props, &buf[0], 1, &buf[1], 1);
324 if (enable)
325 buf[1] = 0x01; /* rising edge sets regs 0x02 - 0x23 */
326 else
327 buf[1] = 0x00; /* reset active bit */
329 tuner_i2c_xfer_send(&priv->i2c_props, buf, 2);
332 static void tda8295_set_video_std(struct dvb_frontend *fe)
334 struct tda8290_priv *priv = fe->analog_demod_priv;
335 unsigned char buf[] = { 0x00, priv->tda8290_easy_mode };
337 tuner_i2c_xfer_send(&priv->i2c_props, buf, 2);
339 tda8295_set_easy_mode(fe, 1);
340 msleep(20);
341 tda8295_set_easy_mode(fe, 0);
344 /*---------------------------------------------------------------------*/
346 static void tda8295_agc1_out(struct dvb_frontend *fe, int enable)
348 struct tda8290_priv *priv = fe->analog_demod_priv;
349 unsigned char buf[] = { 0x02, 0x00 }; /* DIV_FUNC */
351 tuner_i2c_xfer_send_recv(&priv->i2c_props, &buf[0], 1, &buf[1], 1);
353 if (enable)
354 buf[1] &= ~0x40;
355 else
356 buf[1] |= 0x40;
358 tuner_i2c_xfer_send(&priv->i2c_props, buf, 2);
361 static void tda8295_agc2_out(struct dvb_frontend *fe, int enable)
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 };
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);
372 set_gpio_cf[1] &= 0xf0; /* clear GPIO_0 bits 3-0 */
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 */
378 tuner_i2c_xfer_send(&priv->i2c_props, set_gpio_cf, 2);
379 tuner_i2c_xfer_send(&priv->i2c_props, set_gpio_val, 2);
382 static int tda8295_has_signal(struct dvb_frontend *fe, u16 *signal)
384 struct tda8290_priv *priv = fe->analog_demod_priv;
386 unsigned char hvpll_stat = 0x26;
387 unsigned char ret;
389 tuner_i2c_xfer_send_recv(&priv->i2c_props, &hvpll_stat, 1, &ret, 1);
390 *signal = (ret & 0x01) ? 65535 : 0;
391 return 0;
394 /*---------------------------------------------------------------------*/
396 static void tda8295_set_params(struct dvb_frontend *fe,
397 struct analog_parameters *params)
399 struct tda8290_priv *priv = fe->analog_demod_priv;
400 u16 signal = 0;
401 unsigned char blanking_mode[] = { 0x1d, 0x00 };
403 set_audio(fe, params);
405 tuner_dbg("%s: freq = %d\n", __func__, params->frequency);
407 tda8295_power(fe, 1);
408 tda8295_agc1_out(fe, 1);
410 tuner_i2c_xfer_send_recv(&priv->i2c_props,
411 &blanking_mode[0], 1, &blanking_mode[1], 1);
413 tda8295_set_video_std(fe);
415 blanking_mode[1] = 0x03;
416 tuner_i2c_xfer_send(&priv->i2c_props, blanking_mode, 2);
417 msleep(20);
419 if (fe->ops.analog_ops.i2c_gate_ctrl)
420 fe->ops.analog_ops.i2c_gate_ctrl(fe, 1);
422 if (fe->ops.tuner_ops.set_analog_params)
423 fe->ops.tuner_ops.set_analog_params(fe, params);
425 if (priv->cfg.agcf)
426 priv->cfg.agcf(fe);
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");
434 if (fe->ops.analog_ops.i2c_gate_ctrl)
435 fe->ops.analog_ops.i2c_gate_ctrl(fe, 0);
438 /*---------------------------------------------------------------------*/
440 static int tda8290_has_signal(struct dvb_frontend *fe, u16 *signal)
442 struct tda8290_priv *priv = fe->analog_demod_priv;
444 unsigned char i2c_get_afc[1] = { 0x1B };
445 unsigned char afc = 0;
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;
453 /*---------------------------------------------------------------------*/
455 static void tda8290_standby(struct dvb_frontend *fe)
457 struct tda8290_priv *priv = fe->analog_demod_priv;
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};
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);
475 static void tda8295_standby(struct dvb_frontend *fe)
477 tda8295_agc1_out(fe, 0); /* Put AGC in tri-state */
479 tda8295_power(fe, 0);
482 static void tda8290_init_if(struct dvb_frontend *fe)
484 struct tda8290_priv *priv = fe->analog_demod_priv;
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 };
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);
498 static void tda8295_init_if(struct dvb_frontend *fe)
500 struct tda8290_priv *priv = fe->analog_demod_priv;
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 };
510 tda8295_power(fe, 1);
512 tda8295_set_easy_mode(fe, 0);
513 tda8295_set_video_std(fe);
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);
523 tda8295_agc1_out(fe, 0);
524 tda8295_agc2_out(fe, 0);
527 static void tda8290_init_tuner(struct dvb_frontend *fe)
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;
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);
548 /*---------------------------------------------------------------------*/
550 static void tda829x_release(struct dvb_frontend *fe)
552 struct tda8290_priv *priv = fe->analog_demod_priv;
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);
560 kfree(fe->analog_demod_priv);
561 fe->analog_demod_priv = NULL;
564 static struct tda18271_config tda829x_tda18271_config = {
565 .gate = TDA18271_GATE_ANALOG,
568 static int tda829x_find_tuner(struct dvb_frontend *fe)
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 };
576 if (fe->ops.analog_ops.i2c_gate_ctrl)
577 fe->ops.analog_ops.i2c_gate_ctrl(fe, 1);
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;
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
595 if (fe->ops.analog_ops.i2c_gate_ctrl)
596 fe->ops.analog_ops.i2c_gate_ctrl(fe, 0);
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;
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);
616 priv->tda827x_addr = tuner_addrs;
617 msg.addr = tuner_addrs;
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);
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;
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;
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;
646 if (fe->ops.tuner_ops.init)
647 fe->ops.tuner_ops.init(fe);
649 if (fe->ops.tuner_ops.sleep)
650 fe->ops.tuner_ops.sleep(fe);
652 if (fe->ops.analog_ops.i2c_gate_ctrl)
653 fe->ops.analog_ops.i2c_gate_ctrl(fe, 0);
655 return 0;
658 static int tda8290_probe(struct tuner_i2c_props *i2c_props)
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 },
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;
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;
681 return -ENODEV;
684 static int tda8295_probe(struct tuner_i2c_props *i2c_props)
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 },
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;
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;
711 return -ENODEV;
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,
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,
730 struct dvb_frontend *tda829x_attach(struct dvb_frontend *fe,
731 struct i2c_adapter *i2c_adap, u8 i2c_addr,
732 struct tda829x_config *cfg)
734 struct tda8290_priv *priv = NULL;
735 char *name;
737 priv = kzalloc(sizeof(struct tda8290_priv), GFP_KERNEL);
738 if (priv == NULL)
739 return NULL;
740 fe->analog_demod_priv = priv;
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;
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));
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));
762 if (cfg && cfg->no_i2c_gate)
763 fe->ops.analog_ops.i2c_gate_ctrl = NULL;
765 if (!(cfg) || (TDA829X_PROBE_TUNER == cfg->probe_tuner)) {
766 tda8295_power(fe, 1);
767 if (tda829x_find_tuner(fe) < 0)
768 goto fail;
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;
799 tuner_info("type set to %s\n", name);
801 fe->ops.analog_ops.info.name = name;
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);
810 return fe;
812 fail:
813 memset(&fe->ops.analog_ops, 0, sizeof(struct analog_demod_ops));
815 tda829x_release(fe);
816 return NULL;
818 EXPORT_SYMBOL_GPL(tda829x_attach);
820 int tda829x_probe(struct i2c_adapter *i2c_adap, u8 i2c_addr)
822 struct tuner_i2c_props i2c_props = {
823 .adap = i2c_adap,
824 .addr = i2c_addr,
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;
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;
845 /* all bytes are equal, not a tda829x - probably a tda9887 */
846 if (i == PROBE_BUFFER_SIZE)
847 return -ENODEV;
849 if ((tda8290_probe(&i2c_props) == 0) ||
850 (tda8295_probe(&i2c_props) == 0))
851 return 0;
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;
866 tuner_i2c_xfer_send(&i2c_props, restore_9886, 3);
867 return -ENODEV;
869 EXPORT_SYMBOL_GPL(tda829x_probe);
871 MODULE_DESCRIPTION("Philips/NXP TDA8290/TDA8295 analog IF demodulator driver");
872 MODULE_AUTHOR("Gerd Knorr, Hartmut Hackmann, Michael Krufky");
873 MODULE_LICENSE("GPL");