]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/media/tuners/tda8290.c
treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 61
[mirror_ubuntu-jammy-kernel.git] / drivers / media / tuners / tda8290.c
CommitLineData
74ba9207 1// SPDX-License-Identifier: GPL-2.0-or-later
1da177e4 2/*
de48eebc
HH
3
4 i2c tv tuner chip device driver
5 controls the philips tda8290+75 tuner chip combo.
6
910bb3e3
MK
7
8 This "tda8290" module was split apart from the original "tuner" module.
de48eebc
HH
9*/
10
1da177e4 11#include <linux/i2c.h>
5a0e3ad6 12#include <linux/slab.h>
1da177e4 13#include <linux/delay.h>
7f6adeaf 14#include <linux/videodev2.h>
ab166050 15#include "tuner-i2c.h"
910bb3e3 16#include "tda8290.h"
746d9732 17#include "tda827x.h"
5bea1cd3 18#include "tda18271.h"
910bb3e3 19
ab166050
MK
20static int debug;
21module_param(debug, int, 0644);
910bb3e3
MK
22MODULE_PARM_DESC(debug, "enable verbose debug messages");
23
9af0ef27 24static int deemphasis_50;
b450b92e 25module_param(deemphasis_50, int, 0644);
9af0ef27
MCC
26MODULE_PARM_DESC(deemphasis_50, "0 - 75us deemphasis; 1 - 50us deemphasis");
27
1da177e4
LT
28/* ---------------------------------------------------------------------- */
29
b2083199 30struct tda8290_priv {
db8a6956
MK
31 struct tuner_i2c_props i2c_props;
32
b2083199 33 unsigned char tda8290_easy_mode;
746d9732 34
b2083199 35 unsigned char tda827x_addr;
8c125f2c
MK
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
1da177e4 43
746d9732 44 struct tda827x_config cfg;
5b0e5350 45 struct tda18271_std_map *tda18271_std_map;
1da177e4
LT
46};
47
de48eebc 48/*---------------------------------------------------------------------*/
1da177e4 49
a72dd305 50static int tda8290_i2c_bridge(struct dvb_frontend *fe, int close)
de48eebc 51{
4e9154b8 52 struct tda8290_priv *priv = fe->analog_demod_priv;
db8a6956 53
8ff230fb
AB
54 static unsigned char enable[2] = { 0x21, 0xC0 };
55 static unsigned char disable[2] = { 0x21, 0x00 };
de48eebc 56 unsigned char *msg;
a72dd305
MK
57
58 if (close) {
de48eebc 59 msg = enable;
db8a6956 60 tuner_i2c_xfer_send(&priv->i2c_props, msg, 2);
de48eebc
HH
61 /* let the bridge stabilize */
62 msleep(20);
63 } else {
64 msg = disable;
db8a6956 65 tuner_i2c_xfer_send(&priv->i2c_props, msg, 2);
de48eebc 66 }
a72dd305
MK
67
68 return 0;
1da177e4
LT
69}
70
a72dd305 71static int tda8295_i2c_bridge(struct dvb_frontend *fe, int close)
5bea1cd3 72{
4e9154b8 73 struct tda8290_priv *priv = fe->analog_demod_priv;
5bea1cd3 74
8ff230fb
AB
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 };
5bea1cd3 78 unsigned char *msg;
a72dd305 79
5bea1cd3
MK
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;
567aba0b 87 tuner_i2c_xfer_send_recv(&priv->i2c_props, msg, 1, &msg[1], 1);
5bea1cd3
MK
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 }
a72dd305
MK
97
98 return 0;
5bea1cd3
MK
99}
100
de48eebc
HH
101/*---------------------------------------------------------------------*/
102
c7919d52
MK
103static void set_audio(struct dvb_frontend *fe,
104 struct analog_parameters *params)
1da177e4 105{
4e9154b8 106 struct tda8290_priv *priv = fe->analog_demod_priv;
910bb3e3
MK
107 char* mode;
108
c7919d52 109 if (params->std & V4L2_STD_MN) {
910bb3e3 110 priv->tda8290_easy_mode = 0x01;
910bb3e3 111 mode = "MN";
c7919d52 112 } else if (params->std & V4L2_STD_B) {
910bb3e3
MK
113 priv->tda8290_easy_mode = 0x02;
114 mode = "B";
c7919d52 115 } else if (params->std & V4L2_STD_GH) {
910bb3e3
MK
116 priv->tda8290_easy_mode = 0x04;
117 mode = "GH";
c7919d52 118 } else if (params->std & V4L2_STD_PAL_I) {
910bb3e3
MK
119 priv->tda8290_easy_mode = 0x08;
120 mode = "I";
c7919d52 121 } else if (params->std & V4L2_STD_DK) {
910bb3e3
MK
122 priv->tda8290_easy_mode = 0x10;
123 mode = "DK";
c7919d52 124 } else if (params->std & V4L2_STD_SECAM_L) {
910bb3e3
MK
125 priv->tda8290_easy_mode = 0x20;
126 mode = "L";
c7919d52 127 } else if (params->std & V4L2_STD_SECAM_LC) {
910bb3e3
MK
128 priv->tda8290_easy_mode = 0x40;
129 mode = "LC";
130 } else {
910bb3e3
MK
131 priv->tda8290_easy_mode = 0x10;
132 mode = "xx";
133 }
134
9af0ef27 135 if (params->mode == V4L2_TUNER_RADIO) {
3f76cf8c
MK
136 /* Set TDA8295 to FM radio; Start TDA8290 with MN values */
137 priv->tda8290_easy_mode = (priv->ver & TDA8295) ? 0x80 : 0x01;
9af0ef27
MCC
138 tuner_dbg("setting to radio FM\n");
139 } else {
140 tuner_dbg("setting tda829x to system %s\n", mode);
141 }
910bb3e3
MK
142}
143
4c27f1a4 144static struct {
9af0ef27
MCC
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
c7919d52
MK
164static void tda8290_set_params(struct dvb_frontend *fe,
165 struct analog_parameters *params)
910bb3e3 166{
4e9154b8 167 struct tda8290_priv *priv = fe->analog_demod_priv;
4e9154b8 168
8ff230fb 169 static unsigned char soft_reset[] = { 0x00, 0x00 };
b2083199 170 unsigned char easy_mode[] = { 0x01, priv->tda8290_easy_mode };
8ff230fb
AB
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,
9a741ec9 188 pll_stat;
58ef4f92 189 int i;
de48eebc 190
c7919d52 191 set_audio(fe, params);
910bb3e3 192
ab166050 193 if (priv->cfg.config)
7bff4b4d 194 tuner_dbg("tda827xa config is 0x%02x\n", priv->cfg.config);
db8a6956
MK
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);
de48eebc
HH
198 msleep(1);
199
9af0ef27 200 if (params->mode == V4L2_TUNER_RADIO) {
9af0ef27
MCC
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 }
de48eebc 223
ecb71d26 224
2f719f7a
OZ
225 if (fe->ops.analog_ops.i2c_gate_ctrl)
226 fe->ops.analog_ops.i2c_gate_ctrl(fe, 1);
746d9732 227
4e9154b8 228 if (fe->ops.tuner_ops.set_analog_params)
c7919d52 229 fe->ops.tuner_ops.set_analog_params(fe, params);
746d9732 230
58ef4f92 231 for (i = 0; i < 3; i++) {
567aba0b
MCC
232 tuner_i2c_xfer_send_recv(&priv->i2c_props,
233 &addr_pll_stat, 1, &pll_stat, 1);
58ef4f92 234 if (pll_stat & 0x80) {
567aba0b
MCC
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);
58ef4f92
HH
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 }
de48eebc 248 /* adjust headroom resp. gain */
4ac95af9
HH
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);
db8a6956 252 tuner_i2c_xfer_send(&priv->i2c_props, gainset_2, 2);
de48eebc 253 msleep(100);
567aba0b
MCC
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);
de48eebc 258 if ((agc_stat > 115) || !(pll_stat & 0x80)) {
4ac95af9
HH
259 tuner_dbg("adjust gain, step 2. Agc: %d, lock: %d\n",
260 agc_stat, pll_stat & 0x80);
746d9732 261 if (priv->cfg.agcf)
4e9154b8 262 priv->cfg.agcf(fe);
de48eebc 263 msleep(100);
567aba0b
MCC
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);
de48eebc
HH
270 if((agc_stat > 115) || !(pll_stat & 0x80)) {
271 tuner_dbg("adjust gain, step 3. Agc: %d\n", agc_stat);
db8a6956
MK
272 tuner_i2c_xfer_send(&priv->i2c_props, adc_head_12, 2);
273 tuner_i2c_xfer_send(&priv->i2c_props, pll_bw_low, 2);
de48eebc
HH
274 msleep(100);
275 }
276 }
277 }
1da177e4 278
de48eebc 279 /* l/ l' deadlock? */
b2083199 280 if(priv->tda8290_easy_mode & 0x60) {
567aba0b
MCC
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);
de48eebc 287 if ((adc_sat > 20) || !(pll_stat & 0x80)) {
4ac95af9 288 tuner_dbg("trying to resolve SECAM L deadlock\n");
db8a6956 289 tuner_i2c_xfer_send(&priv->i2c_props, agc_rst_on, 2);
de48eebc 290 msleep(40);
db8a6956 291 tuner_i2c_xfer_send(&priv->i2c_props, agc_rst_off, 2);
de48eebc
HH
292 }
293 }
586b0cab 294
2f719f7a
OZ
295 if (fe->ops.analog_ops.i2c_gate_ctrl)
296 fe->ops.analog_ops.i2c_gate_ctrl(fe, 0);
db8a6956 297 tuner_i2c_xfer_send(&priv->i2c_props, if_agc_set, 2);
1da177e4
LT
298}
299
de48eebc
HH
300/*---------------------------------------------------------------------*/
301
4e9154b8 302static void tda8295_power(struct dvb_frontend *fe, int enable)
5bea1cd3 303{
4e9154b8 304 struct tda8290_priv *priv = fe->analog_demod_priv;
5bea1cd3
MK
305 unsigned char buf[] = { 0x30, 0x00 }; /* clb_stdbt */
306
567aba0b 307 tuner_i2c_xfer_send_recv(&priv->i2c_props, &buf[0], 1, &buf[1], 1);
5bea1cd3
MK
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
4e9154b8 317static void tda8295_set_easy_mode(struct dvb_frontend *fe, int enable)
5bea1cd3 318{
4e9154b8 319 struct tda8290_priv *priv = fe->analog_demod_priv;
5bea1cd3
MK
320 unsigned char buf[] = { 0x01, 0x00 };
321
567aba0b 322 tuner_i2c_xfer_send_recv(&priv->i2c_props, &buf[0], 1, &buf[1], 1);
5bea1cd3
MK
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
4e9154b8 332static void tda8295_set_video_std(struct dvb_frontend *fe)
5bea1cd3 333{
4e9154b8 334 struct tda8290_priv *priv = fe->analog_demod_priv;
5bea1cd3
MK
335 unsigned char buf[] = { 0x00, priv->tda8290_easy_mode };
336
337 tuner_i2c_xfer_send(&priv->i2c_props, buf, 2);
338
4e9154b8 339 tda8295_set_easy_mode(fe, 1);
5bea1cd3 340 msleep(20);
4e9154b8 341 tda8295_set_easy_mode(fe, 0);
5bea1cd3
MK
342}
343
344/*---------------------------------------------------------------------*/
345
4e9154b8 346static void tda8295_agc1_out(struct dvb_frontend *fe, int enable)
5bea1cd3 347{
4e9154b8 348 struct tda8290_priv *priv = fe->analog_demod_priv;
5bea1cd3
MK
349 unsigned char buf[] = { 0x02, 0x00 }; /* DIV_FUNC */
350
567aba0b 351 tuner_i2c_xfer_send_recv(&priv->i2c_props, &buf[0], 1, &buf[1], 1);
5bea1cd3
MK
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
4e9154b8 361static void tda8295_agc2_out(struct dvb_frontend *fe, int enable)
5bea1cd3 362{
4e9154b8 363 struct tda8290_priv *priv = fe->analog_demod_priv;
5bea1cd3
MK
364 unsigned char set_gpio_cf[] = { 0x44, 0x00 };
365 unsigned char set_gpio_val[] = { 0x46, 0x00 };
366
567aba0b
MCC
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);
5bea1cd3
MK
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
dfc2e12d 382static int tda8295_has_signal(struct dvb_frontend *fe, u16 *signal)
5bea1cd3 383{
4e9154b8 384 struct tda8290_priv *priv = fe->analog_demod_priv;
5bea1cd3
MK
385
386 unsigned char hvpll_stat = 0x26;
387 unsigned char ret;
388
567aba0b 389 tuner_i2c_xfer_send_recv(&priv->i2c_props, &hvpll_stat, 1, &ret, 1);
dfc2e12d
HV
390 *signal = (ret & 0x01) ? 65535 : 0;
391 return 0;
5bea1cd3
MK
392}
393
394/*---------------------------------------------------------------------*/
395
c7919d52
MK
396static void tda8295_set_params(struct dvb_frontend *fe,
397 struct analog_parameters *params)
5bea1cd3 398{
4e9154b8 399 struct tda8290_priv *priv = fe->analog_demod_priv;
dfc2e12d 400 u16 signal = 0;
5bea1cd3
MK
401 unsigned char blanking_mode[] = { 0x1d, 0x00 };
402
c7919d52 403 set_audio(fe, params);
5bea1cd3 404
7e28adb2 405 tuner_dbg("%s: freq = %d\n", __func__, params->frequency);
5bea1cd3 406
4e9154b8
MK
407 tda8295_power(fe, 1);
408 tda8295_agc1_out(fe, 1);
5bea1cd3 409
567aba0b
MCC
410 tuner_i2c_xfer_send_recv(&priv->i2c_props,
411 &blanking_mode[0], 1, &blanking_mode[1], 1);
5bea1cd3 412
4e9154b8 413 tda8295_set_video_std(fe);
5bea1cd3
MK
414
415 blanking_mode[1] = 0x03;
416 tuner_i2c_xfer_send(&priv->i2c_props, blanking_mode, 2);
417 msleep(20);
418
2f719f7a
OZ
419 if (fe->ops.analog_ops.i2c_gate_ctrl)
420 fe->ops.analog_ops.i2c_gate_ctrl(fe, 1);
5bea1cd3 421
4e9154b8 422 if (fe->ops.tuner_ops.set_analog_params)
c7919d52 423 fe->ops.tuner_ops.set_analog_params(fe, params);
5bea1cd3
MK
424
425 if (priv->cfg.agcf)
4e9154b8 426 priv->cfg.agcf(fe);
5bea1cd3 427
dfc2e12d
HV
428 tda8295_has_signal(fe, &signal);
429 if (signal)
5bea1cd3
MK
430 tuner_dbg("tda8295 is locked\n");
431 else
432 tuner_dbg("tda8295 not locked, no signal?\n");
433
2f719f7a
OZ
434 if (fe->ops.analog_ops.i2c_gate_ctrl)
435 fe->ops.analog_ops.i2c_gate_ctrl(fe, 0);
5bea1cd3
MK
436}
437
438/*---------------------------------------------------------------------*/
439
dfc2e12d 440static int tda8290_has_signal(struct dvb_frontend *fe, u16 *signal)
1da177e4 441{
4e9154b8 442 struct tda8290_priv *priv = fe->analog_demod_priv;
1da177e4 443
910bb3e3
MK
444 unsigned char i2c_get_afc[1] = { 0x1B };
445 unsigned char afc = 0;
1da177e4 446
567aba0b
MCC
447 tuner_i2c_xfer_send_recv(&priv->i2c_props,
448 i2c_get_afc, ARRAY_SIZE(i2c_get_afc), &afc, 1);
dfc2e12d
HV
449 *signal = (afc & 0x80) ? 65535 : 0;
450 return 0;
1da177e4
LT
451}
452
de48eebc
HH
453/*---------------------------------------------------------------------*/
454
4e9154b8 455static void tda8290_standby(struct dvb_frontend *fe)
793cf9e6 456{
4e9154b8
MK
457 struct tda8290_priv *priv = fe->analog_demod_priv;
458
8ff230fb
AB
459 static unsigned char cb1[] = { 0x30, 0xD0 };
460 static unsigned char tda8290_standby[] = { 0x00, 0x02 };
461 static unsigned char tda8290_agc_tri[] = { 0x02, 0x20 };
b2083199 462 struct i2c_msg msg = {.addr = priv->tda827x_addr, .flags=0, .buf=cb1, .len = 2};
de48eebc 463
2f719f7a
OZ
464 if (fe->ops.analog_ops.i2c_gate_ctrl)
465 fe->ops.analog_ops.i2c_gate_ctrl(fe, 1);
8c125f2c 466 if (priv->ver & TDA8275A)
de48eebc 467 cb1[1] = 0x90;
db8a6956 468 i2c_transfer(priv->i2c_props.adap, &msg, 1);
2f719f7a
OZ
469 if (fe->ops.analog_ops.i2c_gate_ctrl)
470 fe->ops.analog_ops.i2c_gate_ctrl(fe, 0);
db8a6956
MK
471 tuner_i2c_xfer_send(&priv->i2c_props, tda8290_agc_tri, 2);
472 tuner_i2c_xfer_send(&priv->i2c_props, tda8290_standby, 2);
793cf9e6
MCC
473}
474
4e9154b8 475static void tda8295_standby(struct dvb_frontend *fe)
5bea1cd3 476{
4e9154b8 477 tda8295_agc1_out(fe, 0); /* Put AGC in tri-state */
5bea1cd3 478
4e9154b8 479 tda8295_power(fe, 0);
5bea1cd3
MK
480}
481
4e9154b8 482static void tda8290_init_if(struct dvb_frontend *fe)
de48eebc 483{
4e9154b8 484 struct tda8290_priv *priv = fe->analog_demod_priv;
db8a6956 485
8ff230fb
AB
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 };
de48eebc 489
1bacb2df
OZ
490 if ((priv->cfg.config == TDA8290_LNA_GP0_HIGH_ON) ||
491 (priv->cfg.config == TDA8290_LNA_GP0_HIGH_OFF))
db8a6956 492 tuner_i2c_xfer_send(&priv->i2c_props, set_GP00_CF, 2);
58ef4f92 493 else
db8a6956
MK
494 tuner_i2c_xfer_send(&priv->i2c_props, set_GP01_CF, 2);
495 tuner_i2c_xfer_send(&priv->i2c_props, set_VS, 2);
de48eebc
HH
496}
497
4e9154b8 498static void tda8295_init_if(struct dvb_frontend *fe)
5bea1cd3 499{
4e9154b8 500 struct tda8290_priv *priv = fe->analog_demod_priv;
5bea1cd3
MK
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
4e9154b8 510 tda8295_power(fe, 1);
5bea1cd3 511
4e9154b8
MK
512 tda8295_set_easy_mode(fe, 0);
513 tda8295_set_video_std(fe);
5bea1cd3
MK
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
4e9154b8
MK
523 tda8295_agc1_out(fe, 0);
524 tda8295_agc2_out(fe, 0);
5bea1cd3
MK
525}
526
4e9154b8 527static void tda8290_init_tuner(struct dvb_frontend *fe)
1da177e4 528{
4e9154b8 529 struct tda8290_priv *priv = fe->analog_demod_priv;
8ff230fb
AB
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 };
b2083199 536 struct i2c_msg msg = {.addr = priv->tda827x_addr, .flags=0,
c2f6f9d8 537 .buf=tda8275_init, .len = 14};
8c125f2c 538 if (priv->ver & TDA8275A)
de48eebc
HH
539 msg.buf = tda8275a_init;
540
2f719f7a
OZ
541 if (fe->ops.analog_ops.i2c_gate_ctrl)
542 fe->ops.analog_ops.i2c_gate_ctrl(fe, 1);
db8a6956 543 i2c_transfer(priv->i2c_props.adap, &msg, 1);
2f719f7a
OZ
544 if (fe->ops.analog_ops.i2c_gate_ctrl)
545 fe->ops.analog_ops.i2c_gate_ctrl(fe, 0);
de48eebc
HH
546}
547
548/*---------------------------------------------------------------------*/
1da177e4 549
4e9154b8 550static void tda829x_release(struct dvb_frontend *fe)
024cf530 551{
a4f263b5
MK
552 struct tda8290_priv *priv = fe->analog_demod_priv;
553
006ed1ec
MK
554 /* only try to release the tuner if we've
555 * attached it from within this module */
556 if (priv->ver & (TDA18271 | TDA8275 | TDA8275A))
a4f263b5
MK
557 if (fe->ops.tuner_ops.release)
558 fe->ops.tuner_ops.release(fe);
024cf530 559
4e9154b8
MK
560 kfree(fe->analog_demod_priv);
561 fe->analog_demod_priv = NULL;
910bb3e3
MK
562}
563
f21e0d7f
MK
564static struct tda18271_config tda829x_tda18271_config = {
565 .gate = TDA18271_GATE_ANALOG,
566};
567
8c125f2c 568static int tda829x_find_tuner(struct dvb_frontend *fe)
de48eebc 569{
8c125f2c 570 struct tda8290_priv *priv = fe->analog_demod_priv;
de48eebc
HH
571 int i, ret, tuners_found;
572 u32 tuner_addrs;
8c125f2c
MK
573 u8 data;
574 struct i2c_msg msg = { .flags = I2C_M_RD, .buf = &data, .len = 1 };
de48eebc 575
2f719f7a
OZ
576 if (fe->ops.analog_ops.i2c_gate_ctrl)
577 fe->ops.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
2f719f7a
OZ
595 if (fe->ops.analog_ops.i2c_gate_ctrl)
596 fe->ops.analog_ops.i2c_gate_ctrl(fe, 0);
8c125f2c
MK
597
598 if (tuners_found > 1)
de48eebc
HH
599 for (i = 0; i < tuners_found; i++) {
600 msg.addr = tuner_addrs & 0xff;
db8a6956 601 ret = i2c_transfer(priv->i2c_props.adap, &msg, 1);
8c125f2c 602 if (ret == 1)
de48eebc
HH
603 tuner_addrs = tuner_addrs >> 8;
604 else
605 break;
606 }
8c125f2c 607
de48eebc 608 if (tuner_addrs == 0) {
8c125f2c 609 tuner_addrs = 0x60;
8ca2e927
MCC
610 tuner_info("could not clearly identify tuner address, defaulting to %x\n",
611 tuner_addrs);
de48eebc
HH
612 } else {
613 tuner_addrs = tuner_addrs & 0xff;
910bb3e3 614 tuner_info("setting tuner address to %x\n", tuner_addrs);
de48eebc 615 }
b2083199 616 priv->tda827x_addr = tuner_addrs;
de48eebc
HH
617 msg.addr = tuner_addrs;
618
2f719f7a
OZ
619 if (fe->ops.analog_ops.i2c_gate_ctrl)
620 fe->ops.analog_ops.i2c_gate_ctrl(fe, 1);
db8a6956 621 ret = i2c_transfer(priv->i2c_props.adap, &msg, 1);
910bb3e3 622
8c125f2c
MK
623 if (ret != 1) {
624 tuner_warn("tuner access failed!\n");
2f719f7a
OZ
625 if (fe->ops.analog_ops.i2c_gate_ctrl)
626 fe->ops.analog_ops.i2c_gate_ctrl(fe, 0);
8c125f2c 627 return -EREMOTEIO;
de48eebc 628 }
746d9732 629
255b5113 630 if ((data == 0x83) || (data == 0x84)) {
8c125f2c 631 priv->ver |= TDA18271;
adcc4b3e 632 tda829x_tda18271_config.config = priv->cfg.config;
5b0e5350 633 tda829x_tda18271_config.std_map = priv->tda18271_std_map;
a07c8779
MK
634 dvb_attach(tda18271_attach, fe, priv->tda827x_addr,
635 priv->i2c_props.adap, &tda829x_tda18271_config);
8c125f2c
MK
636 } else {
637 if ((data & 0x3c) == 0)
638 priv->ver |= TDA8275;
639 else
640 priv->ver |= TDA8275A;
7fd8b263 641
a07c8779
MK
642 dvb_attach(tda827x_attach, fe, priv->tda827x_addr,
643 priv->i2c_props.adap, &priv->cfg);
7bff4b4d 644 priv->cfg.switch_addr = priv->i2c_props.addr;
8c125f2c 645 }
6881647c
MK
646 if (fe->ops.tuner_ops.init)
647 fe->ops.tuner_ops.init(fe);
63c25480 648
6881647c
MK
649 if (fe->ops.tuner_ops.sleep)
650 fe->ops.tuner_ops.sleep(fe);
63c25480 651
2f719f7a
OZ
652 if (fe->ops.analog_ops.i2c_gate_ctrl)
653 fe->ops.analog_ops.i2c_gate_ctrl(fe, 0);
746d9732 654
746d9732 655 return 0;
1da177e4 656}
5bea1cd3 657
f1f32849
MK
658static int tda8290_probe(struct tuner_i2c_props *i2c_props)
659{
660#define TDA8290_ID 0x89
567aba0b
MCC
661 u8 reg = 0x1f, id;
662 struct i2c_msg msg_read[] = {
89a8969a
JW
663 { .addr = i2c_props->addr, .flags = 0, .len = 1, .buf = &reg },
664 { .addr = i2c_props->addr, .flags = I2C_M_RD, .len = 1, .buf = &id },
567aba0b 665 };
f1f32849
MK
666
667 /* detect tda8290 */
567aba0b 668 if (i2c_transfer(i2c_props->adap, msg_read, 2) != 2) {
89a8969a 669 printk(KERN_WARNING "%s: couldn't read register 0x%02x\n",
567aba0b
MCC
670 __func__, reg);
671 return -ENODEV;
672 }
f1f32849 673
567aba0b 674 if (id == TDA8290_ID) {
ab166050 675 if (debug)
f1f32849 676 printk(KERN_DEBUG "%s: tda8290 detected @ %d-%04x\n",
7e28adb2 677 __func__, i2c_adapter_id(i2c_props->adap),
f1f32849
MK
678 i2c_props->addr);
679 return 0;
680 }
1f3a4e32 681 return -ENODEV;
f1f32849
MK
682}
683
684static int tda8295_probe(struct tuner_i2c_props *i2c_props)
685{
686#define TDA8295_ID 0x8a
19f8a6c3 687#define TDA8295C2_ID 0x8b
567aba0b
MCC
688 u8 reg = 0x2f, id;
689 struct i2c_msg msg_read[] = {
89a8969a
JW
690 { .addr = i2c_props->addr, .flags = 0, .len = 1, .buf = &reg },
691 { .addr = i2c_props->addr, .flags = I2C_M_RD, .len = 1, .buf = &id },
567aba0b 692 };
f1f32849 693
89a8969a 694 /* detect tda8295 */
567aba0b 695 if (i2c_transfer(i2c_props->adap, msg_read, 2) != 2) {
89a8969a 696 printk(KERN_WARNING "%s: couldn't read register 0x%02x\n",
567aba0b
MCC
697 __func__, reg);
698 return -ENODEV;
699 }
f1f32849 700
567aba0b 701 if ((id & 0xfe) == TDA8295_ID) {
ab166050 702 if (debug)
19f8a6c3 703 printk(KERN_DEBUG "%s: %s detected @ %d-%04x\n",
567aba0b 704 __func__, (id == TDA8295_ID) ?
19f8a6c3
MK
705 "tda8295c1" : "tda8295c2",
706 i2c_adapter_id(i2c_props->adap),
f1f32849
MK
707 i2c_props->addr);
708 return 0;
709 }
710
1f3a4e32 711 return -ENODEV;
f1f32849
MK
712}
713
bd336e63 714static const struct analog_demod_ops tda8290_ops = {
c7919d52 715 .set_params = tda8290_set_params,
8c125f2c
MK
716 .has_signal = tda8290_has_signal,
717 .standby = tda8290_standby,
718 .release = tda829x_release,
719 .i2c_gate_ctrl = tda8290_i2c_bridge,
720};
721
bd336e63 722static const struct analog_demod_ops tda8295_ops = {
c7919d52 723 .set_params = tda8295_set_params,
8c125f2c
MK
724 .has_signal = tda8295_has_signal,
725 .standby = tda8295_standby,
726 .release = tda829x_release,
727 .i2c_gate_ctrl = tda8295_i2c_bridge,
728};
729
ab166050
MK
730struct dvb_frontend *tda829x_attach(struct dvb_frontend *fe,
731 struct i2c_adapter *i2c_adap, u8 i2c_addr,
732 struct tda829x_config *cfg)
5bea1cd3
MK
733{
734 struct tda8290_priv *priv = NULL;
ab166050 735 char *name;
8c125f2c 736
5bea1cd3
MK
737 priv = kzalloc(sizeof(struct tda8290_priv), GFP_KERNEL);
738 if (priv == NULL)
ab166050 739 return NULL;
8c125f2c 740 fe->analog_demod_priv = priv;
5bea1cd3 741
ab166050
MK
742 priv->i2c_props.addr = i2c_addr;
743 priv->i2c_props.adap = i2c_adap;
2756665c 744 priv->i2c_props.name = "tda829x";
5b0e5350
OZ
745 if (cfg) {
746 priv->cfg.config = cfg->lna_cfg;
747 priv->tda18271_std_map = cfg->tda18271_std_map;
748 }
5bea1cd3 749
f1f32849 750 if (tda8290_probe(&priv->i2c_props) == 0) {
8c125f2c 751 priv->ver = TDA8290;
bc3e5c7f
MK
752 memcpy(&fe->ops.analog_ops, &tda8290_ops,
753 sizeof(struct analog_demod_ops));
5bea1cd3 754 }
8c125f2c 755
f1f32849 756 if (tda8295_probe(&priv->i2c_props) == 0) {
8c125f2c 757 priv->ver = TDA8295;
bc3e5c7f
MK
758 memcpy(&fe->ops.analog_ops, &tda8295_ops,
759 sizeof(struct analog_demod_ops));
5bea1cd3 760 }
5bea1cd3 761
2f719f7a
OZ
762 if (cfg && cfg->no_i2c_gate)
763 fe->ops.analog_ops.i2c_gate_ctrl = NULL;
764
9d700a06
MCC
765 if (!(cfg) || (TDA829X_PROBE_TUNER == cfg->probe_tuner)) {
766 tda8295_power(fe, 1);
767 if (tda829x_find_tuner(fe) < 0)
768 goto fail;
47ab285a 769 }
5bea1cd3 770
ab166050 771 switch (priv->ver) {
c9076279
MK
772 case TDA8290:
773 name = "tda8290";
774 break;
775 case TDA8295:
776 name = "tda8295";
777 break;
ab166050
MK
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
0f2ce983
MK
801 fe->ops.analog_ops.info.name = name;
802
8c125f2c 803 if (priv->ver & TDA8290) {
439b72b6
MK
804 if (priv->ver & (TDA8275 | TDA8275A))
805 tda8290_init_tuner(fe);
8c125f2c
MK
806 tda8290_init_if(fe);
807 } else if (priv->ver & TDA8295)
808 tda8295_init_if(fe);
5bea1cd3 809
ab166050 810 return fe;
fa746aee
MK
811
812fail:
9d700a06
MCC
813 memset(&fe->ops.analog_ops, 0, sizeof(struct analog_demod_ops));
814
fa746aee 815 tda829x_release(fe);
ab166050 816 return NULL;
5bea1cd3 817}
8c125f2c 818EXPORT_SYMBOL_GPL(tda829x_attach);
1da177e4 819
ab166050 820int tda829x_probe(struct i2c_adapter *i2c_adap, u8 i2c_addr)
95736034 821{
910bb3e3 822 struct tuner_i2c_props i2c_props = {
ab166050
MK
823 .adap = i2c_adap,
824 .addr = i2c_addr,
910bb3e3 825 };
db8a6956 826
8ff230fb
AB
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;
95736034 832 unsigned char data;
a818e1c8
MK
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 */
567aba0b
MCC
838 tuner_i2c_xfer_send_recv(&i2c_props,
839 soft_reset, 1, buf, PROBE_BUFFER_SIZE);
a818e1c8 840 for (i = 1; i < PROBE_BUFFER_SIZE; i++) {
bbe1e0ba
MK
841 if (buf[i] != buf[0])
842 break;
a818e1c8
MK
843 }
844
845 /* all bytes are equal, not a tda829x - probably a tda9887 */
846 if (i == PROBE_BUFFER_SIZE)
847 return -ENODEV;
95736034 848
f1f32849
MK
849 if ((tda8290_probe(&i2c_props) == 0) ||
850 (tda8295_probe(&i2c_props) == 0))
851 return 0;
852
853 /* fall back to old probing method */
910bb3e3
MK
854 tuner_i2c_xfer_send(&i2c_props, easy_mode_b, 2);
855 tuner_i2c_xfer_send(&i2c_props, soft_reset, 2);
567aba0b 856 tuner_i2c_xfer_send_recv(&i2c_props, &addr_dto_lsb, 1, &data, 1);
95736034 857 if (data == 0) {
910bb3e3
MK
858 tuner_i2c_xfer_send(&i2c_props, easy_mode_g, 2);
859 tuner_i2c_xfer_send(&i2c_props, soft_reset, 2);
567aba0b
MCC
860 tuner_i2c_xfer_send_recv(&i2c_props,
861 &addr_dto_lsb, 1, &data, 1);
95736034
HH
862 if (data == 0x7b) {
863 return 0;
864 }
865 }
910bb3e3 866 tuner_i2c_xfer_send(&i2c_props, restore_9886, 3);
1f3a4e32 867 return -ENODEV;
95736034 868}
f1f32849 869EXPORT_SYMBOL_GPL(tda829x_probe);
910bb3e3 870
8c125f2c 871MODULE_DESCRIPTION("Philips/NXP TDA8290/TDA8295 analog IF demodulator driver");
5bea1cd3 872MODULE_AUTHOR("Gerd Knorr, Hartmut Hackmann, Michael Krufky");
910bb3e3 873MODULE_LICENSE("GPL");