blob: 2b954b3d2ba65a81981a642b8bffdd554d889345 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -08002
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/i2c.h>
22#include <linux/videodev.h>
23#include <linux/delay.h>
24#include <media/tuner.h>
25
26/* ---------------------------------------------------------------------- */
27
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -080028struct tda827x_data {
29 u32 lomax;
30 u8 spd;
31 u8 bs;
32 u8 bp;
33 u8 cp;
34 u8 gc3;
35 u8 div1p5;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036};
37
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -080038 /* Note lomax entry is lo / 62500 */
39
40static struct tda827x_data tda827x_analog[] = {
41 { .lomax = 992, .spd = 3, .bs = 2, .bp = 0, .cp = 0, .gc3 = 3, .div1p5 = 1}, /* 62 MHz */
42 { .lomax = 1056, .spd = 3, .bs = 3, .bp = 0, .cp = 0, .gc3 = 3, .div1p5 = 1}, /* 66 MHz */
43 { .lomax = 1216, .spd = 3, .bs = 1, .bp = 0, .cp = 0, .gc3 = 3, .div1p5 = 0}, /* 76 MHz */
44 { .lomax = 1344, .spd = 3, .bs = 2, .bp = 0, .cp = 0, .gc3 = 3, .div1p5 = 0}, /* 84 MHz */
45 { .lomax = 1488, .spd = 3, .bs = 2, .bp = 0, .cp = 0, .gc3 = 1, .div1p5 = 0}, /* 93 MHz */
46 { .lomax = 1568, .spd = 3, .bs = 3, .bp = 0, .cp = 0, .gc3 = 1, .div1p5 = 0}, /* 98 MHz */
47 { .lomax = 1744, .spd = 3, .bs = 3, .bp = 1, .cp = 0, .gc3 = 1, .div1p5 = 0}, /* 109 MHz */
48 { .lomax = 1968, .spd = 2, .bs = 2, .bp = 1, .cp = 0, .gc3 = 1, .div1p5 = 1}, /* 123 MHz */
49 { .lomax = 2128, .spd = 2, .bs = 3, .bp = 1, .cp = 0, .gc3 = 1, .div1p5 = 1}, /* 133 MHz */
50 { .lomax = 2416, .spd = 2, .bs = 1, .bp = 1, .cp = 0, .gc3 = 1, .div1p5 = 0}, /* 151 MHz */
51 { .lomax = 2464, .spd = 2, .bs = 2, .bp = 1, .cp = 0, .gc3 = 1, .div1p5 = 0}, /* 154 MHz */
52 { .lomax = 2896, .spd = 2, .bs = 2, .bp = 1, .cp = 0, .gc3 = 0, .div1p5 = 0}, /* 181 MHz */
53 { .lomax = 2960, .spd = 2, .bs = 2, .bp = 2, .cp = 0, .gc3 = 1, .div1p5 = 0}, /* 185 MHz */
54 { .lomax = 3472, .spd = 2, .bs = 3, .bp = 2, .cp = 0, .gc3 = 1, .div1p5 = 0}, /* 217 MHz */
55 { .lomax = 3904, .spd = 1, .bs = 2, .bp = 2, .cp = 0, .gc3 = 1, .div1p5 = 1}, /* 244 MHz */
56 { .lomax = 4240, .spd = 1, .bs = 3, .bp = 2, .cp = 0, .gc3 = 1, .div1p5 = 1}, /* 265 MHz */
57 { .lomax = 4832, .spd = 1, .bs = 1, .bp = 2, .cp = 0, .gc3 = 1, .div1p5 = 0}, /* 302 MHz */
58 { .lomax = 5184, .spd = 1, .bs = 2, .bp = 2, .cp = 0, .gc3 = 1, .div1p5 = 0}, /* 324 MHz */
59 { .lomax = 5920, .spd = 1, .bs = 2, .bp = 3, .cp = 0, .gc3 = 1, .div1p5 = 0}, /* 370 MHz */
60 { .lomax = 7264, .spd = 1, .bs = 3, .bp = 3, .cp = 0, .gc3 = 1, .div1p5 = 0}, /* 454 MHz */
61 { .lomax = 7888, .spd = 0, .bs = 2, .bp = 3, .cp = 0, .gc3 = 1, .div1p5 = 1}, /* 493 MHz */
62 { .lomax = 8480, .spd = 0, .bs = 3, .bp = 3, .cp = 0, .gc3 = 1, .div1p5 = 1}, /* 530 MHz */
63 { .lomax = 8864, .spd = 0, .bs = 1, .bp = 3, .cp = 0, .gc3 = 1, .div1p5 = 0}, /* 554 MHz */
64 { .lomax = 9664, .spd = 0, .bs = 1, .bp = 4, .cp = 0, .gc3 = 0, .div1p5 = 0}, /* 604 MHz */
65 { .lomax = 11088, .spd = 0, .bs = 2, .bp = 4, .cp = 0, .gc3 = 0, .div1p5 = 0}, /* 696 MHz */
66 { .lomax = 11840, .spd = 0, .bs = 2, .bp = 4, .cp = 1, .gc3 = 0, .div1p5 = 0}, /* 740 MHz */
67 { .lomax = 13120, .spd = 0, .bs = 3, .bp = 4, .cp = 0, .gc3 = 0, .div1p5 = 0}, /* 820 MHz */
68 { .lomax = 13840, .spd = 0, .bs = 3, .bp = 4, .cp = 1, .gc3 = 0, .div1p5 = 0}, /* 865 MHz */
69 { .lomax = 0, .spd = 0, .bs = 0, .bp = 0, .cp = 0, .gc3 = 0, .div1p5 = 0} /* End */
Linus Torvalds1da177e2005-04-16 15:20:36 -070070};
71
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -080072static void tda827x_tune(struct i2c_client *c, u16 ifc, unsigned int freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073{
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -080074 unsigned char tuner_reg[8];
75 unsigned char reg2[2];
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -070076 u32 N;
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -080077 int i;
78 struct tuner *t = i2c_get_clientdata(c);
79 struct i2c_msg msg = {.addr = t->tda827x_addr, .flags = 0};
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -070081 if (t->mode == V4L2_TUNER_RADIO)
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -070082 freq = freq / 1000;
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -070083
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -080084 N = freq + ifc;
85 i = 0;
86 while (tda827x_analog[i].lomax < N) {
87 if(tda827x_analog[i + 1].lomax == 0)
88 break;
89 i++;
90 }
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -070091
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -080092 N = N << tda827x_analog[i].spd;
93
94 tuner_reg[0] = 0;
95 tuner_reg[1] = (unsigned char)(N>>8);
96 tuner_reg[2] = (unsigned char) N;
97 tuner_reg[3] = 0x40;
98 tuner_reg[4] = 0x52 + (t->tda827x_lpsel << 5);
99 tuner_reg[5] = (tda827x_analog[i].spd << 6) + (tda827x_analog[i].div1p5 <<5) +
Nickolay V. Shmyrev9a741ec2005-11-08 21:37:50 -0800100 (tda827x_analog[i].bs <<3) + tda827x_analog[i].bp;
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800101 tuner_reg[6] = 0x8f + (tda827x_analog[i].gc3 << 4);
102 tuner_reg[7] = 0x8f;
103
104 msg.buf = tuner_reg;
105 msg.len = 8;
106 i2c_transfer(c->adapter, &msg, 1);
107
108 msg.buf= reg2;
109 msg.len = 2;
110 reg2[0] = 0x80;
111 reg2[1] = 0;
112 i2c_transfer(c->adapter, &msg, 1);
113
114 reg2[0] = 0x60;
115 reg2[1] = 0xbf;
116 i2c_transfer(c->adapter, &msg, 1);
117
118 reg2[0] = 0x30;
119 reg2[1] = tuner_reg[4] + 0x80;
120 i2c_transfer(c->adapter, &msg, 1);
121
122 msleep(1);
123 reg2[0] = 0x30;
124 reg2[1] = tuner_reg[4] + 4;
125 i2c_transfer(c->adapter, &msg, 1);
126
127 msleep(1);
128 reg2[0] = 0x30;
129 reg2[1] = tuner_reg[4];
130 i2c_transfer(c->adapter, &msg, 1);
131
132 msleep(550);
133 reg2[0] = 0x30;
134 reg2[1] = (tuner_reg[4] & 0xfc) + tda827x_analog[i].cp ;
135 i2c_transfer(c->adapter, &msg, 1);
136
137 reg2[0] = 0x60;
Hartmut Hackmann95736032005-11-08 21:38:00 -0800138 reg2[1] = 0x3f;
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800139 i2c_transfer(c->adapter, &msg, 1);
140
141 reg2[0] = 0x80;
142 reg2[1] = 0x08; // Vsync en
143 i2c_transfer(c->adapter, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144}
145
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800146static void tda827x_agcf(struct i2c_client *c)
147{
148 struct tuner *t = i2c_get_clientdata(c);
149 unsigned char data[] = {0x80, 0x0c};
150 struct i2c_msg msg = {.addr = t->tda827x_addr, .buf = data,
151 .flags = 0, .len = 2};
152 i2c_transfer(c->adapter, &msg, 1);
153}
154
155/* ---------------------------------------------------------------------- */
156
157struct tda827xa_data {
158 u32 lomax;
159 u8 svco;
160 u8 spd;
161 u8 scr;
162 u8 sbs;
163 u8 gc3;
164};
165
166static struct tda827xa_data tda827xa_analog[] = {
167 { .lomax = 910, .svco = 3, .spd = 4, .scr = 0, .sbs = 0, .gc3 = 3}, /* 56.875 MHz */
168 { .lomax = 1076, .svco = 0, .spd = 3, .scr = 0, .sbs = 0, .gc3 = 3}, /* 67.25 MHz */
169 { .lomax = 1300, .svco = 1, .spd = 3, .scr = 0, .sbs = 0, .gc3 = 3}, /* 81.25 MHz */
170 { .lomax = 1560, .svco = 2, .spd = 3, .scr = 0, .sbs = 0, .gc3 = 3}, /* 97.5 MHz */
171 { .lomax = 1820, .svco = 3, .spd = 3, .scr = 0, .sbs = 1, .gc3 = 1}, /* 113.75 MHz */
172 { .lomax = 2152, .svco = 0, .spd = 2, .scr = 0, .sbs = 1, .gc3 = 1}, /* 134.5 MHz */
173 { .lomax = 2464, .svco = 1, .spd = 2, .scr = 0, .sbs = 1, .gc3 = 1}, /* 154 MHz */
174 { .lomax = 2600, .svco = 1, .spd = 2, .scr = 0, .sbs = 1, .gc3 = 1}, /* 162.5 MHz */
175 { .lomax = 2928, .svco = 2, .spd = 2, .scr = 0, .sbs = 1, .gc3 = 1}, /* 183 MHz */
176 { .lomax = 3120, .svco = 2, .spd = 2, .scr = 0, .sbs = 2, .gc3 = 1}, /* 195 MHz */
177 { .lomax = 3640, .svco = 3, .spd = 2, .scr = 0, .sbs = 2, .gc3 = 3}, /* 227.5 MHz */
178 { .lomax = 4304, .svco = 0, .spd = 1, .scr = 0, .sbs = 2, .gc3 = 3}, /* 269 MHz */
179 { .lomax = 5200, .svco = 1, .spd = 1, .scr = 0, .sbs = 2, .gc3 = 1}, /* 325 MHz */
180 { .lomax = 6240, .svco = 2, .spd = 1, .scr = 0, .sbs = 3, .gc3 = 3}, /* 390 MHz */
181 { .lomax = 7280, .svco = 3, .spd = 1, .scr = 0, .sbs = 3, .gc3 = 3}, /* 455 MHz */
182 { .lomax = 8320, .svco = 0, .spd = 0, .scr = 0, .sbs = 3, .gc3 = 1}, /* 520 MHz */
183 { .lomax = 8608, .svco = 0, .spd = 0, .scr = 1, .sbs = 3, .gc3 = 1}, /* 538 MHz */
184 { .lomax = 8864, .svco = 1, .spd = 0, .scr = 0, .sbs = 3, .gc3 = 1}, /* 554 MHz */
185 { .lomax = 9920, .svco = 1, .spd = 0, .scr = 0, .sbs = 4, .gc3 = 0}, /* 620 MHz */
186 { .lomax = 10400, .svco = 1, .spd = 0, .scr = 1, .sbs = 4, .gc3 = 0}, /* 650 MHz */
187 { .lomax = 11200, .svco = 2, .spd = 0, .scr = 0, .sbs = 4, .gc3 = 0}, /* 700 MHz */
188 { .lomax = 12480, .svco = 2, .spd = 0, .scr = 1, .sbs = 4, .gc3 = 0}, /* 780 MHz */
189 { .lomax = 13120, .svco = 3, .spd = 0, .scr = 0, .sbs = 4, .gc3 = 0}, /* 820 MHz */
190 { .lomax = 13920, .svco = 3, .spd = 0, .scr = 1, .sbs = 4, .gc3 = 0}, /* 870 MHz */
191 { .lomax = 14576, .svco = 3, .spd = 0, .scr = 2, .sbs = 4, .gc3 = 0}, /* 911 MHz */
192 { .lomax = 0, .svco = 0, .spd = 0, .scr = 0, .sbs = 0, .gc3 = 0} /* End */
193};
194
195static void tda827xa_tune(struct i2c_client *c, u16 ifc, unsigned int freq)
196{
197 unsigned char tuner_reg[14];
198 unsigned char reg2[2];
199 u32 N;
200 int i;
201 struct tuner *t = i2c_get_clientdata(c);
202 struct i2c_msg msg = {.addr = t->tda827x_addr, .flags = 0};
203
204 if (t->mode == V4L2_TUNER_RADIO)
205 freq = freq / 1000;
206
207 N = freq + ifc;
208 i = 0;
209 while (tda827xa_analog[i].lomax < N) {
210 if(tda827xa_analog[i + 1].lomax == 0)
211 break;
212 i++;
213 }
214
215 N = N << tda827xa_analog[i].spd;
216
217 tuner_reg[0] = 0;
218 tuner_reg[1] = (unsigned char)(N>>8);
219 tuner_reg[2] = (unsigned char) N;
220 tuner_reg[3] = 0;
221 tuner_reg[4] = 0x16;
222 tuner_reg[5] = (tda827xa_analog[i].spd << 5) + (tda827xa_analog[i].svco << 3) +
Nickolay V. Shmyrev9a741ec2005-11-08 21:37:50 -0800223 tda827xa_analog[i].sbs;
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800224 tuner_reg[6] = 0x8b + (tda827xa_analog[i].gc3 << 4);
225 tuner_reg[7] = 0x0c;
226 tuner_reg[8] = 4;
227 tuner_reg[9] = 0x20;
228 tuner_reg[10] = 0xff;
229 tuner_reg[11] = 0xe0;
230 tuner_reg[12] = 0;
231 tuner_reg[13] = 0x39 + (t->tda827x_lpsel << 1);
232
233 msg.buf = tuner_reg;
234 msg.len = 14;
235 i2c_transfer(c->adapter, &msg, 1);
236
237 msg.buf= reg2;
238 msg.len = 2;
239 reg2[0] = 0x60;
240 reg2[1] = 0x3c;
241 i2c_transfer(c->adapter, &msg, 1);
242
243 reg2[0] = 0xa0;
244 reg2[1] = 0xc0;
245 i2c_transfer(c->adapter, &msg, 1);
246
247 msleep(2);
248 reg2[0] = 0x30;
249 reg2[1] = 0x10 + tda827xa_analog[i].scr;
250 i2c_transfer(c->adapter, &msg, 1);
251
252 msleep(550);
253 reg2[0] = 0x50;
254 reg2[1] = 0x8f + (tda827xa_analog[i].gc3 << 4);
255 i2c_transfer(c->adapter, &msg, 1);
256
257 reg2[0] = 0x80;
258 reg2[1] = 0x28;
259 i2c_transfer(c->adapter, &msg, 1);
260
261 reg2[0] = 0xb0;
262 reg2[1] = 0x01;
263 i2c_transfer(c->adapter, &msg, 1);
264
265 reg2[0] = 0xc0;
266 reg2[1] = 0x19 + (t->tda827x_lpsel << 1);
267 i2c_transfer(c->adapter, &msg, 1);
268}
269
270static void tda827xa_agcf(struct i2c_client *c)
271{
272 struct tuner *t = i2c_get_clientdata(c);
273 unsigned char data[] = {0x80, 0x2c};
274 struct i2c_msg msg = {.addr = t->tda827x_addr, .buf = data,
275 .flags = 0, .len = 2};
276 i2c_transfer(c->adapter, &msg, 1);
277}
278
279/*---------------------------------------------------------------------*/
280
281static void tda8290_i2c_bridge(struct i2c_client *c, int close)
282{
283 unsigned char enable[2] = { 0x21, 0xC0 };
Hartmut Hackmann0157a9c2006-02-07 06:49:09 -0200284 unsigned char disable[2] = { 0x21, 0x00 };
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800285 unsigned char *msg;
286 if(close) {
287 msg = enable;
288 i2c_master_send(c, msg, 2);
289 /* let the bridge stabilize */
290 msleep(20);
291 } else {
292 msg = disable;
293 i2c_master_send(c, msg, 2);
294 }
295}
296
297/*---------------------------------------------------------------------*/
298
299static int tda8290_tune(struct i2c_client *c, u16 ifc, unsigned int freq)
300{
301 struct tuner *t = i2c_get_clientdata(c);
302 unsigned char soft_reset[] = { 0x00, 0x00 };
303 unsigned char easy_mode[] = { 0x01, t->tda8290_easy_mode };
304 unsigned char expert_mode[] = { 0x01, 0x80 };
Hartmut Hackmann0157a9c2006-02-07 06:49:09 -0200305 unsigned char agc_out_on[] = { 0x02, 0x00 };
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800306 unsigned char gainset_off[] = { 0x28, 0x14 };
307 unsigned char if_agc_spd[] = { 0x0f, 0x88 };
308 unsigned char adc_head_6[] = { 0x05, 0x04 };
309 unsigned char adc_head_9[] = { 0x05, 0x02 };
310 unsigned char adc_head_12[] = { 0x05, 0x01 };
311 unsigned char pll_bw_nom[] = { 0x0d, 0x47 };
312 unsigned char pll_bw_low[] = { 0x0d, 0x27 };
313 unsigned char gainset_2[] = { 0x28, 0x64 };
314 unsigned char agc_rst_on[] = { 0x0e, 0x0b };
315 unsigned char agc_rst_off[] = { 0x0e, 0x09 };
316 unsigned char if_agc_set[] = { 0x0f, 0x81 };
317 unsigned char addr_adc_sat = 0x1a;
318 unsigned char addr_agc_stat = 0x1d;
319 unsigned char addr_pll_stat = 0x1b;
320 unsigned char adc_sat, agc_stat,
Nickolay V. Shmyrev9a741ec2005-11-08 21:37:50 -0800321 pll_stat;
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800322
323 i2c_master_send(c, easy_mode, 2);
Hartmut Hackmann0157a9c2006-02-07 06:49:09 -0200324 i2c_master_send(c, agc_out_on, 2);
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800325 i2c_master_send(c, soft_reset, 2);
326 msleep(1);
327
Hartmut Hackmann4ac95af2005-11-08 21:38:38 -0800328 expert_mode[1] = t->tda8290_easy_mode + 0x80;
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800329 i2c_master_send(c, expert_mode, 2);
330 i2c_master_send(c, gainset_off, 2);
331 i2c_master_send(c, if_agc_spd, 2);
332 if (t->tda8290_easy_mode & 0x60)
333 i2c_master_send(c, adc_head_9, 2);
334 else
335 i2c_master_send(c, adc_head_6, 2);
336 i2c_master_send(c, pll_bw_nom, 2);
337
338 tda8290_i2c_bridge(c, 1);
339 if (t->tda827x_ver != 0)
340 tda827xa_tune(c, ifc, freq);
341 else
342 tda827x_tune(c, ifc, freq);
343 /* adjust headroom resp. gain */
344 i2c_master_send(c, &addr_adc_sat, 1);
345 i2c_master_recv(c, &adc_sat, 1);
346 i2c_master_send(c, &addr_agc_stat, 1);
347 i2c_master_recv(c, &agc_stat, 1);
348 i2c_master_send(c, &addr_pll_stat, 1);
349 i2c_master_recv(c, &pll_stat, 1);
350 if (pll_stat & 0x80)
351 tuner_dbg("tda8290 is locked, AGC: %d\n", agc_stat);
352 else
353 tuner_dbg("tda8290 not locked, no signal?\n");
Hartmut Hackmann4ac95af2005-11-08 21:38:38 -0800354 if ((agc_stat > 115) || (!(pll_stat & 0x80) && (adc_sat < 20))) {
355 tuner_dbg("adjust gain, step 1. Agc: %d, ADC stat: %d, lock: %d\n",
356 agc_stat, adc_sat, pll_stat & 0x80);
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800357 i2c_master_send(c, gainset_2, 2);
358 msleep(100);
359 i2c_master_send(c, &addr_agc_stat, 1);
360 i2c_master_recv(c, &agc_stat, 1);
361 i2c_master_send(c, &addr_pll_stat, 1);
362 i2c_master_recv(c, &pll_stat, 1);
363 if ((agc_stat > 115) || !(pll_stat & 0x80)) {
Hartmut Hackmann4ac95af2005-11-08 21:38:38 -0800364 tuner_dbg("adjust gain, step 2. Agc: %d, lock: %d\n",
365 agc_stat, pll_stat & 0x80);
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800366 if (t->tda827x_ver != 0)
367 tda827xa_agcf(c);
368 else
369 tda827x_agcf(c);
370 msleep(100);
371 i2c_master_send(c, &addr_agc_stat, 1);
372 i2c_master_recv(c, &agc_stat, 1);
373 i2c_master_send(c, &addr_pll_stat, 1);
374 i2c_master_recv(c, &pll_stat, 1);
375 if((agc_stat > 115) || !(pll_stat & 0x80)) {
376 tuner_dbg("adjust gain, step 3. Agc: %d\n", agc_stat);
377 i2c_master_send(c, adc_head_12, 2);
378 i2c_master_send(c, pll_bw_low, 2);
379 msleep(100);
380 }
381 }
382 }
383
384 /* l/ l' deadlock? */
385 if(t->tda8290_easy_mode & 0x60) {
386 i2c_master_send(c, &addr_adc_sat, 1);
387 i2c_master_recv(c, &adc_sat, 1);
388 i2c_master_send(c, &addr_pll_stat, 1);
389 i2c_master_recv(c, &pll_stat, 1);
390 if ((adc_sat > 20) || !(pll_stat & 0x80)) {
Hartmut Hackmann4ac95af2005-11-08 21:38:38 -0800391 tuner_dbg("trying to resolve SECAM L deadlock\n");
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800392 i2c_master_send(c, agc_rst_on, 2);
393 msleep(40);
394 i2c_master_send(c, agc_rst_off, 2);
395 }
396 }
397
398 tda8290_i2c_bridge(c, 0);
399 i2c_master_send(c, if_agc_set, 2);
400 return 0;
401}
402
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800403/*---------------------------------------------------------------------*/
404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405static void set_audio(struct tuner *t)
406{
Hartmut Hackmann4ac95af2005-11-08 21:38:38 -0800407 char* mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Hartmut Hackmann4ac95af2005-11-08 21:38:38 -0800409 t->tda827x_lpsel = 0;
410 mode = "xx";
Nickolay V. Shmyrev70b69342005-11-08 21:36:50 -0800411 if (t->std & V4L2_STD_MN) {
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800412 t->sgIF = 92;
413 t->tda8290_easy_mode = 0x01;
414 t->tda827x_lpsel = 1;
Hartmut Hackmann4ac95af2005-11-08 21:38:38 -0800415 mode = "MN";
Nickolay V. Shmyrev70b69342005-11-08 21:36:50 -0800416 } else if (t->std & V4L2_STD_B) {
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800417 t->sgIF = 108;
418 t->tda8290_easy_mode = 0x02;
Hartmut Hackmann4ac95af2005-11-08 21:38:38 -0800419 mode = "B";
Nickolay V. Shmyrev70b69342005-11-08 21:36:50 -0800420 } else if (t->std & V4L2_STD_GH) {
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800421 t->sgIF = 124;
422 t->tda8290_easy_mode = 0x04;
Hartmut Hackmann4ac95af2005-11-08 21:38:38 -0800423 mode = "GH";
Nickolay V. Shmyrev70b69342005-11-08 21:36:50 -0800424 } else if (t->std & V4L2_STD_PAL_I) {
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800425 t->sgIF = 124;
426 t->tda8290_easy_mode = 0x08;
Hartmut Hackmann4ac95af2005-11-08 21:38:38 -0800427 mode = "I";
Nickolay V. Shmyrev70b69342005-11-08 21:36:50 -0800428 } else if (t->std & V4L2_STD_DK) {
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800429 t->sgIF = 124;
430 t->tda8290_easy_mode = 0x10;
Hartmut Hackmann4ac95af2005-11-08 21:38:38 -0800431 mode = "DK";
Nickolay V. Shmyrev70b69342005-11-08 21:36:50 -0800432 } else if (t->std & V4L2_STD_SECAM_L) {
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800433 t->sgIF = 124;
434 t->tda8290_easy_mode = 0x20;
Hartmut Hackmann4ac95af2005-11-08 21:38:38 -0800435 mode = "L";
Hartmut Hackmann70146cf2005-11-13 16:07:58 -0800436 } else if (t->std & V4L2_STD_SECAM_LC) {
437 t->sgIF = 20;
438 t->tda8290_easy_mode = 0x40;
439 mode = "LC";
Nickolay V. Shmyrev70b69342005-11-08 21:36:50 -0800440 }
Hartmut Hackmann4ac95af2005-11-08 21:38:38 -0800441 tuner_dbg("setting tda8290 to system %s\n", mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442}
443
444static void set_tv_freq(struct i2c_client *c, unsigned int freq)
445{
446 struct tuner *t = i2c_get_clientdata(c);
447
448 set_audio(t);
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800449 tda8290_tune(c, t->sgIF, freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450}
451
452static void set_radio_freq(struct i2c_client *c, unsigned int freq)
453{
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800454 /* if frequency is 5.5 MHz */
455 tda8290_tune(c, 88, freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456}
457
458static int has_signal(struct i2c_client *c)
459{
460 unsigned char i2c_get_afc[1] = { 0x1B };
461 unsigned char afc = 0;
462
463 i2c_master_send(c, i2c_get_afc, ARRAY_SIZE(i2c_get_afc));
464 i2c_master_recv(c, &afc, 1);
465 return (afc & 0x80)? 65535:0;
466}
467
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800468/*---------------------------------------------------------------------*/
469
Mauro Carvalho Chehab793cf9e2005-09-09 13:03:37 -0700470static void standby(struct i2c_client *c)
471{
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800472 struct tuner *t = i2c_get_clientdata(c);
473 unsigned char cb1[] = { 0x30, 0xD0 };
474 unsigned char tda8290_standby[] = { 0x00, 0x02 };
Hartmut Hackmann0157a9c2006-02-07 06:49:09 -0200475 unsigned char tda8290_agc_tri[] = { 0x02, 0x20 };
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800476 struct i2c_msg msg = {.addr = t->tda827x_addr, .flags=0, .buf=cb1, .len = 2};
477
478 tda8290_i2c_bridge(c, 1);
479 if (t->tda827x_ver != 0)
480 cb1[1] = 0x90;
481 i2c_transfer(c->adapter, &msg, 1);
482 tda8290_i2c_bridge(c, 0);
Hartmut Hackmann0157a9c2006-02-07 06:49:09 -0200483 i2c_master_send(c, tda8290_agc_tri, 2);
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800484 i2c_master_send(c, tda8290_standby, 2);
Mauro Carvalho Chehab793cf9e2005-09-09 13:03:37 -0700485}
486
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800487
488static void tda8290_init_if(struct i2c_client *c)
489{
490 unsigned char set_VS[] = { 0x30, 0x6F };
491 unsigned char set_GP01_CF[] = { 0x20, 0x0B };
492
493 i2c_master_send(c, set_VS, 2);
494 i2c_master_send(c, set_GP01_CF, 2);
495}
496
497static void tda8290_init_tuner(struct i2c_client *c)
498{
499 struct tuner *t = i2c_get_clientdata(c);
500 unsigned char tda8275_init[] = { 0x00, 0x00, 0x00, 0x40, 0xdC, 0x04, 0xAf,
Nickolay V. Shmyrev9a741ec2005-11-08 21:37:50 -0800501 0x3F, 0x2A, 0x04, 0xFF, 0x00, 0x00, 0x40 };
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800502 unsigned char tda8275a_init[] = { 0x00, 0x00, 0x00, 0x00, 0xdC, 0x05, 0x8b,
Nickolay V. Shmyrev9a741ec2005-11-08 21:37:50 -0800503 0x0c, 0x04, 0x20, 0xFF, 0x00, 0x00, 0x4b };
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800504 struct i2c_msg msg = {.addr = t->tda827x_addr, .flags=0,
Ricardo Cerqueirac2f6f9d2005-11-08 21:37:51 -0800505 .buf=tda8275_init, .len = 14};
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800506 if (t->tda827x_ver != 0)
507 msg.buf = tda8275a_init;
508
509 tda8290_i2c_bridge(c, 1);
510 i2c_transfer(c->adapter, &msg, 1);
511 tda8290_i2c_bridge(c, 0);
512}
513
514/*---------------------------------------------------------------------*/
515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516int tda8290_init(struct i2c_client *c)
517{
518 struct tuner *t = i2c_get_clientdata(c);
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800519 u8 data;
520 int i, ret, tuners_found;
521 u32 tuner_addrs;
522 struct i2c_msg msg = {.flags=I2C_M_RD, .buf=&data, .len = 1};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800524 tda8290_i2c_bridge(c, 1);
525 /* probe for tuner chip */
526 tuners_found = 0;
527 tuner_addrs = 0;
528 for (i=0x60; i<= 0x63; i++) {
529 msg.addr = i;
530 ret = i2c_transfer(c->adapter, &msg, 1);
531 if (ret == 1) {
532 tuners_found++;
533 tuner_addrs = (tuner_addrs << 8) + i;
534 }
535 }
536 /* if there is more than one tuner, we expect the right one is
537 behind the bridge and we choose the highest address that doesn't
538 give a response now
539 */
540 tda8290_i2c_bridge(c, 0);
541 if(tuners_found > 1)
542 for (i = 0; i < tuners_found; i++) {
543 msg.addr = tuner_addrs & 0xff;
544 ret = i2c_transfer(c->adapter, &msg, 1);
545 if(ret == 1)
546 tuner_addrs = tuner_addrs >> 8;
547 else
548 break;
549 }
550 if (tuner_addrs == 0) {
551 tuner_addrs = 0x61;
Hartmut Hackmann95736032005-11-08 21:38:00 -0800552 tuner_info ("could not clearly identify tuner address, defaulting to %x\n",
Nickolay V. Shmyrev01cb9632005-11-08 21:38:00 -0800553 tuner_addrs);
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800554 } else {
555 tuner_addrs = tuner_addrs & 0xff;
556 tuner_info ("setting tuner address to %x\n", tuner_addrs);
557 }
558 t->tda827x_addr = tuner_addrs;
559 msg.addr = tuner_addrs;
560
561 tda8290_i2c_bridge(c, 1);
562 ret = i2c_transfer(c->adapter, &msg, 1);
563 if( ret != 1)
564 tuner_warn ("TDA827x access failed!\n");
565 if ((data & 0x3c) == 0) {
566 strlcpy(c->name, "tda8290+75", sizeof(c->name));
567 t->tda827x_ver = 0;
568 } else {
569 strlcpy(c->name, "tda8290+75a", sizeof(c->name));
570 t->tda827x_ver = 2;
571 }
Hartmut Hackmann0157a9c2006-02-07 06:49:09 -0200572 tuner_info("type set to %s\n", c->name);
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800573
Hans Verkuil27487d42006-01-15 15:04:52 -0200574 t->set_tv_freq = set_tv_freq;
575 t->set_radio_freq = set_radio_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 t->has_signal = has_signal;
Mauro Carvalho Chehab793cf9e2005-09-09 13:03:37 -0700577 t->standby = standby;
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800578 t->tda827x_lpsel = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800580 tda8290_init_tuner(c);
581 tda8290_init_if(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 return 0;
583}
584
Hartmut Hackmann95736032005-11-08 21:38:00 -0800585int tda8290_probe(struct i2c_client *c)
586{
587 unsigned char soft_reset[] = { 0x00, 0x00 };
588 unsigned char easy_mode_b[] = { 0x01, 0x02 };
589 unsigned char easy_mode_g[] = { 0x01, 0x04 };
590 unsigned char addr_dto_lsb = 0x07;
591 unsigned char data;
592
593 i2c_master_send(c, easy_mode_b, 2);
594 i2c_master_send(c, soft_reset, 2);
595 i2c_master_send(c, &addr_dto_lsb, 1);
596 i2c_master_recv(c, &data, 1);
597 if (data == 0) {
598 i2c_master_send(c, easy_mode_g, 2);
599 i2c_master_send(c, soft_reset, 2);
600 i2c_master_send(c, &addr_dto_lsb, 1);
601 i2c_master_recv(c, &data, 1);
602 if (data == 0x7b) {
603 return 0;
604 }
605 }
606 return -1;
607}
608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609/*
610 * Overrides for Emacs so that we follow Linus's tabbing style.
611 * ---------------------------------------------------------------------------
612 * Local variables:
613 * c-basic-offset: 8
614 * End:
615 */