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