blob: 2d57e8bc0db3467e406d184d80647582abf2c0c8 [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>
Michael Krufky8218b0b2007-06-26 13:12:08 -030011#include <media/tuner-types.h>
12#include "tuner-driver.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
Mauro Carvalho Chehab5c07db02006-01-09 15:25:11 -020014static int offset = 0;
Mauro Carvalho Chehab4d988162006-08-12 21:59:19 -030015module_param(offset, int, 0664);
Mauro Carvalho Chehab5c07db02006-01-09 15:25:11 -020016MODULE_PARM_DESC(offset,"Allows to specify an offset for tuner");
17
Linus Torvalds1da177e2005-04-16 15:20:36 -070018/* ---------------------------------------------------------------------- */
19
20/* tv standard selection for Temic 4046 FM5
21 this value takes the low bits of control byte 2
22 from datasheet Rev.01, Feb.00
23 standard BG I L L2 D
24 picture IF 38.9 38.9 38.9 33.95 38.9
25 sound 1 33.4 32.9 32.4 40.45 32.4
26 sound 2 33.16
27 NICAM 33.05 32.348 33.05 33.05
28 */
29#define TEMIC_SET_PAL_I 0x05
30#define TEMIC_SET_PAL_DK 0x09
31#define TEMIC_SET_PAL_L 0x0a // SECAM ?
32#define TEMIC_SET_PAL_L2 0x0b // change IF !
33#define TEMIC_SET_PAL_BG 0x0c
34
35/* tv tuner system standard selection for Philips FQ1216ME
36 this value takes the low bits of control byte 2
37 from datasheet "1999 Nov 16" (supersedes "1999 Mar 23")
38 standard BG DK I L L`
39 picture carrier 38.90 38.90 38.90 38.90 33.95
40 colour 34.47 34.47 34.47 34.47 38.38
41 sound 1 33.40 32.40 32.90 32.40 40.45
42 sound 2 33.16 - - - -
43 NICAM 33.05 33.05 32.35 33.05 39.80
44 */
45#define PHILIPS_SET_PAL_I 0x01 /* Bit 2 always zero !*/
46#define PHILIPS_SET_PAL_BGDK 0x09
47#define PHILIPS_SET_PAL_L2 0x0a
48#define PHILIPS_SET_PAL_L 0x0b
49
50/* system switching for Philips FI1216MF MK2
51 from datasheet "1996 Jul 09",
52 standard BG L L'
53 picture carrier 38.90 38.90 33.95
54 colour 34.47 34.37 38.38
55 sound 1 33.40 32.40 40.45
56 sound 2 33.16 - -
57 NICAM 33.05 33.05 39.80
58 */
Mauro Carvalho Chehabc57032d2007-05-21 11:39:21 -030059#define PHILIPS_MF_SET_STD_BG 0x01 /* Bit 2 must be zero, Bit 3 is system output */
60#define PHILIPS_MF_SET_STD_L 0x03 /* Used on Secam France */
61#define PHILIPS_MF_SET_STD_LC 0x02 /* Used on SECAM L' */
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -070063/* Control byte */
64
65#define TUNER_RATIO_MASK 0x06 /* Bit cb1:cb2 */
66#define TUNER_RATIO_SELECT_50 0x00
67#define TUNER_RATIO_SELECT_32 0x02
68#define TUNER_RATIO_SELECT_166 0x04
69#define TUNER_RATIO_SELECT_62 0x06
70
71#define TUNER_CHARGE_PUMP 0x40 /* Bit cb6 */
72
73/* Status byte */
74
75#define TUNER_POR 0x80
76#define TUNER_FL 0x40
77#define TUNER_MODE 0x38
78#define TUNER_AFC 0x07
79#define TUNER_SIGNAL 0x07
80#define TUNER_STEREO 0x10
81
82#define TUNER_PLL_LOCKED 0x40
83#define TUNER_STEREO_MK3 0x04
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Linus Torvalds1da177e2005-04-16 15:20:36 -070085/* ---------------------------------------------------------------------- */
86
87static int tuner_getstatus(struct i2c_client *c)
88{
89 unsigned char byte;
90
91 if (1 != i2c_master_recv(c,&byte,1))
92 return 0;
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -070093
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 return byte;
95}
96
Linus Torvalds1da177e2005-04-16 15:20:36 -070097static int tuner_signal(struct i2c_client *c)
98{
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -070099 return (tuner_getstatus(c) & TUNER_SIGNAL) << 13;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100}
101
102static int tuner_stereo(struct i2c_client *c)
103{
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700104 int stereo, status;
105 struct tuner *t = i2c_get_clientdata(c);
106
107 status = tuner_getstatus (c);
108
109 switch (t->type) {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800110 case TUNER_PHILIPS_FM1216ME_MK3:
Trent Piepho657de3c2006-06-20 00:30:57 -0300111 case TUNER_PHILIPS_FM1236_MK3:
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700112 case TUNER_PHILIPS_FM1256_IH3:
Hans Verkuil122b5db2006-12-03 06:45:07 -0300113 case TUNER_LG_NTSC_TAPE:
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700114 stereo = ((status & TUNER_SIGNAL) == TUNER_STEREO_MK3);
115 break;
116 default:
117 stereo = status & TUNER_STEREO;
118 }
119
120 return stereo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121}
122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124/* ---------------------------------------------------------------------- */
125
126static void default_set_tv_freq(struct i2c_client *c, unsigned int freq)
127{
128 struct tuner *t = i2c_get_clientdata(c);
Michael Krufky3fc46d32006-01-23 17:11:11 -0200129 u8 config, cb, tuneraddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 u16 div;
131 struct tunertype *tun;
Hans Verkuil27487d42006-01-15 15:04:52 -0200132 u8 buffer[4];
Michael Krufky7b0ac9c2006-01-13 14:10:25 -0200133 int rc, IFPCoff, i, j;
Michael Krufky476d63d2006-02-07 06:25:34 -0200134 enum param_type desired_type;
Hans Verkuilba8fc392006-06-25 15:34:39 -0300135 struct tuner_params *params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137 tun = &tuners[t->type];
Michael Krufky7b0ac9c2006-01-13 14:10:25 -0200138
Michael Krufky5f594182006-02-07 06:25:33 -0200139 /* IFPCoff = Video Intermediate Frequency - Vif:
140 940 =16*58.75 NTSC/J (Japan)
141 732 =16*45.75 M/N STD
142 704 =16*44 ATSC (at DVB code)
143 632 =16*39.50 I U.K.
144 622.4=16*38.90 B/G D/K I, L STD
145 592 =16*37.00 D China
146 590 =16.36.875 B Australia
147 543.2=16*33.95 L' STD
148 171.2=16*10.70 FM Radio (at set_radio_freq)
149 */
150
151 if (t->std == V4L2_STD_NTSC_M_JP) {
Michael Krufky476d63d2006-02-07 06:25:34 -0200152 IFPCoff = 940;
153 desired_type = TUNER_PARAM_TYPE_NTSC;
Michael Krufky5f594182006-02-07 06:25:33 -0200154 } else if ((t->std & V4L2_STD_MN) &&
155 !(t->std & ~V4L2_STD_MN)) {
Michael Krufky476d63d2006-02-07 06:25:34 -0200156 IFPCoff = 732;
157 desired_type = TUNER_PARAM_TYPE_NTSC;
Michael Krufky5f594182006-02-07 06:25:33 -0200158 } else if (t->std == V4L2_STD_SECAM_LC) {
Michael Krufky476d63d2006-02-07 06:25:34 -0200159 IFPCoff = 543;
160 desired_type = TUNER_PARAM_TYPE_SECAM;
Michael Krufky5f594182006-02-07 06:25:33 -0200161 } else {
Michael Krufky476d63d2006-02-07 06:25:34 -0200162 IFPCoff = 623;
163 desired_type = TUNER_PARAM_TYPE_PAL;
Michael Krufky5f594182006-02-07 06:25:33 -0200164 }
165
Michael Krufky476d63d2006-02-07 06:25:34 -0200166 for (j = 0; j < tun->count-1; j++) {
167 if (desired_type != tun->params[j].type)
168 continue;
169 break;
170 }
Michael Krufky8f1a58d2006-02-07 06:25:39 -0200171 /* use default tuner_params if desired_type not available */
Michael Krufky15207b22006-02-07 06:25:40 -0200172 if (desired_type != tun->params[j].type) {
173 tuner_dbg("IFPCoff = %d: tuner_params undefined for tuner %d\n",
174 IFPCoff,t->type);
Michael Krufky8f1a58d2006-02-07 06:25:39 -0200175 j = 0;
Michael Krufky15207b22006-02-07 06:25:40 -0200176 }
Hans Verkuilba8fc392006-06-25 15:34:39 -0300177 params = &tun->params[j];
Michael Krufky8f1a58d2006-02-07 06:25:39 -0200178
Hans Verkuilba8fc392006-06-25 15:34:39 -0300179 for (i = 0; i < params->count; i++) {
180 if (freq > params->ranges[i].limit)
Michael Krufky90200d22006-01-09 15:25:38 -0200181 continue;
182 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 }
Hans Verkuilba8fc392006-06-25 15:34:39 -0300184 if (i == params->count) {
Hans Verkuil27487d42006-01-15 15:04:52 -0200185 tuner_dbg("TV frequency out of range (%d > %d)",
Hans Verkuilba8fc392006-06-25 15:34:39 -0300186 freq, params->ranges[i - 1].limit);
187 freq = params->ranges[--i].limit;
Hans Verkuil27487d42006-01-15 15:04:52 -0200188 }
Hans Verkuilba8fc392006-06-25 15:34:39 -0300189 config = params->ranges[i].config;
190 cb = params->ranges[i].cb;
Michael Krufky5f594182006-02-07 06:25:33 -0200191 /* i == 0 -> VHF_LO
192 * i == 1 -> VHF_HI
193 * i == 2 -> UHF */
Michael Krufkybd0d0f52006-02-07 06:25:35 -0200194 tuner_dbg("tv: param %d, range %d\n",j,i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Michael Krufky5f594182006-02-07 06:25:33 -0200196 div=freq + IFPCoff + offset;
197
198 tuner_dbg("Freq= %d.%02d MHz, V_IF=%d.%02d MHz, Offset=%d.%02d MHz, div=%0d\n",
199 freq / 16, freq % 16 * 100 / 16,
200 IFPCoff / 16, IFPCoff % 16 * 100 / 16,
201 offset / 16, offset % 16 * 100 / 16,
202 div);
203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 /* tv norm specific stuff for multi-norm tuners */
205 switch (t->type) {
206 case TUNER_PHILIPS_SECAM: // FI1216MF
207 /* 0x01 -> ??? no change ??? */
208 /* 0x02 -> PAL BDGHI / SECAM L */
209 /* 0x04 -> ??? PAL others / SECAM others ??? */
matthieu castet82c01d32007-05-21 11:15:09 -0300210 cb &= ~0x03;
211 if (t->std & V4L2_STD_SECAM_L) //also valid for V4L2_STD_SECAM
Mauro Carvalho Chehabc57032d2007-05-21 11:39:21 -0300212 cb |= PHILIPS_MF_SET_STD_L;
matthieu castet82c01d32007-05-21 11:15:09 -0300213 else if (t->std & V4L2_STD_SECAM_LC)
Mauro Carvalho Chehabc57032d2007-05-21 11:39:21 -0300214 cb |= PHILIPS_MF_SET_STD_LC;
matthieu castet82c01d32007-05-21 11:15:09 -0300215 else /* V4L2_STD_B|V4L2_STD_GH */
Mauro Carvalho Chehabc57032d2007-05-21 11:39:21 -0300216 cb |= PHILIPS_MF_SET_STD_BG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 break;
218
219 case TUNER_TEMIC_4046FM5:
Michael Krufkyab66b222006-01-23 17:11:11 -0200220 cb &= ~0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222 if (t->std & V4L2_STD_PAL_BG) {
Michael Krufkyab66b222006-01-23 17:11:11 -0200223 cb |= TEMIC_SET_PAL_BG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225 } else if (t->std & V4L2_STD_PAL_I) {
Michael Krufkyab66b222006-01-23 17:11:11 -0200226 cb |= TEMIC_SET_PAL_I;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228 } else if (t->std & V4L2_STD_PAL_DK) {
Michael Krufkyab66b222006-01-23 17:11:11 -0200229 cb |= TEMIC_SET_PAL_DK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
231 } else if (t->std & V4L2_STD_SECAM_L) {
Michael Krufkyab66b222006-01-23 17:11:11 -0200232 cb |= TEMIC_SET_PAL_L;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 }
235 break;
236
237 case TUNER_PHILIPS_FQ1216ME:
Michael Krufkyab66b222006-01-23 17:11:11 -0200238 cb &= ~0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240 if (t->std & (V4L2_STD_PAL_BG|V4L2_STD_PAL_DK)) {
Michael Krufkyab66b222006-01-23 17:11:11 -0200241 cb |= PHILIPS_SET_PAL_BGDK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
243 } else if (t->std & V4L2_STD_PAL_I) {
Michael Krufkyab66b222006-01-23 17:11:11 -0200244 cb |= PHILIPS_SET_PAL_I;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246 } else if (t->std & V4L2_STD_SECAM_L) {
Michael Krufkyab66b222006-01-23 17:11:11 -0200247 cb |= PHILIPS_SET_PAL_L;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249 }
250 break;
251
252 case TUNER_PHILIPS_ATSC:
253 /* 0x00 -> ATSC antenna input 1 */
254 /* 0x01 -> ATSC antenna input 2 */
255 /* 0x02 -> NTSC antenna input 1 */
256 /* 0x03 -> NTSC antenna input 2 */
Michael Krufkyab66b222006-01-23 17:11:11 -0200257 cb &= ~0x03;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 if (!(t->std & V4L2_STD_ATSC))
Michael Krufkyab66b222006-01-23 17:11:11 -0200259 cb |= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 /* FIXME: input */
261 break;
262
263 case TUNER_MICROTUNE_4042FI5:
264 /* Set the charge pump for fast tuning */
Michael Krufky3fc46d32006-01-23 17:11:11 -0200265 config |= TUNER_CHARGE_PUMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 break;
Kirk Lapraye976f9372005-11-08 21:37:04 -0800267
268 case TUNER_PHILIPS_TUV1236D:
269 /* 0x40 -> ATSC antenna input 1 */
270 /* 0x48 -> ATSC antenna input 2 */
271 /* 0x00 -> NTSC antenna input 1 */
272 /* 0x08 -> NTSC antenna input 2 */
Kirk Laprayfde6d312005-11-08 21:38:18 -0800273 buffer[0] = 0x14;
274 buffer[1] = 0x00;
275 buffer[2] = 0x17;
276 buffer[3] = 0x00;
Michael Krufkyab66b222006-01-23 17:11:11 -0200277 cb &= ~0x40;
Kirk Laprayfde6d312005-11-08 21:38:18 -0800278 if (t->std & V4L2_STD_ATSC) {
Michael Krufkyab66b222006-01-23 17:11:11 -0200279 cb |= 0x40;
Kirk Laprayfde6d312005-11-08 21:38:18 -0800280 buffer[1] = 0x04;
281 }
282 /* set to the correct mode (analog or digital) */
Kirk Laprayfde6d312005-11-08 21:38:18 -0800283 tuneraddr = c->addr;
284 c->addr = 0x0a;
285 if (2 != (rc = i2c_master_send(c,&buffer[0],2)))
286 tuner_warn("i2c i/o error: rc == %d (should be 2)\n",rc);
287 if (2 != (rc = i2c_master_send(c,&buffer[2],2)))
288 tuner_warn("i2c i/o error: rc == %d (should be 2)\n",rc);
289 c->addr = tuneraddr;
Kirk Lapraye976f9372005-11-08 21:37:04 -0800290 /* FIXME: input */
291 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 }
293
Hans Verkuilba8fc392006-06-25 15:34:39 -0300294 if (params->cb_first_if_lower_freq && div < t->last_div) {
Michael Krufky3fc46d32006-01-23 17:11:11 -0200295 buffer[0] = config;
Michael Krufkyab66b222006-01-23 17:11:11 -0200296 buffer[1] = cb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 buffer[2] = (div>>8) & 0x7f;
298 buffer[3] = div & 0xff;
299 } else {
300 buffer[0] = (div>>8) & 0x7f;
301 buffer[1] = div & 0xff;
Michael Krufky3fc46d32006-01-23 17:11:11 -0200302 buffer[2] = config;
Michael Krufkyab66b222006-01-23 17:11:11 -0200303 buffer[3] = cb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 }
Hans Verkuil27487d42006-01-15 15:04:52 -0200305 t->last_div = div;
Hans Verkuilba8fc392006-06-25 15:34:39 -0300306 if (params->has_tda9887) {
307 int config = 0;
308 int is_secam_l = (t->std & (V4L2_STD_SECAM_L | V4L2_STD_SECAM_LC)) &&
309 !(t->std & ~(V4L2_STD_SECAM_L | V4L2_STD_SECAM_LC));
310
311 if (t->std == V4L2_STD_SECAM_LC) {
312 if (params->port1_active ^ params->port1_invert_for_secam_lc)
313 config |= TDA9887_PORT1_ACTIVE;
314 if (params->port2_active ^ params->port2_invert_for_secam_lc)
315 config |= TDA9887_PORT2_ACTIVE;
316 }
317 else {
318 if (params->port1_active)
319 config |= TDA9887_PORT1_ACTIVE;
320 if (params->port2_active)
321 config |= TDA9887_PORT2_ACTIVE;
322 }
323 if (params->intercarrier_mode)
324 config |= TDA9887_INTERCARRIER;
325 if (is_secam_l) {
326 if (i == 0 && params->default_top_secam_low)
327 config |= TDA9887_TOP(params->default_top_secam_low);
328 else if (i == 1 && params->default_top_secam_mid)
329 config |= TDA9887_TOP(params->default_top_secam_mid);
330 else if (params->default_top_secam_high)
331 config |= TDA9887_TOP(params->default_top_secam_high);
332 }
333 else {
334 if (i == 0 && params->default_top_low)
335 config |= TDA9887_TOP(params->default_top_low);
336 else if (i == 1 && params->default_top_mid)
337 config |= TDA9887_TOP(params->default_top_mid);
338 else if (params->default_top_high)
339 config |= TDA9887_TOP(params->default_top_high);
340 }
Trent Piephod7304de2006-08-24 22:43:45 -0300341 if (params->default_pll_gating_18)
342 config |= TDA9887_GATING_18;
Hans Verkuilba8fc392006-06-25 15:34:39 -0300343 i2c_clients_command(c->adapter, TDA9887_SET_CONFIG, &config);
344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 tuner_dbg("tv 0x%02x 0x%02x 0x%02x 0x%02x\n",
346 buffer[0],buffer[1],buffer[2],buffer[3]);
347
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800348 if (4 != (rc = i2c_master_send(c,buffer,4)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 tuner_warn("i2c i/o error: rc == %d (should be 4)\n",rc);
350
Michael Krufkyd9cd2d92006-07-17 17:15:26 -0300351 switch (t->type) {
352 case TUNER_LG_TDVS_H06XF:
353 /* Set the Auxiliary Byte. */
354 buffer[0] = buffer[2];
355 buffer[0] &= ~0x20;
356 buffer[0] |= 0x18;
357 buffer[1] = 0x20;
358 tuner_dbg("tv 0x%02x 0x%02x\n",buffer[0],buffer[1]);
359
360 if (2 != (rc = i2c_master_send(c,buffer,2)))
361 tuner_warn("i2c i/o error: rc == %d (should be 2)\n",rc);
362 break;
363 case TUNER_MICROTUNE_4042FI5:
364 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 // FIXME - this may also work for other tuners
366 unsigned long timeout = jiffies + msecs_to_jiffies(1);
367 u8 status_byte = 0;
368
369 /* Wait until the PLL locks */
370 for (;;) {
371 if (time_after(jiffies,timeout))
372 return;
373 if (1 != (rc = i2c_master_recv(c,&status_byte,1))) {
374 tuner_warn("i2c i/o read error: rc == %d (should be 1)\n",rc);
375 break;
376 }
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700377 if (status_byte & TUNER_PLL_LOCKED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 break;
379 udelay(10);
380 }
381
382 /* Set the charge pump for optimized phase noise figure */
Michael Krufky3fc46d32006-01-23 17:11:11 -0200383 config &= ~TUNER_CHARGE_PUMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 buffer[0] = (div>>8) & 0x7f;
385 buffer[1] = div & 0xff;
Michael Krufky3fc46d32006-01-23 17:11:11 -0200386 buffer[2] = config;
Michael Krufkyab66b222006-01-23 17:11:11 -0200387 buffer[3] = cb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 tuner_dbg("tv 0x%02x 0x%02x 0x%02x 0x%02x\n",
Michael Krufkyd9cd2d92006-07-17 17:15:26 -0300389 buffer[0],buffer[1],buffer[2],buffer[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391 if (4 != (rc = i2c_master_send(c,buffer,4)))
392 tuner_warn("i2c i/o error: rc == %d (should be 4)\n",rc);
Michael Krufkyd9cd2d92006-07-17 17:15:26 -0300393 break;
394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 }
396}
397
398static void default_set_radio_freq(struct i2c_client *c, unsigned int freq)
399{
400 struct tunertype *tun;
401 struct tuner *t = i2c_get_clientdata(c);
Hans Verkuil27487d42006-01-15 15:04:52 -0200402 u8 buffer[4];
403 u16 div;
Michael Krufky7b0ac9c2006-01-13 14:10:25 -0200404 int rc, j;
Michael Krufky476d63d2006-02-07 06:25:34 -0200405 enum param_type desired_type = TUNER_PARAM_TYPE_RADIO;
Hans Verkuilba8fc392006-06-25 15:34:39 -0300406 struct tuner_params *params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700408 tun = &tuners[t->type];
Michael Krufky476d63d2006-02-07 06:25:34 -0200409
410 for (j = 0; j < tun->count-1; j++) {
411 if (desired_type != tun->params[j].type)
412 continue;
413 break;
414 }
Michael Krufky8f1a58d2006-02-07 06:25:39 -0200415 /* use default tuner_params if desired_type not available */
416 if (desired_type != tun->params[j].type)
417 j = 0;
Michael Krufky7b0ac9c2006-01-13 14:10:25 -0200418
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700419 div = (20 * freq / 16000) + (int)(20*10.7); /* IF 10.7 MHz */
Hans Verkuilba8fc392006-06-25 15:34:39 -0300420 params = &tun->params[j];
421 buffer[2] = (params->ranges[0].config & ~TUNER_RATIO_MASK) | TUNER_RATIO_SELECT_50; /* 50 kHz step */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
423 switch (t->type) {
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700424 case TUNER_TENA_9533_DI:
Mauro Carvalho Chehab391cd722005-06-23 22:02:43 -0700425 case TUNER_YMEC_TVF_5533MF:
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700426 tuner_dbg ("This tuner doesn't have FM. Most cards has a TEA5767 for FM\n");
427 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 case TUNER_PHILIPS_FM1216ME_MK3:
429 case TUNER_PHILIPS_FM1236_MK3:
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700430 case TUNER_PHILIPS_FMD1216ME_MK3:
Hans Verkuil122b5db2006-12-03 06:45:07 -0300431 case TUNER_LG_NTSC_TAPE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 buffer[3] = 0x19;
433 break;
Mauro Carvalho Chehabefcf55c2006-03-09 11:17:43 -0300434 case TUNER_TNF_5335MF:
435 buffer[3] = 0x11;
436 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 case TUNER_PHILIPS_FM1256_IH3:
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700438 div = (20 * freq) / 16000 + (int)(33.3 * 20); /* IF 33.3 MHz */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 buffer[3] = 0x19;
440 break;
441 case TUNER_LG_PAL_FM:
442 buffer[3] = 0xa5;
443 break;
Mauro Carvalho Chehab33ac6b52005-09-09 13:03:56 -0700444 case TUNER_MICROTUNE_4049FM5:
445 div = (20 * freq) / 16000 + (int)(33.3 * 20); /* IF 33.3 MHz */
446 buffer[3] = 0xa4;
447 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 default:
449 buffer[3] = 0xa4;
450 break;
451 }
Hans Verkuilba8fc392006-06-25 15:34:39 -0300452 if (params->cb_first_if_lower_freq && div < t->last_div) {
Hans Verkuil27487d42006-01-15 15:04:52 -0200453 buffer[0] = buffer[2];
454 buffer[1] = buffer[3];
455 buffer[2] = (div>>8) & 0x7f;
456 buffer[3] = div & 0xff;
457 } else {
458 buffer[0] = (div>>8) & 0x7f;
459 buffer[1] = div & 0xff;
460 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462 tuner_dbg("radio 0x%02x 0x%02x 0x%02x 0x%02x\n",
463 buffer[0],buffer[1],buffer[2],buffer[3]);
Hans Verkuil27487d42006-01-15 15:04:52 -0200464 t->last_div = div;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
Hans Verkuilba8fc392006-06-25 15:34:39 -0300466 if (params->has_tda9887) {
467 int config = 0;
468 if (params->port1_active && !params->port1_fm_high_sensitivity)
469 config |= TDA9887_PORT1_ACTIVE;
470 if (params->port2_active && !params->port2_fm_high_sensitivity)
471 config |= TDA9887_PORT2_ACTIVE;
472 if (params->intercarrier_mode)
473 config |= TDA9887_INTERCARRIER;
474/* if (params->port1_set_for_fm_mono)
475 config &= ~TDA9887_PORT1_ACTIVE;*/
Mauro Carvalho Chehab483deb02006-12-04 08:31:38 -0300476 if (params->fm_gain_normal)
477 config |= TDA9887_GAIN_NORMAL;
Hans Verkuilba8fc392006-06-25 15:34:39 -0300478 i2c_clients_command(c->adapter, TDA9887_SET_CONFIG, &config);
479 }
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800480 if (4 != (rc = i2c_master_send(c,buffer,4)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 tuner_warn("i2c i/o error: rc == %d (should be 4)\n",rc);
482}
483
Michael Krufky5d807c92007-06-06 16:17:57 -0300484static struct tuner_operations simple_tuner_ops = {
485 .set_tv_freq = default_set_tv_freq,
486 .set_radio_freq = default_set_radio_freq,
487 .has_signal = tuner_signal,
488 .is_stereo = tuner_stereo,
489};
490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491int default_tuner_init(struct i2c_client *c)
492{
493 struct tuner *t = i2c_get_clientdata(c);
494
495 tuner_info("type set to %d (%s)\n",
496 t->type, tuners[t->type].name);
497 strlcpy(c->name, tuners[t->type].name, sizeof(c->name));
498
Michael Krufky5d807c92007-06-06 16:17:57 -0300499 memcpy(&t->ops, &simple_tuner_ops, sizeof(struct tuner_operations));
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 return 0;
502}
503
504/*
505 * Overrides for Emacs so that we follow Linus's tabbing style.
506 * ---------------------------------------------------------------------------
507 * Local variables:
508 * c-basic-offset: 8
509 * End:
510 */