blob: c40b92ce1fadefcd0c46db8ece473ee3581d3a33 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
3 * i2c tv tuner chip device driver
4 * controls all those simple 4-control-bytes style tuners.
5 */
6#include <linux/delay.h>
7#include <linux/i2c.h>
8#include <linux/videodev.h>
9#include <media/tuner.h>
Hans Verkuilba8fc392006-06-25 15:34:39 -030010#include <media/v4l2-common.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011
Mauro Carvalho Chehab5c07db02006-01-09 15:25:11 -020012static int offset = 0;
Mauro Carvalho Chehab4d988162006-08-12 21:59:19 -030013module_param(offset, int, 0664);
Mauro Carvalho Chehab5c07db02006-01-09 15:25:11 -020014MODULE_PARM_DESC(offset,"Allows to specify an offset for tuner");
15
Linus Torvalds1da177e2005-04-16 15:20:36 -070016/* ---------------------------------------------------------------------- */
17
18/* tv standard selection for Temic 4046 FM5
19 this value takes the low bits of control byte 2
20 from datasheet Rev.01, Feb.00
21 standard BG I L L2 D
22 picture IF 38.9 38.9 38.9 33.95 38.9
23 sound 1 33.4 32.9 32.4 40.45 32.4
24 sound 2 33.16
25 NICAM 33.05 32.348 33.05 33.05
26 */
27#define TEMIC_SET_PAL_I 0x05
28#define TEMIC_SET_PAL_DK 0x09
29#define TEMIC_SET_PAL_L 0x0a // SECAM ?
30#define TEMIC_SET_PAL_L2 0x0b // change IF !
31#define TEMIC_SET_PAL_BG 0x0c
32
33/* tv tuner system standard selection for Philips FQ1216ME
34 this value takes the low bits of control byte 2
35 from datasheet "1999 Nov 16" (supersedes "1999 Mar 23")
36 standard BG DK I L L`
37 picture carrier 38.90 38.90 38.90 38.90 33.95
38 colour 34.47 34.47 34.47 34.47 38.38
39 sound 1 33.40 32.40 32.90 32.40 40.45
40 sound 2 33.16 - - - -
41 NICAM 33.05 33.05 32.35 33.05 39.80
42 */
43#define PHILIPS_SET_PAL_I 0x01 /* Bit 2 always zero !*/
44#define PHILIPS_SET_PAL_BGDK 0x09
45#define PHILIPS_SET_PAL_L2 0x0a
46#define PHILIPS_SET_PAL_L 0x0b
47
48/* system switching for Philips FI1216MF MK2
49 from datasheet "1996 Jul 09",
50 standard BG L L'
51 picture carrier 38.90 38.90 33.95
52 colour 34.47 34.37 38.38
53 sound 1 33.40 32.40 40.45
54 sound 2 33.16 - -
55 NICAM 33.05 33.05 39.80
56 */
57#define PHILIPS_MF_SET_BG 0x01 /* Bit 2 must be zero, Bit 3 is system output */
58#define PHILIPS_MF_SET_PAL_L 0x03 // France
59#define PHILIPS_MF_SET_PAL_L2 0x02 // L'
60
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -070061/* Control byte */
62
63#define TUNER_RATIO_MASK 0x06 /* Bit cb1:cb2 */
64#define TUNER_RATIO_SELECT_50 0x00
65#define TUNER_RATIO_SELECT_32 0x02
66#define TUNER_RATIO_SELECT_166 0x04
67#define TUNER_RATIO_SELECT_62 0x06
68
69#define TUNER_CHARGE_PUMP 0x40 /* Bit cb6 */
70
71/* Status byte */
72
73#define TUNER_POR 0x80
74#define TUNER_FL 0x40
75#define TUNER_MODE 0x38
76#define TUNER_AFC 0x07
77#define TUNER_SIGNAL 0x07
78#define TUNER_STEREO 0x10
79
80#define TUNER_PLL_LOCKED 0x40
81#define TUNER_STEREO_MK3 0x04
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Linus Torvalds1da177e2005-04-16 15:20:36 -070083/* ---------------------------------------------------------------------- */
84
85static int tuner_getstatus(struct i2c_client *c)
86{
87 unsigned char byte;
88
89 if (1 != i2c_master_recv(c,&byte,1))
90 return 0;
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -070091
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 return byte;
93}
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095static int tuner_signal(struct i2c_client *c)
96{
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -070097 return (tuner_getstatus(c) & TUNER_SIGNAL) << 13;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098}
99
100static int tuner_stereo(struct i2c_client *c)
101{
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700102 int stereo, status;
103 struct tuner *t = i2c_get_clientdata(c);
104
105 status = tuner_getstatus (c);
106
107 switch (t->type) {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800108 case TUNER_PHILIPS_FM1216ME_MK3:
Trent Piepho657de3c2006-06-20 00:30:57 -0300109 case TUNER_PHILIPS_FM1236_MK3:
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700110 case TUNER_PHILIPS_FM1256_IH3:
Hans Verkuil122b5db2006-12-03 06:45:07 -0300111 case TUNER_LG_NTSC_TAPE:
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700112 stereo = ((status & TUNER_SIGNAL) == TUNER_STEREO_MK3);
113 break;
114 default:
115 stereo = status & TUNER_STEREO;
116 }
117
118 return stereo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119}
120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122/* ---------------------------------------------------------------------- */
123
124static void default_set_tv_freq(struct i2c_client *c, unsigned int freq)
125{
126 struct tuner *t = i2c_get_clientdata(c);
Michael Krufky3fc46d32006-01-23 17:11:11 -0200127 u8 config, cb, tuneraddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 u16 div;
129 struct tunertype *tun;
Hans Verkuil27487d42006-01-15 15:04:52 -0200130 u8 buffer[4];
Michael Krufky7b0ac9c2006-01-13 14:10:25 -0200131 int rc, IFPCoff, i, j;
Michael Krufky476d63d2006-02-07 06:25:34 -0200132 enum param_type desired_type;
Hans Verkuilba8fc392006-06-25 15:34:39 -0300133 struct tuner_params *params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135 tun = &tuners[t->type];
Michael Krufky7b0ac9c2006-01-13 14:10:25 -0200136
Michael Krufky5f594182006-02-07 06:25:33 -0200137 /* IFPCoff = Video Intermediate Frequency - Vif:
138 940 =16*58.75 NTSC/J (Japan)
139 732 =16*45.75 M/N STD
140 704 =16*44 ATSC (at DVB code)
141 632 =16*39.50 I U.K.
142 622.4=16*38.90 B/G D/K I, L STD
143 592 =16*37.00 D China
144 590 =16.36.875 B Australia
145 543.2=16*33.95 L' STD
146 171.2=16*10.70 FM Radio (at set_radio_freq)
147 */
148
149 if (t->std == V4L2_STD_NTSC_M_JP) {
Michael Krufky476d63d2006-02-07 06:25:34 -0200150 IFPCoff = 940;
151 desired_type = TUNER_PARAM_TYPE_NTSC;
Michael Krufky5f594182006-02-07 06:25:33 -0200152 } else if ((t->std & V4L2_STD_MN) &&
153 !(t->std & ~V4L2_STD_MN)) {
Michael Krufky476d63d2006-02-07 06:25:34 -0200154 IFPCoff = 732;
155 desired_type = TUNER_PARAM_TYPE_NTSC;
Michael Krufky5f594182006-02-07 06:25:33 -0200156 } else if (t->std == V4L2_STD_SECAM_LC) {
Michael Krufky476d63d2006-02-07 06:25:34 -0200157 IFPCoff = 543;
158 desired_type = TUNER_PARAM_TYPE_SECAM;
Michael Krufky5f594182006-02-07 06:25:33 -0200159 } else {
Michael Krufky476d63d2006-02-07 06:25:34 -0200160 IFPCoff = 623;
161 desired_type = TUNER_PARAM_TYPE_PAL;
Michael Krufky5f594182006-02-07 06:25:33 -0200162 }
163
Michael Krufky476d63d2006-02-07 06:25:34 -0200164 for (j = 0; j < tun->count-1; j++) {
165 if (desired_type != tun->params[j].type)
166 continue;
167 break;
168 }
Michael Krufky8f1a58d2006-02-07 06:25:39 -0200169 /* use default tuner_params if desired_type not available */
Michael Krufky15207b22006-02-07 06:25:40 -0200170 if (desired_type != tun->params[j].type) {
171 tuner_dbg("IFPCoff = %d: tuner_params undefined for tuner %d\n",
172 IFPCoff,t->type);
Michael Krufky8f1a58d2006-02-07 06:25:39 -0200173 j = 0;
Michael Krufky15207b22006-02-07 06:25:40 -0200174 }
Hans Verkuilba8fc392006-06-25 15:34:39 -0300175 params = &tun->params[j];
Michael Krufky8f1a58d2006-02-07 06:25:39 -0200176
Hans Verkuilba8fc392006-06-25 15:34:39 -0300177 for (i = 0; i < params->count; i++) {
178 if (freq > params->ranges[i].limit)
Michael Krufky90200d22006-01-09 15:25:38 -0200179 continue;
180 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 }
Hans Verkuilba8fc392006-06-25 15:34:39 -0300182 if (i == params->count) {
Hans Verkuil27487d42006-01-15 15:04:52 -0200183 tuner_dbg("TV frequency out of range (%d > %d)",
Hans Verkuilba8fc392006-06-25 15:34:39 -0300184 freq, params->ranges[i - 1].limit);
185 freq = params->ranges[--i].limit;
Hans Verkuil27487d42006-01-15 15:04:52 -0200186 }
Hans Verkuilba8fc392006-06-25 15:34:39 -0300187 config = params->ranges[i].config;
188 cb = params->ranges[i].cb;
Michael Krufky5f594182006-02-07 06:25:33 -0200189 /* i == 0 -> VHF_LO
190 * i == 1 -> VHF_HI
191 * i == 2 -> UHF */
Michael Krufkybd0d0f52006-02-07 06:25:35 -0200192 tuner_dbg("tv: param %d, range %d\n",j,i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Michael Krufky5f594182006-02-07 06:25:33 -0200194 div=freq + IFPCoff + offset;
195
196 tuner_dbg("Freq= %d.%02d MHz, V_IF=%d.%02d MHz, Offset=%d.%02d MHz, div=%0d\n",
197 freq / 16, freq % 16 * 100 / 16,
198 IFPCoff / 16, IFPCoff % 16 * 100 / 16,
199 offset / 16, offset % 16 * 100 / 16,
200 div);
201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 /* tv norm specific stuff for multi-norm tuners */
203 switch (t->type) {
204 case TUNER_PHILIPS_SECAM: // FI1216MF
205 /* 0x01 -> ??? no change ??? */
206 /* 0x02 -> PAL BDGHI / SECAM L */
207 /* 0x04 -> ??? PAL others / SECAM others ??? */
matthieu castet82c01d32007-05-21 11:15:09 -0300208 cb &= ~0x03;
209 if (t->std & V4L2_STD_SECAM_L) //also valid for V4L2_STD_SECAM
210 cb |= PHILIPS_MF_SET_PAL_L;
211 else if (t->std & V4L2_STD_SECAM_LC)
212 cb |= PHILIPS_MF_SET_PAL_L2;
213 else /* V4L2_STD_B|V4L2_STD_GH */
214 cb |= PHILIPS_MF_SET_BG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 break;
216
217 case TUNER_TEMIC_4046FM5:
Michael Krufkyab66b222006-01-23 17:11:11 -0200218 cb &= ~0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220 if (t->std & V4L2_STD_PAL_BG) {
Michael Krufkyab66b222006-01-23 17:11:11 -0200221 cb |= TEMIC_SET_PAL_BG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
223 } else if (t->std & V4L2_STD_PAL_I) {
Michael Krufkyab66b222006-01-23 17:11:11 -0200224 cb |= TEMIC_SET_PAL_I;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226 } else if (t->std & V4L2_STD_PAL_DK) {
Michael Krufkyab66b222006-01-23 17:11:11 -0200227 cb |= TEMIC_SET_PAL_DK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229 } else if (t->std & V4L2_STD_SECAM_L) {
Michael Krufkyab66b222006-01-23 17:11:11 -0200230 cb |= TEMIC_SET_PAL_L;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232 }
233 break;
234
235 case TUNER_PHILIPS_FQ1216ME:
Michael Krufkyab66b222006-01-23 17:11:11 -0200236 cb &= ~0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
238 if (t->std & (V4L2_STD_PAL_BG|V4L2_STD_PAL_DK)) {
Michael Krufkyab66b222006-01-23 17:11:11 -0200239 cb |= PHILIPS_SET_PAL_BGDK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241 } else if (t->std & V4L2_STD_PAL_I) {
Michael Krufkyab66b222006-01-23 17:11:11 -0200242 cb |= PHILIPS_SET_PAL_I;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
244 } else if (t->std & V4L2_STD_SECAM_L) {
Michael Krufkyab66b222006-01-23 17:11:11 -0200245 cb |= PHILIPS_SET_PAL_L;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
247 }
248 break;
249
250 case TUNER_PHILIPS_ATSC:
251 /* 0x00 -> ATSC antenna input 1 */
252 /* 0x01 -> ATSC antenna input 2 */
253 /* 0x02 -> NTSC antenna input 1 */
254 /* 0x03 -> NTSC antenna input 2 */
Michael Krufkyab66b222006-01-23 17:11:11 -0200255 cb &= ~0x03;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 if (!(t->std & V4L2_STD_ATSC))
Michael Krufkyab66b222006-01-23 17:11:11 -0200257 cb |= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 /* FIXME: input */
259 break;
260
261 case TUNER_MICROTUNE_4042FI5:
262 /* Set the charge pump for fast tuning */
Michael Krufky3fc46d32006-01-23 17:11:11 -0200263 config |= TUNER_CHARGE_PUMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 break;
Kirk Lapraye976f9372005-11-08 21:37:04 -0800265
266 case TUNER_PHILIPS_TUV1236D:
267 /* 0x40 -> ATSC antenna input 1 */
268 /* 0x48 -> ATSC antenna input 2 */
269 /* 0x00 -> NTSC antenna input 1 */
270 /* 0x08 -> NTSC antenna input 2 */
Kirk Laprayfde6d312005-11-08 21:38:18 -0800271 buffer[0] = 0x14;
272 buffer[1] = 0x00;
273 buffer[2] = 0x17;
274 buffer[3] = 0x00;
Michael Krufkyab66b222006-01-23 17:11:11 -0200275 cb &= ~0x40;
Kirk Laprayfde6d312005-11-08 21:38:18 -0800276 if (t->std & V4L2_STD_ATSC) {
Michael Krufkyab66b222006-01-23 17:11:11 -0200277 cb |= 0x40;
Kirk Laprayfde6d312005-11-08 21:38:18 -0800278 buffer[1] = 0x04;
279 }
280 /* set to the correct mode (analog or digital) */
Kirk Laprayfde6d312005-11-08 21:38:18 -0800281 tuneraddr = c->addr;
282 c->addr = 0x0a;
283 if (2 != (rc = i2c_master_send(c,&buffer[0],2)))
284 tuner_warn("i2c i/o error: rc == %d (should be 2)\n",rc);
285 if (2 != (rc = i2c_master_send(c,&buffer[2],2)))
286 tuner_warn("i2c i/o error: rc == %d (should be 2)\n",rc);
287 c->addr = tuneraddr;
Kirk Lapraye976f9372005-11-08 21:37:04 -0800288 /* FIXME: input */
289 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 }
291
Hans Verkuilba8fc392006-06-25 15:34:39 -0300292 if (params->cb_first_if_lower_freq && div < t->last_div) {
Michael Krufky3fc46d32006-01-23 17:11:11 -0200293 buffer[0] = config;
Michael Krufkyab66b222006-01-23 17:11:11 -0200294 buffer[1] = cb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 buffer[2] = (div>>8) & 0x7f;
296 buffer[3] = div & 0xff;
297 } else {
298 buffer[0] = (div>>8) & 0x7f;
299 buffer[1] = div & 0xff;
Michael Krufky3fc46d32006-01-23 17:11:11 -0200300 buffer[2] = config;
Michael Krufkyab66b222006-01-23 17:11:11 -0200301 buffer[3] = cb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 }
Hans Verkuil27487d42006-01-15 15:04:52 -0200303 t->last_div = div;
Hans Verkuilba8fc392006-06-25 15:34:39 -0300304 if (params->has_tda9887) {
305 int config = 0;
306 int is_secam_l = (t->std & (V4L2_STD_SECAM_L | V4L2_STD_SECAM_LC)) &&
307 !(t->std & ~(V4L2_STD_SECAM_L | V4L2_STD_SECAM_LC));
308
309 if (t->std == V4L2_STD_SECAM_LC) {
310 if (params->port1_active ^ params->port1_invert_for_secam_lc)
311 config |= TDA9887_PORT1_ACTIVE;
312 if (params->port2_active ^ params->port2_invert_for_secam_lc)
313 config |= TDA9887_PORT2_ACTIVE;
314 }
315 else {
316 if (params->port1_active)
317 config |= TDA9887_PORT1_ACTIVE;
318 if (params->port2_active)
319 config |= TDA9887_PORT2_ACTIVE;
320 }
321 if (params->intercarrier_mode)
322 config |= TDA9887_INTERCARRIER;
323 if (is_secam_l) {
324 if (i == 0 && params->default_top_secam_low)
325 config |= TDA9887_TOP(params->default_top_secam_low);
326 else if (i == 1 && params->default_top_secam_mid)
327 config |= TDA9887_TOP(params->default_top_secam_mid);
328 else if (params->default_top_secam_high)
329 config |= TDA9887_TOP(params->default_top_secam_high);
330 }
331 else {
332 if (i == 0 && params->default_top_low)
333 config |= TDA9887_TOP(params->default_top_low);
334 else if (i == 1 && params->default_top_mid)
335 config |= TDA9887_TOP(params->default_top_mid);
336 else if (params->default_top_high)
337 config |= TDA9887_TOP(params->default_top_high);
338 }
Trent Piephod7304de2006-08-24 22:43:45 -0300339 if (params->default_pll_gating_18)
340 config |= TDA9887_GATING_18;
Hans Verkuilba8fc392006-06-25 15:34:39 -0300341 i2c_clients_command(c->adapter, TDA9887_SET_CONFIG, &config);
342 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 tuner_dbg("tv 0x%02x 0x%02x 0x%02x 0x%02x\n",
344 buffer[0],buffer[1],buffer[2],buffer[3]);
345
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800346 if (4 != (rc = i2c_master_send(c,buffer,4)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 tuner_warn("i2c i/o error: rc == %d (should be 4)\n",rc);
348
Michael Krufkyd9cd2d92006-07-17 17:15:26 -0300349 switch (t->type) {
350 case TUNER_LG_TDVS_H06XF:
351 /* Set the Auxiliary Byte. */
352 buffer[0] = buffer[2];
353 buffer[0] &= ~0x20;
354 buffer[0] |= 0x18;
355 buffer[1] = 0x20;
356 tuner_dbg("tv 0x%02x 0x%02x\n",buffer[0],buffer[1]);
357
358 if (2 != (rc = i2c_master_send(c,buffer,2)))
359 tuner_warn("i2c i/o error: rc == %d (should be 2)\n",rc);
360 break;
361 case TUNER_MICROTUNE_4042FI5:
362 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 // FIXME - this may also work for other tuners
364 unsigned long timeout = jiffies + msecs_to_jiffies(1);
365 u8 status_byte = 0;
366
367 /* Wait until the PLL locks */
368 for (;;) {
369 if (time_after(jiffies,timeout))
370 return;
371 if (1 != (rc = i2c_master_recv(c,&status_byte,1))) {
372 tuner_warn("i2c i/o read error: rc == %d (should be 1)\n",rc);
373 break;
374 }
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700375 if (status_byte & TUNER_PLL_LOCKED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 break;
377 udelay(10);
378 }
379
380 /* Set the charge pump for optimized phase noise figure */
Michael Krufky3fc46d32006-01-23 17:11:11 -0200381 config &= ~TUNER_CHARGE_PUMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 buffer[0] = (div>>8) & 0x7f;
383 buffer[1] = div & 0xff;
Michael Krufky3fc46d32006-01-23 17:11:11 -0200384 buffer[2] = config;
Michael Krufkyab66b222006-01-23 17:11:11 -0200385 buffer[3] = cb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 tuner_dbg("tv 0x%02x 0x%02x 0x%02x 0x%02x\n",
Michael Krufkyd9cd2d92006-07-17 17:15:26 -0300387 buffer[0],buffer[1],buffer[2],buffer[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389 if (4 != (rc = i2c_master_send(c,buffer,4)))
390 tuner_warn("i2c i/o error: rc == %d (should be 4)\n",rc);
Michael Krufkyd9cd2d92006-07-17 17:15:26 -0300391 break;
392 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 }
394}
395
396static void default_set_radio_freq(struct i2c_client *c, unsigned int freq)
397{
398 struct tunertype *tun;
399 struct tuner *t = i2c_get_clientdata(c);
Hans Verkuil27487d42006-01-15 15:04:52 -0200400 u8 buffer[4];
401 u16 div;
Michael Krufky7b0ac9c2006-01-13 14:10:25 -0200402 int rc, j;
Michael Krufky476d63d2006-02-07 06:25:34 -0200403 enum param_type desired_type = TUNER_PARAM_TYPE_RADIO;
Hans Verkuilba8fc392006-06-25 15:34:39 -0300404 struct tuner_params *params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700406 tun = &tuners[t->type];
Michael Krufky476d63d2006-02-07 06:25:34 -0200407
408 for (j = 0; j < tun->count-1; j++) {
409 if (desired_type != tun->params[j].type)
410 continue;
411 break;
412 }
Michael Krufky8f1a58d2006-02-07 06:25:39 -0200413 /* use default tuner_params if desired_type not available */
414 if (desired_type != tun->params[j].type)
415 j = 0;
Michael Krufky7b0ac9c2006-01-13 14:10:25 -0200416
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700417 div = (20 * freq / 16000) + (int)(20*10.7); /* IF 10.7 MHz */
Hans Verkuilba8fc392006-06-25 15:34:39 -0300418 params = &tun->params[j];
419 buffer[2] = (params->ranges[0].config & ~TUNER_RATIO_MASK) | TUNER_RATIO_SELECT_50; /* 50 kHz step */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
421 switch (t->type) {
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700422 case TUNER_TENA_9533_DI:
Mauro Carvalho Chehab391cd722005-06-23 22:02:43 -0700423 case TUNER_YMEC_TVF_5533MF:
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700424 tuner_dbg ("This tuner doesn't have FM. Most cards has a TEA5767 for FM\n");
425 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 case TUNER_PHILIPS_FM1216ME_MK3:
427 case TUNER_PHILIPS_FM1236_MK3:
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700428 case TUNER_PHILIPS_FMD1216ME_MK3:
Hans Verkuil122b5db2006-12-03 06:45:07 -0300429 case TUNER_LG_NTSC_TAPE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 buffer[3] = 0x19;
431 break;
Mauro Carvalho Chehabefcf55c2006-03-09 11:17:43 -0300432 case TUNER_TNF_5335MF:
433 buffer[3] = 0x11;
434 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 case TUNER_PHILIPS_FM1256_IH3:
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700436 div = (20 * freq) / 16000 + (int)(33.3 * 20); /* IF 33.3 MHz */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 buffer[3] = 0x19;
438 break;
439 case TUNER_LG_PAL_FM:
440 buffer[3] = 0xa5;
441 break;
Mauro Carvalho Chehab33ac6b52005-09-09 13:03:56 -0700442 case TUNER_MICROTUNE_4049FM5:
443 div = (20 * freq) / 16000 + (int)(33.3 * 20); /* IF 33.3 MHz */
444 buffer[3] = 0xa4;
445 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 default:
447 buffer[3] = 0xa4;
448 break;
449 }
Hans Verkuilba8fc392006-06-25 15:34:39 -0300450 if (params->cb_first_if_lower_freq && div < t->last_div) {
Hans Verkuil27487d42006-01-15 15:04:52 -0200451 buffer[0] = buffer[2];
452 buffer[1] = buffer[3];
453 buffer[2] = (div>>8) & 0x7f;
454 buffer[3] = div & 0xff;
455 } else {
456 buffer[0] = (div>>8) & 0x7f;
457 buffer[1] = div & 0xff;
458 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460 tuner_dbg("radio 0x%02x 0x%02x 0x%02x 0x%02x\n",
461 buffer[0],buffer[1],buffer[2],buffer[3]);
Hans Verkuil27487d42006-01-15 15:04:52 -0200462 t->last_div = div;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
Hans Verkuilba8fc392006-06-25 15:34:39 -0300464 if (params->has_tda9887) {
465 int config = 0;
466 if (params->port1_active && !params->port1_fm_high_sensitivity)
467 config |= TDA9887_PORT1_ACTIVE;
468 if (params->port2_active && !params->port2_fm_high_sensitivity)
469 config |= TDA9887_PORT2_ACTIVE;
470 if (params->intercarrier_mode)
471 config |= TDA9887_INTERCARRIER;
472/* if (params->port1_set_for_fm_mono)
473 config &= ~TDA9887_PORT1_ACTIVE;*/
Mauro Carvalho Chehab483deb02006-12-04 08:31:38 -0300474 if (params->fm_gain_normal)
475 config |= TDA9887_GAIN_NORMAL;
Hans Verkuilba8fc392006-06-25 15:34:39 -0300476 i2c_clients_command(c->adapter, TDA9887_SET_CONFIG, &config);
477 }
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800478 if (4 != (rc = i2c_master_send(c,buffer,4)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 tuner_warn("i2c i/o error: rc == %d (should be 4)\n",rc);
480}
481
482int default_tuner_init(struct i2c_client *c)
483{
484 struct tuner *t = i2c_get_clientdata(c);
485
486 tuner_info("type set to %d (%s)\n",
487 t->type, tuners[t->type].name);
488 strlcpy(c->name, tuners[t->type].name, sizeof(c->name));
489
Hans Verkuil27487d42006-01-15 15:04:52 -0200490 t->set_tv_freq = default_set_tv_freq;
491 t->set_radio_freq = default_set_radio_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 t->has_signal = tuner_signal;
Hans Verkuil27487d42006-01-15 15:04:52 -0200493 t->is_stereo = tuner_stereo;
Mauro Carvalho Chehab793cf9e2005-09-09 13:03:37 -0700494 t->standby = NULL;
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 return 0;
497}
498
499/*
500 * Overrides for Emacs so that we follow Linus's tabbing style.
501 * ---------------------------------------------------------------------------
502 * Local variables:
503 * c-basic-offset: 8
504 * End:
505 */