blob: 0dde6af251dd816cbf4a25524d93d064bfb78d37 [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 };
284 unsigned char disable[2] = { 0x21, 0x80 };
285 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 };
305 unsigned char gainset_off[] = { 0x28, 0x14 };
306 unsigned char if_agc_spd[] = { 0x0f, 0x88 };
307 unsigned char adc_head_6[] = { 0x05, 0x04 };
308 unsigned char adc_head_9[] = { 0x05, 0x02 };
309 unsigned char adc_head_12[] = { 0x05, 0x01 };
310 unsigned char pll_bw_nom[] = { 0x0d, 0x47 };
311 unsigned char pll_bw_low[] = { 0x0d, 0x27 };
312 unsigned char gainset_2[] = { 0x28, 0x64 };
313 unsigned char agc_rst_on[] = { 0x0e, 0x0b };
314 unsigned char agc_rst_off[] = { 0x0e, 0x09 };
315 unsigned char if_agc_set[] = { 0x0f, 0x81 };
316 unsigned char addr_adc_sat = 0x1a;
317 unsigned char addr_agc_stat = 0x1d;
318 unsigned char addr_pll_stat = 0x1b;
319 unsigned char adc_sat, agc_stat,
Nickolay V. Shmyrev9a741ec2005-11-08 21:37:50 -0800320 pll_stat;
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800321
322 i2c_master_send(c, easy_mode, 2);
323 i2c_master_send(c, soft_reset, 2);
324 msleep(1);
325
326 i2c_master_send(c, expert_mode, 2);
327 i2c_master_send(c, gainset_off, 2);
328 i2c_master_send(c, if_agc_spd, 2);
329 if (t->tda8290_easy_mode & 0x60)
330 i2c_master_send(c, adc_head_9, 2);
331 else
332 i2c_master_send(c, adc_head_6, 2);
333 i2c_master_send(c, pll_bw_nom, 2);
334
335 tda8290_i2c_bridge(c, 1);
336 if (t->tda827x_ver != 0)
337 tda827xa_tune(c, ifc, freq);
338 else
339 tda827x_tune(c, ifc, freq);
340 /* adjust headroom resp. gain */
341 i2c_master_send(c, &addr_adc_sat, 1);
342 i2c_master_recv(c, &adc_sat, 1);
343 i2c_master_send(c, &addr_agc_stat, 1);
344 i2c_master_recv(c, &agc_stat, 1);
345 i2c_master_send(c, &addr_pll_stat, 1);
346 i2c_master_recv(c, &pll_stat, 1);
347 if (pll_stat & 0x80)
348 tuner_dbg("tda8290 is locked, AGC: %d\n", agc_stat);
349 else
350 tuner_dbg("tda8290 not locked, no signal?\n");
351 if ((agc_stat > 115) || (!(pll_stat & 0x80) && (adc_sat <20))) {
352 tuner_dbg("adjust gain, step 1. Agc: %d\n", agc_stat);
353 i2c_master_send(c, gainset_2, 2);
354 msleep(100);
355 i2c_master_send(c, &addr_agc_stat, 1);
356 i2c_master_recv(c, &agc_stat, 1);
357 i2c_master_send(c, &addr_pll_stat, 1);
358 i2c_master_recv(c, &pll_stat, 1);
359 if ((agc_stat > 115) || !(pll_stat & 0x80)) {
360 tuner_dbg("adjust gain, step 2. Agc: %d\n", agc_stat);
361 if (t->tda827x_ver != 0)
362 tda827xa_agcf(c);
363 else
364 tda827x_agcf(c);
365 msleep(100);
366 i2c_master_send(c, &addr_agc_stat, 1);
367 i2c_master_recv(c, &agc_stat, 1);
368 i2c_master_send(c, &addr_pll_stat, 1);
369 i2c_master_recv(c, &pll_stat, 1);
370 if((agc_stat > 115) || !(pll_stat & 0x80)) {
371 tuner_dbg("adjust gain, step 3. Agc: %d\n", agc_stat);
372 i2c_master_send(c, adc_head_12, 2);
373 i2c_master_send(c, pll_bw_low, 2);
374 msleep(100);
375 }
376 }
377 }
378
379 /* l/ l' deadlock? */
380 if(t->tda8290_easy_mode & 0x60) {
381 i2c_master_send(c, &addr_adc_sat, 1);
382 i2c_master_recv(c, &adc_sat, 1);
383 i2c_master_send(c, &addr_pll_stat, 1);
384 i2c_master_recv(c, &pll_stat, 1);
385 if ((adc_sat > 20) || !(pll_stat & 0x80)) {
386 i2c_master_send(c, agc_rst_on, 2);
387 msleep(40);
388 i2c_master_send(c, agc_rst_off, 2);
389 }
390 }
391
392 tda8290_i2c_bridge(c, 0);
393 i2c_master_send(c, if_agc_set, 2);
394 return 0;
395}
396
397
398/*---------------------------------------------------------------------*/
399
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400#define V4L2_STD_MN (V4L2_STD_PAL_M|V4L2_STD_PAL_N|V4L2_STD_PAL_Nc|V4L2_STD_NTSC)
401#define V4L2_STD_B (V4L2_STD_PAL_B|V4L2_STD_PAL_B1|V4L2_STD_SECAM_B)
402#define V4L2_STD_GH (V4L2_STD_PAL_G|V4L2_STD_PAL_H|V4L2_STD_SECAM_G|V4L2_STD_SECAM_H)
403#define V4L2_STD_DK (V4L2_STD_PAL_DK|V4L2_STD_SECAM_DK)
404
405static void set_audio(struct tuner *t)
406{
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800407 t->tda827x_lpsel = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Nickolay V. Shmyrev70b69342005-11-08 21:36:50 -0800409 if (t->std & V4L2_STD_MN) {
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800410 t->sgIF = 92;
411 t->tda8290_easy_mode = 0x01;
412 t->tda827x_lpsel = 1;
Nickolay V. Shmyrev70b69342005-11-08 21:36:50 -0800413 } else if (t->std & V4L2_STD_B) {
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800414 t->sgIF = 108;
415 t->tda8290_easy_mode = 0x02;
Nickolay V. Shmyrev70b69342005-11-08 21:36:50 -0800416 } else if (t->std & V4L2_STD_GH) {
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800417 t->sgIF = 124;
418 t->tda8290_easy_mode = 0x04;
Nickolay V. Shmyrev70b69342005-11-08 21:36:50 -0800419 } else if (t->std & V4L2_STD_PAL_I) {
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800420 t->sgIF = 124;
421 t->tda8290_easy_mode = 0x08;
Nickolay V. Shmyrev70b69342005-11-08 21:36:50 -0800422 } else if (t->std & V4L2_STD_DK) {
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800423 t->sgIF = 124;
424 t->tda8290_easy_mode = 0x10;
Nickolay V. Shmyrev70b69342005-11-08 21:36:50 -0800425 } else if (t->std & V4L2_STD_SECAM_L) {
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800426 t->sgIF = 124;
427 t->tda8290_easy_mode = 0x20;
Nickolay V. Shmyrev70b69342005-11-08 21:36:50 -0800428 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429}
430
431static void set_tv_freq(struct i2c_client *c, unsigned int freq)
432{
433 struct tuner *t = i2c_get_clientdata(c);
434
435 set_audio(t);
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800436 tda8290_tune(c, t->sgIF, freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437}
438
439static void set_radio_freq(struct i2c_client *c, unsigned int freq)
440{
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800441 /* if frequency is 5.5 MHz */
442 tda8290_tune(c, 88, freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443}
444
445static int has_signal(struct i2c_client *c)
446{
447 unsigned char i2c_get_afc[1] = { 0x1B };
448 unsigned char afc = 0;
449
450 i2c_master_send(c, i2c_get_afc, ARRAY_SIZE(i2c_get_afc));
451 i2c_master_recv(c, &afc, 1);
452 return (afc & 0x80)? 65535:0;
453}
454
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800455/*---------------------------------------------------------------------*/
456
Mauro Carvalho Chehab793cf9e2005-09-09 13:03:37 -0700457static void standby(struct i2c_client *c)
458{
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800459 struct tuner *t = i2c_get_clientdata(c);
460 unsigned char cb1[] = { 0x30, 0xD0 };
461 unsigned char tda8290_standby[] = { 0x00, 0x02 };
462 struct i2c_msg msg = {.addr = t->tda827x_addr, .flags=0, .buf=cb1, .len = 2};
463
464 tda8290_i2c_bridge(c, 1);
465 if (t->tda827x_ver != 0)
466 cb1[1] = 0x90;
467 i2c_transfer(c->adapter, &msg, 1);
468 tda8290_i2c_bridge(c, 0);
469 i2c_master_send(c, tda8290_standby, 2);
Mauro Carvalho Chehab793cf9e2005-09-09 13:03:37 -0700470}
471
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800472
473static void tda8290_init_if(struct i2c_client *c)
474{
475 unsigned char set_VS[] = { 0x30, 0x6F };
476 unsigned char set_GP01_CF[] = { 0x20, 0x0B };
477
478 i2c_master_send(c, set_VS, 2);
479 i2c_master_send(c, set_GP01_CF, 2);
480}
481
482static void tda8290_init_tuner(struct i2c_client *c)
483{
484 struct tuner *t = i2c_get_clientdata(c);
485 unsigned char tda8275_init[] = { 0x00, 0x00, 0x00, 0x40, 0xdC, 0x04, 0xAf,
Nickolay V. Shmyrev9a741ec2005-11-08 21:37:50 -0800486 0x3F, 0x2A, 0x04, 0xFF, 0x00, 0x00, 0x40 };
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800487 unsigned char tda8275a_init[] = { 0x00, 0x00, 0x00, 0x00, 0xdC, 0x05, 0x8b,
Nickolay V. Shmyrev9a741ec2005-11-08 21:37:50 -0800488 0x0c, 0x04, 0x20, 0xFF, 0x00, 0x00, 0x4b };
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800489 struct i2c_msg msg = {.addr = t->tda827x_addr, .flags=0,
Ricardo Cerqueirac2f6f9d2005-11-08 21:37:51 -0800490 .buf=tda8275_init, .len = 14};
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800491 if (t->tda827x_ver != 0)
492 msg.buf = tda8275a_init;
493
494 tda8290_i2c_bridge(c, 1);
495 i2c_transfer(c->adapter, &msg, 1);
496 tda8290_i2c_bridge(c, 0);
497}
498
499/*---------------------------------------------------------------------*/
500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501int tda8290_init(struct i2c_client *c)
502{
503 struct tuner *t = i2c_get_clientdata(c);
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800504 u8 data;
505 int i, ret, tuners_found;
506 u32 tuner_addrs;
507 struct i2c_msg msg = {.flags=I2C_M_RD, .buf=&data, .len = 1};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800509 tda8290_i2c_bridge(c, 1);
510 /* probe for tuner chip */
511 tuners_found = 0;
512 tuner_addrs = 0;
513 for (i=0x60; i<= 0x63; i++) {
514 msg.addr = i;
515 ret = i2c_transfer(c->adapter, &msg, 1);
516 if (ret == 1) {
517 tuners_found++;
518 tuner_addrs = (tuner_addrs << 8) + i;
519 }
520 }
521 /* if there is more than one tuner, we expect the right one is
522 behind the bridge and we choose the highest address that doesn't
523 give a response now
524 */
525 tda8290_i2c_bridge(c, 0);
526 if(tuners_found > 1)
527 for (i = 0; i < tuners_found; i++) {
528 msg.addr = tuner_addrs & 0xff;
529 ret = i2c_transfer(c->adapter, &msg, 1);
530 if(ret == 1)
531 tuner_addrs = tuner_addrs >> 8;
532 else
533 break;
534 }
535 if (tuner_addrs == 0) {
536 tuner_addrs = 0x61;
Hartmut Hackmann95736032005-11-08 21:38:00 -0800537 tuner_info ("could not clearly identify tuner address, defaulting to %x\n",
Nickolay V. Shmyrev01cb9632005-11-08 21:38:00 -0800538 tuner_addrs);
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800539 } else {
540 tuner_addrs = tuner_addrs & 0xff;
541 tuner_info ("setting tuner address to %x\n", tuner_addrs);
542 }
543 t->tda827x_addr = tuner_addrs;
544 msg.addr = tuner_addrs;
545
546 tda8290_i2c_bridge(c, 1);
547 ret = i2c_transfer(c->adapter, &msg, 1);
548 if( ret != 1)
549 tuner_warn ("TDA827x access failed!\n");
550 if ((data & 0x3c) == 0) {
551 strlcpy(c->name, "tda8290+75", sizeof(c->name));
552 t->tda827x_ver = 0;
553 } else {
554 strlcpy(c->name, "tda8290+75a", sizeof(c->name));
555 t->tda827x_ver = 2;
556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 tuner_info("tuner: type set to %s\n", c->name);
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 t->tv_freq = set_tv_freq;
560 t->radio_freq = set_radio_freq;
561 t->has_signal = has_signal;
Mauro Carvalho Chehab793cf9e2005-09-09 13:03:37 -0700562 t->standby = standby;
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800563 t->tda827x_lpsel = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -0800565 tda8290_init_tuner(c);
566 tda8290_init_if(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 return 0;
568}
569
Hartmut Hackmann95736032005-11-08 21:38:00 -0800570int tda8290_probe(struct i2c_client *c)
571{
572 unsigned char soft_reset[] = { 0x00, 0x00 };
573 unsigned char easy_mode_b[] = { 0x01, 0x02 };
574 unsigned char easy_mode_g[] = { 0x01, 0x04 };
575 unsigned char addr_dto_lsb = 0x07;
576 unsigned char data;
577
578 i2c_master_send(c, easy_mode_b, 2);
579 i2c_master_send(c, soft_reset, 2);
580 i2c_master_send(c, &addr_dto_lsb, 1);
581 i2c_master_recv(c, &data, 1);
582 if (data == 0) {
583 i2c_master_send(c, easy_mode_g, 2);
584 i2c_master_send(c, soft_reset, 2);
585 i2c_master_send(c, &addr_dto_lsb, 1);
586 i2c_master_recv(c, &data, 1);
587 if (data == 0x7b) {
588 return 0;
589 }
590 }
591 return -1;
592}
593
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594/*
595 * Overrides for Emacs so that we follow Linus's tabbing style.
596 * ---------------------------------------------------------------------------
597 * Local variables:
598 * c-basic-offset: 8
599 * End:
600 */