V4L/DVB (6435): tda8290: add support for NXP TDA18271 tuner and TDA8295 analog demod
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / media / video / tda8290.c
1 /*
2
3 i2c tv tuner chip device driver
4 controls the philips tda8290+75 tuner chip combo.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
20 This "tda8290" module was split apart from the original "tuner" module.
21 */
22
23 #include <linux/i2c.h>
24 #include <linux/delay.h>
25 #include <linux/videodev.h>
26 #include "tda8290.h"
27 #include "tda827x.h"
28 #include "tda18271.h"
29
30 static int tuner_debug;
31 module_param_named(debug, tuner_debug, int, 0644);
32 MODULE_PARM_DESC(debug, "enable verbose debug messages");
33
34 #define PREFIX "tda8290 "
35
36 /* ---------------------------------------------------------------------- */
37
38 struct tda8290_priv {
39 struct tuner_i2c_props i2c_props;
40
41 unsigned char tda8290_easy_mode;
42
43 unsigned char tda827x_addr;
44 unsigned char tda827x_ver;
45
46 struct tda827x_config cfg;
47 };
48
49 /*---------------------------------------------------------------------*/
50
51 static void tda8290_i2c_bridge(struct tuner *t, int close)
52 {
53 struct tda8290_priv *priv = t->priv;
54
55 unsigned char enable[2] = { 0x21, 0xC0 };
56 unsigned char disable[2] = { 0x21, 0x00 };
57 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);
66 }
67 }
68
69 static void tda8295_i2c_bridge(struct tuner *t, int close)
70 {
71 struct tda8290_priv *priv = t->priv;
72
73 unsigned char enable[2] = { 0x45, 0xc1 };
74 unsigned char disable[2] = { 0x46, 0x00 };
75 unsigned char buf[3] = { 0x45, 0x01, 0x00 };
76 unsigned char *msg;
77 if (close) {
78 msg = enable;
79 tuner_i2c_xfer_send(&priv->i2c_props, msg, 2);
80 /* let the bridge stabilize */
81 msleep(20);
82 } else {
83 msg = disable;
84 tuner_i2c_xfer_send(&priv->i2c_props, msg, 1);
85 tuner_i2c_xfer_recv(&priv->i2c_props, &msg[1], 1);
86
87 buf[2] = msg[1];
88 buf[2] &= ~0x04;
89 tuner_i2c_xfer_send(&priv->i2c_props, buf, 3);
90 msleep(5);
91
92 msg[1] |= 0x04;
93 tuner_i2c_xfer_send(&priv->i2c_props, msg, 2);
94 }
95 }
96
97 /*---------------------------------------------------------------------*/
98
99 static void set_audio(struct tuner *t)
100 {
101 struct tda8290_priv *priv = t->priv;
102 char* mode;
103
104 priv->cfg.tda827x_lpsel = 0;
105 if (t->std & V4L2_STD_MN) {
106 priv->cfg.sgIF = 92;
107 priv->tda8290_easy_mode = 0x01;
108 priv->cfg.tda827x_lpsel = 1;
109 mode = "MN";
110 } else if (t->std & V4L2_STD_B) {
111 priv->cfg.sgIF = 108;
112 priv->tda8290_easy_mode = 0x02;
113 mode = "B";
114 } else if (t->std & V4L2_STD_GH) {
115 priv->cfg.sgIF = 124;
116 priv->tda8290_easy_mode = 0x04;
117 mode = "GH";
118 } else if (t->std & V4L2_STD_PAL_I) {
119 priv->cfg.sgIF = 124;
120 priv->tda8290_easy_mode = 0x08;
121 mode = "I";
122 } else if (t->std & V4L2_STD_DK) {
123 priv->cfg.sgIF = 124;
124 priv->tda8290_easy_mode = 0x10;
125 mode = "DK";
126 } else if (t->std & V4L2_STD_SECAM_L) {
127 priv->cfg.sgIF = 124;
128 priv->tda8290_easy_mode = 0x20;
129 mode = "L";
130 } else if (t->std & V4L2_STD_SECAM_LC) {
131 priv->cfg.sgIF = 20;
132 priv->tda8290_easy_mode = 0x40;
133 mode = "LC";
134 } else {
135 priv->cfg.sgIF = 124;
136 priv->tda8290_easy_mode = 0x10;
137 mode = "xx";
138 }
139
140 if (t->mode == V4L2_TUNER_RADIO)
141 priv->cfg.sgIF = 88; /* if frequency is 5.5 MHz */
142
143 tuner_dbg("setting tda8290 to system %s\n", mode);
144 }
145
146 static void tda8290_set_freq(struct tuner *t, unsigned int freq)
147 {
148 struct tda8290_priv *priv = t->priv;
149 unsigned char soft_reset[] = { 0x00, 0x00 };
150 unsigned char easy_mode[] = { 0x01, priv->tda8290_easy_mode };
151 unsigned char expert_mode[] = { 0x01, 0x80 };
152 unsigned char agc_out_on[] = { 0x02, 0x00 };
153 unsigned char gainset_off[] = { 0x28, 0x14 };
154 unsigned char if_agc_spd[] = { 0x0f, 0x88 };
155 unsigned char adc_head_6[] = { 0x05, 0x04 };
156 unsigned char adc_head_9[] = { 0x05, 0x02 };
157 unsigned char adc_head_12[] = { 0x05, 0x01 };
158 unsigned char pll_bw_nom[] = { 0x0d, 0x47 };
159 unsigned char pll_bw_low[] = { 0x0d, 0x27 };
160 unsigned char gainset_2[] = { 0x28, 0x64 };
161 unsigned char agc_rst_on[] = { 0x0e, 0x0b };
162 unsigned char agc_rst_off[] = { 0x0e, 0x09 };
163 unsigned char if_agc_set[] = { 0x0f, 0x81 };
164 unsigned char addr_adc_sat = 0x1a;
165 unsigned char addr_agc_stat = 0x1d;
166 unsigned char addr_pll_stat = 0x1b;
167 unsigned char adc_sat, agc_stat,
168 pll_stat;
169 int i;
170
171 struct analog_parameters params = {
172 .frequency = freq,
173 .mode = t->mode,
174 .audmode = t->audmode,
175 .std = t->std
176 };
177
178 set_audio(t);
179
180 tuner_dbg("tda827xa config is 0x%02x\n", t->config);
181 tuner_i2c_xfer_send(&priv->i2c_props, easy_mode, 2);
182 tuner_i2c_xfer_send(&priv->i2c_props, agc_out_on, 2);
183 tuner_i2c_xfer_send(&priv->i2c_props, soft_reset, 2);
184 msleep(1);
185
186 expert_mode[1] = priv->tda8290_easy_mode + 0x80;
187 tuner_i2c_xfer_send(&priv->i2c_props, expert_mode, 2);
188 tuner_i2c_xfer_send(&priv->i2c_props, gainset_off, 2);
189 tuner_i2c_xfer_send(&priv->i2c_props, if_agc_spd, 2);
190 if (priv->tda8290_easy_mode & 0x60)
191 tuner_i2c_xfer_send(&priv->i2c_props, adc_head_9, 2);
192 else
193 tuner_i2c_xfer_send(&priv->i2c_props, adc_head_6, 2);
194 tuner_i2c_xfer_send(&priv->i2c_props, pll_bw_nom, 2);
195
196 tda8290_i2c_bridge(t, 1);
197
198 if (t->fe.ops.tuner_ops.set_analog_params)
199 t->fe.ops.tuner_ops.set_analog_params(&t->fe, &params);
200
201 for (i = 0; i < 3; i++) {
202 tuner_i2c_xfer_send(&priv->i2c_props, &addr_pll_stat, 1);
203 tuner_i2c_xfer_recv(&priv->i2c_props, &pll_stat, 1);
204 if (pll_stat & 0x80) {
205 tuner_i2c_xfer_send(&priv->i2c_props, &addr_adc_sat, 1);
206 tuner_i2c_xfer_recv(&priv->i2c_props, &adc_sat, 1);
207 tuner_i2c_xfer_send(&priv->i2c_props, &addr_agc_stat, 1);
208 tuner_i2c_xfer_recv(&priv->i2c_props, &agc_stat, 1);
209 tuner_dbg("tda8290 is locked, AGC: %d\n", agc_stat);
210 break;
211 } else {
212 tuner_dbg("tda8290 not locked, no signal?\n");
213 msleep(100);
214 }
215 }
216 /* adjust headroom resp. gain */
217 if ((agc_stat > 115) || (!(pll_stat & 0x80) && (adc_sat < 20))) {
218 tuner_dbg("adjust gain, step 1. Agc: %d, ADC stat: %d, lock: %d\n",
219 agc_stat, adc_sat, pll_stat & 0x80);
220 tuner_i2c_xfer_send(&priv->i2c_props, gainset_2, 2);
221 msleep(100);
222 tuner_i2c_xfer_send(&priv->i2c_props, &addr_agc_stat, 1);
223 tuner_i2c_xfer_recv(&priv->i2c_props, &agc_stat, 1);
224 tuner_i2c_xfer_send(&priv->i2c_props, &addr_pll_stat, 1);
225 tuner_i2c_xfer_recv(&priv->i2c_props, &pll_stat, 1);
226 if ((agc_stat > 115) || !(pll_stat & 0x80)) {
227 tuner_dbg("adjust gain, step 2. Agc: %d, lock: %d\n",
228 agc_stat, pll_stat & 0x80);
229 if (priv->cfg.agcf)
230 priv->cfg.agcf(&t->fe);
231 msleep(100);
232 tuner_i2c_xfer_send(&priv->i2c_props, &addr_agc_stat, 1);
233 tuner_i2c_xfer_recv(&priv->i2c_props, &agc_stat, 1);
234 tuner_i2c_xfer_send(&priv->i2c_props, &addr_pll_stat, 1);
235 tuner_i2c_xfer_recv(&priv->i2c_props, &pll_stat, 1);
236 if((agc_stat > 115) || !(pll_stat & 0x80)) {
237 tuner_dbg("adjust gain, step 3. Agc: %d\n", agc_stat);
238 tuner_i2c_xfer_send(&priv->i2c_props, adc_head_12, 2);
239 tuner_i2c_xfer_send(&priv->i2c_props, pll_bw_low, 2);
240 msleep(100);
241 }
242 }
243 }
244
245 /* l/ l' deadlock? */
246 if(priv->tda8290_easy_mode & 0x60) {
247 tuner_i2c_xfer_send(&priv->i2c_props, &addr_adc_sat, 1);
248 tuner_i2c_xfer_recv(&priv->i2c_props, &adc_sat, 1);
249 tuner_i2c_xfer_send(&priv->i2c_props, &addr_pll_stat, 1);
250 tuner_i2c_xfer_recv(&priv->i2c_props, &pll_stat, 1);
251 if ((adc_sat > 20) || !(pll_stat & 0x80)) {
252 tuner_dbg("trying to resolve SECAM L deadlock\n");
253 tuner_i2c_xfer_send(&priv->i2c_props, agc_rst_on, 2);
254 msleep(40);
255 tuner_i2c_xfer_send(&priv->i2c_props, agc_rst_off, 2);
256 }
257 }
258
259 tda8290_i2c_bridge(t, 0);
260 tuner_i2c_xfer_send(&priv->i2c_props, if_agc_set, 2);
261 }
262
263 /*---------------------------------------------------------------------*/
264
265 static void tda8295_power(struct tuner *t, int enable)
266 {
267 struct tda8290_priv *priv = t->priv;
268 unsigned char buf[] = { 0x30, 0x00 }; /* clb_stdbt */
269
270 tuner_i2c_xfer_send(&priv->i2c_props, &buf[0], 1);
271 tuner_i2c_xfer_recv(&priv->i2c_props, &buf[1], 1);
272
273 if (enable)
274 buf[1] = 0x01;
275 else
276 buf[1] = 0x03;
277
278 tuner_i2c_xfer_send(&priv->i2c_props, buf, 2);
279 }
280
281 static void tda8295_set_easy_mode(struct tuner *t, int enable)
282 {
283 struct tda8290_priv *priv = t->priv;
284 unsigned char buf[] = { 0x01, 0x00 };
285
286 tuner_i2c_xfer_send(&priv->i2c_props, &buf[0], 1);
287 tuner_i2c_xfer_recv(&priv->i2c_props, &buf[1], 1);
288
289 if (enable)
290 buf[1] = 0x01; /* rising edge sets regs 0x02 - 0x23 */
291 else
292 buf[1] = 0x00; /* reset active bit */
293
294 tuner_i2c_xfer_send(&priv->i2c_props, buf, 2);
295 }
296
297 static void tda8295_set_video_std(struct tuner *t)
298 {
299 struct tda8290_priv *priv = t->priv;
300 unsigned char buf[] = { 0x00, priv->tda8290_easy_mode };
301
302 tuner_i2c_xfer_send(&priv->i2c_props, buf, 2);
303
304 tda8295_set_easy_mode(t, 1);
305 msleep(20);
306 tda8295_set_easy_mode(t, 0);
307 }
308
309 /*---------------------------------------------------------------------*/
310
311 static void tda8295_agc1_out(struct tuner *t, int enable)
312 {
313 struct tda8290_priv *priv = t->priv;
314 unsigned char buf[] = { 0x02, 0x00 }; /* DIV_FUNC */
315
316 tuner_i2c_xfer_send(&priv->i2c_props, &buf[0], 1);
317 tuner_i2c_xfer_recv(&priv->i2c_props, &buf[1], 1);
318
319 if (enable)
320 buf[1] &= ~0x40;
321 else
322 buf[1] |= 0x40;
323
324 tuner_i2c_xfer_send(&priv->i2c_props, buf, 2);
325 }
326
327 static void tda8295_agc2_out(struct tuner *t, int enable)
328 {
329 struct tda8290_priv *priv = t->priv;
330 unsigned char set_gpio_cf[] = { 0x44, 0x00 };
331 unsigned char set_gpio_val[] = { 0x46, 0x00 };
332
333 tuner_i2c_xfer_send(&priv->i2c_props, &set_gpio_cf[0], 1);
334 tuner_i2c_xfer_recv(&priv->i2c_props, &set_gpio_cf[1], 1);
335 tuner_i2c_xfer_send(&priv->i2c_props, &set_gpio_val[0], 1);
336 tuner_i2c_xfer_recv(&priv->i2c_props, &set_gpio_val[1], 1);
337
338 set_gpio_cf[1] &= 0xf0; /* clear GPIO_0 bits 3-0 */
339
340 if (enable) {
341 set_gpio_cf[1] |= 0x01; /* config GPIO_0 as Open Drain Out */
342 set_gpio_val[1] &= 0xfe; /* set GPIO_0 pin low */
343 }
344 tuner_i2c_xfer_send(&priv->i2c_props, set_gpio_cf, 2);
345 tuner_i2c_xfer_send(&priv->i2c_props, set_gpio_val, 2);
346 }
347
348 static int tda8295_has_signal(struct tuner *t)
349 {
350 struct tda8290_priv *priv = t->priv;
351
352 unsigned char hvpll_stat = 0x26;
353 unsigned char ret;
354
355 tuner_i2c_xfer_send(&priv->i2c_props, &hvpll_stat, 1);
356 tuner_i2c_xfer_recv(&priv->i2c_props, &ret, 1);
357 return (ret & 0x01) ? 65535 : 0;
358 }
359
360 /*---------------------------------------------------------------------*/
361
362 static void tda8295_set_freq(struct tuner *t, unsigned int freq)
363 {
364 struct tda8290_priv *priv = t->priv;
365 u16 ifc;
366
367 unsigned char blanking_mode[] = { 0x1d, 0x00 };
368
369 struct analog_parameters params = {
370 .frequency = freq,
371 .mode = t->mode,
372 .audmode = t->audmode,
373 .std = t->std
374 };
375
376 set_audio(t);
377
378 ifc = priv->cfg.sgIF; /* FIXME */
379
380 tuner_dbg("%s: ifc = %u, freq = %d\n", __FUNCTION__, ifc, freq);
381
382 tda8295_power(t, 1);
383 tda8295_agc1_out(t, 1);
384
385 tuner_i2c_xfer_send(&priv->i2c_props, &blanking_mode[0], 1);
386 tuner_i2c_xfer_recv(&priv->i2c_props, &blanking_mode[1], 1);
387
388 tda8295_set_video_std(t);
389
390 blanking_mode[1] = 0x03;
391 tuner_i2c_xfer_send(&priv->i2c_props, blanking_mode, 2);
392 msleep(20);
393
394 tda8295_i2c_bridge(t, 1);
395
396 if (t->fe.ops.tuner_ops.set_analog_params)
397 t->fe.ops.tuner_ops.set_analog_params(&t->fe, &params);
398
399 if (priv->cfg.agcf)
400 priv->cfg.agcf(&t->fe);
401
402 if (tda8295_has_signal(t))
403 tuner_dbg("tda8295 is locked\n");
404 else
405 tuner_dbg("tda8295 not locked, no signal?\n");
406
407 tda8295_i2c_bridge(t, 0);
408 }
409
410 /*---------------------------------------------------------------------*/
411
412 static int tda8290_has_signal(struct tuner *t)
413 {
414 struct tda8290_priv *priv = t->priv;
415
416 unsigned char i2c_get_afc[1] = { 0x1B };
417 unsigned char afc = 0;
418
419 tuner_i2c_xfer_send(&priv->i2c_props, i2c_get_afc, ARRAY_SIZE(i2c_get_afc));
420 tuner_i2c_xfer_recv(&priv->i2c_props, &afc, 1);
421 return (afc & 0x80)? 65535:0;
422 }
423
424 /*---------------------------------------------------------------------*/
425
426 static void tda8290_standby(struct tuner *t)
427 {
428 struct tda8290_priv *priv = t->priv;
429 unsigned char cb1[] = { 0x30, 0xD0 };
430 unsigned char tda8290_standby[] = { 0x00, 0x02 };
431 unsigned char tda8290_agc_tri[] = { 0x02, 0x20 };
432 struct i2c_msg msg = {.addr = priv->tda827x_addr, .flags=0, .buf=cb1, .len = 2};
433
434 tda8290_i2c_bridge(t, 1);
435 if (priv->tda827x_ver != 0)
436 cb1[1] = 0x90;
437 i2c_transfer(priv->i2c_props.adap, &msg, 1);
438 tda8290_i2c_bridge(t, 0);
439 tuner_i2c_xfer_send(&priv->i2c_props, tda8290_agc_tri, 2);
440 tuner_i2c_xfer_send(&priv->i2c_props, tda8290_standby, 2);
441 }
442
443 static void tda8295_standby(struct tuner *t)
444 {
445 tda8295_agc1_out(t, 0); /* Put AGC in tri-state */
446
447 tda8295_power(t, 0);
448 }
449
450 static void tda8290_init_if(struct tuner *t)
451 {
452 struct tda8290_priv *priv = t->priv;
453
454 unsigned char set_VS[] = { 0x30, 0x6F };
455 unsigned char set_GP00_CF[] = { 0x20, 0x01 };
456 unsigned char set_GP01_CF[] = { 0x20, 0x0B };
457
458 if ((t->config == 1) || (t->config == 2))
459 tuner_i2c_xfer_send(&priv->i2c_props, set_GP00_CF, 2);
460 else
461 tuner_i2c_xfer_send(&priv->i2c_props, set_GP01_CF, 2);
462 tuner_i2c_xfer_send(&priv->i2c_props, set_VS, 2);
463 }
464
465 static void tda8295_init_if(struct tuner *t)
466 {
467 struct tda8290_priv *priv = t->priv;
468
469 static unsigned char set_adc_ctl[] = { 0x33, 0x14 };
470 static unsigned char set_adc_ctl2[] = { 0x34, 0x00 };
471 static unsigned char set_pll_reg6[] = { 0x3e, 0x63 };
472 static unsigned char set_pll_reg0[] = { 0x38, 0x23 };
473 static unsigned char set_pll_reg7[] = { 0x3f, 0x01 };
474 static unsigned char set_pll_reg10[] = { 0x42, 0x61 };
475 static unsigned char set_gpio_reg0[] = { 0x44, 0x0b };
476
477 tda8295_power(t, 1);
478
479 tda8295_set_easy_mode(t, 0);
480 tda8295_set_video_std(t);
481
482 tuner_i2c_xfer_send(&priv->i2c_props, set_adc_ctl, 2);
483 tuner_i2c_xfer_send(&priv->i2c_props, set_adc_ctl2, 2);
484 tuner_i2c_xfer_send(&priv->i2c_props, set_pll_reg6, 2);
485 tuner_i2c_xfer_send(&priv->i2c_props, set_pll_reg0, 2);
486 tuner_i2c_xfer_send(&priv->i2c_props, set_pll_reg7, 2);
487 tuner_i2c_xfer_send(&priv->i2c_props, set_pll_reg10, 2);
488 tuner_i2c_xfer_send(&priv->i2c_props, set_gpio_reg0, 2);
489
490 tda8295_agc1_out(t, 0);
491 tda8295_agc2_out(t, 0);
492 }
493
494 static void tda8290_init_tuner(struct tuner *t)
495 {
496 struct tda8290_priv *priv = t->priv;
497 unsigned char tda8275_init[] = { 0x00, 0x00, 0x00, 0x40, 0xdC, 0x04, 0xAf,
498 0x3F, 0x2A, 0x04, 0xFF, 0x00, 0x00, 0x40 };
499 unsigned char tda8275a_init[] = { 0x00, 0x00, 0x00, 0x00, 0xdC, 0x05, 0x8b,
500 0x0c, 0x04, 0x20, 0xFF, 0x00, 0x00, 0x4b };
501 struct i2c_msg msg = {.addr = priv->tda827x_addr, .flags=0,
502 .buf=tda8275_init, .len = 14};
503 if (priv->tda827x_ver != 0)
504 msg.buf = tda8275a_init;
505
506 tda8290_i2c_bridge(t, 1);
507 i2c_transfer(priv->i2c_props.adap, &msg, 1);
508 tda8290_i2c_bridge(t, 0);
509 }
510
511 /*---------------------------------------------------------------------*/
512
513 static void tda829x_release(struct tuner *t)
514 {
515 if (t->fe.ops.tuner_ops.release)
516 t->fe.ops.tuner_ops.release(&t->fe);
517
518 kfree(t->priv);
519 t->priv = NULL;
520 }
521
522 static struct tuner_operations tda8290_tuner_ops = {
523 .set_tv_freq = tda8290_set_freq,
524 .set_radio_freq = tda8290_set_freq,
525 .has_signal = tda8290_has_signal,
526 .standby = tda8290_standby,
527 .release = tda829x_release,
528 };
529
530 static struct tuner_operations tda8295_tuner_ops = {
531 .set_tv_freq = tda8295_set_freq,
532 .set_radio_freq = tda8295_set_freq,
533 .has_signal = tda8295_has_signal,
534 .standby = tda8295_standby,
535 .release = tda829x_release,
536 };
537
538 int tda8290_attach(struct tuner *t)
539 {
540 struct tda8290_priv *priv = NULL;
541 u8 data;
542 int i, ret, tuners_found;
543 u32 tuner_addrs;
544 struct i2c_msg msg = {.flags=I2C_M_RD, .buf=&data, .len = 1};
545
546 priv = kzalloc(sizeof(struct tda8290_priv), GFP_KERNEL);
547 if (priv == NULL)
548 return -ENOMEM;
549 t->priv = priv;
550
551 priv->i2c_props.addr = t->i2c.addr;
552 priv->i2c_props.adap = t->i2c.adapter;
553 priv->cfg.config = &t->config;
554 priv->cfg.tuner_callback = t->tuner_callback;
555
556 tda8290_i2c_bridge(t, 1);
557 /* probe for tuner chip */
558 tuners_found = 0;
559 tuner_addrs = 0;
560 for (i=0x60; i<= 0x63; i++) {
561 msg.addr = i;
562 ret = i2c_transfer(priv->i2c_props.adap, &msg, 1);
563 if (ret == 1) {
564 tuners_found++;
565 tuner_addrs = (tuner_addrs << 8) + i;
566 }
567 }
568 /* if there is more than one tuner, we expect the right one is
569 behind the bridge and we choose the highest address that doesn't
570 give a response now
571 */
572 tda8290_i2c_bridge(t, 0);
573 if(tuners_found > 1)
574 for (i = 0; i < tuners_found; i++) {
575 msg.addr = tuner_addrs & 0xff;
576 ret = i2c_transfer(priv->i2c_props.adap, &msg, 1);
577 if(ret == 1)
578 tuner_addrs = tuner_addrs >> 8;
579 else
580 break;
581 }
582 if (tuner_addrs == 0) {
583 tuner_addrs = 0x61;
584 tuner_info("could not clearly identify tuner address, defaulting to %x\n",
585 tuner_addrs);
586 } else {
587 tuner_addrs = tuner_addrs & 0xff;
588 tuner_info("setting tuner address to %x\n", tuner_addrs);
589 }
590 priv->tda827x_addr = tuner_addrs;
591 msg.addr = tuner_addrs;
592
593 tda8290_i2c_bridge(t, 1);
594
595 ret = i2c_transfer(priv->i2c_props.adap, &msg, 1);
596 if( ret != 1)
597 tuner_warn("TDA827x access failed!\n");
598
599 if ((data & 0x3c) == 0) {
600 strlcpy(t->i2c.name, "tda8290+75", sizeof(t->i2c.name));
601 priv->tda827x_ver = 0;
602 } else {
603 strlcpy(t->i2c.name, "tda8290+75a", sizeof(t->i2c.name));
604 priv->tda827x_ver = 2;
605 }
606 tda827x_attach(&t->fe, priv->tda827x_addr,
607 priv->i2c_props.adap, &priv->cfg);
608
609 /* FIXME: tda827x module doesn't probe the tuner until
610 * tda827x_initial_sleep is called
611 */
612 if (t->fe.ops.tuner_ops.sleep)
613 t->fe.ops.tuner_ops.sleep(&t->fe);
614
615 memcpy(&t->ops, &tda8290_tuner_ops, sizeof(struct tuner_operations));
616
617 tuner_info("type set to %s\n", t->i2c.name);
618
619 priv->cfg.tda827x_lpsel = 0;
620 t->mode = V4L2_TUNER_ANALOG_TV;
621
622 tda8290_init_tuner(t);
623 tda8290_init_if(t);
624 return 0;
625 }
626 EXPORT_SYMBOL_GPL(tda8290_attach);
627
628 int tda8295_attach(struct tuner *t)
629 {
630 struct tda8290_priv *priv = NULL;
631 u8 data;
632 int i, ret, tuners_found;
633 u32 tuner_addrs;
634 struct i2c_msg msg = { .flags = I2C_M_RD, .buf = &data, .len = 1 };
635
636 priv = kzalloc(sizeof(struct tda8290_priv), GFP_KERNEL);
637 if (priv == NULL)
638 return -ENOMEM;
639 t->priv = priv;
640
641 priv->i2c_props.addr = t->i2c.addr;
642 priv->i2c_props.adap = t->i2c.adapter;
643
644 tda8295_i2c_bridge(t, 1);
645 /* probe for tuner chip */
646 tuners_found = 0;
647 tuner_addrs = 0;
648 for (i = 0x60; i <= 0x63; i++) {
649 msg.addr = i;
650 ret = i2c_transfer(priv->i2c_props.adap, &msg, 1);
651 if (ret == 1) {
652 tuners_found++;
653 tuner_addrs = (tuner_addrs << 8) + i;
654 }
655 }
656 /* if there is more than one tuner, we expect the right one is
657 behind the bridge and we choose the highest address that doesn't
658 give a response now
659 */
660 tda8295_i2c_bridge(t, 0);
661 if (tuners_found > 1)
662 for (i = 0; i < tuners_found; i++) {
663 msg.addr = tuner_addrs & 0xff;
664 ret = i2c_transfer(priv->i2c_props.adap, &msg, 1);
665 if (ret == 1)
666 tuner_addrs = tuner_addrs >> 8;
667 else
668 break;
669 }
670 if (tuner_addrs == 0) {
671 tuner_addrs = 0x60;
672 tuner_info("could not clearly identify tuner address, "
673 "defaulting to %x\n", tuner_addrs);
674 } else {
675 tuner_addrs = tuner_addrs & 0xff;
676 tuner_info("setting tuner address to %x\n", tuner_addrs);
677 }
678 priv->tda827x_addr = tuner_addrs;
679 msg.addr = tuner_addrs;
680
681 tda8295_i2c_bridge(t, 1);
682 ret = i2c_transfer(priv->i2c_props.adap, &msg, 1);
683 tda8295_i2c_bridge(t, 0);
684 if (ret != 1)
685 tuner_warn("TDA827x access failed!\n");
686 if ((data & 0x3c) == 0) {
687 strlcpy(t->i2c.name, "tda8295+18271", sizeof(t->i2c.name));
688 tda18271_attach(&t->fe, priv->tda827x_addr,
689 priv->i2c_props.adap);
690 priv->tda827x_ver = 4;
691 } else {
692 strlcpy(t->i2c.name, "tda8295+75a", sizeof(t->i2c.name));
693 tda827x_attach(&t->fe, priv->tda827x_addr,
694 priv->i2c_props.adap, &priv->cfg);
695
696 /* FIXME: tda827x module doesn't probe the tuner until
697 * tda827x_initial_sleep is called
698 */
699 if (t->fe.ops.tuner_ops.sleep)
700 t->fe.ops.tuner_ops.sleep(&t->fe);
701 priv->tda827x_ver = 2;
702 }
703 priv->tda827x_ver |= 1; /* signifies 8295 vs 8290 */
704 tuner_info("type set to %s\n", t->i2c.name);
705
706 memcpy(&t->ops, &tda8295_tuner_ops, sizeof(struct tuner_operations));
707
708 priv->cfg.tda827x_lpsel = 0;
709 t->mode = V4L2_TUNER_ANALOG_TV;
710
711 tda8295_init_if(t);
712 return 0;
713 }
714 EXPORT_SYMBOL_GPL(tda8295_attach);
715
716 int tda8290_probe(struct tuner *t)
717 {
718 struct tuner_i2c_props i2c_props = {
719 .adap = t->i2c.adapter,
720 .addr = t->i2c.addr
721 };
722
723 unsigned char soft_reset[] = { 0x00, 0x00 };
724 unsigned char easy_mode_b[] = { 0x01, 0x02 };
725 unsigned char easy_mode_g[] = { 0x01, 0x04 };
726 unsigned char restore_9886[] = { 0x00, 0xd6, 0x30 };
727 unsigned char addr_dto_lsb = 0x07;
728 unsigned char data;
729
730 tuner_i2c_xfer_send(&i2c_props, easy_mode_b, 2);
731 tuner_i2c_xfer_send(&i2c_props, soft_reset, 2);
732 tuner_i2c_xfer_send(&i2c_props, &addr_dto_lsb, 1);
733 tuner_i2c_xfer_recv(&i2c_props, &data, 1);
734 if (data == 0) {
735 tuner_i2c_xfer_send(&i2c_props, easy_mode_g, 2);
736 tuner_i2c_xfer_send(&i2c_props, soft_reset, 2);
737 tuner_i2c_xfer_send(&i2c_props, &addr_dto_lsb, 1);
738 tuner_i2c_xfer_recv(&i2c_props, &data, 1);
739 if (data == 0x7b) {
740 return 0;
741 }
742 }
743 tuner_i2c_xfer_send(&i2c_props, restore_9886, 3);
744 return -1;
745 }
746 EXPORT_SYMBOL_GPL(tda8290_probe);
747
748 MODULE_DESCRIPTION("Philips TDA8290 + TDA8275 / TDA8275a tuner driver");
749 MODULE_AUTHOR("Gerd Knorr, Hartmut Hackmann, Michael Krufky");
750 MODULE_LICENSE("GPL");
751
752 /*
753 * Overrides for Emacs so that we follow Linus's tabbing style.
754 * ---------------------------------------------------------------------------
755 * Local variables:
756 * c-basic-offset: 8
757 * End:
758 */