V4L/DVB (6006): tuner: move last_div to tuner-simple private data
[wrt350n-kernel.git] / drivers / media / video / tda8290.c
blob59cff5a3c59e106fb47cf243160ccdf62b2cafe3
1 /*
3 i2c tv tuner chip device driver
4 controls the philips tda8290+75 tuner chip combo.
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.
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.
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.
21 #include <linux/i2c.h>
22 #include <linux/videodev.h>
23 #include <linux/delay.h>
24 #include "tuner-driver.h"
26 /* ---------------------------------------------------------------------- */
28 struct tda8290_priv {
29 unsigned char tda8290_easy_mode;
30 unsigned char tda827x_lpsel;
31 unsigned char tda827x_addr;
32 unsigned char tda827x_ver;
33 unsigned int sgIF;
36 /* ---------------------------------------------------------------------- */
38 struct tda827x_data {
39 u32 lomax;
40 u8 spd;
41 u8 bs;
42 u8 bp;
43 u8 cp;
44 u8 gc3;
45 u8 div1p5;
48 /* Note lomax entry is lo / 62500 */
50 static struct tda827x_data tda827x_analog[] = {
51 { .lomax = 992, .spd = 3, .bs = 2, .bp = 0, .cp = 0, .gc3 = 3, .div1p5 = 1}, /* 62 MHz */
52 { .lomax = 1056, .spd = 3, .bs = 3, .bp = 0, .cp = 0, .gc3 = 3, .div1p5 = 1}, /* 66 MHz */
53 { .lomax = 1216, .spd = 3, .bs = 1, .bp = 0, .cp = 0, .gc3 = 3, .div1p5 = 0}, /* 76 MHz */
54 { .lomax = 1344, .spd = 3, .bs = 2, .bp = 0, .cp = 0, .gc3 = 3, .div1p5 = 0}, /* 84 MHz */
55 { .lomax = 1488, .spd = 3, .bs = 2, .bp = 0, .cp = 0, .gc3 = 1, .div1p5 = 0}, /* 93 MHz */
56 { .lomax = 1568, .spd = 3, .bs = 3, .bp = 0, .cp = 0, .gc3 = 1, .div1p5 = 0}, /* 98 MHz */
57 { .lomax = 1744, .spd = 3, .bs = 3, .bp = 1, .cp = 0, .gc3 = 1, .div1p5 = 0}, /* 109 MHz */
58 { .lomax = 1968, .spd = 2, .bs = 2, .bp = 1, .cp = 0, .gc3 = 1, .div1p5 = 1}, /* 123 MHz */
59 { .lomax = 2128, .spd = 2, .bs = 3, .bp = 1, .cp = 0, .gc3 = 1, .div1p5 = 1}, /* 133 MHz */
60 { .lomax = 2416, .spd = 2, .bs = 1, .bp = 1, .cp = 0, .gc3 = 1, .div1p5 = 0}, /* 151 MHz */
61 { .lomax = 2464, .spd = 2, .bs = 2, .bp = 1, .cp = 0, .gc3 = 1, .div1p5 = 0}, /* 154 MHz */
62 { .lomax = 2896, .spd = 2, .bs = 2, .bp = 1, .cp = 0, .gc3 = 0, .div1p5 = 0}, /* 181 MHz */
63 { .lomax = 2960, .spd = 2, .bs = 2, .bp = 2, .cp = 0, .gc3 = 1, .div1p5 = 0}, /* 185 MHz */
64 { .lomax = 3472, .spd = 2, .bs = 3, .bp = 2, .cp = 0, .gc3 = 1, .div1p5 = 0}, /* 217 MHz */
65 { .lomax = 3904, .spd = 1, .bs = 2, .bp = 2, .cp = 0, .gc3 = 1, .div1p5 = 1}, /* 244 MHz */
66 { .lomax = 4240, .spd = 1, .bs = 3, .bp = 2, .cp = 0, .gc3 = 1, .div1p5 = 1}, /* 265 MHz */
67 { .lomax = 4832, .spd = 1, .bs = 1, .bp = 2, .cp = 0, .gc3 = 1, .div1p5 = 0}, /* 302 MHz */
68 { .lomax = 5184, .spd = 1, .bs = 2, .bp = 2, .cp = 0, .gc3 = 1, .div1p5 = 0}, /* 324 MHz */
69 { .lomax = 5920, .spd = 1, .bs = 2, .bp = 3, .cp = 0, .gc3 = 1, .div1p5 = 0}, /* 370 MHz */
70 { .lomax = 7264, .spd = 1, .bs = 3, .bp = 3, .cp = 0, .gc3 = 1, .div1p5 = 0}, /* 454 MHz */
71 { .lomax = 7888, .spd = 0, .bs = 2, .bp = 3, .cp = 0, .gc3 = 1, .div1p5 = 1}, /* 493 MHz */
72 { .lomax = 8480, .spd = 0, .bs = 3, .bp = 3, .cp = 0, .gc3 = 1, .div1p5 = 1}, /* 530 MHz */
73 { .lomax = 8864, .spd = 0, .bs = 1, .bp = 3, .cp = 0, .gc3 = 1, .div1p5 = 0}, /* 554 MHz */
74 { .lomax = 9664, .spd = 0, .bs = 1, .bp = 4, .cp = 0, .gc3 = 0, .div1p5 = 0}, /* 604 MHz */
75 { .lomax = 11088, .spd = 0, .bs = 2, .bp = 4, .cp = 0, .gc3 = 0, .div1p5 = 0}, /* 696 MHz */
76 { .lomax = 11840, .spd = 0, .bs = 2, .bp = 4, .cp = 1, .gc3 = 0, .div1p5 = 0}, /* 740 MHz */
77 { .lomax = 13120, .spd = 0, .bs = 3, .bp = 4, .cp = 0, .gc3 = 0, .div1p5 = 0}, /* 820 MHz */
78 { .lomax = 13840, .spd = 0, .bs = 3, .bp = 4, .cp = 1, .gc3 = 0, .div1p5 = 0}, /* 865 MHz */
79 { .lomax = 0, .spd = 0, .bs = 0, .bp = 0, .cp = 0, .gc3 = 0, .div1p5 = 0} /* End */
82 static void tda827x_tune(struct i2c_client *c, u16 ifc, unsigned int freq)
84 unsigned char tuner_reg[8];
85 unsigned char reg2[2];
86 u32 N;
87 int i;
88 struct tuner *t = i2c_get_clientdata(c);
89 struct tda8290_priv *priv = t->priv;
90 struct i2c_msg msg = {.addr = priv->tda827x_addr, .flags = 0};
92 if (t->mode == V4L2_TUNER_RADIO)
93 freq = freq / 1000;
95 N = freq + ifc;
96 i = 0;
97 while (tda827x_analog[i].lomax < N) {
98 if(tda827x_analog[i + 1].lomax == 0)
99 break;
100 i++;
103 N = N << tda827x_analog[i].spd;
105 tuner_reg[0] = 0;
106 tuner_reg[1] = (unsigned char)(N>>8);
107 tuner_reg[2] = (unsigned char) N;
108 tuner_reg[3] = 0x40;
109 tuner_reg[4] = 0x52 + (priv->tda827x_lpsel << 5);
110 tuner_reg[5] = (tda827x_analog[i].spd << 6) + (tda827x_analog[i].div1p5 <<5) +
111 (tda827x_analog[i].bs <<3) + tda827x_analog[i].bp;
112 tuner_reg[6] = 0x8f + (tda827x_analog[i].gc3 << 4);
113 tuner_reg[7] = 0x8f;
115 msg.buf = tuner_reg;
116 msg.len = 8;
117 i2c_transfer(c->adapter, &msg, 1);
119 msg.buf= reg2;
120 msg.len = 2;
121 reg2[0] = 0x80;
122 reg2[1] = 0;
123 i2c_transfer(c->adapter, &msg, 1);
125 reg2[0] = 0x60;
126 reg2[1] = 0xbf;
127 i2c_transfer(c->adapter, &msg, 1);
129 reg2[0] = 0x30;
130 reg2[1] = tuner_reg[4] + 0x80;
131 i2c_transfer(c->adapter, &msg, 1);
133 msleep(1);
134 reg2[0] = 0x30;
135 reg2[1] = tuner_reg[4] + 4;
136 i2c_transfer(c->adapter, &msg, 1);
138 msleep(1);
139 reg2[0] = 0x30;
140 reg2[1] = tuner_reg[4];
141 i2c_transfer(c->adapter, &msg, 1);
143 msleep(550);
144 reg2[0] = 0x30;
145 reg2[1] = (tuner_reg[4] & 0xfc) + tda827x_analog[i].cp ;
146 i2c_transfer(c->adapter, &msg, 1);
148 reg2[0] = 0x60;
149 reg2[1] = 0x3f;
150 i2c_transfer(c->adapter, &msg, 1);
152 reg2[0] = 0x80;
153 reg2[1] = 0x08; // Vsync en
154 i2c_transfer(c->adapter, &msg, 1);
157 static void tda827x_agcf(struct i2c_client *c)
159 struct tuner *t = i2c_get_clientdata(c);
160 struct tda8290_priv *priv = t->priv;
161 unsigned char data[] = {0x80, 0x0c};
162 struct i2c_msg msg = {.addr = priv->tda827x_addr, .buf = data,
163 .flags = 0, .len = 2};
164 i2c_transfer(c->adapter, &msg, 1);
167 /* ---------------------------------------------------------------------- */
169 struct tda827xa_data {
170 u32 lomax;
171 u8 svco;
172 u8 spd;
173 u8 scr;
174 u8 sbs;
175 u8 gc3;
178 static struct tda827xa_data tda827xa_analog[] = {
179 { .lomax = 910, .svco = 3, .spd = 4, .scr = 0, .sbs = 0, .gc3 = 3}, /* 56.875 MHz */
180 { .lomax = 1076, .svco = 0, .spd = 3, .scr = 0, .sbs = 0, .gc3 = 3}, /* 67.25 MHz */
181 { .lomax = 1300, .svco = 1, .spd = 3, .scr = 0, .sbs = 0, .gc3 = 3}, /* 81.25 MHz */
182 { .lomax = 1560, .svco = 2, .spd = 3, .scr = 0, .sbs = 0, .gc3 = 3}, /* 97.5 MHz */
183 { .lomax = 1820, .svco = 3, .spd = 3, .scr = 0, .sbs = 1, .gc3 = 1}, /* 113.75 MHz */
184 { .lomax = 2152, .svco = 0, .spd = 2, .scr = 0, .sbs = 1, .gc3 = 1}, /* 134.5 MHz */
185 { .lomax = 2464, .svco = 1, .spd = 2, .scr = 0, .sbs = 1, .gc3 = 1}, /* 154 MHz */
186 { .lomax = 2600, .svco = 1, .spd = 2, .scr = 0, .sbs = 1, .gc3 = 1}, /* 162.5 MHz */
187 { .lomax = 2928, .svco = 2, .spd = 2, .scr = 0, .sbs = 1, .gc3 = 1}, /* 183 MHz */
188 { .lomax = 3120, .svco = 2, .spd = 2, .scr = 0, .sbs = 2, .gc3 = 1}, /* 195 MHz */
189 { .lomax = 3640, .svco = 3, .spd = 2, .scr = 0, .sbs = 2, .gc3 = 3}, /* 227.5 MHz */
190 { .lomax = 4304, .svco = 0, .spd = 1, .scr = 0, .sbs = 2, .gc3 = 3}, /* 269 MHz */
191 { .lomax = 5200, .svco = 1, .spd = 1, .scr = 0, .sbs = 2, .gc3 = 1}, /* 325 MHz */
192 { .lomax = 6240, .svco = 2, .spd = 1, .scr = 0, .sbs = 3, .gc3 = 3}, /* 390 MHz */
193 { .lomax = 7280, .svco = 3, .spd = 1, .scr = 0, .sbs = 3, .gc3 = 3}, /* 455 MHz */
194 { .lomax = 8320, .svco = 0, .spd = 0, .scr = 0, .sbs = 3, .gc3 = 1}, /* 520 MHz */
195 { .lomax = 8608, .svco = 0, .spd = 0, .scr = 1, .sbs = 3, .gc3 = 1}, /* 538 MHz */
196 { .lomax = 8864, .svco = 1, .spd = 0, .scr = 0, .sbs = 3, .gc3 = 1}, /* 554 MHz */
197 { .lomax = 9920, .svco = 1, .spd = 0, .scr = 0, .sbs = 4, .gc3 = 0}, /* 620 MHz */
198 { .lomax = 10400, .svco = 1, .spd = 0, .scr = 1, .sbs = 4, .gc3 = 0}, /* 650 MHz */
199 { .lomax = 11200, .svco = 2, .spd = 0, .scr = 0, .sbs = 4, .gc3 = 0}, /* 700 MHz */
200 { .lomax = 12480, .svco = 2, .spd = 0, .scr = 1, .sbs = 4, .gc3 = 0}, /* 780 MHz */
201 { .lomax = 13120, .svco = 3, .spd = 0, .scr = 0, .sbs = 4, .gc3 = 0}, /* 820 MHz */
202 { .lomax = 13920, .svco = 3, .spd = 0, .scr = 1, .sbs = 4, .gc3 = 0}, /* 870 MHz */
203 { .lomax = 14576, .svco = 3, .spd = 0, .scr = 2, .sbs = 4, .gc3 = 0}, /* 911 MHz */
204 { .lomax = 0, .svco = 0, .spd = 0, .scr = 0, .sbs = 0, .gc3 = 0} /* End */
207 static void tda827xa_lna_gain(struct i2c_client *c, int high)
209 struct tuner *t = i2c_get_clientdata(c);
210 unsigned char buf[] = {0x22, 0x01};
211 int arg;
212 struct i2c_msg msg = {.addr = c->addr, .flags = 0, .buf = buf, .len = sizeof(buf)};
213 if (t->config) {
214 if (high)
215 tuner_dbg("setting LNA to high gain\n");
216 else
217 tuner_dbg("setting LNA to low gain\n");
219 switch (t->config) {
220 case 0: /* no LNA */
221 break;
222 case 1: /* switch is GPIO 0 of tda8290 */
223 case 2:
224 /* turn Vsync on */
225 if (t->std & V4L2_STD_MN)
226 arg = 1;
227 else
228 arg = 0;
229 if (t->tuner_callback)
230 t->tuner_callback(c->adapter->algo_data, 1, arg);
231 buf[1] = high ? 0 : 1;
232 if (t->config == 2)
233 buf[1] = high ? 1 : 0;
234 i2c_transfer(c->adapter, &msg, 1);
235 break;
236 case 3: /* switch with GPIO of saa713x */
237 if (t->tuner_callback)
238 t->tuner_callback(c->adapter->algo_data, 0, high);
239 break;
243 static void tda827xa_tune(struct i2c_client *c, u16 ifc, unsigned int freq)
245 unsigned char tuner_reg[11];
246 u32 N;
247 int i;
248 struct tuner *t = i2c_get_clientdata(c);
249 struct tda8290_priv *priv = t->priv;
250 struct i2c_msg msg = {.addr = priv->tda827x_addr, .flags = 0, .buf = tuner_reg};
252 tda827xa_lna_gain( c, 1);
253 msleep(10);
255 if (t->mode == V4L2_TUNER_RADIO)
256 freq = freq / 1000;
258 N = freq + ifc;
259 i = 0;
260 while (tda827xa_analog[i].lomax < N) {
261 if(tda827xa_analog[i + 1].lomax == 0)
262 break;
263 i++;
266 N = N << tda827xa_analog[i].spd;
268 tuner_reg[0] = 0;
269 tuner_reg[1] = (unsigned char)(N>>8);
270 tuner_reg[2] = (unsigned char) N;
271 tuner_reg[3] = 0;
272 tuner_reg[4] = 0x16;
273 tuner_reg[5] = (tda827xa_analog[i].spd << 5) + (tda827xa_analog[i].svco << 3) +
274 tda827xa_analog[i].sbs;
275 tuner_reg[6] = 0x8b + (tda827xa_analog[i].gc3 << 4);
276 tuner_reg[7] = 0x1c;
277 tuner_reg[8] = 4;
278 tuner_reg[9] = 0x20;
279 tuner_reg[10] = 0x00;
280 msg.len = 11;
281 i2c_transfer(c->adapter, &msg, 1);
283 tuner_reg[0] = 0x90;
284 tuner_reg[1] = 0xff;
285 tuner_reg[2] = 0xe0;
286 tuner_reg[3] = 0;
287 tuner_reg[4] = 0x99 + (priv->tda827x_lpsel << 1);
288 msg.len = 5;
289 i2c_transfer(c->adapter, &msg, 1);
291 tuner_reg[0] = 0xa0;
292 tuner_reg[1] = 0xc0;
293 msg.len = 2;
294 i2c_transfer(c->adapter, &msg, 1);
296 tuner_reg[0] = 0x30;
297 tuner_reg[1] = 0x10 + tda827xa_analog[i].scr;
298 i2c_transfer(c->adapter, &msg, 1);
300 msg.flags = I2C_M_RD;
301 i2c_transfer(c->adapter, &msg, 1);
302 msg.flags = 0;
303 tuner_reg[1] >>= 4;
304 tuner_dbg("AGC2 gain is: %d\n", tuner_reg[1]);
305 if (tuner_reg[1] < 1)
306 tda827xa_lna_gain( c, 0);
308 msleep(100);
309 tuner_reg[0] = 0x60;
310 tuner_reg[1] = 0x3c;
311 i2c_transfer(c->adapter, &msg, 1);
313 msleep(163);
314 tuner_reg[0] = 0x50;
315 tuner_reg[1] = 0x8f + (tda827xa_analog[i].gc3 << 4);
316 i2c_transfer(c->adapter, &msg, 1);
318 tuner_reg[0] = 0x80;
319 tuner_reg[1] = 0x28;
320 i2c_transfer(c->adapter, &msg, 1);
322 tuner_reg[0] = 0xb0;
323 tuner_reg[1] = 0x01;
324 i2c_transfer(c->adapter, &msg, 1);
326 tuner_reg[0] = 0xc0;
327 tuner_reg[1] = 0x19 + (priv->tda827x_lpsel << 1);
328 i2c_transfer(c->adapter, &msg, 1);
331 static void tda827xa_agcf(struct i2c_client *c)
333 struct tuner *t = i2c_get_clientdata(c);
334 struct tda8290_priv *priv = t->priv;
335 unsigned char data[] = {0x80, 0x2c};
336 struct i2c_msg msg = {.addr = priv->tda827x_addr, .buf = data,
337 .flags = 0, .len = 2};
338 i2c_transfer(c->adapter, &msg, 1);
341 /*---------------------------------------------------------------------*/
343 static void tda8290_i2c_bridge(struct i2c_client *c, int close)
345 unsigned char enable[2] = { 0x21, 0xC0 };
346 unsigned char disable[2] = { 0x21, 0x00 };
347 unsigned char *msg;
348 if(close) {
349 msg = enable;
350 i2c_master_send(c, msg, 2);
351 /* let the bridge stabilize */
352 msleep(20);
353 } else {
354 msg = disable;
355 i2c_master_send(c, msg, 2);
359 /*---------------------------------------------------------------------*/
361 static int tda8290_tune(struct i2c_client *c, u16 ifc, unsigned int freq)
363 struct tuner *t = i2c_get_clientdata(c);
364 struct tda8290_priv *priv = t->priv;
365 unsigned char soft_reset[] = { 0x00, 0x00 };
366 unsigned char easy_mode[] = { 0x01, priv->tda8290_easy_mode };
367 unsigned char expert_mode[] = { 0x01, 0x80 };
368 unsigned char agc_out_on[] = { 0x02, 0x00 };
369 unsigned char gainset_off[] = { 0x28, 0x14 };
370 unsigned char if_agc_spd[] = { 0x0f, 0x88 };
371 unsigned char adc_head_6[] = { 0x05, 0x04 };
372 unsigned char adc_head_9[] = { 0x05, 0x02 };
373 unsigned char adc_head_12[] = { 0x05, 0x01 };
374 unsigned char pll_bw_nom[] = { 0x0d, 0x47 };
375 unsigned char pll_bw_low[] = { 0x0d, 0x27 };
376 unsigned char gainset_2[] = { 0x28, 0x64 };
377 unsigned char agc_rst_on[] = { 0x0e, 0x0b };
378 unsigned char agc_rst_off[] = { 0x0e, 0x09 };
379 unsigned char if_agc_set[] = { 0x0f, 0x81 };
380 unsigned char addr_adc_sat = 0x1a;
381 unsigned char addr_agc_stat = 0x1d;
382 unsigned char addr_pll_stat = 0x1b;
383 unsigned char adc_sat, agc_stat,
384 pll_stat;
385 int i;
387 tuner_dbg("tda827xa config is 0x%02x\n", t->config);
388 i2c_master_send(c, easy_mode, 2);
389 i2c_master_send(c, agc_out_on, 2);
390 i2c_master_send(c, soft_reset, 2);
391 msleep(1);
393 expert_mode[1] = priv->tda8290_easy_mode + 0x80;
394 i2c_master_send(c, expert_mode, 2);
395 i2c_master_send(c, gainset_off, 2);
396 i2c_master_send(c, if_agc_spd, 2);
397 if (priv->tda8290_easy_mode & 0x60)
398 i2c_master_send(c, adc_head_9, 2);
399 else
400 i2c_master_send(c, adc_head_6, 2);
401 i2c_master_send(c, pll_bw_nom, 2);
403 tda8290_i2c_bridge(c, 1);
404 if (priv->tda827x_ver != 0)
405 tda827xa_tune(c, ifc, freq);
406 else
407 tda827x_tune(c, ifc, freq);
408 for (i = 0; i < 3; i++) {
409 i2c_master_send(c, &addr_pll_stat, 1);
410 i2c_master_recv(c, &pll_stat, 1);
411 if (pll_stat & 0x80) {
412 i2c_master_send(c, &addr_adc_sat, 1);
413 i2c_master_recv(c, &adc_sat, 1);
414 i2c_master_send(c, &addr_agc_stat, 1);
415 i2c_master_recv(c, &agc_stat, 1);
416 tuner_dbg("tda8290 is locked, AGC: %d\n", agc_stat);
417 break;
418 } else {
419 tuner_dbg("tda8290 not locked, no signal?\n");
420 msleep(100);
423 /* adjust headroom resp. gain */
424 if ((agc_stat > 115) || (!(pll_stat & 0x80) && (adc_sat < 20))) {
425 tuner_dbg("adjust gain, step 1. Agc: %d, ADC stat: %d, lock: %d\n",
426 agc_stat, adc_sat, pll_stat & 0x80);
427 i2c_master_send(c, gainset_2, 2);
428 msleep(100);
429 i2c_master_send(c, &addr_agc_stat, 1);
430 i2c_master_recv(c, &agc_stat, 1);
431 i2c_master_send(c, &addr_pll_stat, 1);
432 i2c_master_recv(c, &pll_stat, 1);
433 if ((agc_stat > 115) || !(pll_stat & 0x80)) {
434 tuner_dbg("adjust gain, step 2. Agc: %d, lock: %d\n",
435 agc_stat, pll_stat & 0x80);
436 if (priv->tda827x_ver != 0)
437 tda827xa_agcf(c);
438 else
439 tda827x_agcf(c);
440 msleep(100);
441 i2c_master_send(c, &addr_agc_stat, 1);
442 i2c_master_recv(c, &agc_stat, 1);
443 i2c_master_send(c, &addr_pll_stat, 1);
444 i2c_master_recv(c, &pll_stat, 1);
445 if((agc_stat > 115) || !(pll_stat & 0x80)) {
446 tuner_dbg("adjust gain, step 3. Agc: %d\n", agc_stat);
447 i2c_master_send(c, adc_head_12, 2);
448 i2c_master_send(c, pll_bw_low, 2);
449 msleep(100);
454 /* l/ l' deadlock? */
455 if(priv->tda8290_easy_mode & 0x60) {
456 i2c_master_send(c, &addr_adc_sat, 1);
457 i2c_master_recv(c, &adc_sat, 1);
458 i2c_master_send(c, &addr_pll_stat, 1);
459 i2c_master_recv(c, &pll_stat, 1);
460 if ((adc_sat > 20) || !(pll_stat & 0x80)) {
461 tuner_dbg("trying to resolve SECAM L deadlock\n");
462 i2c_master_send(c, agc_rst_on, 2);
463 msleep(40);
464 i2c_master_send(c, agc_rst_off, 2);
468 tda8290_i2c_bridge(c, 0);
469 i2c_master_send(c, if_agc_set, 2);
470 return 0;
473 /*---------------------------------------------------------------------*/
475 static void set_audio(struct tuner *t)
477 struct tda8290_priv *priv = t->priv;
478 char* mode;
480 priv->tda827x_lpsel = 0;
481 if (t->std & V4L2_STD_MN) {
482 priv->sgIF = 92;
483 priv->tda8290_easy_mode = 0x01;
484 priv->tda827x_lpsel = 1;
485 mode = "MN";
486 } else if (t->std & V4L2_STD_B) {
487 priv->sgIF = 108;
488 priv->tda8290_easy_mode = 0x02;
489 mode = "B";
490 } else if (t->std & V4L2_STD_GH) {
491 priv->sgIF = 124;
492 priv->tda8290_easy_mode = 0x04;
493 mode = "GH";
494 } else if (t->std & V4L2_STD_PAL_I) {
495 priv->sgIF = 124;
496 priv->tda8290_easy_mode = 0x08;
497 mode = "I";
498 } else if (t->std & V4L2_STD_DK) {
499 priv->sgIF = 124;
500 priv->tda8290_easy_mode = 0x10;
501 mode = "DK";
502 } else if (t->std & V4L2_STD_SECAM_L) {
503 priv->sgIF = 124;
504 priv->tda8290_easy_mode = 0x20;
505 mode = "L";
506 } else if (t->std & V4L2_STD_SECAM_LC) {
507 priv->sgIF = 20;
508 priv->tda8290_easy_mode = 0x40;
509 mode = "LC";
510 } else {
511 priv->sgIF = 124;
512 priv->tda8290_easy_mode = 0x10;
513 mode = "xx";
515 tuner_dbg("setting tda8290 to system %s\n", mode);
518 static void set_tv_freq(struct i2c_client *c, unsigned int freq)
520 struct tuner *t = i2c_get_clientdata(c);
521 struct tda8290_priv *priv = t->priv;
523 set_audio(t);
524 tda8290_tune(c, priv->sgIF, freq);
527 static void set_radio_freq(struct i2c_client *c, unsigned int freq)
529 /* if frequency is 5.5 MHz */
530 tda8290_tune(c, 88, freq);
533 static int has_signal(struct i2c_client *c)
535 unsigned char i2c_get_afc[1] = { 0x1B };
536 unsigned char afc = 0;
538 i2c_master_send(c, i2c_get_afc, ARRAY_SIZE(i2c_get_afc));
539 i2c_master_recv(c, &afc, 1);
540 return (afc & 0x80)? 65535:0;
543 /*---------------------------------------------------------------------*/
545 static void standby(struct i2c_client *c)
547 struct tuner *t = i2c_get_clientdata(c);
548 struct tda8290_priv *priv = t->priv;
549 unsigned char cb1[] = { 0x30, 0xD0 };
550 unsigned char tda8290_standby[] = { 0x00, 0x02 };
551 unsigned char tda8290_agc_tri[] = { 0x02, 0x20 };
552 struct i2c_msg msg = {.addr = priv->tda827x_addr, .flags=0, .buf=cb1, .len = 2};
554 tda8290_i2c_bridge(c, 1);
555 if (priv->tda827x_ver != 0)
556 cb1[1] = 0x90;
557 i2c_transfer(c->adapter, &msg, 1);
558 tda8290_i2c_bridge(c, 0);
559 i2c_master_send(c, tda8290_agc_tri, 2);
560 i2c_master_send(c, tda8290_standby, 2);
564 static void tda8290_init_if(struct i2c_client *c)
566 struct tuner *t = i2c_get_clientdata(c);
567 unsigned char set_VS[] = { 0x30, 0x6F };
568 unsigned char set_GP00_CF[] = { 0x20, 0x01 };
569 unsigned char set_GP01_CF[] = { 0x20, 0x0B };
571 if ((t->config == 1) || (t->config == 2))
572 i2c_master_send(c, set_GP00_CF, 2);
573 else
574 i2c_master_send(c, set_GP01_CF, 2);
575 i2c_master_send(c, set_VS, 2);
578 static void tda8290_init_tuner(struct i2c_client *c)
580 struct tuner *t = i2c_get_clientdata(c);
581 struct tda8290_priv *priv = t->priv;
582 unsigned char tda8275_init[] = { 0x00, 0x00, 0x00, 0x40, 0xdC, 0x04, 0xAf,
583 0x3F, 0x2A, 0x04, 0xFF, 0x00, 0x00, 0x40 };
584 unsigned char tda8275a_init[] = { 0x00, 0x00, 0x00, 0x00, 0xdC, 0x05, 0x8b,
585 0x0c, 0x04, 0x20, 0xFF, 0x00, 0x00, 0x4b };
586 struct i2c_msg msg = {.addr = priv->tda827x_addr, .flags=0,
587 .buf=tda8275_init, .len = 14};
588 if (priv->tda827x_ver != 0)
589 msg.buf = tda8275a_init;
591 tda8290_i2c_bridge(c, 1);
592 i2c_transfer(c->adapter, &msg, 1);
593 tda8290_i2c_bridge(c, 0);
596 /*---------------------------------------------------------------------*/
598 static void tda8290_release(struct i2c_client *c)
600 struct tuner *t = i2c_get_clientdata(c);
602 kfree(t->priv);
603 t->priv = NULL;
606 static struct tuner_operations tda8290_tuner_ops = {
607 .set_tv_freq = set_tv_freq,
608 .set_radio_freq = set_radio_freq,
609 .has_signal = has_signal,
610 .standby = standby,
611 .release = tda8290_release,
614 int tda8290_init(struct i2c_client *c)
616 struct tda8290_priv *priv = NULL;
617 struct tuner *t = i2c_get_clientdata(c);
618 u8 data;
619 int i, ret, tuners_found;
620 u32 tuner_addrs;
621 struct i2c_msg msg = {.flags=I2C_M_RD, .buf=&data, .len = 1};
623 priv = kzalloc(sizeof(struct tda8290_priv), GFP_KERNEL);
624 if (priv == NULL)
625 return -ENOMEM;
626 t->priv = priv;
628 tda8290_i2c_bridge(c, 1);
629 /* probe for tuner chip */
630 tuners_found = 0;
631 tuner_addrs = 0;
632 for (i=0x60; i<= 0x63; i++) {
633 msg.addr = i;
634 ret = i2c_transfer(c->adapter, &msg, 1);
635 if (ret == 1) {
636 tuners_found++;
637 tuner_addrs = (tuner_addrs << 8) + i;
640 /* if there is more than one tuner, we expect the right one is
641 behind the bridge and we choose the highest address that doesn't
642 give a response now
644 tda8290_i2c_bridge(c, 0);
645 if(tuners_found > 1)
646 for (i = 0; i < tuners_found; i++) {
647 msg.addr = tuner_addrs & 0xff;
648 ret = i2c_transfer(c->adapter, &msg, 1);
649 if(ret == 1)
650 tuner_addrs = tuner_addrs >> 8;
651 else
652 break;
654 if (tuner_addrs == 0) {
655 tuner_addrs = 0x61;
656 tuner_info ("could not clearly identify tuner address, defaulting to %x\n",
657 tuner_addrs);
658 } else {
659 tuner_addrs = tuner_addrs & 0xff;
660 tuner_info ("setting tuner address to %x\n", tuner_addrs);
662 priv->tda827x_addr = tuner_addrs;
663 msg.addr = tuner_addrs;
665 tda8290_i2c_bridge(c, 1);
666 ret = i2c_transfer(c->adapter, &msg, 1);
667 if( ret != 1)
668 tuner_warn ("TDA827x access failed!\n");
669 if ((data & 0x3c) == 0) {
670 strlcpy(c->name, "tda8290+75", sizeof(c->name));
671 priv->tda827x_ver = 0;
672 } else {
673 strlcpy(c->name, "tda8290+75a", sizeof(c->name));
674 priv->tda827x_ver = 2;
676 tuner_info("type set to %s\n", c->name);
678 memcpy(&t->ops, &tda8290_tuner_ops, sizeof(struct tuner_operations));
680 priv->tda827x_lpsel = 0;
681 t->mode = V4L2_TUNER_ANALOG_TV;
683 tda8290_init_tuner(c);
684 tda8290_init_if(c);
685 return 0;
688 int tda8290_probe(struct i2c_client *c)
690 unsigned char soft_reset[] = { 0x00, 0x00 };
691 unsigned char easy_mode_b[] = { 0x01, 0x02 };
692 unsigned char easy_mode_g[] = { 0x01, 0x04 };
693 unsigned char restore_9886[] = { 0x00, 0xd6, 0x30 };
694 unsigned char addr_dto_lsb = 0x07;
695 unsigned char data;
697 i2c_master_send(c, easy_mode_b, 2);
698 i2c_master_send(c, soft_reset, 2);
699 i2c_master_send(c, &addr_dto_lsb, 1);
700 i2c_master_recv(c, &data, 1);
701 if (data == 0) {
702 i2c_master_send(c, easy_mode_g, 2);
703 i2c_master_send(c, soft_reset, 2);
704 i2c_master_send(c, &addr_dto_lsb, 1);
705 i2c_master_recv(c, &data, 1);
706 if (data == 0x7b) {
707 return 0;
710 i2c_master_send(c, restore_9886, 3);
711 return -1;
715 * Overrides for Emacs so that we follow Linus's tabbing style.
716 * ---------------------------------------------------------------------------
717 * Local variables:
718 * c-basic-offset: 8
719 * End: